[Python] Trying to write a midi file with 2 tracks

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a midi file (using midiutils) so when I open it in eg Audacity it will be two tracks, one called track 1 and one called track 2. however - it combines them into one track. I am specifying two tracks and stating file format 2 (I have also tried format 1), both should produce multiple tracks.

    try and write a midi file with 2 tracks


    # midi
    from midiutil import MIDIFile
    # now create the midi file
    numTracks = 2
    #track = 1
    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(2,file_format=2) # two tracks,
    MyMIDI.addTempo(0,time, tempo)
    MyMIDI.addTrackName(0, time, "Track1")
    MyMIDI.addNote(0, channel, 60, time, duration, volume)
    MyMIDI.addTrackName(1, time, "Track2")
    MyMIDI.addNote(1,channel, 62, time, duration, volume)
    output_path = "D:/python_dev/test5.mid"
    with open(output_path, "wb") as midi_file:
    MyMIDI.writeFile(midi_file)

    Continue reading...

Compartilhe esta Página