Generating and Sampling AI-Generated Music Pieces

By Bill Sharlow

Day 5: Building an AI-Powered Music Composer

Welcome back to our AI-powered music composition journey! Today, we’re reaching a significant milestone as we dive into the exhilarating process of generating and sampling AI-generated music pieces using our trained LSTM-based music generation model. With our model trained and fine-tuned, we’re ready to unleash its creative potential and listen to the musical compositions it produces.

Generating Music with the Trained Model

Using our trained LSTM-based music generation model, we can generate new musical compositions by providing it with a seed sequence as input and letting it predict the continuation of the music. The generated sequences can be interpreted as musical notes, chords, or other musical events, depending on the encoding used.

Sampling and Evaluation

Once we’ve generated a sequence of musical events, it’s essential to sample and evaluate the quality of the generated music. Sampling involves converting the predicted musical events into a format that can be played back and listened to. Evaluation criteria may include musical coherence, harmony, rhythm, and overall aesthetic appeal.

Example Code: Generating and Sampling Music

Let’s continue our example code by incorporating the generation and sampling process for AI-generated music pieces:

def generate_music(model, seed_sequence, num_steps):
    generated_sequence = []

    for _ in range(num_steps):
        # Predict the next step in the music sequence
        next_step_probs = model.predict(seed_sequence)[0]
        next_step = np.random.choice(range(len(next_step_probs)), p=next_step_probs)

        # Update the seed sequence with the new step
        seed_sequence = np.append(seed_sequence[:, 1:, :], [[next_step]], axis=1)

    return generated_sequence

# Example usage
seed_sequence = np.array(...)  # Seed sequence to start music generation
num_steps = 100  # Number of steps to generate
generated_sequence = generate_music(model, seed_sequence, num_steps)

In this code snippet, we define a function generate_music to generate a sequence of musical events using our trained LSTM-based music generation model. We iteratively predict the next step in the music sequence based on the seed sequence and sample from the predicted probability distribution to choose the next musical event.

Listening to AI-Generated Music

After generating the music sequence, it’s time to listen to the AI-generated music and evaluate its quality and coherence. You can play back the generated sequence using MIDI playback software or convert it into an audio file for playback using digital audio workstations (DAWs) or music player software.


In today’s blog post, we’ve explored the thrilling process of generating and sampling AI-generated music pieces using our trained LSTM-based music generation model. By leveraging the model’s creative capabilities, we’ve unlocked the potential to produce original musical compositions autonomously.

In the next blog post, we’ll reflect on the AI music composition journey so far and discuss future directions and possibilities for further exploration. Stay tuned for more insights and discoveries in the world of AI-powered music composition!

If you have any questions or thoughts, feel free to share them in the comments section below!

Leave a Comment