fix the error please and submit fully edited code

from midiutil import MIDIFile

# --- Constants ---
VELOCITY_LOW = 60
VELOCITY_MID = 85
VELOCITY_HIGH = 110
VELOCITY_ACCENT = 127

# Tracks/Channels
PIANO_TRACK, SYNTH_PAD_TRACK, BASS_TRACK, GUITAR_TRACK, SOLO_PIANO_TRACK = 0, 1, 2, 3, 4
PERCUSSION_TRACK = 9
PIANO_CHANNEL, SYNTH_PAD_CHANNEL, BASS_CHANNEL, GUITAR_CHANNEL, SOLO_PIANO_CHANNEL = 0, 1, 2, 3, 4
PERCUSSION_CHANNEL = 9

# Programs
GRAND_PIANO, SYNTH_PAD_WARM, FRETLESS_BASS, DISTORTION_GUITAR, HONKY_TONK_PIANO = 0, 89, 35, 30, 3

# Percussion Notes
KICK, SNARE, HIHAT_CLOSED, HIHAT_OPEN, CRASH = 36, 38, 42, 46, 49

def add_core_melody(midi, time, velocity, octave_shift=0):
    note_duration = 0.25
    measure_type = 'B' if int(time / 4) % 2 != 0 else 'A'
    notes = {0: [36, 48], 1: [60, 64], 2: [31, 43], 3: [60, 64], 3.75: [47] if measure_type == 'B' else []}
    for beat, pitches in notes.items():
        for pitch in pitches:
            midi.addNote(PIANO_TRACK, PIANO_CHANNEL, pitch + octave_shift, time + beat, note_duration, velocity)

def add_threat_guitar_riff(midi, time, velocity):
    for beat in range(4):
        pitch_base = 36 if beat % 2 == 0 else 31
        for offset in [0.25, 0.5, 1.25]:
            midi.addNote(GUITAR_TRACK, GUITAR_CHANNEL, pitch_base, time + beat + offset, 0.25, velocity)
            midi.addNote(GUITAR_TRACK, GUITAR_CHANNEL, pitch_base + 6, time + beat + offset + 0.05, 0.25, velocity - 10)

def add_threat_bassline(midi, time, velocity):
    pattern = [36, 31, 29, 36]
    for i, root in enumerate(pattern):
        midi.addNote(BASS_TRACK, BASS_CHANNEL, root, time + i, 0.4, velocity)
        midi.addNote(BASS_TRACK, BASS_CHANNEL, root, time + i + 0.75, 0.2, velocity - 10)

def add_drums(midi, time, phase="intense"):
    for i in range(8):
        pos = time + i * 0.5
        midi.addNote(PERCUSSION_TRACK, PERCUSSION_CHANNEL, KICK if i % 2 == 0 else SNARE, pos, 0.4, VELOCITY_HIGH)
        if phase == "intense":
            midi.addNote(PERCUSSION_TRACK, PERCUSSION_CHANNEL, HIHAT_CLOSED, pos + 0.25, 0.25, VELOCITY_MID)

def add_ambient_pad(midi, time, velocity):
    for note in [24, 26, 29]:
        midi.addNote(SYNTH_PAD_TRACK, SYNTH_PAD_CHANNEL, note, time, 4, velocity)

def add_improved_piano_solo(midi, time):
    run = [72, 74, 76, 77, 79, 81, 83, 84]
    for i in range(8):
        midi.addNote(SOLO_PIANO_TRACK, SOLO_PIANO_CHANNEL, run[i], time + i*0.25, 0.25, VELOCITY_ACCENT)

    stabs = [(84, 0.5), (81, 0.75), (48, 1.0), (50, 1.5)]
    for note, offset in stabs:
        midi.addNote(SOLO_PIANO_TRACK, SOLO_PIANO_CHANNEL, note, time + offset + 2, 0.5, VELOCITY_HIGH)

    end_run = [72 + i for i in range(10)]
    for i, pitch in enumerate(end_run):
        midi.addNote(SOLO_PIANO_TRACK, SOLO_PIANO_CHANNEL, pitch, time + 4 + i * 0.2, 0.2, VELOCITY_HIGH + i)

def build_song(midi):
    current_time = 0

    instruments = [(PIANO_TRACK, PIANO_CHANNEL, GRAND_PIANO),
                   (SYNTH_PAD_TRACK, SYNTH_PAD_CHANNEL, SYNTH_PAD_WARM),
                   (BASS_TRACK, BASS_CHANNEL, FRETLESS_BASS),
                   (GUITAR_TRACK, GUITAR_CHANNEL, DISTORTION_GUITAR),
                   (SOLO_PIANO_TRACK, SOLO_PIANO_CHANNEL, HONKY_TONK_PIANO)]

    for track, ch, prog in instruments:
        midi.addProgramChange(track, ch, 0, prog)

    midi.addTempo(PIANO_TRACK, 0, 115)  # Phase 1: slow, weary

    for _ in range(3):  # Phase 1
        add_core_melody(midi, current_time, VELOCITY_LOW)
        add_ambient_pad(midi, current_time, VELOCITY_LOW - 10)
        current_time += 4

    midi.addTempo(PIANO_TRACK, current_time, 125)  # Phase 2: more motion
    for _ in range(3):
        add_core_melody(midi, current_time, VELOCITY_MID)
        add_drums(midi, current_time, phase="soft")
        add_ambient_pad(midi, current_time, VELOCITY_LOW)
        current_time += 4

    midi.addTempo(PIANO_TRACK, current_time, 145)  # Phase 3
    for _ in range(4):
        add_core_melody(midi, current_time, VELOCITY_HIGH)
        add_threat_guitar_riff(midi, current_time, VELOCITY_HIGH)
        add_drums(midi, current_time, phase="medium")
        add_threat_bassline(midi, current_time, VELOCITY_MID)
        current_time += 4

    # --- Phase 4: Final Chase (2 minutes = 120 sec = 240 beats at 120 BPM or 300 at 150 BPM) ---
    midi.addTempo(PIANO_TRACK, current_time, 170)  # High BPM
    for _ in range(37):  # ~148 seconds worth (37 * 4 = 148 beats)
        add_core_melody(midi, current_time, VELOCITY_ACCENT, octave_shift=12)
        add_threat_guitar_riff(midi, current_time, VELOCITY_ACCENT - 5)
        add_threat_bassline(midi, current_time, VELOCITY_ACCENT)
        add_drums(midi, current_time, phase="intense")
        current_time += 4

    # --- Final Piano Solo ---
    for _ in range(6):
        add_improved_piano_solo(midi, current_time)
        add_threat_bassline(midi, current_time, VELOCITY_HIGH)
        add_drums(midi, current_time, phase="intense")
        current_time += 4

    # --- Outro ---
    midi.addNote(PERCUSSION_TRACK, PERCUSSION_CHANNEL, CRASH, current_time, 4, VELOCITY_ACCENT)
    add_core_melody(midi, current_time, VELOCITY_ACCENT)

# Main
if __name__ == "__main__":
    midi = MIDIFile(10)
    build_song(midi)
    with open("groove_of_the_pursuer.mid", "wb") as f:
        midi.writeFile(f)

(venv) C:\home\mulltrik\Projects\watcharthurnoaw> python gyat.py
Traceback (most recent call last):
  File "/home/mulltrik/Projects/watcharthurnoaw/gyat.py", line 125, in <module>
    midi.writeFile(f)
    ~~~~~~~~~~~~~~^^^
  File "/home/mulltrik/Projects/watcharthurnoaw/venv/lib/python3.13/site-packages/midiutil/MidiFile.py", line 1637, in writeFile
    self.close()
    ~~~~~~~~~~^^
  File "/home/mulltrik/Projects/watcharthurnoaw/venv/lib/python3.13/site-packages/midiutil/MidiFile.py", line 1688, in close
    self.tracks[i].closeTrack()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/mulltrik/Projects/watcharthurnoaw/venv/lib/python3.13/site-packages/midiutil/MidiFile.py", line 826, in closeTrack
    self.processEventList()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/mulltrik/Projects/watcharthurnoaw/venv/lib/python3.13/site-packages/midiutil/MidiFile.py", line 789, in processEventList
    self.deInterleaveNotes()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/mulltrik/Projects/watcharthurnoaw/venv/lib/python3.13/site-packages/midiutil/MidiFile.py", line 889, in deInterleaveNotes
    stack[noteeventkey].pop()
    ~~~~~~~~~~~~~~~~~~~~~~~^^