MIDI packages in Python midi and pretty_midi

Handle MIDI with Python

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.

Installation

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.

Summary

Basically, it seems better to use pretty_midi when dealing with midi files in Python.

Recommended Posts

MIDI packages in Python midi and pretty_midi
Modules and packages in Python are "namespaces"
Python packages and modules
Organize python modules and packages in a mess
Create a MIDI file in Python using pretty_midi
Understand Python packages and modules
Stack and Queue in Python
Unittest and CI in Python
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
A standard way to develop and distribute packages in Python
Python Basic Course (14 Modules and Packages)
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Python virtual environment and packages on Ubuntu
Implementation module "deque" in queue and Python
Line graphs and scale lines in python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find and check inverse matrix in Python
Linear Independence and Basis: Linear Algebra in Python <6>
Call sudo in Python and autofill password
Manage python packages to install in containers
Differences in multithreading between Python and Jython
Module import and exception handling in python
How to use is and == in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python