4
4
Fork
You've already forked python-musical-midi
2
Python tools for procedural music generation with MIDI.
  • Python 100%
Find a file
2024年01月04日 02:34:55 +00:00
musical_theory Update scale.py 2023年09月21日 03:39:31 +01:00
.gitignore Modernize code, fix imports 2017年06月08日 06:19:29 -05:00
example-01-notes-scales-and-timeline.py Update example-01-notes-scales-and-timeline.py 2023年09月23日 11:27:14 +01:00
example-02-notes-scales-and-timeline.py Update example-02-notes-scales-and-timeline.py 2023年09月23日 11:12:05 +01:00
example-03-tracks-and-channels.py Update example-03-tracks-and-channels.py 2023年09月23日 11:14:25 +01:00
example-04-chords.py Update example-04-chords.py 2023年09月23日 11:28:41 +01:00
example-05-simple-percussion-blend.py Update example-05-simple-percussion-blend.py 2023年09月23日 11:18:02 +01:00
example-06-image-to-midi.py Added example 06 2023年12月12日 05:17:32 +00:00
example-07-image-to-midi-gui.py Updated example-07-image-to-midi-gui.py 2024年01月04日 01:18:36 +00:00
LICENSE Updated LICENSE to GPLv3 2024年01月04日 02:28:43 +00:00
README.md Updated README 2024年01月04日 02:34:55 +00:00
screenshot.png Added example 07 screenshot 2024年01月04日 01:13:00 +00:00
timeline.py Update timeline.py 2023年09月23日 11:06:01 +01:00

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.

Example 07 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.