Skip to content

Instantly share code, notes, and snippets.

@ZoomTen
Last active June 8, 2023 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZoomTen/3208a493181744d99dfea655a45dc024 to your computer and use it in GitHub Desktop.
Save ZoomTen/3208a493181744d99dfea655a45dc024 to your computer and use it in GitHub Desktop.
Given MIDICSV output, convert MIDI timebase to a target timebase
#!/usr/bin/python
import csv
import sys
if len(sys.argv) < 2:
print("midicsv_convert_timebases.py [to_timebase]")
print()
print("e.g. midicsv MYMIDI.mid | midicsv_convert_timebases.py 96 | csvmidi - > MYMIDI_corrected.mid")
exit(0)
to_timebase = int(sys.argv[1].strip())
# CSV entry list.
command_bin = []
with sys.stdin as csvfile:
reader = csv.reader(csvfile, delimiter=',')
old_tick = -1
from_timebase = -1
for row in reader:
track = int(row[0])
tick = int(row[1])
event = row[2].lower().strip()
data = [x.strip() for x in row[3:]]
if event == "header":
from_timebase = int(data[2])
data[2] = str(to_timebase)
if from_timebase > 0:
tick = round(tick/from_timebase*to_timebase)
csv_string = "%d, %d, %s, %s" % (
track, tick, event, ', '.join(data)
)
command_bin.append(csv_string)
sys.stdout.write(
'\n'.join(command_bin)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment