A Python program that generates music using Markov chains based on input MIDI files.
The Markov Music Generator analyzes one or more MIDI files and uses a Markov chain to generate new music sequences. By modeling the probabilities of note transitions, the program creates music that reflects the patterns found in the input files.
- Markov Chain Implementation: Supports variable-order Markov chains, allowing for control over the complexity and resemblance to the input music.
- MIDI File Handling: Reads and writes standard MIDI files using the mido library.
- Command-Line Interface: Accepts MIDI files and Markov chain order as command-line arguments.
- Customizable Parameters: Allows adjustment of rounding properties to influence the diversity of generated music.
- Python 3.6 or higher
pip install mido
- MIDI_FILE: One or more MIDI files to use as input for generating music.
- -o, --order: (Optional) The order of the Markov chain (default is 3).
- -of, --output-file: (Optional) The output MIDI file name (default is
output.mid
). - -mm, --max-measures: (Optional) The maximum number of measures to generate.
- -w, –weights: (Optional) Comma-separated list of weights corresponding to the input files.
python markov_music.py [OPTIONS] MIDI_FILE [MIDI_FILE ...]
-
Using a single MIDI file with default order:
python markov_music.py twinkle.mid
-
Using multiple MIDI files:
python markov_music.py song1.mid song2.mid song3.mid
-
Specifying the Markov chain order:
python markov_music.py --order 4 twinkle.mid
The generated MIDI file will be saved into the current working directory.
-
Reading MIDI Files: The program reads the input MIDI files and parses the note events, collecting information about pitches, velocities, durations, and timing.
-
Building the Markov Chain:
- Creates mappings of note sequences based on the specified order.
- Calculates the probabilities of transitioning from one note sequence to the next.
- Generating New Music:
- Starts with a random note sequence from the mappings.
- Uses the Markov chain to probabilistically select subsequent notes.
- Continues until no further transitions are possible or a termination condition is met.
- Writing the Output File:
- Compiles the generated note events into a MIDI file.
- Ensures correct timing and event ordering.
- Higher Order: The generated music will more closely resemble the input files, as it considers longer sequences of notes.
- Lower Order: Introduces more randomness, potentially creating more novel compositions.
The following attributes can be adjusted in the MarkovMusic class to influence the generated music:
- velocity_rounding: Controls the rounding of note velocities.
- duration_rounding: Controls the rounding of note durations.
- tempo_rounding: Controls the rounding of tempo changes.
These parameters affect how similar notes are grouped during the Markov chain construction.
Ensure you have mido
library installed:
pip install mido
- Input Files: The MIDI files provided should be valid and accessible. Use full paths if the files are not in the current directory.
- Error Handling: The program includes basic error handling to inform you of issues with file reading or writing.
- Ouput File: The output MIDI file output.mid will be overwritten each time the program runs. Rename or move the file after generation if you wish to keep it.
- No Mappings Available: If the program outputs “No mappings available to generate music,” ensure that the input MIDI files contain note data and are correctly parsed.
- Empty Output: If no output is generated, check that the Markov chain order is appropriate for the length of the input sequences.
- Exceptions: If you encounter exceptions or errors, verify that all dependencies are installed and that your MIDI files are not corrupted.
This project is released under the MIT License.
- Original Java Implementation: This program is a Python adaptation of an original Java implementation for music generation using Markov chains by @aaronvanzyl.
- mido Library: https://mido.readthedocs.io/en/latest/