LoginSignup
26
27

More than 5 years have passed since last update.

pretty_midiを使って、PythonでMIDIファイルを作成する

Posted at

はじめに

TensorFlow製の音楽生成プロジェクト「Magenta」をいじっています。

参考:サカナクションさんをTensorFlow製アート・音楽生成プロジェクト「Magenta」に学習させてみる。

その前処理として、PythonでMIDIファイルをいじりたいなと思い調べたところ、Magentaではpretty_midiを利用しているようでした。
この投稿では、pretty_midi 0.2.6 documentationのサンプルを動かして、pretty_midiの仕組みについてみてみます。

サンプルコード

サンプルコードと、コメントを書き記載します。

sample.py
import pretty_midi

# Create a PrettyMIDI object
# Pretty MIDIオブジェクトを作る。
cello_c_chord = pretty_midi.PrettyMIDI()

# Create an Instrument instance for a cello instrument
# Instrument Instanceを作る。ここではCello

# 楽器名を入れると、対応するGeneral MIDI program numberを返してくれる
cello_program = pretty_midi.instrument_name_to_program('Cello')


# Instrument instanceをCelloとして作成
cello = pretty_midi.Instrument(program=cello_program)


# Iterate over note names, which will be converted to note number later
# メロディをNoteNameで記載していますが、後ほどNoteNumberに変換されます。

for note_name in ['C5', 'E5', 'G5']:
    # Retrieve the MIDI note number for this note name
    # NoteNameからNote Numberを検索しています。
    note_number = pretty_midi.note_name_to_number(note_name)

    # Create a Note instance, starting at 0s and ending at .5s
    # NoteInstanceを作成します。音(pitch)の開始時間と終了時間、
    # velocityを定義します。
    note = pretty_midi.Note(
        velocity=100, pitch=note_number, start=0, end=.5)

    # Add it to our cello instrument
    # 上記で作成したNoteInstanceをCelloInstrumentに加えます。
    cello.notes.append(note)


# Add the cello instrument to the PrettyMIDI object
# ChelloInstrumentをPrettyMIDIオブジェクトに加えます。
cello_c_chord.instruments.append(cello)


# Write out the MIDI data
# PrettyMIDIオブジェクトをMIDIファイルとして書き出しましょう。
cello_c_chord.write('cello-C-chord.mid')

作成されたMIDI

ドミソの音が0.5秒(120 BPMだと一拍)のMIDIファイルができました。(logic Xで表示)

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

終わりに

次は、既存のMIDIファイルを読み込み、楽器ごとにファイルを分割する、ということをやってみたいと思います。

ありがとうございました。

26
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
27