knfc.cc (1325B)
1 #include "knfc.h" 2 #include "kaldi-native-fbank/csrc/feature-fbank.h" 3 #include "kaldi-native-fbank/csrc/feature-window.h" 4 #include "kaldi-native-fbank/csrc/mel-computations.h" 5 #include "kaldi-native-fbank/csrc/online-feature.h" 6 #include <iostream> 7 8 FbankResult ComputeFbank(const float *waveform, int32_t waveform_size) { 9 knf::FbankOptions options; 10 options.htk_compat = true; 11 options.use_energy = false; 12 13 // Frame options 14 options.frame_opts.window_type = "hanning"; 15 options.frame_opts.dither = 0.0; 16 17 // Mel options 18 options.mel_opts.num_bins = 128; 19 20 knf::OnlineFbank fbank(options); 21 fbank.AcceptWaveform(options.frame_opts.samp_freq, waveform, waveform_size); 22 fbank.InputFinished(); 23 24 int32_t num_frames = fbank.NumFramesReady(); 25 int32_t num_bins = options.mel_opts.num_bins; 26 27 // Allocate memory for the frames 28 float *frames = new float[num_frames * num_bins]; 29 30 // Fill the frames 31 for (int32_t i = 0; i < num_frames; i++) { 32 const float *frame = fbank.GetFrame(i); 33 std::copy(frame, frame + num_bins, frames + i * num_bins); 34 } 35 36 FbankResult result; 37 result.frames = frames; 38 result.num_frames = num_frames; 39 return result; 40 } 41 42 void DestroyFbankResult(FbankResult *result) { 43 if (result) { 44 delete[] result->frames; 45 result->frames = nullptr; 46 result->num_frames = 0; 47 } 48 }