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.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.
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')
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