Skip to content

Instantly share code, notes, and snippets.

@weibangtuo
Last active May 25, 2022 09:16
Show Gist options
  • Save weibangtuo/6a5a60a7984247548ad685d77fbd9f58 to your computer and use it in GitHub Desktop.
Save weibangtuo/6a5a60a7984247548ad685d77fbd9f58 to your computer and use it in GitHub Desktop.
MIDI file type convert
import mido
import os
def convert_type0_type1(midi_type0_file):
type_0 = mido.MidiFile(midi_type0_file)
if mid.type != 0:
return
type_1 = mido.MidiFile(
type=1,
ticks_per_beat=type_0.ticks_per_beat)
time = 0
channels = {}
for item in type_0.tracks[0]:
msg = item.copy()
if hasattr(msg, 'time'):
time += msg.time
channel = msg.channel if hasattr(msg, 'channel') else -1
if channel not in channels:
channels[channel] = {
'time': 0,
'msg': []
}
chl = channels[channel]
if hasattr(msg, 'time'):
msg.time = time - chl['time']
chl['time'] = time
chl['msg'].append(msg)
for chan in channels:
track = type_1.add_track()
track.extend(channels[chan]['msg'])
type_1.save(os.path.join('~/type_0_to_1_result', os.path.basename(mid.filename)))
@Majda995
Copy link

Hello,
is there a solution to convert midi type_1 to type_0 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment