features.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "sherpa-ncnn/csrc/features.h"
  19. #include <algorithm>
  20. #include <memory>
  21. #include <mutex> // NOLINT
  22. #include <vector>
  23. #include "kaldi-native-fbank/csrc/online-feature.h"
  24. #include "mat.h" // NOLINT
  25. #include "sherpa-ncnn/csrc/resample.h"
  26. namespace sherpa_ncnn {
  27. std::string FeatureExtractorConfig::ToString() const {
  28. std::ostringstream os;
  29. os << "FeatureExtractorConfig(";
  30. os << "sampling_rate=" << sampling_rate << ", ";
  31. os << "feature_dim=" << feature_dim << ")";
  32. return os.str();
  33. }
  34. class FeatureExtractor::Impl {
  35. public:
  36. explicit Impl(const FeatureExtractorConfig &config) {
  37. opts_.frame_opts.dither = 0;
  38. opts_.frame_opts.snip_edges = false;
  39. opts_.frame_opts.samp_freq = config.sampling_rate;
  40. opts_.mel_opts.num_bins = config.feature_dim;
  41. fbank_ = std::make_unique<knf::OnlineFbank>(opts_);
  42. }
  43. void AcceptWaveform(int32_t sampling_rate, const float *waveform, int32_t n) {
  44. std::lock_guard<std::mutex> lock(mutex_);
  45. if (resampler_) {
  46. if (sampling_rate != resampler_->GetInputSamplingRate()) {
  47. NCNN_LOGE(
  48. "You changed the input sampling rate!! Expected: %d, given: "
  49. "%d",
  50. resampler_->GetInputSamplingRate(), sampling_rate);
  51. exit(-1);
  52. }
  53. std::vector<float> samples;
  54. resampler_->Resample(waveform, n, false, &samples);
  55. fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(),
  56. samples.size());
  57. return;
  58. }
  59. if (sampling_rate != opts_.frame_opts.samp_freq) {
  60. NCNN_LOGE(
  61. "Creating a resampler:\n"
  62. " in_sample_rate: %d\n"
  63. " output_sample_rate: %d\n",
  64. sampling_rate, static_cast<int32_t>(opts_.frame_opts.samp_freq));
  65. float min_freq =
  66. std::min<int32_t>(sampling_rate, opts_.frame_opts.samp_freq);
  67. float lowpass_cutoff = 0.99 * 0.5 * min_freq;
  68. int32_t lowpass_filter_width = 6;
  69. resampler_ = std::make_unique<LinearResample>(
  70. sampling_rate, opts_.frame_opts.samp_freq, lowpass_cutoff,
  71. lowpass_filter_width);
  72. std::vector<float> samples;
  73. resampler_->Resample(waveform, n, false, &samples);
  74. fbank_->AcceptWaveform(opts_.frame_opts.samp_freq, samples.data(),
  75. samples.size());
  76. return;
  77. }
  78. fbank_->AcceptWaveform(sampling_rate, waveform, n);
  79. }
  80. void InputFinished() {
  81. std::lock_guard<std::mutex> lock(mutex_);
  82. fbank_->InputFinished();
  83. }
  84. int32_t NumFramesReady() const {
  85. std::lock_guard<std::mutex> lock(mutex_);
  86. return fbank_->NumFramesReady();
  87. }
  88. bool IsLastFrame(int32_t frame) const {
  89. std::lock_guard<std::mutex> lock(mutex_);
  90. return fbank_->IsLastFrame(frame);
  91. }
  92. ncnn::Mat GetFrames(int32_t frame_index, int32_t n) {
  93. std::lock_guard<std::mutex> lock(mutex_);
  94. if (frame_index + n > fbank_->NumFramesReady()) {
  95. NCNN_LOGE("%d + %d > %d", frame_index, n, fbank_->NumFramesReady());
  96. exit(-1);
  97. }
  98. int32_t discard_num = frame_index - last_frame_index_;
  99. if (discard_num < 0) {
  100. NCNN_LOGE("last_frame_index_: %d, frame_index_: %d", last_frame_index_,
  101. frame_index);
  102. exit(-1);
  103. }
  104. fbank_->Pop(discard_num);
  105. int32_t feature_dim = fbank_->Dim();
  106. ncnn::Mat features;
  107. features.create(feature_dim, n);
  108. for (int32_t i = 0; i != n; ++i) {
  109. const float *f = fbank_->GetFrame(i + frame_index);
  110. std::copy(f, f + feature_dim, features.row(i));
  111. }
  112. last_frame_index_ = frame_index;
  113. return features;
  114. }
  115. private:
  116. std::unique_ptr<knf::OnlineFbank> fbank_;
  117. knf::FbankOptions opts_;
  118. mutable std::mutex mutex_;
  119. std::unique_ptr<LinearResample> resampler_;
  120. int32_t last_frame_index_ = 0;
  121. };
  122. FeatureExtractor::FeatureExtractor(const FeatureExtractorConfig &config)
  123. : impl_(std::make_unique<Impl>(config)) {}
  124. FeatureExtractor::~FeatureExtractor() = default;
  125. void FeatureExtractor::AcceptWaveform(int32_t sampling_rate,
  126. const float *waveform, int32_t n) {
  127. impl_->AcceptWaveform(sampling_rate, waveform, n);
  128. }
  129. void FeatureExtractor::InputFinished() { impl_->InputFinished(); }
  130. int32_t FeatureExtractor::NumFramesReady() const {
  131. return impl_->NumFramesReady();
  132. }
  133. bool FeatureExtractor::IsLastFrame(int32_t frame) const {
  134. return impl_->IsLastFrame(frame);
  135. }
  136. ncnn::Mat FeatureExtractor::GetFrames(int32_t frame_index, int32_t n) const {
  137. return impl_->GetFrames(frame_index, n);
  138. }
  139. } // namespace sherpa_ncnn