jmwang66
2023-05-09 8dab6d184a034ca86eafa644ea0d2100aadfe27d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
 * MIT License  (https://opensource.org/licenses/MIT)
*/
#pragma once 
#include <vector>
#include "precomp.h"
 
using namespace std;
namespace funasr {
class OnlineFeature {
 
public:
  OnlineFeature(int sample_rate, knf::FbankOptions fbank_opts, int lfr_m_, int lfr_n_,
                std::vector<std::vector<float>> cmvns_);
 
  void ExtractFeats(vector<vector<float>> &vad_feats, vector<float> waves, bool input_finished);
 
private:
  void OnlineFbank(vector<vector<float>> &vad_feats, vector<float> &waves);
  int OnlineLfrCmvn(vector<vector<float>> &vad_feats);
  
  static int ComputeFrameNum(int sample_length, int frame_sample_length, int frame_shift_sample_length) {
    int frame_num = static_cast<int>((sample_length - frame_sample_length) / frame_shift_sample_length + 1);
    if (frame_num >= 1 && sample_length >= frame_sample_length)
      return frame_num;
    else
      return 0;
  }
 
  void ResetCache() {
    reserve_waveforms_.clear();
    input_cache_.clear();
    lfr_splice_cache_.clear();
    input_finished_ = false;
 
  }
 
  knf::FbankOptions fbank_opts_;
  // The reserved waveforms by fbank
  std::vector<float> reserve_waveforms_;
  // waveforms reserved after last shift position
  std::vector<float> input_cache_;
  // lfr reserved cache
  std::vector<std::vector<float>> lfr_splice_cache_;
  std::vector<std::vector<float>> cmvns_;
 
  int sample_rate_ = 16000;
  int frame_sample_length_ = sample_rate_ / 1000 * 25;;
  int frame_shift_sample_length_ = sample_rate_ / 1000 * 10;
  int lfr_m_;
  int lfr_n_;
  bool input_finished_ = false;
 
};
 
} // namespace funasr