From 48e6117a3a1952207182ed905d8547bbc5b81503 Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期二, 11 四月 2023 21:50:29 +0800
Subject: [PATCH] Merge pull request #334 from alibaba-damo-academy/dev_lyb

---
 funasr/runtime/onnxruntime/src/FeatureExtract.cpp |   28 ++++++++--------------------
 1 files changed, 8 insertions(+), 20 deletions(-)

diff --git a/funasr/runtime/onnxruntime/src/FeatureExtract.cpp b/funasr/runtime/onnxruntime/src/FeatureExtract.cpp
index 1b0c3c4..6d2826a 100644
--- a/funasr/runtime/onnxruntime/src/FeatureExtract.cpp
+++ b/funasr/runtime/onnxruntime/src/FeatureExtract.cpp
@@ -5,14 +5,10 @@
 
 FeatureExtract::FeatureExtract(int mode) : mode(mode)
 {
-    fftw_init();
 }
 
 FeatureExtract::~FeatureExtract()
 {
-    fftwf_free(fft_input);
-    fftwf_free(fft_out);
-    fftwf_destroy_plan(p);
 }
 
 void FeatureExtract::reset()
@@ -26,34 +22,25 @@
     return fqueue.size();
 }
 
-void FeatureExtract::fftw_init()
+void FeatureExtract::insert(fftwf_plan plan, float *din, int len, int flag)
 {
-    int fft_size = 512;
-    fft_input = (float *)fftwf_malloc(sizeof(float) * fft_size);
-    fft_out = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * fft_size);
+    float* fft_input = (float *)fftwf_malloc(sizeof(float) * fft_size);
+    fftwf_complex* fft_out = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * fft_size);
     memset(fft_input, 0, sizeof(float) * fft_size);
-    p = fftwf_plan_dft_r2c_1d(fft_size, fft_input, fft_out, FFTW_ESTIMATE);
-}
 
-void FeatureExtract::insert(float *din, int len, int flag)
-{
     const float *window = (const float *)&window_hex;
     if (mode == 3)
         window = (const float *)&window_hamm_hex;
-
-    int window_size = 400;
-    int fft_size = 512;
-    int window_shift = 160;
 
     speech.load(din, len);
     int i, j;
     float tmp_feature[80];
     if (mode == 0 || mode == 2 || mode == 3) {
-        int ll = (speech.size() - 400) / 160 + 1;
+        int ll = (speech.size() - window_size) / window_shift + 1;
         fqueue.reinit(ll);
     }
 
-    for (i = 0; i <= speech.size() - 400; i = i + window_shift) {
+    for (i = 0; i <= speech.size() - window_size; i = i + window_shift) {
         float tmp_mean = 0;
         for (j = 0; j < window_size; j++) {
             tmp_mean += speech[i + j];
@@ -70,7 +57,7 @@
             pre_val = cur_val;
         }
 
-        fftwf_execute(p);
+        fftwf_execute_dft_r2c(plan, fft_input, fft_out);
 
         melspect((float *)fft_out, tmp_feature);
         int tmp_flag = S_MIDDLE;
@@ -80,6 +67,8 @@
         fqueue.push(tmp_feature, tmp_flag);
     }
     speech.update(i);
+    fftwf_free(fft_input);
+    fftwf_free(fft_out);
 }
 
 bool FeatureExtract::fetch(Tensor<float> *&dout)
@@ -128,7 +117,6 @@
 void FeatureExtract::melspect(float *din, float *dout)
 {
     float fftmag[256];
-//    float tmp;
     const float *melcoe = (const float *)melcoe_hex;
     int i;
     for (i = 0; i < 256; i++) {

--
Gitblit v1.9.1