-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathseparate.py
172 lines (124 loc) · 7.77 KB
/
separate.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# A Wavenet For Source Separation - Francesc Lluis - 25.10.2018
# Separate.py
from __future__ import division
import os
import util
import tqdm
import numpy as np
def separate_sample(model, input, batch_size, output_filename_prefix, sample_rate, output_path, target):
if target == 'singing-voice':
if len(input['mixture']) < model.receptive_field_length:
raise ValueError('Input is not long enough to be used with this model.')
num_output_samples = input['mixture'].shape[0] - (model.receptive_field_length - 1)
num_fragments = int(np.ceil(num_output_samples / model.target_field_length))
num_batches = int(np.ceil(num_fragments / batch_size))
vocals_output = []
num_pad_values = 0
fragment_i = 0
for batch_i in tqdm.tqdm(range(0, num_batches)):
if batch_i == num_batches - 1: # If its the last batch
batch_size = num_fragments - batch_i * batch_size
input_batch = np.zeros((batch_size, model.input_length))
# Assemble batch
for batch_fragment_i in range(0, batch_size):
if fragment_i + model.target_field_length > num_output_samples:
remainder = input['mixture'][fragment_i:]
current_fragment = np.zeros((model.input_length,))
current_fragment[:remainder.shape[0]] = remainder
num_pad_values = model.input_length - remainder.shape[0]
else:
current_fragment = input['mixture'][fragment_i:fragment_i + model.input_length]
input_batch[batch_fragment_i, :] = current_fragment
fragment_i += model.target_field_length
separated_output_fragments = model.separate_batch({'data_input': input_batch})
if type(separated_output_fragments) is list:
vocals_output_fragment = separated_output_fragments[0]
vocals_output_fragment = vocals_output_fragment[:,
model.target_padding: model.target_padding + model.target_field_length]
vocals_output_fragment = vocals_output_fragment.flatten().tolist()
if type(separated_output_fragments) is float:
vocals_output_fragment = [vocals_output_fragment]
vocals_output = vocals_output + vocals_output_fragment
vocals_output = np.array(vocals_output)
if num_pad_values != 0:
vocals_output = vocals_output[:-num_pad_values]
mixture_valid_signal = input['mixture'][
model.half_receptive_field_length:model.half_receptive_field_length + len(vocals_output)]
accompaniment_output = mixture_valid_signal - vocals_output
output_vocals_filename = output_filename_prefix + '_vocals.wav'
output_accompaniment_filename = output_filename_prefix + '_accompaniment.wav'
output_vocals_filepath = os.path.join(output_path, output_vocals_filename)
output_accompaniment_filepath = os.path.join(output_path, output_accompaniment_filename)
util.write_wav(vocals_output, output_vocals_filepath, sample_rate)
util.write_wav(accompaniment_output, output_accompaniment_filepath, sample_rate)
if target == 'multi-instrument':
if len(input['mixture']) < model.receptive_field_length:
raise ValueError('Input is not long enough to be used with this model.')
num_output_samples = input['mixture'].shape[0] - (model.receptive_field_length - 1)
num_fragments = int(np.ceil(num_output_samples / model.target_field_length))
num_batches = int(np.ceil(num_fragments / batch_size))
vocals_output = []
drums_output = []
bass_output = []
num_pad_values = 0
fragment_i = 0
for batch_i in tqdm.tqdm(range(0, num_batches)):
if batch_i == num_batches - 1: # If its the last batch
batch_size = num_fragments - batch_i * batch_size
input_batch = np.zeros((batch_size, model.input_length))
# Assemble batch
for batch_fragment_i in range(0, batch_size):
if fragment_i + model.target_field_length > num_output_samples:
remainder = input['mixture'][fragment_i:]
current_fragment = np.zeros((model.input_length,))
current_fragment[:remainder.shape[0]] = remainder
num_pad_values = model.input_length - remainder.shape[0]
else:
current_fragment = input['mixture'][fragment_i:fragment_i + model.input_length]
input_batch[batch_fragment_i, :] = current_fragment
fragment_i += model.target_field_length
separated_output_fragments = model.separate_batch({'data_input': input_batch})
if type(separated_output_fragments) is list:
vocals_output_fragment = separated_output_fragments[0]
drums_output_fragment = separated_output_fragments[1]
bass_output_fragment = separated_output_fragments[2]
vocals_output_fragment = vocals_output_fragment[:,
model.target_padding: model.target_padding + model.target_field_length]
vocals_output_fragment = vocals_output_fragment.flatten().tolist()
drums_output_fragment = drums_output_fragment[:,
model.target_padding: model.target_padding + model.target_field_length]
drums_output_fragment = drums_output_fragment.flatten().tolist()
bass_output_fragment = bass_output_fragment[:,
model.target_padding: model.target_padding + model.target_field_length]
bass_output_fragment = bass_output_fragment.flatten().tolist()
if type(separated_output_fragments) is float:
vocals_output_fragment = [vocals_output_fragment]
if type(drums_output_fragment) is float:
drums_output_fragment = [drums_output_fragment]
if type(bass_output_fragment) is float:
bass_output_fragment = [bass_output_fragment]
vocals_output = vocals_output + vocals_output_fragment
drums_output = drums_output + drums_output_fragment
bass_output = bass_output + bass_output_fragment
vocals_output = np.array(vocals_output)
drums_output = np.array(drums_output)
bass_output = np.array(bass_output)
if num_pad_values != 0:
vocals_output = vocals_output[:-num_pad_values]
drums_output = drums_output[:-num_pad_values]
bass_output = bass_output[:-num_pad_values]
mixture_valid_signal = input['mixture'][
model.half_receptive_field_length:model.half_receptive_field_length + len(vocals_output)]
other_output = mixture_valid_signal - vocals_output - drums_output - bass_output
output_vocals_filename = output_filename_prefix + '_vocals.wav'
output_drums_filename = output_filename_prefix + '_drums.wav'
output_bass_filename = output_filename_prefix + '_bass.wav'
output_other_filename = output_filename_prefix + '_other.wav'
output_vocals_filepath = os.path.join(output_path, output_vocals_filename)
output_drums_filepath = os.path.join(output_path, output_drums_filename)
output_bass_filepath = os.path.join(output_path, output_bass_filename)
output_other_filepath = os.path.join(output_path, output_other_filename)
util.write_wav(vocals_output, output_vocals_filepath, sample_rate)
util.write_wav(drums_output, output_drums_filepath, sample_rate)
util.write_wav(bass_output, output_bass_filepath, sample_rate)
util.write_wav(other_output, output_other_filepath, sample_rate)