|
@@ -24,6 +24,7 @@
|
|
|
|
|
|
#include "kaldi-native-fbank/csrc/online-feature.h"
|
|
|
#include "mat.h" // NOLINT
|
|
|
+#include "sherpa-ncnn/csrc/resample.h"
|
|
|
|
|
|
namespace sherpa_ncnn {
|
|
|
|
|
@@ -52,8 +53,47 @@ class FeatureExtractor::Impl {
|
|
|
fbank_ = std::make_unique<knf::OnlineFbank>(opts_);
|
|
|
}
|
|
|
|
|
|
- void AcceptWaveform(float sampling_rate, const float *waveform, int32_t n) {
|
|
|
+ void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) {
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
+ if (resampler_) {
|
|
|
+ if (sampling_rate != resampler_->GetInputSamplingRate()) {
|
|
|
+ NCNN_LOGE(
|
|
|
+ "You changed the input sampling rate!! Expected: %d, given: "
|
|
|
+ "%d",
|
|
|
+ resampler_->GetInputSamplingRate(), sampling_rate);
|
|
|
+ exit(-1);
|
|
|
+ }
|
|
|
+
|
|
|
+ std::vector<float> samples;
|
|
|
+ resampler_->Resample(waveform, n, false, &samples);
|
|
|
+ fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(),
|
|
|
+ samples.size());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sampling_rate != opts_.frame_opts.samp_freq) {
|
|
|
+ NCNN_LOGE(
|
|
|
+ "Creating a resampler:\n"
|
|
|
+ " in_sample_rate: %d\n"
|
|
|
+ " output_sample_rate: %d\n",
|
|
|
+ sampling_rate, static_cast<int32_t>(opts_.frame_opts.samp_freq));
|
|
|
+
|
|
|
+ float min_freq =
|
|
|
+ std::min<int32_t>(sampling_rate, opts_.frame_opts.samp_freq);
|
|
|
+ float lowpass_cutoff = 0.99 * 0.5 * min_freq;
|
|
|
+
|
|
|
+ int32_t lowpass_filter_width = 6;
|
|
|
+ resampler_ = std::make_unique<LinearResample>(
|
|
|
+ sampling_rate, opts_.frame_opts.samp_freq, lowpass_cutoff,
|
|
|
+ lowpass_filter_width);
|
|
|
+
|
|
|
+ std::vector<float> samples;
|
|
|
+ resampler_->Resample(waveform, n, false, &samples);
|
|
|
+ fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(),
|
|
|
+ samples.size());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
fbank_->AcceptWaveform(sampling_rate, waveform, n);
|
|
|
}
|
|
|
|
|
@@ -95,6 +135,7 @@ class FeatureExtractor::Impl {
|
|
|
std::unique_ptr<knf::OnlineFbank> fbank_;
|
|
|
knf::FbankOptions opts_;
|
|
|
mutable std::mutex mutex_;
|
|
|
+ std::unique_ptr<LinearResample> resampler_;
|
|
|
};
|
|
|
|
|
|
FeatureExtractor::FeatureExtractor(const FeatureExtractorConfig &config)
|
|
@@ -102,7 +143,7 @@ FeatureExtractor::FeatureExtractor(const FeatureExtractorConfig &config)
|
|
|
|
|
|
FeatureExtractor::~FeatureExtractor() = default;
|
|
|
|
|
|
-void FeatureExtractor::AcceptWaveform(float sampling_rate,
|
|
|
+void FeatureExtractor::AcceptWaveform(int32_t sampling_rate,
|
|
|
const float *waveform, int32_t n) {
|
|
|
impl_->AcceptWaveform(sampling_rate, waveform, n);
|
|
|
}
|