audio.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef AUDIO_H
  2. #define AUDIO_H
  3. #include <queue>
  4. #include <stdint.h>
  5. #include "vad-model.h"
  6. #include "offline-stream.h"
  7. #include "com-define.h"
  8. #ifndef WAV_HEADER_SIZE
  9. #define WAV_HEADER_SIZE 44
  10. #endif
  11. using namespace std;
  12. namespace funasr {
  13. class AudioFrame {
  14. private:
  15. int start;
  16. int end;
  17. public:
  18. AudioFrame();
  19. AudioFrame(int len);
  20. AudioFrame(const AudioFrame &other);
  21. AudioFrame(int start, int end, bool is_final);
  22. ~AudioFrame();
  23. int SetStart(int val);
  24. int SetEnd(int val);
  25. int GetStart();
  26. int GetLen();
  27. int Disp();
  28. // 2pass
  29. bool is_final = false;
  30. float* data = nullptr;
  31. int len;
  32. };
  33. class Audio {
  34. private:
  35. float *speech_data=nullptr;
  36. int16_t *speech_buff=nullptr;
  37. char* speech_char=nullptr;
  38. int speech_len;
  39. int speech_align_len;
  40. float align_size;
  41. int data_type;
  42. queue<AudioFrame *> frame_queue;
  43. queue<AudioFrame *> asr_online_queue;
  44. queue<AudioFrame *> asr_offline_queue;
  45. public:
  46. Audio(int data_type);
  47. Audio(int data_type, int size);
  48. ~Audio();
  49. void Disp();
  50. void WavResample(int32_t sampling_rate, const float *waveform, int32_t n);
  51. bool LoadWav(const char* buf, int n_len, int32_t* sampling_rate);
  52. bool LoadWav(const char* filename, int32_t* sampling_rate);
  53. bool LoadWav2Char(const char* filename, int32_t* sampling_rate);
  54. bool LoadPcmwav(const char* buf, int n_file_len, int32_t* sampling_rate);
  55. bool LoadPcmwav(const char* filename, int32_t* sampling_rate);
  56. bool LoadPcmwav2Char(const char* filename, int32_t* sampling_rate);
  57. bool LoadOthers2Char(const char* filename);
  58. bool FfmpegLoad(const char *filename, bool copy2char=false);
  59. bool FfmpegLoad(const char* buf, int n_file_len);
  60. int FetchChunck(AudioFrame *&frame);
  61. int FetchTpass(AudioFrame *&frame);
  62. int Fetch(float *&dout, int &len, int &flag);
  63. int Fetch(float *&dout, int &len, int &flag, float &start_time);
  64. void Padding();
  65. void Split(OfflineStream* offline_streamj);
  66. void Split(VadModel* vad_obj, vector<std::vector<int>>& vad_segments, bool input_finished=true);
  67. void Split(VadModel* vad_obj, int chunk_len, bool input_finished=true, ASR_TYPE asr_mode=ASR_TWO_PASS);
  68. float GetTimeLen();
  69. int GetQueueSize() { return (int)frame_queue.size(); }
  70. char* GetSpeechChar(){return speech_char;}
  71. int GetSpeechLen(){return speech_len;}
  72. // 2pass
  73. vector<float> all_samples;
  74. int offset = 0;
  75. int speech_start=-1, speech_end=0;
  76. int speech_offline_start=-1;
  77. int seg_sample = MODEL_SAMPLE_RATE/1000;
  78. bool LoadPcmwavOnline(const char* buf, int n_file_len, int32_t* sampling_rate);
  79. void ResetIndex(){
  80. speech_start=-1;
  81. speech_end=0;
  82. speech_offline_start=-1;
  83. offset = 0;
  84. all_samples.clear();
  85. }
  86. };
  87. } // namespace funasr
  88. #endif