From c576fcf935917a176fe9fca3bb308919afb1cde5 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sat, 3 Dec 2022 13:01:46 +0800 Subject: [PATCH] Change the way how FbankOptions is displayed --- kaldifeat/csrc/feature-fbank.h | 22 +++++++-------- kaldifeat/python/csrc/feature-fbank.cc | 28 ++++++++++++++++++++ kaldifeat/python/tests/test_fbank_options.py | 1 + 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/kaldifeat/csrc/feature-fbank.h b/kaldifeat/csrc/feature-fbank.h index 05a467f..a30ff66 100644 --- a/kaldifeat/csrc/feature-fbank.h +++ b/kaldifeat/csrc/feature-fbank.h @@ -44,20 +44,18 @@ struct FbankOptions { std::string ToString() const { std::ostringstream os; - os << "frame_opts: \n"; - os << frame_opts << "\n"; - os << "\n"; + os << "FbankOptions("; - os << "mel_opts: \n"; - os << mel_opts << "\n"; + os << "frame_opts=" << frame_opts.ToString() << ", "; + os << "mel_opts=" << mel_opts.ToString() << ", "; - os << "use_energy: " << use_energy << "\n"; - os << "energy_floor: " << energy_floor << "\n"; - os << "raw_energy: " << raw_energy << "\n"; - os << "htk_compat: " << htk_compat << "\n"; - os << "use_log_fbank: " << use_log_fbank << "\n"; - os << "use_power: " << use_power << "\n"; - os << "device: " << device << "\n"; + os << "use_energy=" << (use_energy ? "True" : "False") << ", "; + os << "energy_floor=" << energy_floor << ", "; + os << "raw_energy=" << (raw_energy ? "True" : "False") << ", "; + os << "htk_compat=" << (htk_compat ? "True" : "False") << ", "; + os << "use_log_fbank=" << (use_log_fbank ? "True" : "False") << ", "; + os << "use_power=" << (use_power ? "True" : "False") << ", "; + os << "device=\"" << device << "\")"; return os.str(); } }; diff --git a/kaldifeat/python/csrc/feature-fbank.cc b/kaldifeat/python/csrc/feature-fbank.cc index 6e52f0c..07dbed8 100644 --- a/kaldifeat/python/csrc/feature-fbank.cc +++ b/kaldifeat/python/csrc/feature-fbank.cc @@ -16,6 +16,34 @@ static void PybindFbankOptions(py::module &m) { using PyClass = FbankOptions; py::class_(m, "FbankOptions") .def(py::init<>()) + .def(py::init([](const FrameExtractionOptions &frame_opts = + FrameExtractionOptions(), + bool use_energy = false, float energy_floor = 0.0f, + bool raw_energy = true, bool htk_compat = false, + bool use_log_fbank = true, bool use_power = true, + py::object device = py::str("cpu"), + const MelBanksOptions &mel_opts) + -> std::unique_ptr { + auto opts = std::make_unique(); + opts->frame_opts = frame_opts; + opts->mel_opts = mel_opts; + opts->use_energy = use_energy; + opts->energy_floor = energy_floor; + opts->raw_energy = raw_energy; + opts->htk_compat = htk_compat; + opts->use_log_fbank = use_log_fbank; + opts->use_power = use_power; + + std::string s = static_cast(device); + opts->device = torch::Device(s); + + return opts; + }), + py::arg("frame_opts") = FrameExtractionOptions(), + py::arg("use_energy") = false, py::arg("energy_floor") = 0.0f, + py::arg("raw_energy") = true, py::arg("htk_compat") = false, + py::arg("use_log_fbank") = true, py::arg("use_power") = true, + py::arg("device") = py::str("cpu"), py::arg("mel_opts")) .def_readwrite("frame_opts", &PyClass::frame_opts) .def_readwrite("mel_opts", &PyClass::mel_opts) .def_readwrite("use_energy", &PyClass::use_energy) diff --git a/kaldifeat/python/tests/test_fbank_options.py b/kaldifeat/python/tests/test_fbank_options.py index f2fffdc..6f822b1 100755 --- a/kaldifeat/python/tests/test_fbank_options.py +++ b/kaldifeat/python/tests/test_fbank_options.py @@ -12,6 +12,7 @@ import kaldifeat def test_default(): opts = kaldifeat.FbankOptions() + 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