Create a MIDI file in Python using pretty_midi


I'm playing with the music generation project "Magenta" made by TensorFlow.

Reference: [Let Sakanaction learn from TensorFlow's art and music generation project "Magenta". ] (

As a pre-process, I found out that I wanted to play with MIDI files in Python, and it seemed that Magenta was using pretty_midi. In this post, I'll run the sample pretty_midi 0.2.6 documentation to see how pretty_midi works.

Sample code

Write the sample code and comments.

import pretty_midi

# Create a PrettyMIDI object
#Create a Pretty MIDI object.
cello_c_chord = pretty_midi.PrettyMIDI()

# Create an Instrument instance for a cello instrument
#Create an Instrument Instance. Here Cello

#Enter the instrument name and it will return the corresponding General MIDI program number
cello_program = pretty_midi.instrument_name_to_program('Cello')

#Create Instrument instance as Cello
cello = pretty_midi.Instrument(program=cello_program)

# Iterate over note names, which will be converted to note number later
#The melody is described by NoteName, but it will be converted to NoteNumber later.

for note_name in ['C5', 'E5', 'G5']:
    # Retrieve the MIDI note number for this note name
    #Searching for Note Number from Note Name.
    note_number = pretty_midi.note_name_to_number(note_name)

    # Create a Note instance, starting at 0s and ending at .5s
    #Create a NoteInstance. sound(pitch)Start time and end time,
    #Define velocity.
    note = pretty_midi.Note(
        velocity=100, pitch=note_number, start=0, end=.5)
    # Add it to our cello instrument
    #Add the NoteInstance created above to the Cello Instrument.
# Add the cello instrument to the PrettyMIDI object
#Add the Chello Instrument to the PrettyMIDI object.

# Write out the MIDI data
#Export a PrettyMIDI object as a MIDI file.

MIDI created

A MIDI file with a domiso sound of 0.5 seconds (1 beat at 120 BPM) has been created. (Displayed in logic X)

スクリーンショット 2016-10-07 17.56.55.png

At the end

Next, I would like to read an existing MIDI file and divide the file for each instrument.

Thank you very much.

Recommended Posts

Create a MIDI file in Python using pretty_midi
Create a binary file in Python
[GPS] Create a kml file in Python
How to create a JSON file in Python
Create a function in Python
Create a dictionary in Python
Create a data collection bot in Python using Selenium
Create your first GDSII file in Python using gdspy
MIDI packages in Python midi and pretty_midi
Create a python GUI using tkinter
Create a DI Container in Python
Create a Kubernetes Operator in Python
Create a random string in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
[Python] Create a Batch environment using AWS-CDK
Scraping a website using JavaScript in Python
Draw a tree in Python 3 using graphviz
File operations in Python
Create a shortcut to run a Python file in VScode on your terminal
File processing in Python
[Python] Split a large Flask file using Blueprint
Read a file containing garbled lines in Python
Create a Python module
I want to create a window in Python
Create a standard normal distribution graph in Python
Create a web map using Python and GDAL
Create wav file from GLSL shader using python3
File operations in Python
I tried reading a CSV file using Python
Create a simple momentum investment model in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Run a Python file from html using Django
Create a Photoshop format file (.psd) with python
Create a package containing global commands in Python
Create a record with attachments in KINTONE using the Python requests module
Create a Python environment
Create a Mac app using py2app and Python3! !!
Create a loop antenna pattern in Python in KiCad
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Create a Python image in Django without a dummy image file and test the image upload
Create an elliptical scatter plot in Python without using a multivariate normal distribution
Parse a JSON string written to a file in Python
Upload JPG file using Google Drive API in Python
Try creating a compressed file using Python and zlib
Collectively register data in Firestore using csv file in Python
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[LINE Messaging API] Create a rich menu in Python
Create an image file using PIL (Python Imaging Library).
[Python] Create a ValueObject with a complete constructor using dataclasses
Create a plugin to run Python Doctest in Vim (1)
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
Python script to create a JSON file from a CSV file
How to execute a command using subprocess in Python
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a fake Minecraft server in Python with Quarry