From 0baa34cc5e29605d43a2d03853f5c671a04346f6 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sat, 3 Dec 2022 13:31:25 +0800 Subject: [PATCH] Change the way how PlpOptions is displayed --- kaldifeat/csrc/feature-plp.h | 28 ++++++++--------- kaldifeat/python/csrc/feature-plp.cc | 35 ++++++++++++++++++++++ kaldifeat/python/tests/test_plp_options.py | 1 + 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/kaldifeat/csrc/feature-plp.h b/kaldifeat/csrc/feature-plp.h index dd5247c..0c39595 100644 --- a/kaldifeat/csrc/feature-plp.h +++ b/kaldifeat/csrc/feature-plp.h @@ -61,23 +61,21 @@ struct PlpOptions { std::string ToString() const { std::ostringstream os; - os << "frame_opts: \n"; - os << frame_opts << "\n"; - os << "\n"; + os << "PlpOptions("; - os << "mel_opts: \n"; - os << mel_opts << "\n"; + os << "frame_opts=" << frame_opts.ToString() << ", "; + os << "mel_opts=" << mel_opts.ToString() << ", "; - os << "lpc_order: " << lpc_order << "\n"; - os << "num_ceps: " << num_ceps << "\n"; - os << "use_energy: " << use_energy << "\n"; - os << "energy_floor: " << energy_floor << "\n"; - os << "raw_energy: " << raw_energy << "\n"; - os << "compress_factor: " << compress_factor << "\n"; - os << "cepstral_lifter: " << cepstral_lifter << "\n"; - os << "cepstral_scale: " << cepstral_scale << "\n"; - os << "htk_compat: " << htk_compat << "\n"; - os << "device: " << device << "\n"; + os << "lpc_order=" << lpc_order << ", "; + os << "num_ceps=" << num_ceps << ", "; + os << "use_energy=" << (use_energy ? "True" : "False") << ", "; + os << "energy_floor=" << energy_floor << ", "; + os << "raw_energy=" << (raw_energy ? "True" : "False") << ", "; + os << "compress_factor=" << compress_factor << ", "; + os << "cepstral_lifter=" << cepstral_lifter << ", "; + os << "cepstral_scale=" << cepstral_scale << ", "; + os << "htk_compat=" << (htk_compat ? "True" : "False") << ", "; + os << "device=\"" << device << "\")"; return os.str(); } }; diff --git a/kaldifeat/python/csrc/feature-plp.cc b/kaldifeat/python/csrc/feature-plp.cc index 364ef93..7553f3a 100644 --- a/kaldifeat/python/csrc/feature-plp.cc +++ b/kaldifeat/python/csrc/feature-plp.cc @@ -16,6 +16,41 @@ void PybindPlpOptions(py::module &m) { using PyClass = PlpOptions; py::class_(m, "PlpOptions") .def(py::init<>()) + .def(py::init([](const MelBanksOptions &mel_opts, + const FrameExtractionOptions &frame_opts = + FrameExtractionOptions(), + int32_t lpc_order = 12, int32_t num_ceps = 13, + bool use_energy = true, float energy_floor = 0.0, + bool raw_energy = true, float compress_factor = 0.33333, + int32_t cepstral_lifter = 22, float cepstral_scale = 1.0, + bool htk_compat = false, + py::object device = + py::str("cpu")) -> std::unique_ptr { + auto opts = std::make_unique(); + opts->frame_opts = frame_opts; + opts->mel_opts = mel_opts; + opts->lpc_order = lpc_order; + opts->num_ceps = num_ceps; + opts->use_energy = use_energy; + opts->energy_floor = energy_floor; + opts->raw_energy = raw_energy; + opts->compress_factor = compress_factor; + opts->cepstral_lifter = cepstral_lifter; + opts->cepstral_scale = cepstral_scale; + opts->htk_compat = htk_compat; + + std::string s = static_cast(device); + opts->device = torch::Device(s); + + return opts; + }), + py::arg("mel_opts"), + py::arg("frame_opts") = FrameExtractionOptions(), + py::arg("lpc_order") = 12, py::arg("num_ceps") = 13, + py::arg("use_energy") = true, py::arg("energy_floor") = 0.0, + py::arg("raw_energy") = true, py::arg("compress_factor") = 0.33333, + py::arg("cepstral_lifter") = 22, py::arg("cepstral_scale") = 1.0, + py::arg("htk_compat") = false, py::arg("device") = py::str("cpu")) .def_readwrite("frame_opts", &PyClass::frame_opts) .def_readwrite("mel_opts", &PyClass::mel_opts) .def_readwrite("lpc_order", &PyClass::lpc_order) diff --git a/kaldifeat/python/tests/test_plp_options.py b/kaldifeat/python/tests/test_plp_options.py index c30dd64..fc81111 100755 --- a/kaldifeat/python/tests/test_plp_options.py +++ b/kaldifeat/python/tests/test_plp_options.py @@ -12,6 +12,7 @@ import kaldifeat def test_default(): opts = kaldifeat.PlpOptions() + print(opts) assert opts.frame_opts.samp_freq == 16000 assert opts.frame_opts.frame_shift_ms == 10.0 assert opts.frame_opts.frame_length_ms == 25.0