audio.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. int dest_sample_rate;
  48. public:
  49. Audio(int data_type);
  50. Audio(int model_sample_rate,int data_type);
  51. Audio(int model_sample_rate,int data_type, int size);
  52. ~Audio();
  53. void Disp();
  54. void WavResample(int32_t sampling_rate, const float *waveform, int32_t n);
  55. bool LoadWav(const char* buf, int n_len, int32_t* sampling_rate);
  56. bool LoadWav(const char* filename, int32_t* sampling_rate);
  57. bool LoadWav2Char(const char* filename, int32_t* sampling_rate);
  58. bool LoadPcmwav(const char* buf, int n_file_len, int32_t* sampling_rate);
  59. bool LoadPcmwav(const char* filename, int32_t* sampling_rate);
  60. bool LoadPcmwav2Char(const char* filename, int32_t* sampling_rate);
  61. bool LoadOthers2Char(const char* filename);
  62. bool FfmpegLoad(const char *filename, bool copy2char=false);
  63. bool FfmpegLoad(const char* buf, int n_file_len);
  64. int FetchChunck(AudioFrame *&frame);
  65. int FetchTpass(AudioFrame *&frame);
  66. int Fetch(float *&dout, int &len, int &flag);
  67. int Fetch(float *&dout, int &len, int &flag, float &start_time);
  68. void Padding();
  69. void Split(OfflineStream* offline_streamj);
  70. void Split(VadModel* vad_obj, vector<std::vector<int>>& vad_segments, bool input_finished=true);
  71. void Split(VadModel* vad_obj, int chunk_len, bool input_finished=true, ASR_TYPE asr_mode=ASR_TWO_PASS);
  72. float GetTimeLen();
  73. int GetQueueSize() { return (int)frame_queue.size(); }
  74. char* GetSpeechChar(){return speech_char;}
  75. int GetSpeechLen(){return speech_len;}
  76. // 2pass
  77. vector<float> all_samples;
  78. int offset = 0;
  79. int speech_start=-1, speech_end=0;
  80. int speech_offline_start=-1;
  81. int seg_sample = MODEL_SAMPLE_RATE/1000;
  82. bool LoadPcmwavOnline(const char* buf, int n_file_len, int32_t* sampling_rate);
  83. void ResetIndex(){
  84. speech_start=-1;
  85. speech_end=0;
  86. speech_offline_start=-1;
  87. offset = 0;
  88. all_samples.clear();
  89. }
  90. };
  91. } // namespace funasr
  92. #endif