MIDIUtil
MIDIUtil copied to clipboard
How to change instrument?
Could you please give an example with two or more instruments? https://github.com/MarkCWirt/MIDIUtil/issues/1 This is unclear to me..
This is done with the program change. As an example, the following snippet creates two tracks, assigns program 0 to channel 0 and program 6 to channel 1. Then it adds two notes to the separate track and channel.
If you listed to the MIDI file with a player that maps pograms to General MIDI, you will hear the first note as a piano, and the second as a harpsicord.
from midiutil import MIDIFile
# Create a two-track file
MyMIDI = MIDIFile(2, adjust_origin=False)
MyMIDI.addTempo(0, 0, 30)
MyMIDI.addProgramChange(0, 0, 0, 0)
MyMIDI.addProgramChange(0, 1, 0, 6)
track = 0
time = 0
channel = 0
pitch = 60
duration = 1
volume = 100
MyMIDI.addNote(track, channel, pitch, time, duration, volume)
track = 1
time = 1
channel = 1
MyMIDI.addNote(track, channel, pitch, time, duration, volume)
with open("output.mid", 'wb') as binfile:
MyMIDI.writeFile(binfile)
Note that the library uses a zero-origin for program, whereas most documentation is one-origined!