features.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef SHERPA_NCNN_CSRC_FEATURES_H_
  19. #define SHERPA_NCNN_CSRC_FEATURES_H_
  20. #include <memory>
  21. #include <mutex> // NOLINT
  22. #include "kaldi-native-fbank/csrc/online-feature.h"
  23. namespace ncnn {
  24. class Mat;
  25. }
  26. namespace sherpa_ncnn {
  27. class FeatureExtractor {
  28. public:
  29. explicit FeatureExtractor(const knf::FbankOptions &fbank_opts);
  30. /**
  31. @param sampling_rate The sampling_rate of the input waveform. Should match
  32. the one expected by the feature extractor.
  33. @param waveform Pointer to a 1-D array of size n
  34. @param n Number of entries in waveform
  35. */
  36. void AcceptWaveform(float sampling_rate, const float *waveform, int32_t n);
  37. // InputFinished() tells the class you won't be providing any
  38. // more waveform. This will help flush out the last frame or two
  39. // of features, in the case where snip-edges == false; it also
  40. // affects the return value of IsLastFrame().
  41. void InputFinished();
  42. int32_t NumFramesReady() const;
  43. // Note: IsLastFrame() will only ever return true if you have called
  44. // InputFinished() (and this frame is the last frame).
  45. bool IsLastFrame(int32_t frame) const;
  46. /** Get n frames starting from the given frame index.
  47. *
  48. * @param frame_index The starting frame index
  49. * @param n Number of frames to get.
  50. * @return Return a 2-D tensor of shape (n, feature_dim).
  51. * ans.w == feature_dim; ans.h == n
  52. */
  53. ncnn::Mat GetFrames(int32_t frame_index, int32_t n) const;
  54. void Reset();
  55. private:
  56. std::unique_ptr<knf::OnlineFbank> fbank_;
  57. knf::FbankOptions opts_;
  58. mutable std::mutex mutex_;
  59. };
  60. } // namespace sherpa_ncnn
  61. #endif // SHERPA_NCNN_CSRC_FEATURES_H_