Extract only the sound of a specific instrument from a MIDI file and make it a separate file

Introduction

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". ] (http://qiita.com/tackey/items/1295a0347a8b4cc30d46)

As a pre-processing, I want to convert a MIDI file to a file for each instrument, so I will split it using pretty_midi.

Reference: pretty_midi 0.2.6 documentation Reference: Create a MIDI file in Python using pretty_midi

The environment is Python 2.7, and the MIDI file is logic X.

Sample code 1 (take out one instrument and make it into a file)

sample.py


import pretty_midi

#Read MIDI file
midi_data = pretty_midi.PrettyMIDI('hogehoge.MID')

#List of musical instruments
midi_data.instruments

'''
 [Instrument(program=80, is_drum=False, name="hoge"),
 Instrument(program=4, is_drum=False, name="hoge"),
 Instrument(program=0, is_drum=True, name="hoge"),
 Instrument(program=16, is_drum=False, name="hoge"),
 Instrument(program=52, is_drum=False, name="hoge"),
 Instrument(program=71, is_drum=False, name="hoge"),
 Instrument(program=4, is_drum=False, name="hoge"),
 Instrument(program=29, is_drum=False, name="hoge"),
 Instrument(program=30, is_drum=False, name="hoge"),
 Instrument(program=51, is_drum=False, name="hoge"),
 Instrument(program=33, is_drum=False, name="hoge"),
 Instrument(program=27, is_drum=False, name="hoge"),
 Instrument(program=81, is_drum=False, name="hoge")]
'''

#80th instrument(ReverseEngineering)Take out and make it an instance
for instrument in midi_data.instruments:
    if instrument.program == 80:
        ins_80 = instrument
        
#Create a Pretty MIDI object for new creation
rev_en_chord = pretty_midi.PrettyMIDI()

#Add number 80 to PrettyMIDI object
rev_en_chord.instruments.append(ins_80)

#save
rev_en_chord.write('ins_80.mid')

A file called ins_80.mid will be created in the same folder.

Sample code 2 (take out all instruments and put them in a file)

sample2.py


import pretty_midi
import os

title = "aoi"

midi_data = pretty_midi.PrettyMIDI('sakanaction_'+ title +'.MID')

#Create if there is no output directory
if not os.path.isdir('output'):
    os.mkdir(output)

#Create a directory for the song
if not os.path.isdir('output/' + str(title)):
    os.mkdir('output/' + str(title))
    
#Take out the instruments one by one and make them instances
for i in range(0,len(midi_data.instruments)):
    
    instrument = midi_data.instruments[i]
    program_num = midi_data.instruments[i].program

    #Create a Pretty MIDI object for new creation
    rev_en_chord = pretty_midi.PrettyMIDI()

    #Add instrument to PrettyMIDI object
    rev_en_chord.instruments.append(instrument)

    #save
    rev_en_chord.write('output/'+str(title) + '/' + str(title) +'_ins_' + str(program_num) + '.mid')

At the end

I feel like I can write the code more beautifully. Based on the divided files, I would like to learn music individually for each instrument.

Thank you very much.

Recommended Posts

Extract only the sound of a specific instrument from a MIDI file and make it a separate file
Extract only complete from the result of Trinity
Use Pillow to make the image transparent and overlay only part of it
Specify the volume on linux and make a sound
Make a copy of a Google Drive file from Python
(Memo) Until you extract only the part you want from a certain Web page, convert it to a Sphinx page, and print it as a PDF
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
I want to extract the tag information (title and artist) of a music file (flac, wav).
Dig the directory and create a list of directory paths + file names
Find all patterns to extract a specific number from the set
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Tips: [Python] Randomly restore and extract an array from a fasta file
Extract lines that match the conditions from a text file with python
[Personal memo] Get data on the Web and make it a DataFrame
The story of making a sound camera with Touch Designer and ReSpeaker
Make a DNN-CRF with Chainer and recognize the chord progression of music
Make a decision tree from 0 with Python and understand it (4. Data structure)
How to save the feature point information of an image in a file and use it for matching
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Make a Python program a daemon and run it automatically when the OS starts
Python --Read data from a numeric data file and find the multiple regression line.
I want to make a music player and file music at the same time
Get the stock price of a Japanese company with Python and make a graph
Extract only the file name excluding the directory in the directory
Make a copy of the list in Python
Extract files from RPM file without installing it
A discussion of the strengths and weaknesses of Python
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
Extract the lyrics information in the MP3 / MP4 file and save it in the lyrics file (* .lrc) for Sony walkman.
[Python3] Take a screenshot of a web page on the server and crop it further
Get the value of a specific key in a list from the dictionary type in the list with Python
The result of making a map album of Italy honeymoon in Python and sharing it
[Python] Change the text color and background color of a specific keyword in print output
Extract images and tables from pdf with python to reduce the burden of reporting
[Shell art] Only when it is a multiple of 3 and a number with 3 becomes stupid
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Read the csv file with jupyter notebook and write the graph on top of it
It seems that the module of train_test_split changes from 0.20, and Deprecation Warning appears at 0.18.
Make a list of latitude and longitude and convert UTM coordinates at once → File output
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row
PGM that takes the difference of the specified rectangular area from the camera and makes a sound when the rate of change exceeds a certain rate
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~