online-feature.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "kaldi-native-fbank/python/csrc/online-feature.h"
  19. #include <string>
  20. #include <vector>
  21. #include "online-feature.h"
  22. namespace knf {
  23. template <typename C>
  24. void PybindOnlineFeatureTpl(py::module &m, // NOLINT
  25. const std::string &class_name,
  26. const std::string &class_help_doc = "") {
  27. using PyClass = OnlineGenericBaseFeature<C>;
  28. using Options = typename C::Options;
  29. py::class_<PyClass>(m, class_name.c_str(), class_help_doc.c_str())
  30. .def(py::init<const Options &>(), py::arg("opts"))
  31. .def_property_readonly("dim", &PyClass::Dim)
  32. .def_property_readonly("frame_shift_in_seconds",
  33. &PyClass::FrameShiftInSeconds)
  34. .def_property_readonly("num_frames_ready", &PyClass::NumFramesReady)
  35. .def("is_last_frame", &PyClass::IsLastFrame, py::arg("frame"))
  36. .def(
  37. "get_frame",
  38. [](py::object obj, int32_t frame) {
  39. auto *self = obj.cast<PyClass *>();
  40. const float *f = self->GetFrame(frame);
  41. return py::array_t<float>({self->Dim()}, // shape
  42. {sizeof(float)}, // stride in bytes
  43. f, // ptr
  44. obj); // it will increase the reference
  45. // count of **this** vector
  46. },
  47. py::arg("frame"))
  48. .def(
  49. "accept_waveform",
  50. [](PyClass &self, float sampling_rate,
  51. const std::vector<float> &waveform) {
  52. self.AcceptWaveform(sampling_rate, waveform.data(),
  53. waveform.size());
  54. },
  55. py::arg("sampling_rate"), py::arg("waveform"),
  56. py::call_guard<py::gil_scoped_release>())
  57. .def("input_finished", &PyClass::InputFinished);
  58. }
  59. void PybindOnlineFeature(py::module &m) { // NOLINT
  60. PybindOnlineFeatureTpl<FbankComputer>(m, "OnlineFbank");
  61. }
  62. } // namespace knf