utils.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "kaldi-native-fbank/python/csrc/utils.h"
  19. #include <string>
  20. #include "feature-window.h"
  21. #define FROM_DICT(type, key) \
  22. if (dict.contains(#key)) { \
  23. opts.key = py::type(dict[#key]); \
  24. }
  25. #define AS_DICT(key) dict[#key] = opts.key
  26. namespace knf {
  27. FrameExtractionOptions FrameExtractionOptionsFromDict(py::dict dict) {
  28. FrameExtractionOptions opts;
  29. FROM_DICT(float_, samp_freq);
  30. FROM_DICT(float_, frame_shift_ms);
  31. FROM_DICT(float_, frame_length_ms);
  32. FROM_DICT(float_, dither);
  33. FROM_DICT(float_, preemph_coeff);
  34. FROM_DICT(bool_, remove_dc_offset);
  35. FROM_DICT(str, window_type);
  36. FROM_DICT(bool_, round_to_power_of_two);
  37. FROM_DICT(float_, blackman_coeff);
  38. FROM_DICT(bool_, snip_edges);
  39. return opts;
  40. }
  41. py::dict AsDict(const FrameExtractionOptions &opts) {
  42. py::dict dict;
  43. AS_DICT(samp_freq);
  44. AS_DICT(frame_shift_ms);
  45. AS_DICT(frame_length_ms);
  46. AS_DICT(dither);
  47. AS_DICT(preemph_coeff);
  48. AS_DICT(remove_dc_offset);
  49. AS_DICT(window_type);
  50. AS_DICT(round_to_power_of_two);
  51. AS_DICT(blackman_coeff);
  52. AS_DICT(snip_edges);
  53. return dict;
  54. }
  55. MelBanksOptions MelBanksOptionsFromDict(py::dict dict) {
  56. MelBanksOptions opts;
  57. FROM_DICT(int_, num_bins);
  58. FROM_DICT(float_, low_freq);
  59. FROM_DICT(float_, high_freq);
  60. FROM_DICT(float_, vtln_low);
  61. FROM_DICT(float_, vtln_high);
  62. FROM_DICT(bool_, debug_mel);
  63. FROM_DICT(bool_, htk_mode);
  64. return opts;
  65. }
  66. py::dict AsDict(const MelBanksOptions &opts) {
  67. py::dict dict;
  68. AS_DICT(num_bins);
  69. AS_DICT(low_freq);
  70. AS_DICT(high_freq);
  71. AS_DICT(vtln_low);
  72. AS_DICT(vtln_high);
  73. AS_DICT(debug_mel);
  74. AS_DICT(htk_mode);
  75. return dict;
  76. }
  77. FbankOptions FbankOptionsFromDict(py::dict dict) {
  78. FbankOptions opts;
  79. if (dict.contains("frame_opts")) {
  80. opts.frame_opts = FrameExtractionOptionsFromDict(dict["frame_opts"]);
  81. }
  82. if (dict.contains("mel_opts")) {
  83. opts.mel_opts = MelBanksOptionsFromDict(dict["mel_opts"]);
  84. }
  85. FROM_DICT(bool_, use_energy);
  86. FROM_DICT(float_, energy_floor);
  87. FROM_DICT(bool_, raw_energy);
  88. FROM_DICT(bool_, htk_compat);
  89. FROM_DICT(bool_, use_log_fbank);
  90. FROM_DICT(bool_, use_power);
  91. return opts;
  92. }
  93. py::dict AsDict(const FbankOptions &opts) {
  94. py::dict dict;
  95. dict["frame_opts"] = AsDict(opts.frame_opts);
  96. dict["mel_opts"] = AsDict(opts.mel_opts);
  97. AS_DICT(use_energy);
  98. AS_DICT(energy_floor);
  99. AS_DICT(raw_energy);
  100. AS_DICT(htk_compat);
  101. AS_DICT(use_log_fbank);
  102. AS_DICT(use_power);
  103. return dict;
  104. }
  105. #undef FROM_DICT
  106. #undef AS_DICT
  107. } // namespace knf