-
Notifications
You must be signed in to change notification settings - Fork 0
/
superpermidi.py
executable file
·75 lines (62 loc) · 2.16 KB
/
superpermidi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
import argparse
import enum
import sys
import aubio
from midiutil.MidiFile import MAJOR, SHARPS, MIDIFile
def write_file(fname, mf):
with open(fname, 'wb') as outf:
mf.writeFile(outf)
def start_midifile(track, time, fname_no_ext, channel, volume, bpm):
mf = MIDIFile(1)
mf.addTrackName(track, time, fname_no_ext)
mf.addTempo(track, time, bpm)
mf.addKeySignature(track, time, 0, SHARPS, MAJOR)
return mf
def write_notes(fname, mf, lookup, track, channel, volume):
# source / writing midi file:
# https://stackoverflow.com/questions/11059801/how-can-i-write-a-midi-file-with-python
with open(fname, 'r') as f:
for l in f:
if not l.startswith("#"):
for i, n in enumerate(l):
try:
pitch = lookup[n]
time = i/4 # 4 per beat
duration = 1./4 # quater note
mf.addNote(track, channel, pitch,
time, duration, volume)
except KeyError:
if n != '\n':
print("KeyError: " + n)
pass
return mf
def get_lookup():
# ref note numbers: https://newt.phys.unsw.edu.au/jw/notes.html
lookup = {
'1': 60, # C4
'2': 62, # D
'3': 64, # E
'4': 65, # F
'5': 67, # G
'6': 69, # A
'7': 71 # B
}
return lookup
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Parse superperm txt file into midi file.')
parser.add_argument('fname', type=open)
parser.add_argument('--bpm', dest='bpm', type=int, default=120,
help='Speed fo the file (bpm)')
args = parser.parse_args()
fname = args.fname.name
fname_no_ext = fname.split('.')[0]
track = 0
time = 0
channel = 0
volume = 100
lookup = get_lookup()
mf = start_midifile(track, time, fname_no_ext, channel, volume, args.bpm)
mf = write_notes(fname, mf, lookup, track, channel, volume)
write_file(fname_no_ext+".mid", mf)