MIDIUtil
MIDIUtil copied to clipboard
How to create a multi channel track
I'm trying to expand on the example file but cant find documentation on adding multiple channels.
what if I wanted say 3 channels.
Channel 1: degrees = [60, 62, 64, 65, 67, 69, 71, 72]
Channel 2: degrees = [72, 71, 69, 67, 65, 64, 62, 60]
Channel 3: degrees = [50, ---- 50, ---- 50, ---- 50]
how can I accomplish this
#!/usr/bin/env python
from midiutil import MIDIFile
degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
# automatically)
MyMIDI.addTempo(track, time, tempo)
for i, pitch in enumerate(degrees):
MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
with open("major-scale.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
Hi! You don't need to add a channel manually, just give a different one as an argument to addNote
.
Here's an example script using your degrees:
#!/usr/bin/env python
from midiutil import MIDIFile
degrees = [60, 62, 64, 65, 67, 69, 71, 72] # MIDI note number
track = 0
channel = 0
time = 0 # In beats
duration = 1 # In beats
tempo = 60 # In BPM
volume = 100 # 0-127, as per the MIDI standard
MyMIDI = MIDIFile(1) # One track, defaults to format 1 (tempo track is created
# automatically)
MyMIDI.addTempo(track, time, tempo)
def addNotes(channel, degrees):
for i, pitch in enumerate(degrees):
if pitch >= 0: # MIDI note numbers are greater than zero, so this
# allows skipping beats using negative degrees
MyMIDI.addNote(track, channel, pitch, time + i, duration, volume)
addNotes(channel=channel, # function argument "channel" gets the variable "channel"
degrees=degrees) # same with degrees
addNotes(channel=1, # here's a more direct approach
degrees=[72, 71, 69, 67, 65, 64, 62, 60])
addNotes(channel=2, # the same with some beats skipped
degrees=[50, -1, 50, -1, 50, -1, 50])
with open("pyquest.mid", "wb") as output_file:
MyMIDI.writeFile(output_file)
And here's the resulting MIDI in MuseScore:
Hope this helps!