feature-window.cc (3098B)
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/feature-window.h" 20 21 #include <string> 22 #include <vector> 23 24 #include "kaldi-native-fbank/csrc/feature-window.h" 25 #include "kaldi-native-fbank/python/csrc/utils.h" 26 27 namespace knf { 28 29 static void PybindFrameExtractionOptions(py::module &m) { // NOLINT 30 using PyClass = FrameExtractionOptions; 31 py::class_<PyClass>(m, "FrameExtractionOptions") 32 .def(py::init<>()) 33 .def_readwrite("samp_freq", &PyClass::samp_freq) 34 .def_readwrite("frame_shift_ms", &PyClass::frame_shift_ms) 35 .def_readwrite("frame_length_ms", &PyClass::frame_length_ms) 36 .def_readwrite("dither", &PyClass::dither) 37 .def_readwrite("preemph_coeff", &PyClass::preemph_coeff) 38 .def_readwrite("remove_dc_offset", &PyClass::remove_dc_offset) 39 .def_readwrite("window_type", &PyClass::window_type) 40 .def_readwrite("round_to_power_of_two", &PyClass::round_to_power_of_two) 41 .def_readwrite("blackman_coeff", &PyClass::blackman_coeff) 42 .def_readwrite("snip_edges", &PyClass::snip_edges) 43 .def("as_dict", 44 [](const PyClass &self) -> py::dict { return AsDict(self); }) 45 .def_static("from_dict", 46 [](py::dict dict) -> PyClass { 47 return FrameExtractionOptionsFromDict(dict); 48 }) 49 #if 0 50 .def_readwrite("allow_downsample", 51 &PyClass::allow_downsample) 52 .def_readwrite("allow_upsample", &PyClass::allow_upsample) 53 #endif 54 .def("__str__", 55 [](const PyClass &self) -> std::string { return self.ToString(); }) 56 .def(py::pickle( 57 [](const PyClass &self) -> py::dict { return AsDict(self); }, 58 [](py::dict dict) -> PyClass { 59 return FrameExtractionOptionsFromDict(dict); 60 })); 61 } 62 static void PybindFeatureWindowFunction(py::module *m) { 63 using PyClass = FeatureWindowFunction; 64 py::class_<PyClass>(*m, "FeatureWindowFunction") 65 .def(py::init<const FrameExtractionOptions &>(), py::arg("opts")) 66 .def("apply", 67 [](const PyClass &self, 68 std::vector<float> &wave) -> std::vector<float> { 69 self.Apply(wave.data()); 70 return wave; 71 }) 72 .def_property_readonly("window", &PyClass::GetWindow); 73 } 74 75 void PybindFeatureWindow(py::module &m) { // NOLINT 76 PybindFrameExtractionOptions(m); 77 PybindFeatureWindowFunction(&m); 78 } 79 80 } // namespace knf