Spaces:
Running
on
Zero
Running
on
Zero
Update midi_synthesizer.py
Browse files- midi_synthesizer.py +43 -38
midi_synthesizer.py
CHANGED
@@ -4,45 +4,50 @@ import numpy as np
|
|
4 |
|
5 |
def synthesis(midi_opus, soundfont_path, sample_rate=44100):
|
6 |
ticks_per_beat = midi_opus[0]
|
7 |
-
|
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 |
-
|
22 |
-
for
|
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 |
fl.delete()
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def synthesis(midi_opus, soundfont_path, sample_rate=44100):
|
6 |
ticks_per_beat = midi_opus[0]
|
7 |
+
tempo = int((60 / 140) * 10 ** 6) # default 140 bpm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
9 |
fl = fluidsynth.Synth(samplerate=float(sample_rate))
|
10 |
sfid = fl.sfload(soundfont_path)
|
11 |
+
waveforms = []
|
12 |
+
for track_idx, track in enumerate(midi_opus[1:]):
|
13 |
+
ss = np.empty((0, 2), dtype=np.int16)
|
14 |
+
cur_t = 0
|
15 |
+
last_t = 0
|
16 |
+
fl.system_reset()
|
17 |
+
fl.get_samples(int(2 * sample_rate)) # get 2 seconds sample to eliminate reverb sound from previous track
|
18 |
+
for c in range(16):
|
19 |
+
fl.program_select(c, sfid, 128 if c == 9 else 0, 0)
|
20 |
+
for event in track:
|
21 |
+
name = event[0]
|
22 |
+
cur_t += event[1]
|
23 |
+
sample_len = int(((cur_t / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate)
|
24 |
+
sample_len -= int(((last_t / ticks_per_beat) * tempo / (10 ** 6)) * sample_rate)
|
25 |
+
last_t = cur_t
|
26 |
+
if sample_len > 0:
|
27 |
+
sample = fl.get_samples(sample_len).reshape(sample_len, 2)
|
28 |
+
ss = np.concatenate([ss, sample])
|
29 |
+
if name == "set_tempo":
|
30 |
+
tempo = event[2]
|
31 |
+
elif name == "patch_change":
|
32 |
+
c, p = event[2:4]
|
33 |
+
fl.program_select(c, sfid, 128 if c == 9 else 0, p)
|
34 |
+
elif name == "control_change":
|
35 |
+
c, cc, v = event[2:5]
|
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 |
+
max_val = np.abs(synthesized).max()
|
49 |
+
if max_val != 0:
|
50 |
+
synthesized = (synthesized / max_val) * np.iinfo(np.int16).max
|
51 |
+
synthesized = synthesized.astype(np.int16)
|
52 |
+
|
53 |
+
return synthesized
|