Skip to content

Instantly share code, notes, and snippets.

@SamusAranX
Last active April 5, 2022 09:07
Show Gist options
  • Save SamusAranX/26e2653ffade956f3e531d7aa2666ef0 to your computer and use it in GitHub Desktop.
Save SamusAranX/26e2653ffade956f3e531d7aa2666ef0 to your computer and use it in GitHub Desktop.
MIDI reverb remover

Usage

First, install python-midi:

pip3 install "https://github.com/mgedmin/python-midi/archive/refs/heads/python3.zip"

Then, run this script:

./noreverb.py path/to/midi/directory

The script will create a directory called "noreverb" inside the specified directory and copy the output MIDI files into that.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import midi, glob, os, sys, re, fnmatch, shutil, argparse
def nglob(which, where='.'):
rule = re.compile(fnmatch.translate(which), re.IGNORECASE)
return [name for name in os.listdir(where) if rule.match(name)]
set_to_zero_cc_events = [91, 93] # Reverb/Chorus
set_to_zero_evt_found = False
blacklisted_cc_events = [9, 38, 100, 101] #
parser = argparse.ArgumentParser(description="Remove MIDI reverb")
parser.add_argument("mididirpath", type=str, help="The path to a folder containing MIDI files")
parser.add_argument("-s", "--soundfonts", action="store_true", help="Copy soundfonts to output dir")
args = parser.parse_args()
midi_path = os.path.abspath(args.mididirpath)
midi_files = nglob("*.mid", midi_path) + nglob("*.midi", midi_path)
soundfonts = nglob("*.sf2", midi_path) + nglob("*.dls", midi_path)
print(midi_files)
noreverb_path = os.path.join(midi_path, "noreverb")
os.makedirs(noreverb_path, exist_ok=True)
for file in midi_files:
t = 0
print("Processing: " + file)
midi_pattern = midi.read_midifile(os.path.join(midi_path, file))
new_pattern = midi.Pattern()
new_pattern.resolution = midi_pattern.resolution
for track in midi_pattern:
print("Track " + str(t))
set_to_zero_evt_found = False
track_channel = 0
for event in track:
track_copy = track.copy()
event_type = type(event)
try:
track_channel = event.channel
except:
pass
if event_type is midi.events.ControlChangeEvent:
if event.data[0] in blacklisted_cc_events:
print("blacklisted: " + str(event))
track_copy.remove(event)
elif event.data[0] in set_to_zero_cc_events:
set_to_zero_evt_found = True
print("set to zero: " + str(event))
new_event = midi.events.ControlChangeEvent(tick=event.tick, channel=event.channel, control=event.control, value=0)
print(new_event)
evt_index = track.index(event)
track_copy.remove(event)
track_copy.insert(evt_index, new_event)
if not set_to_zero_evt_found:
print("Manually setting reverb and chorus on track " + str(t) + " to zero")
reverb = midi.events.ControlChangeEvent(tick=0, channel=track_channel, control=91, value=0)
chorus = midi.events.ControlChangeEvent(tick=0, channel=track_channel, control=93, value=0)
# reset = midi.events.ControlChangeEvent(tick=0, channel=track_channel, control=121, value=0)
print(reverb)
print(chorus)
# print(reset)
track_copy.insert(0, reverb)
track_copy.insert(0, chorus)
# track_copy.insert(0, reset)
new_pattern.append(track_copy)
t += 1
midi.write_midifile(os.path.join(noreverb_path, os.path.basename(file)), new_pattern)
if args.soundfonts:
print("Copying soundfonts…")
for sf in soundfonts:
print(sf)
shutil.copy2(os.path.join(midi_path, sf), os.path.join(noreverb_path, os.path.basename(sf)))
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment