mirror of
https://github.com/csukuangfj/kaldifeat.git
synced 2025-08-18 05:32:18 +00:00
Add pickle support to FrameExtractionOptions.
This commit is contained in:
parent
ec66d87fae
commit
e5dd272963
@ -12,39 +12,38 @@
|
|||||||
namespace kaldifeat {
|
namespace kaldifeat {
|
||||||
|
|
||||||
static void PybindFrameExtractionOptions(py::module &m) {
|
static void PybindFrameExtractionOptions(py::module &m) {
|
||||||
py::class_<FrameExtractionOptions>(m, "FrameExtractionOptions")
|
using PyClass = FrameExtractionOptions;
|
||||||
|
py::class_<PyClass>(m, "FrameExtractionOptions")
|
||||||
.def(py::init<>())
|
.def(py::init<>())
|
||||||
.def_readwrite("samp_freq", &FrameExtractionOptions::samp_freq)
|
.def_readwrite("samp_freq", &PyClass::samp_freq)
|
||||||
.def_readwrite("frame_shift_ms", &FrameExtractionOptions::frame_shift_ms)
|
.def_readwrite("frame_shift_ms", &PyClass::frame_shift_ms)
|
||||||
.def_readwrite("frame_length_ms",
|
.def_readwrite("frame_length_ms", &PyClass::frame_length_ms)
|
||||||
&FrameExtractionOptions::frame_length_ms)
|
.def_readwrite("dither", &PyClass::dither)
|
||||||
.def_readwrite("dither", &FrameExtractionOptions::dither)
|
.def_readwrite("preemph_coeff", &PyClass::preemph_coeff)
|
||||||
.def_readwrite("preemph_coeff", &FrameExtractionOptions::preemph_coeff)
|
.def_readwrite("remove_dc_offset", &PyClass::remove_dc_offset)
|
||||||
.def_readwrite("remove_dc_offset",
|
.def_readwrite("window_type", &PyClass::window_type)
|
||||||
&FrameExtractionOptions::remove_dc_offset)
|
.def_readwrite("round_to_power_of_two", &PyClass::round_to_power_of_two)
|
||||||
.def_readwrite("window_type", &FrameExtractionOptions::window_type)
|
.def_readwrite("blackman_coeff", &PyClass::blackman_coeff)
|
||||||
.def_readwrite("round_to_power_of_two",
|
.def_readwrite("snip_edges", &PyClass::snip_edges)
|
||||||
&FrameExtractionOptions::round_to_power_of_two)
|
|
||||||
.def_readwrite("blackman_coeff", &FrameExtractionOptions::blackman_coeff)
|
|
||||||
.def_readwrite("snip_edges", &FrameExtractionOptions::snip_edges)
|
|
||||||
.def("as_dict",
|
.def("as_dict",
|
||||||
[](const FrameExtractionOptions &self) -> py::dict {
|
[](const PyClass &self) -> py::dict { return AsDict(self); })
|
||||||
return AsDict(self);
|
|
||||||
})
|
|
||||||
.def_static("from_dict",
|
.def_static("from_dict",
|
||||||
[](py::dict dict) -> FrameExtractionOptions {
|
[](py::dict dict) -> PyClass {
|
||||||
return FrameExtractionOptionsFromDict(dict);
|
return FrameExtractionOptionsFromDict(dict);
|
||||||
})
|
})
|
||||||
#if 0
|
#if 0
|
||||||
.def_readwrite("allow_downsample",
|
.def_readwrite("allow_downsample",
|
||||||
&FrameExtractionOptions::allow_downsample)
|
&PyClass::allow_downsample)
|
||||||
.def_readwrite("allow_upsample", &FrameExtractionOptions::allow_upsample)
|
.def_readwrite("allow_upsample", &PyClass::allow_upsample)
|
||||||
.def_readwrite("max_feature_vectors",
|
.def_readwrite("max_feature_vectors",
|
||||||
&FrameExtractionOptions::max_feature_vectors)
|
&PyClass::max_feature_vectors)
|
||||||
#endif
|
#endif
|
||||||
.def("__str__", [](const FrameExtractionOptions &self) -> std::string {
|
.def("__str__",
|
||||||
return self.ToString();
|
[](const PyClass &self) -> std::string { return self.ToString(); })
|
||||||
});
|
.def(py::pickle([](const PyClass &self) { return AsDict(self); },
|
||||||
|
[](py::dict dict) -> PyClass {
|
||||||
|
return FrameExtractionOptionsFromDict(dict);
|
||||||
|
}));
|
||||||
|
|
||||||
m.def("num_frames", &NumFrames, py::arg("num_samples"), py::arg("opts"),
|
m.def("num_frames", &NumFrames, py::arg("num_samples"), py::arg("opts"),
|
||||||
py::arg("flush") = true);
|
py::arg("flush") = true);
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This file contains code about `from_dict` and
|
* This file contains code about `from_dict` and
|
||||||
* `to_dict` for various options in kaldifeat.
|
* `as_dict` for various options in kaldifeat.
|
||||||
*
|
*
|
||||||
* Regarding `from_dict`, users don't need to provide
|
* Regarding `from_dict`, users don't need to provide
|
||||||
* all the fields in the options. If some fields
|
* all the fields in the options. If some fields
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
#
|
#
|
||||||
# Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang)
|
# Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||||
|
|
||||||
|
import pickle
|
||||||
|
|
||||||
import kaldifeat
|
import kaldifeat
|
||||||
|
|
||||||
|
|
||||||
@ -94,12 +96,23 @@ def test_from_dict_full_and_as_dict():
|
|||||||
assert opts3.window_type == "hanning"
|
assert opts3.window_type == "hanning"
|
||||||
|
|
||||||
|
|
||||||
|
def test_pickle():
|
||||||
|
opts = kaldifeat.FrameExtractionOptions()
|
||||||
|
opts.samp_freq = 44100
|
||||||
|
opts.dither = 5.5
|
||||||
|
data = pickle.dumps(opts)
|
||||||
|
|
||||||
|
opts2 = pickle.loads(data)
|
||||||
|
assert str(opts) == str(opts2)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
test_default()
|
test_default()
|
||||||
test_set_get()
|
test_set_get()
|
||||||
test_from_empty_dict()
|
test_from_empty_dict()
|
||||||
test_from_dict_partial()
|
test_from_dict_partial()
|
||||||
test_from_dict_full_and_as_dict()
|
test_from_dict_full_and_as_dict()
|
||||||
|
test_pickle()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user