You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.8 KiB
61 lines
1.8 KiB
import sys
|
|
import os
|
|
from mido import MidiFile, MidiTrack, merge_tracks
|
|
|
|
def convert_to_type0(input_filepath):
|
|
"""
|
|
Converts a MIDI file to Type 0 and saves it with "_0" appended to the original filename.
|
|
|
|
Parameters:
|
|
input_filepath (str): Path to the input MIDI file.
|
|
|
|
Returns:
|
|
output_filepath (str): Path to the converted Type 0 MIDI file.
|
|
"""
|
|
try:
|
|
# Load the original MIDI file
|
|
midi = MidiFile(input_filepath)
|
|
print(f"Loaded MIDI file: {input_filepath}")
|
|
print(f"Original MIDI type: {midi.type}")
|
|
print(f"Number of tracks: {len(midi.tracks)}")
|
|
|
|
# Merge all tracks into one
|
|
merged_track = merge_tracks(midi.tracks)
|
|
print("Merged all tracks into a single track for Type 0 MIDI.")
|
|
|
|
# Create a new MIDI file with one track
|
|
type0_midi = MidiFile()
|
|
type0_midi.type = 0 # Explicitly set to Type 0
|
|
type0_midi.ticks_per_beat = midi.ticks_per_beat # Preserve timing
|
|
|
|
type0_midi.tracks.append(MidiTrack(merged_track))
|
|
|
|
# Prepare output file name
|
|
base, ext = os.path.splitext(input_filepath)
|
|
output_filepath = f"{base}_0{ext}"
|
|
|
|
# Save the new Type 0 MIDI file
|
|
type0_midi.save(output_filepath)
|
|
print(f"Type 0 MIDI file saved as: {output_filepath}")
|
|
|
|
return output_filepath
|
|
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
sys.exit(1)
|
|
|
|
def main():
|
|
if len(sys.argv) != 2:
|
|
print("Usage: python midi_to_type0.py <input_midi_file>")
|
|
sys.exit(1)
|
|
|
|
input_filepath = sys.argv[1]
|
|
|
|
if not os.path.isfile(input_filepath):
|
|
print(f"Error: File '{input_filepath}' does not exist.")
|
|
sys.exit(1)
|
|
|
|
convert_to_type0(input_filepath)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|