janitor

janitor
git clone https://git.prime8.dev/janitor.git
Log | Files | Refs | README | LICENSE

utils.cc (4740B)


      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/utils.h"
     20 
     21 #include <string>
     22 
     23 #include "kaldi-native-fbank/csrc/feature-fbank.h"
     24 #include "kaldi-native-fbank/csrc/feature-mfcc.h"
     25 #include "kaldi-native-fbank/csrc/feature-window.h"
     26 #include "kaldi-native-fbank/csrc/whisper-feature.h"
     27 
     28 #define FROM_DICT(type, key)         \
     29   if (dict.contains(#key)) {         \
     30     opts.key = py::type(dict[#key]); \
     31   }
     32 
     33 #define AS_DICT(key) dict[#key] = opts.key
     34 
     35 namespace knf {
     36 
     37 FrameExtractionOptions FrameExtractionOptionsFromDict(py::dict dict) {
     38   FrameExtractionOptions opts;
     39 
     40   FROM_DICT(float_, samp_freq);
     41   FROM_DICT(float_, frame_shift_ms);
     42   FROM_DICT(float_, frame_length_ms);
     43   FROM_DICT(float_, dither);
     44   FROM_DICT(float_, preemph_coeff);
     45   FROM_DICT(bool_, remove_dc_offset);
     46   FROM_DICT(str, window_type);
     47   FROM_DICT(bool_, round_to_power_of_two);
     48   FROM_DICT(float_, blackman_coeff);
     49   FROM_DICT(bool_, snip_edges);
     50 
     51   return opts;
     52 }
     53 
     54 py::dict AsDict(const FrameExtractionOptions &opts) {
     55   py::dict dict;
     56 
     57   AS_DICT(samp_freq);
     58   AS_DICT(frame_shift_ms);
     59   AS_DICT(frame_length_ms);
     60   AS_DICT(dither);
     61   AS_DICT(preemph_coeff);
     62   AS_DICT(remove_dc_offset);
     63   AS_DICT(window_type);
     64   AS_DICT(round_to_power_of_two);
     65   AS_DICT(blackman_coeff);
     66   AS_DICT(snip_edges);
     67 
     68   return dict;
     69 }
     70 
     71 MelBanksOptions MelBanksOptionsFromDict(py::dict dict) {
     72   MelBanksOptions opts;
     73 
     74   FROM_DICT(int_, num_bins);
     75   FROM_DICT(float_, low_freq);
     76   FROM_DICT(float_, high_freq);
     77   FROM_DICT(float_, vtln_low);
     78   FROM_DICT(float_, vtln_high);
     79   FROM_DICT(bool_, debug_mel);
     80   FROM_DICT(bool_, htk_mode);
     81   FROM_DICT(bool_, is_librosa);
     82   FROM_DICT(str, norm);
     83 
     84   return opts;
     85 }
     86 
     87 py::dict AsDict(const MelBanksOptions &opts) {
     88   py::dict dict;
     89 
     90   AS_DICT(num_bins);
     91   AS_DICT(low_freq);
     92   AS_DICT(high_freq);
     93   AS_DICT(vtln_low);
     94   AS_DICT(vtln_high);
     95   AS_DICT(debug_mel);
     96   AS_DICT(htk_mode);
     97   AS_DICT(is_librosa);
     98   AS_DICT(norm);
     99 
    100   return dict;
    101 }
    102 
    103 FbankOptions FbankOptionsFromDict(py::dict dict) {
    104   FbankOptions opts;
    105 
    106   if (dict.contains("frame_opts")) {
    107     opts.frame_opts = FrameExtractionOptionsFromDict(dict["frame_opts"]);
    108   }
    109 
    110   if (dict.contains("mel_opts")) {
    111     opts.mel_opts = MelBanksOptionsFromDict(dict["mel_opts"]);
    112   }
    113 
    114   FROM_DICT(bool_, use_energy);
    115   FROM_DICT(float_, energy_floor);
    116   FROM_DICT(bool_, raw_energy);
    117   FROM_DICT(bool_, htk_compat);
    118   FROM_DICT(bool_, use_log_fbank);
    119   FROM_DICT(bool_, use_power);
    120 
    121   return opts;
    122 }
    123 
    124 py::dict AsDict(const FbankOptions &opts) {
    125   py::dict dict;
    126 
    127   dict["frame_opts"] = AsDict(opts.frame_opts);
    128   dict["mel_opts"] = AsDict(opts.mel_opts);
    129   AS_DICT(use_energy);
    130   AS_DICT(energy_floor);
    131   AS_DICT(raw_energy);
    132   AS_DICT(htk_compat);
    133   AS_DICT(use_log_fbank);
    134   AS_DICT(use_power);
    135 
    136   return dict;
    137 }
    138 
    139 MfccOptions MfccOptionsFromDict(py::dict dict) {
    140   MfccOptions opts;
    141 
    142   if (dict.contains("frame_opts")) {
    143     opts.frame_opts = FrameExtractionOptionsFromDict(dict["frame_opts"]);
    144   }
    145 
    146   if (dict.contains("mel_opts")) {
    147     opts.mel_opts = MelBanksOptionsFromDict(dict["mel_opts"]);
    148   }
    149 
    150   FROM_DICT(int_, num_ceps);
    151   FROM_DICT(bool_, use_energy);
    152   FROM_DICT(float_, energy_floor);
    153   FROM_DICT(bool_, raw_energy);
    154   FROM_DICT(float_, cepstral_lifter);
    155   FROM_DICT(bool_, htk_compat);
    156 
    157   return opts;
    158 }
    159 
    160 py::dict AsDict(const MfccOptions &opts) {
    161   py::dict dict;
    162 
    163   dict["frame_opts"] = AsDict(opts.frame_opts);
    164   dict["mel_opts"] = AsDict(opts.mel_opts);
    165   AS_DICT(num_ceps);
    166   AS_DICT(use_energy);
    167   AS_DICT(energy_floor);
    168   AS_DICT(raw_energy);
    169   AS_DICT(cepstral_lifter);
    170   AS_DICT(htk_compat);
    171 
    172   return dict;
    173 }
    174 
    175 WhisperFeatureOptions WhisperFeatureOptionsFromDict(py::dict dict) {
    176   WhisperFeatureOptions opts;
    177 
    178   if (dict.contains("frame_opts")) {
    179     opts.frame_opts = FrameExtractionOptionsFromDict(dict["frame_opts"]);
    180   }
    181 
    182   FROM_DICT(int_, dim);
    183 
    184   return opts;
    185 }
    186 
    187 py::dict AsDict(const WhisperFeatureOptions &opts) {
    188   py::dict dict;
    189 
    190   dict["frame_opts"] = AsDict(opts.frame_opts);
    191   AS_DICT(dim);
    192 
    193   return dict;
    194 }
    195 
    196 #undef FROM_DICT
    197 #undef AS_DICT
    198 
    199 }  // namespace knf