- Python 100%
python-musical-midi
Tools for procedural music generation with MIDI.
See the examples for details.
Running each example generates a midi file. Examples 02, 03 and 05 are semi-random and produce somewhat different results each time. Examples 06 and 07 require an image, from which they generate midi files.
python-musical-midi is based on python-musical.
Dependencies
mido is required. mido can be installed with pip:
python3 -m pip install mido
See the mido installation instructions here
Examples 06 and 07 have an extra dependency for image analysis, Pillow, which can be also be installed with pip:
python3 -m pip install Pillow
Using a virtual environment to isolate the installation from your current environment is recommended.
Extra notes for examples 06 and 07: Generating a midi file from an image
Examples 06 and 07 demonstrate a basic way to generate midi data from an image.
Examples 06 and 07 have an extra dependency for image analysis, Pillow, which can be installed with pip:
python3 -m pip install Pillow
Example 07: image to midi (with GUI)
Example 07 has a few more options than example 06, and also provides a GUI.
The chosen image in this screenshot is derived from Fireworks at Ikenohata by Kobayashi Kiyochika. Creative Commons CC0 1.0 Universal Public Domain Dedication.
Example 07 usage
Choose an image, set the parameters, then press Generate. A .mid file will be generated. The .mid file name has the same name as the selected image. The .mid file and notes image (if selected) are saved in the same folder as the original chosen image, when 'Generate' is clicked.
The key, scale, BPM and note chance threshold are chosen randomly, but can be overridden.
The parameters are:
Rows: The number of rows to divide the image into. Each row will be translated into a midi track.
Columns: The number of columns to divide the image into. Each column of each track can be translated into a midi note. Columns represent steps in time of half a beat.
Lowest / Highest pitch: These define the pitch range within which notes will be generated. (If the lowest pitch is greater than the highest pitch, the values will be swapped.)
Force key: If selected, the chosen key will be used.
Force scale: If selected, the chosen scale will be used.
Force chance threshold: If selected, the chosen chance threshold will be used. The chance threshold is a number from 0 to 254. Generally, the lower the number, the more notes there will be.
Force BPM: If selected, the chosen BPM will be used.
Vary fit direction: When fitting notes to a scale, sometimes the pitch is an equal distance between two notes in the target scale. By default, the higher note is chosen. Selecting this will instead randomly vary whether the higher or lower note is chosen each time this happens during midi generation.
Add 'noise': Selecting this will remove notes randomly (may or may not have much effect depending on the image and chance threshold).
Save notes image: Selecting this will save a copy of the notes image visible in the app. The saved image has the same dimensions as the original chosen image.
Notes image line width: The width in pixels of the lines representing notes on the notes image.
Example 07 fits the generated midi pitches to a scale. If you don't want to fit the notes, select the 'chromatic' scale, which includes all midi note pitches.
Example 06 usage
When running example 06 you need to provide an image and a number of rows and columns as arguments, e.g. to use image.jpg with 8 rows and 120 columns:
python example-06-image-to-midi.py image.jpg 8 120
Each row will be translated to a midi track. Each column in the track represents one step along the track in time, with each step defined as half a beat in the script.
The above command will generate a midi file with 8 tracks, and the track should be about 60 beats long at most. Some extra time is added at the end to allow notes to finish playing.
Track bpm, scale, key and the chance threshold for a note to sound are decided randomly, but the randomisation is seeded with the image hash, so the random choices should be the same every time for a specific image.
Generally, areas of the image with brighter colors will produce higher pitches. A note will only sound if the mean value of at least one color band in that part of the image is higher than the note threshold value.
If you get too few or too many notes, a fourth argument can be added to modify the note chance threshold. e.g. the following command will halve the threshold, so there should be more notes than in the original:
python example-06-image-to-midi.py image.jpg 8 120 0.5
The next command will multiply the chance threshold by an additional half, so there should be less notes than in the original:
python example-06-image-to-midi.py image.jpg 8 120 1.5
Finally a fifth argument can add a bit of random 'noise' which should remove notes to add a bit more variety to melody lines. Add 'noise' as the fifth argument:
python example-06-image-to-midi.py image.jpg 8 120 0.5 noise
Note that the argument parsing in this example is very basic. If you want to use 'noise' but don't want to modify the note chance threshold, you'll have to specify the chance modifier as 1:
python example-06-image-to-midi.py image.jpg 8 120 1 noise
Example 06 fits the notes to a scale for listenability
If you want to stop fitting the pitches to a scale, comment out or delete this line in the while loop:
new_note = scale.fit_note(new_note)
License
python-musical-midi is licensed under GPLv3. See LICENSE.
python-musical-midi is based on python-musical, which has an MIT license (included below).
The original python-musical license is included below.
Copyright (c) 2017 Davy Wybiral
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.