back to home

04-14-2024

Developing AI-Powered Music Composition with Python

Creating AI-powered music has always been a fascinating challenge for me, and Python has provided the perfect toolkit to explore this niche field. My goal has been to blend my love for music with cutting-edge AI technology to create something truly unique.

Tools and Libraries:

Example Experience:

import magenta.music as mm
from magenta.protobuf import music_pb2
import midiutil

# Create a simple melody using Magenta
melody = mm.Melody([60, 62, 64, 65, 67, 69, 71, 72])

# Convert the melody to a note sequence
note_sequence = melody.to_sequence()

# Save the note sequence as a MIDI file
mm.sequence_proto_to_midi_file(note_sequence, 'melody.mid')

# Load the MIDI file for further processing
midi_data = music_pb2.NoteSequence()
with open('melody.mid', 'rb') as f:
    midi_data.ParseFromString(f.read())

# Modify the MIDI file using MIDIUtil
midi_file = midiutil.MIDIFile(1)
track = 0
time = 0
midi_file.addTempo(track, time, 120)

# Add notes to the MIDI file
for note in melody:
    midi_file.addNote(track, 0, note, time, 1, 100)
    time += 1

# Save the modified MIDI file
with open("modified_melody.mid", "wb") as output_file:
    midi_file.writeFile(output_file)

Through Python and these powerful libraries, I’ve been able to compose original pieces of music that blend human creativity with AI-driven innovation, opening new horizons in music technology.