offline-source-separation-spleeter-impl.h
9.1 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// sherpa-onnx/csrc/offline-source-separation-spleeter-impl.h
//
// Copyright (c) 2025 Xiaomi Corporation
#ifndef SHERPA_ONNX_CSRC_OFFLINE_SOURCE_SEPARATION_SPLEETER_IMPL_H_
#define SHERPA_ONNX_CSRC_OFFLINE_SOURCE_SEPARATION_SPLEETER_IMPL_H_
#include "Eigen/Dense"
#include "kaldi-native-fbank/csrc/istft.h"
#include "kaldi-native-fbank/csrc/stft.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/offline-source-separation-spleeter-model.h"
#include "sherpa-onnx/csrc/offline-source-separation.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/resample.h"
namespace sherpa_onnx {
class OfflineSourceSeparationSpleeterImpl : public OfflineSourceSeparationImpl {
public:
OfflineSourceSeparationSpleeterImpl(
const OfflineSourceSeparationConfig &config)
: config_(config), model_(config_.model) {}
template <typename Manager>
OfflineSourceSeparationSpleeterImpl(
Manager *mgr, const OfflineSourceSeparationConfig &config)
: config_(config), model_(mgr, config_.model) {}
OfflineSourceSeparationOutput Process(
const OfflineSourceSeparationInput &input) const override {
const OfflineSourceSeparationInput *p_input = &input;
OfflineSourceSeparationInput tmp_input;
int32_t output_sample_rate = GetOutputSampleRate();
if (input.sample_rate != output_sample_rate) {
SHERPA_ONNX_LOGE(
"Creating a resampler:\n"
" in_sample_rate: %d\n"
" output_sample_rate: %d\n",
input.sample_rate, output_sample_rate);
float min_freq = std::min<int32_t>(input.sample_rate, output_sample_rate);
float lowpass_cutoff = 0.99 * 0.5 * min_freq;
int32_t lowpass_filter_width = 6;
auto resampler = std::make_unique<LinearResample>(
input.sample_rate, output_sample_rate, lowpass_cutoff,
lowpass_filter_width);
std::vector<float> s;
for (const auto &samples : input.samples.data) {
resampler->Reset();
resampler->Resample(samples.data(), samples.size(), true, &s);
tmp_input.samples.data.push_back(std::move(s));
}
tmp_input.sample_rate = output_sample_rate;
p_input = &tmp_input;
}
if (p_input->samples.data.size() > 1) {
if (config_.model.debug) {
SHERPA_ONNX_LOGE("input ch1 samples size: %d",
static_cast<int32_t>(p_input->samples.data[1].size()));
}
if (p_input->samples.data[0].size() != p_input->samples.data[1].size()) {
SHERPA_ONNX_LOGE("ch0 samples size %d vs ch1 samples size %d",
static_cast<int32_t>(p_input->samples.data[0].size()),
static_cast<int32_t>(p_input->samples.data[1].size()));
SHERPA_ONNX_EXIT(-1);
}
}
auto stft_ch0 = ComputeStft(*p_input, 0);
auto stft_ch1 = ComputeStft(*p_input, 1);
knf::StftResult *p_stft_ch1 = stft_ch1.real.empty() ? &stft_ch0 : &stft_ch1;
int32_t num_frames = stft_ch0.num_frames;
int32_t fft_bins = stft_ch0.real.size() / num_frames;
int32_t pad = 512 - (stft_ch0.num_frames % 512);
if (pad < 512) {
num_frames += pad;
}
if (num_frames % 512) {
SHERPA_ONNX_LOGE("num_frames should be multiple of 512, actual: %d. %d",
num_frames, num_frames % 512);
SHERPA_ONNX_EXIT(-1);
}
Eigen::VectorXf real(2 * num_frames * 1024);
Eigen::VectorXf imag(2 * num_frames * 1024);
real.setZero();
imag.setZero();
float *p_real = &real[0];
float *p_imag = &imag[0];
// copy stft result of channel 0
for (int32_t i = 0; i != stft_ch0.num_frames; ++i) {
std::copy(stft_ch0.real.data() + i * fft_bins,
stft_ch0.real.data() + i * fft_bins + 1024, p_real + 1024 * i);
std::copy(stft_ch0.imag.data() + i * fft_bins,
stft_ch0.imag.data() + i * fft_bins + 1024, p_imag + 1024 * i);
}
p_real += num_frames * 1024;
p_imag += num_frames * 1024;
// copy stft result of channel 1
for (int32_t i = 0; i != stft_ch1.num_frames; ++i) {
std::copy(p_stft_ch1->real.data() + i * fft_bins,
p_stft_ch1->real.data() + i * fft_bins + 1024,
p_real + 1024 * i);
std::copy(p_stft_ch1->imag.data() + i * fft_bins,
p_stft_ch1->imag.data() + i * fft_bins + 1024,
p_imag + 1024 * i);
}
Eigen::VectorXf x = (real.array().square() + imag.array().square()).sqrt();
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
std::array<int64_t, 4> x_shape{2, num_frames / 512, 512, 1024};
Ort::Value x_tensor = Ort::Value::CreateTensor(
memory_info, &x[0], x.size(), x_shape.data(), x_shape.size());
Ort::Value vocals_spec_tensor = model_.RunVocals(View(&x_tensor));
Ort::Value accompaniment_spec_tensor =
model_.RunAccompaniment(std::move(x_tensor));
Eigen::VectorXf vocals_spec = Eigen::Map<Eigen::VectorXf>(
vocals_spec_tensor.GetTensorMutableData<float>(), x.size());
Eigen::VectorXf accompaniment_spec = Eigen::Map<Eigen::VectorXf>(
accompaniment_spec_tensor.GetTensorMutableData<float>(), x.size());
Eigen::VectorXf sum_spec = vocals_spec.array().square() +
accompaniment_spec.array().square() + 1e-10;
vocals_spec = (vocals_spec.array().square() + 1e-10 / 2) / sum_spec.array();
accompaniment_spec =
(accompaniment_spec.array().square() + 1e-10 / 2) / sum_spec.array();
auto vocals_samples_ch0 = ProcessSpec(vocals_spec, stft_ch0, 0);
auto vocals_samples_ch1 = ProcessSpec(vocals_spec, *p_stft_ch1, 1);
auto accompaniment_samples_ch0 =
ProcessSpec(accompaniment_spec, stft_ch0, 0);
auto accompaniment_samples_ch1 =
ProcessSpec(accompaniment_spec, *p_stft_ch1, 1);
OfflineSourceSeparationOutput ans;
ans.sample_rate = GetOutputSampleRate();
ans.stems.resize(2);
ans.stems[0].data.reserve(2);
ans.stems[1].data.reserve(2);
ans.stems[0].data.push_back(std::move(vocals_samples_ch0));
ans.stems[0].data.push_back(std::move(vocals_samples_ch1));
ans.stems[1].data.push_back(std::move(accompaniment_samples_ch0));
ans.stems[1].data.push_back(std::move(accompaniment_samples_ch1));
return ans;
}
int32_t GetOutputSampleRate() const override {
return model_.GetMetaData().sample_rate;
}
int32_t GetNumberOfStems() const override {
return model_.GetMetaData().num_stems;
}
private:
// spec is of shape (2, num_chunks, 512, 1024)
std::vector<float> ProcessSpec(const Eigen::VectorXf &spec,
const knf::StftResult &stft,
int32_t channel) const {
int32_t fft_bins = stft.real.size() / stft.num_frames;
Eigen::VectorXf mask(stft.real.size());
mask.setZero();
float *p_mask = &mask[0];
// assume there are 2 channels
const float *p_spec = &spec[0] + (spec.size() / 2) * channel;
for (int32_t i = 0; i != stft.num_frames; ++i) {
std::copy(p_spec + i * 1024, p_spec + (i + 1) * 1024,
p_mask + i * fft_bins);
}
knf::StftResult masked_stft;
masked_stft.num_frames = stft.num_frames;
masked_stft.real.resize(stft.real.size());
masked_stft.imag.resize(stft.imag.size());
Eigen::Map<Eigen::VectorXf>(masked_stft.real.data(),
masked_stft.real.size()) =
mask.array() *
Eigen::Map<Eigen::VectorXf>(const_cast<float *>(stft.real.data()),
stft.real.size())
.array();
Eigen::Map<Eigen::VectorXf>(masked_stft.imag.data(),
masked_stft.imag.size()) =
mask.array() *
Eigen::Map<Eigen::VectorXf>(const_cast<float *>(stft.imag.data()),
stft.imag.size())
.array();
auto stft_config = GetStftConfig();
knf::IStft istft(stft_config);
return istft.Compute(masked_stft);
}
knf::StftResult ComputeStft(const OfflineSourceSeparationInput &input,
int32_t ch) const {
if (ch >= input.samples.data.size()) {
SHERPA_ONNX_LOGE("Invalid channel %d. Max %d", ch,
static_cast<int32_t>(input.samples.data.size()));
SHERPA_ONNX_EXIT(-1);
}
if (input.samples.data[ch].empty()) {
return {};
}
return ComputeStft(input.samples.data[ch]);
}
knf::StftResult ComputeStft(const std::vector<float> &samples) const {
auto stft_config = GetStftConfig();
knf::Stft stft(stft_config);
return stft.Compute(samples.data(), samples.size());
}
knf::StftConfig GetStftConfig() const {
const auto &meta = model_.GetMetaData();
knf::StftConfig stft_config;
stft_config.n_fft = meta.n_fft;
stft_config.hop_length = meta.hop_length;
stft_config.win_length = meta.window_length;
stft_config.window_type = meta.window_type;
stft_config.center = meta.center;
stft_config.center = false;
return stft_config;
}
private:
OfflineSourceSeparationConfig config_;
OfflineSourceSeparationSpleeterModel model_;
};
} // namespace sherpa_onnx
#endif // SHERPA_ONNX_CSRC_OFFLINE_SOURCE_SEPARATION_SPLEETER_IMPL_H_