How can I write a MIDI file with Python?

后端 未结 2 1014
梦如初夏
梦如初夏 2021-02-02 02:22

I am writing a script to convert a picture into MIDI notes based on the RGBA values of the individual pixels. However, I cannot seem to get the last step working, which is to ac

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 02:47

    Looking at the sample, something like

    from midiutil.MidiFile import MIDIFile
    
    # create your MIDI object
    mf = MIDIFile(1)     # only 1 track
    track = 0   # the only track
    
    time = 0    # start at the beginning
    mf.addTrackName(track, time, "Sample Track")
    mf.addTempo(track, time, 120)
    
    # add some notes
    channel = 0
    volume = 100
    
    pitch = 60           # C4 (middle C)
    time = 0             # start on beat 0
    duration = 1         # 1 beat long
    mf.addNote(track, channel, pitch, time, duration, volume)
    
    pitch = 64           # E4
    time = 2             # start on beat 2
    duration = 1         # 1 beat long
    mf.addNote(track, channel, pitch, time, duration, volume)
    
    pitch = 67           # G4
    time = 4             # start on beat 4
    duration = 1         # 1 beat long
    mf.addNote(track, channel, pitch, time, duration, volume)
    
    # write it to disk
    with open("output.mid", 'wb') as outf:
        mf.writeFile(outf)
    

提交回复
热议问题