skytnt commited on
Commit
e47ebd2
1 Parent(s): 1752002

Update midi_synthesizer.py

Browse files
Files changed (1) hide show
  1. 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
- 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
- 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
- return synthesized
 
 
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