{}const=>[]async()letfn</>var
AIBackendDevelopment

AI composer in Python

How to teach a neural network to compose music: the first step is data preparation. A simple explanation, even if you are just starting out with Python and ML.

К

Kodik

Author

9 min read

🎼 AI composer in Python: Create music from scratch

How we teach a neural network to understand music, generate it, and save it to a file to listen to!

You turn on the music and hear the melody. Now imagine: the neural network also “hears” music, remembers it — and then composes its own! 🤯

💡 We will create such a neural network from scratch. And we will start with the very first step — we will prepare the music in a convenient format for training, train the model and, finally, generate the first composition!


🔧 Part 1: Data Preparation and Basics

1. Install the tool for working with music

Open the terminal (or command line) and write:

pip install music21

📦 This is the music21 library. It's like a musical translator:

  • reads .mid files (they contain music),

  • pulls out notes and chords,

  • allows you to save and create new melodies.

This is our assistant to turn music into data that Python and neural networks can understand.

2. Prepare the music

For AI to learn how to compose, it needs examples. Like a child: first he listens to adults singing, and only then begins to sing himself 🎵

🎼 We will use MIDI-файлы — these are "musical drafts" where it is written which notes are played, in what order and for how long.

🔍 Where to get it?

  1. Go to the site like https://bitmidi.com

  2. Download a few melodies (for example, Bach or Beethoven)

  3. Create a folder next to the code with the name midi_songs

  4. Place files with the extension .mid there

3. Read music in Python

Now, the code. It will read all MIDI files, extract notes, and add them to a list. Here is a complete example:

from music21 import converter, note, chord
import glob

notes = []

# Go through all the files in the midi_songs folder
for file in glob.glob("midi_songs/*.mid"):
    # Uploading file as a musical work
    midi = converter.parse(file)

    # Get all the notes and chords, ignoring the instruments
    elements = midi.flat.notes

    for element in elements:
        if isinstance(element, note.Note):
            # This is a single note - for example, "C4" (up to the 4th octave)
            notes.append(str(element.pitch))
        elif isinstance(element, chord.Chord):
            # This is a chord - for example, "60.64.67"
            # We take the numbers of all the notes in the chord and combine them into a string
            notes.append('.'.join(str(n) for n in element.normalOrder))

📌 Explanation:

  • glob.glob("midi_songs/*.mid") — searches for all MIDI files in the folder.

  • converter.parse(file) — reads the file and turns it into an object that can be worked with.

  • midi.flat.notes — gets only the notes (we do not touch the rhythm, tempo and instruments yet).

  • note.Note — single notes (do, re, mi, etc.)

  • chord.Chord — chords (several notes at the same time, like in a chord on a guitar).

🎵 Why do we save notes as strings?

Because it's easier to pass them into the neural network. For example:

  • "C4" — a regular note.

  • "60.64.67" — a chord of three notes, where each digit is the number of the note in pitch.

What we have achieved

👉 The variable notes now contains all the music from the MIDI files — note by note, chord by chord.

For example:

notes[:10]
# ['E4', 'D4', 'C4', 'C4', 'D4', 'E4', 'E4', 'E4', 'D4', 'D4']

It's like if you recorded a melody by ear - only not with your ears, but with a code 🎧

✅ SUMMARY OF THE FIRST PART:

  • We have installed the required library

  • Downloaded music

  • Turned it into a list of notes

This is the "training material" for AI.


🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

🔁 Part 2: Training the neural network

Last time, we turned music from .mid files into a list of notes. Now the most interesting thing is to teach AI to understand how a melody is built and to generate its own 🎶

Even if you've just started learning Python, you'll figure it out! Let's go 🚀

1. Break the music into pieces

For AI to learn how to compose, it needs examples: “Here's a piece of notes — guess what the next one will be.” It's like listening to a familiar song and guessing what's next 🎧

# We get a list of unique notes
pitchnames = sorted(set(notes))

# Create a dictionary: note -> number
note_to_int = {note: number for number, note in enumerate(pitchnames)}

sequence_length = 50  # piece length (50 notes)
network_input = []
network_output = []

for i in range(len(notes) - sequence_length):
    # We take 50 consecutive notes
    sequence_in = notes[i:i + sequence_length]
    # The next note after them is the "answer"se"
    sequence_out = notes[i + sequence_length]
    
    # Converting notes into numbers
    network_input.append([note_to_int[n] for n in sequence_in])
    network_output.append(note_to_int[sequence_out])

📌 Here we are:

  • We find all the unique notes (for example, C4, D4, 60.64.67).

  • We turn each note into a number — because the neural network does not understand the text, only the numbers.

🧠 In simple words:

  • We take a piece of 50 notes as a context.

  • And we ask the neural network to guess the 51st note.

  • We will have thousands of such examples, and the neural network will learn from each one.

2. Prepare data for the neural network

The neural network works with numbers from 0 to 1, not just with note numbers. Therefore, we normalize the input data:

import numpy as np
from tensorflow.keras.utils import to_categorical

n_patterns = len(network_input)
n_vocab = len(pitchnames)

# Convert to the required format and divide by the total number of notes
network_input = np.reshape(network_input, (n_patterns, sequence_length, 1))
network_input = network_input / float(n_vocab)

# Convert outputs to categories (one-hot)
network_output = to_categorical(network_output)

🔍 What's going on here:

  • We are changing the login form from the usual list to a 3D format (needed for the neural network to work).

  • We divide each number by the total number of notes to get values from 0 to 1.

  • We turn the outputs (correct answers) into a special kind, where each note is a separate "exit" of the network.

    3. Building a simple neural network

    Now the magic:

    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import LSTM, Dropout, Dense
    
    model = Sequential()
    model.add(LSTM(256, input_shape=(sequence_length, 1)))
    model.add(Dropout(0.3))
    model.add(Dense(n_vocab, activation='softmax'))

    📌 Explanation:

    • LSTM is a special type of neural network that remembers sequences well (for example, music).

    • Dropout — so that the model does not "memorize", but learns for real.

    • Dense(n_vocab, activation='softmax') — at the output, the network will say: "I think that with a probability of 80% the next note is C4, 10% - D4..."

    4. Training the neural network

    model.compile(loss='categorical_crossentropy', optimizer='adam')
    model.fit(network_input, network_output, epochs=50, batch_size=64)

    💡 Training will take a little time. The point is:

    • The network is looking at 50 notes.

    • Trying to guess the 51st.

    • He will find out whether I was wrong or not.

    • And it adjusts its "logic" to guess more accurately next time.

    So - thousands of times in a row. And every time it gets better and better. Like a child who learns to compose melodies by ear 🎶

    🔥 That's it! Now we have a trained model that can predict the next note in a melody.


    🎼 Part 3: Generating and Saving Music

    You have already collected the data, trained the neural network — and now the magic moment is coming: 👉 AI will compose the music itself! And you will be able to hear the result.

    1. Start with a short melody

    We taught the network: "Here are 50 notes — guess the 51st." Now we do the opposite: We give it 50 notes, it predicts the next one, then more and more...

    import random
    import numpy as np
    
    # Dictionary: from the number — to the note
    int_to_note = {number: note for number, note in enumerate(pitchnames)}
    
    # Choose a random starting sequence (50 notes)
    start = random.randint(0, len(network_input) - 1)
    pattern = network_input[start]

    💡 We take one random piece (the same format that was served during the training) — it will be a "prompt", the beginning of a new melody.

    2. Generate the continuation of the melody

    Next, we will ask the model 300 times in a row: “What note do you want to put next?”

    prediction_output = []
    
    for i in range(300):  # we want 300 new notes
        # Preparing the input: reshape + normalization (from 0 to 1)
        prediction_input = pattern.reshape(1, len(pattern), 1)
        prediction_input = prediction_input / float(n_vocab)
    
        # Predicting the next note
        prediction = model.predict(prediction_input, verbose=0)
    
        # We take the index of the most probable note
        index = np.argmax(prediction)
    
        # Turn the number back into a note name
        result = int_to_note[index]
        prediction_output.append(result)
    
        # Update the sequence: delete the 1st, add a new one
        pattern = np.append(pattern[1:], [[index]], axis=0)

    🔍 What is important here:

    • .reshape(...) — convert the data to the required format (1 melody, 50 notes, 1 value per note).

    • / float(n_vocab) — normalize the values so that the network can work correctly.

    • model.predict(...) — the network predicts probabilities for all notes.

    • np.argmax(...) — select the one that has the highest probability.

    • append(..., [[index]], ...) — scroll the melody one step.

    📌 So, step by step, the AI composes a new melody: first the 51st note, then the 52nd, and so on up to the 300th.

    3. Turn notes back into music

    Now we have a list of notes in the form of strings: ['E4', 'F4', 'G4', '60.64.67', ...]

    This is a future melody, but to hear it, you need to collect a .mid file from it — it's like a draft for a music program.

    from music21 import stream, note, chord, instrument
    
    output_notes = []
    
    for pattern in prediction_output:
        if '.' in pattern or pattern.isdigit():
            # This is a chord
            notes_in_chord = pattern.split('.')
            chord_notes = [note.Note(int(n)) for n in notes_in_chord]
            new_chord = chord.Chord(chord_notes)
            output_notes.append(new_chord)
        else:
            # This is a single note, like "C4"
            new_note = note.Note(pattern)
            new_note.storedInstrument = instrument.Piano()
            output_notes.append(new_note)

    📌 Explanation:

    • If the line contains dots or only numbers, it is a chord (several notes).

    • If it is a regular note, we do note.Note(...) and add it as a separate sound.

    • All notes/chords are added to output_notes.

    4. Save music to a file

    Now create a music stream and save it to a file:

    midi_stream = stream.Stream(output_notes)
    midi_stream.write('midi', fp='generated_music.mid')

    🎉 That's it! You now have the file generated_music.mid — open it in:

    Press Play and listen to what your AI composer has composed 🤖🎵


    ✅ What you did:

    • Turned music into data

    • Taught the neural network to continue melodies

    • Received my first generated composition

    🧑‍🎼 It's not just Python code anymore. This is your AI co-author!

    🎁 Do you want to add:

    • Writing in a specific style?

    • Rhythm and duration support?

    • Music generation at the click of a button in a web application?

    If it turned out cool, share it on Telegram or upload it to TikTok — AI musicians are trending now 😎

Source code

You can find the full source code of the project on GitHub:

https://github.com/Coursme/ai-composer-python

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card