Spaces:
Runtime error
Runtime error
Update midi_synthesizer.py
Browse files- midi_synthesizer.py +38 -44
midi_synthesizer.py
CHANGED
@@ -4,51 +4,45 @@ import numpy as np
|
|
4 |
|
5 |
def synthesis(midi_opus, soundfont_path, sample_rate=44100):
|
6 |
ticks_per_beat = midi_opus[0]
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
fl = fluidsynth.Synth(samplerate=float(sample_rate))
|
10 |
sfid = fl.sfload(soundfont_path)
|
11 |
-
|
12 |
-
for
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
if
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
fl.cc(c, cc, v)
|
37 |
-
elif name == "note_on" and event[3] > 0:
|
38 |
-
c, p, v = event[2:5]
|
39 |
-
fl.noteon(c, p, v)
|
40 |
-
elif name == "note_off" or (name == "note_on" and event[3] == 0):
|
41 |
-
c, p = event[2:4]
|
42 |
-
fl.noteoff(c, p)
|
43 |
-
waveforms.append(ss)
|
44 |
-
fl.delete()
|
45 |
-
synthesized = np.zeros((max([w.shape[0] for w in waveforms]), 2), dtype=np.int32)
|
46 |
-
for waveform in waveforms:
|
47 |
-
synthesized[:waveform.shape[0]] += waveform
|
48 |
-
if synthesized.shape[0] > 0:
|
49 |
-
max_val = np.abs(synthesized).max()
|
50 |
-
if max_val != 0:
|
51 |
-
synthesized = (synthesized / max_val) * np.iinfo(np.int16).max
|
52 |
-
synthesized = synthesized.astype(np.int16)
|
53 |
|
54 |
-
|
|
|
|
4 |
|
5 |
def synthesis(midi_opus, soundfont_path, sample_rate=44100):
|
6 |
ticks_per_beat = midi_opus[0]
|
7 |
+
event_list = []
|
8 |
+
for track_idx, track in enumerate(midi_opus[1:]):
|
9 |
+
abs_t = 0
|
10 |
+
for event in track:
|
11 |
+
abs_t += event[1]
|
12 |
+
event_new = [*event]
|
13 |
+
event_new[1] = abs_t
|
14 |
+
event_list.append(event_new)
|
15 |
+
event_list = sorted(event_list, key=lambda e: e[1])
|
16 |
|
17 |
+
tempo = int((60 / 120) * 10 ** 6) # default 120 bpm
|
18 |
+
ss = np.empty((0, 2), dtype=np.int16)
|
19 |
fl = fluidsynth.Synth(samplerate=float(sample_rate))
|
20 |
sfid = fl.sfload(soundfont_path)
|
21 |
+
last_t = 0
|
22 |
+
for c in range(16):
|
23 |
+
fl.program_select(c, sfid, 128 if c == 9 else 0, 0)
|
24 |
+
for event in event_list:
|
25 |
+
name = event[0]
|
26 |
+
sample_len = int(((event[1] / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate)
|
27 |
+
sample_len -= int(((last_t / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate)
|
28 |
+
last_t = event[1]
|
29 |
+
if sample_len > 0:
|
30 |
+
sample = fl.get_samples(sample_len).reshape(sample_len, 2)
|
31 |
+
ss = np.concatenate([ss, sample])
|
32 |
+
if name == "set_tempo":
|
33 |
+
tempo = event[2]
|
34 |
+
elif name == "patch_change":
|
35 |
+
c, p = event[2:4]
|
36 |
+
fl.program_select(c, sfid, 128 if c == 9 else 0, p)
|
37 |
+
elif name == "control_change":
|
38 |
+
c, cc, v = event[2:5]
|
39 |
+
fl.cc(c, cc, v)
|
40 |
+
elif name == "note_on" and event[3] > 0:
|
41 |
+
c, p, v = event[2:5]
|
42 |
+
fl.noteon(c, p, v)
|
43 |
+
elif name == "note_off" or (name == "note_on" and event[3] == 0):
|
44 |
+
c, p = event[2:4]
|
45 |
+
fl.noteoff(c, p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
fl.delete()
|
48 |
+
return ss
|