|
11 | 11 | #include <vector>
|
12 | 12 |
|
13 | 13 | #include "kaldi-native-fbank/csrc/online-feature.h"
|
| 14 | +#include "sherpa-onnx/csrc/macros.h" |
| 15 | +#include "sherpa-onnx/csrc/resample.h" |
14 | 16 |
|
15 | 17 | namespace sherpa_onnx {
|
16 | 18 |
|
@@ -50,6 +52,46 @@ class FeatureExtractor::Impl {
|
50 | 52 |
|
51 | 53 | void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) {
|
52 | 54 | std::lock_guard<std::mutex> lock(mutex_);
|
| 55 | + |
| 56 | + if (resampler_) { |
| 57 | + if (sampling_rate != resampler_->GetInputSamplingRate()) { |
| 58 | + SHERPA_ONNX_LOGE( |
| 59 | + "You changed the input sampling rate!! Expected: %d, given: " |
| 60 | + "%d", |
| 61 | + resampler_->GetInputSamplingRate(), sampling_rate); |
| 62 | + exit(-1); |
| 63 | + } |
| 64 | + |
| 65 | + std::vector<float> samples; |
| 66 | + resampler_->Resample(waveform, n, false, &samples); |
| 67 | + fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(), |
| 68 | + samples.size()); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (sampling_rate != opts_.frame_opts.samp_freq) { |
| 73 | + SHERPA_ONNX_LOGE( |
| 74 | + "Creating a resampler:\n" |
| 75 | + " in_sample_rate: %d\n" |
| 76 | + " output_sample_rate: %d\n", |
| 77 | + sampling_rate, static_cast<int32_t>(opts_.frame_opts.samp_freq)); |
| 78 | + |
| 79 | + float min_freq = |
| 80 | + std::min<int32_t>(sampling_rate, opts_.frame_opts.samp_freq); |
| 81 | + float lowpass_cutoff = 0.99 * 0.5 * min_freq; |
| 82 | + |
| 83 | + int32_t lowpass_filter_width = 6; |
| 84 | + resampler_ = std::make_unique<LinearResample>( |
| 85 | + sampling_rate, opts_.frame_opts.samp_freq, lowpass_cutoff, |
| 86 | + lowpass_filter_width); |
| 87 | + |
| 88 | + std::vector<float> samples; |
| 89 | + resampler_->Resample(waveform, n, false, &samples); |
| 90 | + fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(), |
| 91 | + samples.size()); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
53 | 95 | fbank_->AcceptWaveform(sampling_rate, waveform, n);
|
54 | 96 | }
|
55 | 97 |
|
@@ -100,6 +142,7 @@ class FeatureExtractor::Impl {
|
100 | 142 | std::unique_ptr<knf::OnlineFbank> fbank_;
|
101 | 143 | knf::FbankOptions opts_;
|
102 | 144 | mutable std::mutex mutex_;
|
| 145 | + std::unique_ptr<LinearResample> resampler_; |
103 | 146 | };
|
104 | 147 |
|
105 | 148 | FeatureExtractor::FeatureExtractor(const FeatureExtractorConfig &config /*={}*/)
|
|
0 commit comments