#!/usr/bin/env python3

import math
from midi import midi
from pico8.game import game

path = 'bwv578.mid'
CART_PATH = 'bwv578.p8'

m = midi.MidiFile()
m.open(path)
m.read()
#print(m.tracks[1])

# lowest midi pitch: 43
# g in pico8: 31
# g in MIDI: 67
# MIDI pitch - 36 = pico8 pitch

#ticksPerQuarterNote = m.tracks[0].events[1].data[2]
#print(ticksPerQuarterNote = m.tracks[0].events[1].data[2])
ticksPerQuarterNote = 240

picoNotes = []
previousTime = 0
previousPicoNote = None

for event in m.tracks[1].events:
    if event.type == 'NOTE_ON' and event.velocity > 0:
        #print(event)

        # Repeat the previous PICO-8 note as necessary to match the length of the MIDI note
        previousNoteLength = (event.time - previousTime) / ticksPerQuarterNote
        picoNoteCount = int(previousNoteLength * 8)
        for i in range(picoNoteCount - 1):
            picoNotes.append(previousPicoNote)

        note = {}
        note['pitch'] = event.pitch - 36
        note['volume'] = math.floor((event.velocity / 127) * 7)
        picoNotes.append(note)

        previousPicoNote = note
        previousTime = event.time


print(picoNotes)


cart = game.Game.from_filename(CART_PATH)

i = 0
for patternIndex in range(64):
    for noteIndex in range(32):
        note = picoNotes[i]

        cart.sfx.set_note(patternIndex, noteIndex,
                         pitch = note['pitch'],
                         volume = note['volume'],
                         waveform = 0)

with open(CART_PATH, 'wb') as outfile:
    cart.to_p8_file(outfile)

#print(cart.sfx.get_note(0, 0))
#print(cart.sfx.get_note(0, 8))

@@ -15,16 +15,18 @@
  python3
+.5
 %0A%0Aimport
@@ -1284,16 +1284,95 @@
 otes%5Bi%5D%0A
+        i += 1%0A%0A        cart.sfx.set_properties(patternIndex, note_duration=14)
 %0A       
@@ -1557,45 +1557,170 @@
 0)%0A%0A
-with open(CART_PATH, 'wb') as outfile
+sfxIndex = 0%0Afor musicIndex in range(64):%0A    cart.music.set_channel(musicIndex, 0, sfxIndex)%0A    sfxIndex += 1%0A%0Awith open(CART_PATH, 'w', encoding='utf-8') as fh
 :%0A  
@@ -1741,15 +1741,10 @@
 ile(
-outfile
+fh
 )%0A%0A#

Make it work for the first channel!