audio.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. int global_start = 0; // the start of a frame in the global time axis. in ms
  33. int global_end = 0; // the end of a frame in the global time axis. in ms
  34. };
  35. class Audio {
  36. private:
  37. float *speech_data=nullptr;
  38. int16_t *speech_buff=nullptr;
  39. char* speech_char=nullptr;
  40. int speech_len;
  41. int speech_align_len;
  42. float align_size;
  43. int data_type;
  44. queue<AudioFrame *> frame_queue;
  45. queue<AudioFrame *> asr_online_queue;
  46. queue<AudioFrame *> asr_offline_queue;
  47. public:
  48. Audio(int data_type);
  49. Audio(int data_type, int size);
  50. ~Audio();
  51. void Disp();
  52. void WavResample(int32_t sampling_rate, const float *waveform, int32_t n);
  53. bool LoadWav(const char* buf, int n_len, int32_t* sampling_rate);
  54. bool LoadWav(const char* filename, int32_t* sampling_rate);
  55. bool LoadWav2Char(const char* filename, int32_t* sampling_rate);
  56. bool LoadPcmwav(const char* buf, int n_file_len, int32_t* sampling_rate);
  57. bool LoadPcmwav(const char* filename, int32_t* sampling_rate);
  58. bool LoadPcmwav2Char(const char* filename, int32_t* sampling_rate);
  59. bool LoadOthers2Char(const char* filename);
  60. bool FfmpegLoad(const char *filename, bool copy2char=false);
  61. bool FfmpegLoad(const char* buf, int n_file_len);
  62. int FetchChunck(AudioFrame *&frame);
  63. int FetchTpass(AudioFrame *&frame);
  64. int Fetch(float *&dout, int &len, int &flag);
  65. int Fetch(float *&dout, int &len, int &flag, float &start_time);
  66. void Padding();
  67. void Split(OfflineStream* offline_streamj);
  68. void Split(VadModel* vad_obj, vector<std::vector<int>>& vad_segments, bool input_finished=true);
  69. void Split(VadModel* vad_obj, int chunk_len, bool input_finished=true, ASR_TYPE asr_mode=ASR_TWO_PASS);
  70. float GetTimeLen();
  71. int GetQueueSize() { return (int)frame_queue.size(); }
  72. char* GetSpeechChar(){return speech_char;}
  73. int GetSpeechLen(){return speech_len;}
  74. // 2pass
  75. vector<float> all_samples;
  76. int offset = 0;
  77. int speech_start=-1, speech_end=0;
  78. int speech_offline_start=-1;
  79. int seg_sample = MODEL_SAMPLE_RATE/1000;
  80. bool LoadPcmwavOnline(const char* buf, int n_file_len, int32_t* sampling_rate);
  81. void ResetIndex(){
  82. speech_start=-1;
  83. speech_end=0;
  84. speech_offline_start=-1;
  85. offset = 0;
  86. all_samples.clear();
  87. }
  88. };
  89. } // namespace funasr
  90. #endif