top of page

AI-based Music Composer | Python Project

Updated: May 7

This project is an AI-based music composer that generates unique pieces of music based on user preferences or specific genres. The script uses the `magenta` library, which provides pre-trained models for music generation.


This python project involves building an AI music composer. Imagine having a musical companion that can create melodies across different genres. These melodies can be saved as MIDI files, providing a foundation for your own music compositions and creative exploration. 🎵✨


  • Unique Melodies: The AI creates one-of-a-kind melodies using pre-trained models.

  • User Input: You can choose the genre and tempo you prefer.

  • Chord Progressions: Based on your chosen genre, the AI adds harmonious chord progressions to complement the melody.

  • Drum Patterns: It even includes a basic drum pattern for rhythm.

  • MIDI Files: The final output is saved as MIDI files, ready for your musical exploration! 🎶🤖

AI music composer , ai project , project for beginner , musician , python project , project for beginner


Install the following library:

Magenta library includes utilities for manipulating source data (primarily music and images), using this data to train machine learning models, and finally generating new content from these models.

pip install magenta
Project code:

create a python file with name: , and copy paste the following code:

import os
import as mm
from magenta.models.melody_rnn import melody_rnn_sequence_generator

# Set the directory to save the generated MIDI files
output_dir = 'generated_music'
os.makedirs(output_dir, exist_ok=True)

# Initialize the Melody RNN model
model_name = 'attention_rnn'
melody_rnn = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator(

# Set the temperature for music generation (higher values lead to more randomness)
temperature = 1.0

# Set the number of music pieces to generate
num_music_pieces = 3

# Set the number of steps per music piece
steps_per_music_piece = 128

# User input for preferred genre and tempo
preferred_genre = input(
    "Enter your preferred genre (e.g., classical, jazz, rock): ")
preferred_tempo = int(input("Enter your preferred tempo (BPM): "))

# Chord progression for the chosen genre (you can add more genres and progressions)
chord_progressions = {
    "classical": ["C", "Am", "F", "G"],
    "jazz": ["Cmaj7", "Dm7", "Em7", "A7"],
    "rock": ["C", "G", "Am", "F"],

# Basic drum pattern for accompaniment
drum_pattern = mm.DrumTrack(
    # Kick drum and Hi-hat pattern (adjust as needed)
    [36, 0, 42, 0, 36, 0, 42, 0],
    steps_per_bar=steps_per_music_piece // 4,

# Generate music pieces
for i in range(num_music_pieces):
    # Generate a melody sequence
    melody_sequence = melody_rnn.generate(

    # Add chords to the melody sequence based on the preferred genre
    chords = [chord_progressions.get(preferred_genre, ["C"])[i % len(
        chord_progressions.get(preferred_genre, ["C"]))] for i in range(steps_per_music_piece)]
    chord_sequence = mm.ChordSequence(chords)
    melody_with_chords_sequence = mm.sequences_lib.concatenate_sequences(
        melody_sequence, chord_sequence)

    # Create a MIDI file from the melody with chords sequence and drum pattern
    music_sequence = mm.sequences_lib.concatenate_sequences(
        melody_with_chords_sequence, drum_pattern)
    music_sequence.tempos[0].qpm = preferred_tempo

    midi_file = os.path.join(output_dir, f'music_piece_{i + 1}.mid')
    mm.sequence_proto_to_midi_file(music_sequence, midi_file)
    print(f'Music piece {i + 1} generated and saved as {midi_file}')

print('Music generation complete!')


To generate music pieces using the AI-based music composer, run the following command:


The script will prompt you to enter your preferred genre and tempo (BPM) for music generation.

User Input:

The script allows you to input your preferred genre, which can be one of the following:

  • Classical

  • Jazz

  • Rock

Additionally, you can specify the tempo (in BPM) for the generated music.

Chord Progressions

The chosen genre determines the chord progression for the generated music. Currently, the supported chord progressions are:

  • Classical: ["C", "Am", "F", "G"]

  • Jazz: ["Cmaj7", "Dm7", "Em7", "A7"]

  • Rock: ["C", "G", "Am", "F"]

You can modify or extend these chord progressions in the file.

Drum Accompaniment

The music pieces include a basic drum pattern for rhythmic accompaniment. The drum pattern consists of a kick drum (36) and hi-hat (42). You can adjust the drum pattern in the file to create different rhythms.

Generated Music

The generated music will be saved as MIDI files in the generated_music directory. Each music piece will have a unique file name, such as music_piece_1.mid, music_piece_2.mid, and so on.


bottom of page