MIDI may be used when processing music.
MIDI is a file containing so-called musical score information. In other words, it is written such as which sound is produced at what timing.
When using Python, I will explain because there is a package that processes MIDI.
midi and pretty_midi
These packages are also used in a project called Magenta, which composes with artificial intelligence.
We will use Python2 here.
It seems that it can be installed with pip (corrected on 2016/11/22)
pip install python-midi
pip install pretty_midi
If you can import it, you're done.
import midi
import pretty_midi
In Python3, you can use pretty_midi by cloning and installing from github. (If you do it with pip, you will get an error when importing)
git clone https://github.com/craffel/pretty-midi
cd pretty-midi
python setup.py install
For midi, there is a Python3 version at here.
midi MIDI files basically consist of tracks and events.
The track contains performance data for each instrument.
And the performance data is composed of events. An event is information such as when to change the tempo and when to start (end) the sound.
It is midi that describes them directly.
Now, let's create a midi file that contains the performance information of the So quarter notes.
import midi
file = "test.mid"
pattern = midi.Pattern(resolution=960) #This pattern corresponds to midi files.
track = midi.Track() #Make a truck
pattern.append(track) #Add the track you created to the pattern.
ev = midi.SetTempoEvent(tick=0, bpm=120) #Create an event to set the tempo
track.append(ev) #Add the event to the track.
e = midi.NoteOnEvent(tick=0, velocity=100, pitch=midi.G_4) #Create an event that starts to make the sound of Seo.
track.append(e)
e = midi.NoteOffEvent(tick=960, velocity=100, pitch=midi.G_4) #Create an event that finishes the sound of Seo.
track.append(e)
eot = midi.EndOfTrackEvent(tick=1) #Create an event to finish the track
track.append(eot)
midi.write_midifile(file, pattern) #Write the pattern to a file.
Now read the midi file
pattern = midi.read_midifile("test.mid")
print(pattern)
The following information is displayed. These are the same as the information I wrote earlier.
midi.Pattern(format=1, resolution=960, tracks=\
[midi.Track(\
[midi.SetTempoEvent(tick=0, data=[7, 161, 32]),
midi.NoteOnEvent(tick=0, channel=0, data=[55, 100]),
midi.NoteOffEvent(tick=960, channel=0, data=[55, 100]),
midi.EndOfTrackEvent(tick=1, data=[])])])
pretty_midi pretty_midi is even easier to understand.
You can do the same with pretty_midi, which is shorter code.
Create a midi file in the same way.
import pretty_midi
pm = pretty_midi.PrettyMIDI(resolution=960, initial_tempo=120) #pretty_Create a midi object
instrument = pretty_midi.Instrument(0) #An instrument is like a truck.
note_number = pretty_midi.note_name_to_number('G4')
note = pretty_midi.Note(velocity=100, pitch=note_number, start=0, end=1) #note is equivalent to NoteOnEvent and NoteOffEvent.
instrument.notes.append(note)
pm.instruments.append(instrument)
pm.write('test.mid') #Write a midi file.
(Note) When an error occurs
note = pretty_midi.Note(velocity=100, pitch=note_number, >start=1e-20, end=1) #note is equivalent to NoteOnEvent and NoteOffEvent.
Please rewrite as>. This is due to the fact that the PrettyMIDI method time_to_tick returns as numpy.int64 type for some reason only when the input is 0. (In other cases, it is returned as an int type)
Read midi file
import pretty_midi
midi_data = pretty_midi.PrettyMIDI('test.mid') #Read midi file
print(midi_data.get_piano_roll()) #Outputs the piano roll
print(midi_data.synthesize()) #Outputs a waveform using a sine wave.
It seems that there are other useful functions as well.
Basically, it seems better to use pretty_midi when dealing with midi files in Python.
Recommended Posts