Sunday, January 19, 2025

Generate Music Through Python – A Complete Guide

Introduction

Music generation with Python has become more accessible with powerful libraries that allow us to compose melodies, generate MIDI files, and convert them into audio formats like WAV and MP3. This guide walks through the process of creating a MIDI file, synthesizing it into WAV using FluidSynth, and finally converting it to MP3 using pydub.

By the end of this guide, you will have a fully functional Python script that generates music and exports it as an MP3 file.


Why Use Python for Music Generation?

Python provides several libraries that make it easy to create and manipulate music:

  • MIDIUtil – Generates MIDI files programmatically.
  • Mingus – Provides music theory functions and chord generation.
  • FluidSynth – A real-time synthesizer that converts MIDI to WAV.
  • pydub – Converts audio formats, such as WAV to MP3.

Using these libraries, we can generate music from scratch and export it into an audio format that can be played on any device.


Setting Up the Environment

Before running the script, install the necessary dependencies:

Install Python Libraries

Run the following command in your terminal:

pip install midiutil mingus pyfluidsynth pydub

Install FluidSynth

  1. Download FluidSynth from the official repository:
    FluidSynth Releases
  2. Extract it to C:\tools\fluidsynth
  3. Add C:\tools\fluidsynth\bin to your system PATH (for command-line access).
  4. Verify the installation by running:
    fluidsynth --version

Download a SoundFont (.sf2) File

FluidSynth requires a SoundFont file to map MIDI notes to instrument sounds.

How Music is Generated in Python

Music generation in Python follows these key principles:

Understanding MIDI File Structure

A MIDI (Musical Instrument Digital Interface) file contains:

  • Note Data – The pitches and durations of notes.
  • Velocity – The intensity of each note.
  • Instrument Information – Which instruments to use for playback.

Unlike audio formats like MP3 or WAV, MIDI does not contain actual sound data, meaning it must be played back using a synthesizer like FluidSynth.

Breaking Down the Composition Process

  1. Chords and Progressions

    • Chords are groups of notes played together.
    • A chord progression is a sequence of chords that forms a harmonic structure for the music.
    • Example: "C → G → Am → F" is a common progression.
  2. Melody Generation

    • A melody is a sequence of individual notes that create a recognizable tune.
    • The script selects notes from a chord to create a simple melodic line.
  3. Bassline Generation

    • The bassline is usually the root note of each chord, played in a lower octave.
    • It provides rhythm and harmonic stability.
  4. MIDI to Audio Conversion

    • Since MIDI files do not contain actual sound, FluidSynth uses a SoundFont to generate audio.
    • Finally, we convert the generated WAV file to MP3 using pydub.

Python Script to Generate MIDI and Convert to MP3

This script will:

  1. Generate a MIDI file with chord progressions, a melody, and a bassline.
  2. Convert MIDI to WAV using FluidSynth and a SoundFont.
  3. Convert WAV to MP3 using pydub.

Python Script


import random import os import subprocess from midiutil import MIDIFile from mingus.core import chords from pydub import AudioSegment # Define paths SOUNDFONT_PATH =os.path.join(os.getcwd(), "FluidR3_GM.sf2") # Update your SoundFont path MIDI_FILENAME = "generated_music.mid" WAV_FILENAME = "generated_music.wav" MP3_FILENAME = "generated_music.mp3" # Define chord progressions verse = ["C", "G", "Am", "F"] chorus = ["F", "C", "G", "Am"] bridge = ["Dm", "A7", "G", "C"] song_structure = [verse, verse, chorus, verse, bridge, chorus] # MIDI settings track = 0 channel = 0 time = 0 # Start time in beats tempo = 120 # BPM volume = 100 # MIDI velocity # Create a MIDI file MyMIDI = MIDIFile(1) MyMIDI.addTempo(track, time, tempo) # Assign instruments instrument_chords = 0 # Acoustic Piano instrument_melody = 40 # Violin instrument_bass = 33 # Acoustic Bass MyMIDI.addProgramChange(track, channel, time, instrument_chords) MyMIDI.addProgramChange(track, channel + 1, time, instrument_melody) MyMIDI.addProgramChange(track, channel + 2, time, instrument_bass) # Convert note names to MIDI numbers def note_to_number(note: str, octave: int) -> int: NOTES = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'] NOTES_IN_OCTAVE = len(NOTES) return NOTES.index(note) + (NOTES_IN_OCTAVE * octave) # Generate music for section in song_structure: for chord in section: chord_notes = chords.from_shorthand(chord) random.shuffle(chord_notes) rhythm_pattern = [0, 0.5, 1, 1.5, 2, 2.5, 3] # Add chords for i, note in enumerate(chord_notes): octave = 3 midi_note = note_to_number(note, octave) MyMIDI.addNote(track, channel, midi_note, time + rhythm_pattern[i % len(rhythm_pattern)], 1, volume) # Add bassline bass_note = note_to_number(chord_notes[0], 2) MyMIDI.addNote(track, channel + 2, bass_note, time, 4, volume) # Add melody melody_note = note_to_number(random.choice(chord_notes), 5) melody_duration = random.choice([0.5, 1, 1.5]) MyMIDI.addNote(track, channel + 1, melody_note, time + 2, melody_duration, volume) time += 4 # Save MIDI file with open(MIDI_FILENAME, "wb") as output_file: MyMIDI.writeFile(output_file) # Convert MIDI to WAV using FluidSynth subprocess.run(f'fluidsynth -ni -F {WAV_FILENAME} -r 44100 {SOUNDFONT_PATH} {MIDI_FILENAME}', shell=True, check=True) # Convert WAV to MP3 using pydub AudioSegment.from_wav(WAV_FILENAME).export(MP3_FILENAME, format="mp3")

Running the Script

Once dependencies are installed, run:

python generate_music.py

This generates:

  • generated_music.mid (MIDI file)
  • generated_music.wav (WAV file)
  • generated_music.mp3 (MP3 file)

Next Steps

  • Customize the chord progressions
  • Experiment with different instruments
  • Generate longer compositions
  • Integrate AI-generated melodies

Start generating music with Python today!

No comments:

Post a Comment