janitor

janitor
git clone https://git.prime8.dev/janitor.git
Log | Files | Refs | README | LICENSE

online-feature.cc (4211B)


      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 
     19 #include "kaldi-native-fbank/python/csrc/online-feature.h"
     20 
     21 #include <cstdint>
     22 #include <string>
     23 #include <vector>
     24 
     25 #include "kaldi-native-fbank/csrc/feature-fbank.h"
     26 #include "kaldi-native-fbank/csrc/feature-mfcc.h"
     27 #include "kaldi-native-fbank/csrc/online-feature.h"
     28 #include "kaldi-native-fbank/csrc/whisper-feature.h"
     29 #include "kaldi-native-fbank/python/csrc/utils.h"
     30 
     31 namespace pybind11 {
     32 class gil_scoped_release;
     33 }  // namespace pybind11
     34 
     35 namespace knf {
     36 
     37 static void PybindWhisperFeatureOptions(py::module &m) {  // NOLINT
     38   using PyClass = WhisperFeatureOptions;
     39   py::class_<PyClass>(m, "WhisperFeatureOptions")
     40       .def(py::init<>())
     41       .def_readwrite("frame_opts", &PyClass::frame_opts)
     42       .def_readwrite("dim", &PyClass::dim)
     43       .def("__str__",
     44            [](const PyClass &self) -> std::string { return self.ToString(); })
     45       .def("as_dict",
     46            [](const PyClass &self) -> py::dict { return AsDict(self); })
     47       .def_static("from_dict",
     48                   [](py::dict dict) -> PyClass {
     49                     return WhisperFeatureOptionsFromDict(dict);
     50                   })
     51       .def(py::pickle(
     52           [](const PyClass &self) -> py::dict { return AsDict(self); },
     53           [](py::dict dict) -> PyClass {
     54             return WhisperFeatureOptionsFromDict(dict);
     55           }));
     56 }
     57 
     58 template <typename C>
     59 void PybindOnlineFeatureTpl(py::module &m,  // NOLINT
     60                             const std::string &class_name,
     61                             const std::string &class_help_doc = "") {
     62   using PyClass = OnlineGenericBaseFeature<C>;
     63   using Options = typename C::Options;
     64   py::class_<PyClass>(m, class_name.c_str(), class_help_doc.c_str())
     65       .def(py::init<const Options &>(), py::arg("opts"))
     66       .def_property_readonly("dim", &PyClass::Dim)
     67       .def_property_readonly("frame_shift_in_seconds",
     68                              &PyClass::FrameShiftInSeconds)
     69       .def_property_readonly("num_frames_ready", &PyClass::NumFramesReady)
     70       .def("is_last_frame", &PyClass::IsLastFrame, py::arg("frame"))
     71       .def(
     72           "get_frame",
     73           [](py::object obj, int32_t frame) {
     74             auto *self = obj.cast<PyClass *>();
     75             const float *f = self->GetFrame(frame);
     76             return py::array_t<float>({self->Dim()},    // shape
     77                                       {sizeof(float)},  // stride in bytes
     78                                       f,                // ptr
     79                                       obj);  // it will increase the reference
     80                                              // count of **this** vector
     81           },
     82           py::arg("frame"))
     83       .def(
     84           "accept_waveform",
     85           [](PyClass &self, float sampling_rate,
     86              const std::vector<float> &waveform) {
     87             self.AcceptWaveform(sampling_rate, waveform.data(),
     88                                 waveform.size());
     89           },
     90           py::arg("sampling_rate"), py::arg("waveform"),
     91           py::call_guard<py::gil_scoped_release>())
     92       .def("input_finished", &PyClass::InputFinished,
     93            py::call_guard<py::gil_scoped_release>())
     94       .def("pop", &PyClass::Pop, py::arg("n"),
     95            py::call_guard<py::gil_scoped_release>());
     96 }
     97 
     98 void PybindOnlineFeature(py::module &m) {  // NOLINT
     99   PybindOnlineFeatureTpl<FbankComputer>(m, "OnlineFbank");
    100   PybindOnlineFeatureTpl<MfccComputer>(m, "OnlineMfcc");
    101 
    102   PybindWhisperFeatureOptions(m);
    103 
    104   PybindOnlineFeatureTpl<WhisperFeatureComputer>(m, "OnlineWhisperFbank");
    105 }
    106 
    107 }  // namespace knf