Untitled
unknown
python
2 years ago
3.0 kB
9
No Index
import mido
import os
# Define the input and output file paths
input_file_path = 'Requiem - Der Wille Von Surtur, Der Macht Von Banhammer.mid'
input_file_path = 'test.mid'
output_file_path = 'Requiem - Der Wille Von Surtur, Der Macht Von Banhammer_out.mid'
output_file_path = 'test2.mid'
def clean_midi_file(input_path, output_path):
# Load the MIDI file
midi = mido.MidiFile(input_path)
# Create a new MIDI file for output, preserving the same ticks per beat (timing information)
cleaned_midi = mido.MidiFile(ticks_per_beat=midi.ticks_per_beat)
# Process each track in the original MIDI file
for track in midi.tracks:
cleaned_track = mido.MidiTrack() # New track to store cleaned messages
cleaned_midi.tracks.append(cleaned_track)
# Process all messages in the track
accumulated_time = 0 # Used to accumulate time of removed messages
for msg in track:
print(f'total : {msg}')
# Only process the message types we care about
if msg.type in ['note_on', 'note_off', 'set_tempo', 'time_signature', 'key_signature', 'sysex']:
# Add the message as is
msg.time += accumulated_time
cleaned_track.append(msg)
accumulated_time = 0 # Reset after applying
elif msg.is_meta:
# Keep all meta messages (tempo, time signature, etc.)
msg.time += accumulated_time
cleaned_track.append(msg)
accumulated_time = 0 # Reset after applying
elif msg.type == 'control_change':
# Remove certain Control Change messages like modulation (CC1), expression (CC11), etc.
if msg.control not in [64]: # Preserve Control Change 64 (Sustain Pedal)
accumulated_time += msg.time
print(f'Removed Control Change: {msg}')
print(f'Times : {msg.time} , {accumulated_time}')
else:
msg.time += accumulated_time
cleaned_track.append(msg)
accumulated_time = 0 # Reset after applying
else:
# Keep all other messages except aftertouch, program_change, and pitchwheel
if msg.type not in ['aftertouch', 'program_change', 'pitchwheel']:
msg.time += accumulated_time
cleaned_track.append(msg)
accumulated_time = 0 # Reset after applying
# Save the cleaned MIDI file
cleaned_midi.save(output_path)
print(f'Cleaned MIDI file saved to {output_path}')
# Clean the MIDI file
if __name__ == "__main__":
if os.path.exists(input_file_path):
clean_midi_file(input_file_path, output_file_path)
else:
print(f'Input file {input_file_path} does not exist.')
Editor is loading...
Leave a Comment