-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample2midi.py
61 lines (40 loc) · 1.38 KB
/
sample2midi.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
from midiutil.MidiFile import *
import pickle
from chorale import Chorale
# TODO testing
with open('model-chords-dur/DICTIONARY.pkl', 'rb') as f:
dictionary = pickle.load(f)
sample = open("sample.txt")
lines = sample.readlines()
sample.close()
input_midi = MIDIFile(1, deinterleave=False)
input_midi.addTempo(0, 0, 100)
input_midi.addTimeSignature(0, 0, 4, 2, 24)
choraleManager = Chorale()
stressbar = False
def addnote(start, note, duration, newbar, channel, time):
if note == "P" or note == "":
return 1
volume = 80
if newbar and stressbar:
volume = 96
print(str(channel) + " " + str(choraleManager.notepitch(note)) +
" " + str(start) + " " + str(duration) + " " + str(volume))
if choraleManager.notepitch(note) == -1:
input_midi.addNote(0, channel, 0, start, 4, 0)
time += 4
else:
input_midi.addNote(0, channel, choraleManager.notepitch(note), start, duration, volume)
time = 0
duration = 1
for line in lines:
hid = int(line.split("\n")[0].split(" ")[0]) + 1
vis = int(line.split("\n")[0].split(" ")[1]) + 1
row = dictionary[str(hid) + " " + str(vis)].split(" ")
if row[0] == "END":
break
for i in range(0, 4):
addnote(time, row[i].replace("_", " "), duration, False, i, time)
time += 1
with open("sample.mid", "wb") as output_file:
input_midi.writeFile(output_file)