This project is a Python-based tool that generates MIDI files from an expressive text file of musical notes, with support for multiple tracks, instruments, and various music-related commands.
- Generate MIDI files from a text-based note representation.
- Support for multiple instruments, percussion, channels, and looping.
- Integration with click for command-line interaction.
- Uses pygame for MIDI playback.
- Python 3.10+
click
(for command-line interaction)yaml
(to load instrument configuration)mido
(to handle MIDI file creation)pygame
(to play MIDI files)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Use Homebrew to install pyenv and dependencies:
brew update
brew install pyenv
brew install pyenv-virtualenv
brew install portaudio
brew install fluidsynth
Add the following lines to your shell profile (e.g., ~/.bashrc, ~/.zshrc, etc.) to initialize pyenv automatically:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Restart your terminal or run:
source ~/.zshrc # Or source your profile file accordingly
Install the required Python version (e.g., Python 3.12.0):
pyenv install 3.12.0
pyenv global 3.12.0
To clone the project repository:
git clone https://github.com/glyfnet/text-to-midi.git
cd text-to-midi
This project uses Poetry for dependency management. Install Poetry:
curl -sSL https://install.python-poetry.org | python3 -
Add Poetry to your path (if needed):
export PATH="$HOME/.local/bin:$PATH"
Use Poetry to install the required dependencies:
poetry install
To activate the virtual environment, use:
poetry shell
Now you can use the text-to-midi script to generate MIDI files. Running the Script
The script is defined in the pyproject.toml as a Poetry script command. To generate a MIDI file from a text-based representation of the music, use:
poetry run text-to-midi sample.txt -o output.mid --play
This command will:
- Parse the file sample.txt to generate a MIDI file named output.mid.
- Optionally play the MIDI file using --play.
Alternatively, if you are already in the virtual environment (poetry shell), you can use:
text-to-midi sample.txt -o output.mid --play
Command-Line Options
file_path (Required): Path to the text file containing music notes.
--output, -o (Optional): Specify the output MIDI file name. Defaults to output.mid.
--play (Optional): Plays the generated MIDI file after creation.
Text File Format:
The text file should contain commands to define:
- Tempo (TEMPO)
- Scale (SCALE)
- Track Setup (TRACK)
- Channel Selection (CHANNEL)
- Instrument Selection (INSTRUMENT)
- Note Velocity (VELOCITY)
- Note Duration (DURATION)
- Music Notes or Rest (REST)
- Looping (LOOP and END)
To loop sections of the music, use the LOOP and END commands. Here's how:
- LOOP X: Start a loop block, where X is the number of times to repeat the section.
- END: End the loop block.
Example File:
# Global settings
TEMPO 120
SCALE C major
# Define Track 1
TRACK 1
CHANNEL 1
INSTRUMENT acoustic_grand_piano
VELOCITY 80
DURATION 1.0
LOOP 2
G4 A4 B4
REST
END
# Define Track 2
TRACK 2
CHANNEL 10
INSTRUMENT side_stick
E4 E4 E4
- MIDI Channels: Channel 10 is reserved for percussion instruments.
- Instruments YAML: Instrument definitions are loaded from instruments.yaml. Make sure you have a properly formatted YAML file for this to work correctly.
- Delta Time: In MIDI, the time value represents the delay (in ticks) between subsequent events, not the absolute time.
If you encounter issues with pygame initialization, ensure that your audio setup is properly configured on your machine. Make sure the instruments.yaml file contains valid instrument names and values.
This project is licensed under the MIT License. Contributing
Feel free to open an issue or a pull request if you find a bug or want to contribute to the project.