bsp_opus.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include "include.h"
  2. #include "api.h"
  3. #if OPUS_ENC_EN
  4. #define OPUS_BIT_RATE 16000 //32kbps: 压缩比1:8, 每帧输出80字节; 16kbps: 压缩比1:16, 每帧输出40字节
  5. u16 opus_pcm_len(void);
  6. int opus_enc_frame(s16 *pcm, u8 *packet);
  7. void opus_kick_start(void);
  8. int sco_set_mic_gain_after_aec(void);
  9. void mic_post_gain_process_s(s16 *ptr, int gain, int samples);
  10. bool opus_enc_init(u32 spr, u32 nch, u32 bitrate);
  11. void opus_enc_exit(void);
  12. static u8 opus_sysclk, opus_init = 0;
  13. bool opus_start;
  14. static uint8_t opus_skip_frame = 0;
  15. //---------------------------------------------------------------------
  16. //
  17. #define MAX_PCM_BUF_SIZE 1280 //50ms
  18. typedef struct {
  19. s16 pcm[MAX_PCM_BUF_SIZE];
  20. u32 len;
  21. u16 widx;
  22. u16 ridx;
  23. } opus_cb_t;
  24. static opus_cb_t opus_pcm_cb AT(.opus.buf.bsp);
  25. static volatile int opus_enc_sta;
  26. static u8 opus_channel;
  27. static u8 opus_packet[40] AT(.opus.buf.bsp);
  28. //---------------------------------------------------------------------
  29. //缓存压缩数据
  30. #define OPUS_CACHE_LEN 480
  31. struct opus_mcb_t {
  32. u16 wptr;
  33. u16 rptr;
  34. volatile u16 len;
  35. u8 output[OPUS_CACHE_LEN];
  36. u8 nr_type;
  37. int gain;
  38. } bsp_opus_mcb AT(.opus.buf.bsp);
  39. //--------------------------------------------------------------------------------
  40. AT(.com_text.opus)
  41. void opus_skip_frame_do(s16 *ptr, u32 samples, int ch_mode)
  42. {
  43. opus_skip_frame--;
  44. memset(ptr, 0, samples << (1+ch_mode));
  45. }
  46. //--------------------------------------------------------------------------------
  47. //128点输入一次
  48. AT(.com_text.sndp)
  49. WEAK void opus_nr_process(s16 *buf)
  50. {
  51. }
  52. //---------------------------------------------------------------------
  53. AT(.com_text.opus)
  54. void bsp_opus_put_pcm(u8 *buf, u16 len, int ch_mode)
  55. {
  56. opus_channel = ch_mode + 1;
  57. opus_cb_t *p = &opus_pcm_cb;
  58. if (p->len <= MAX_PCM_BUF_SIZE) {
  59. memcpy(&p->pcm[p->widx], buf, len * 2 * opus_channel);
  60. p->widx += (len * opus_channel);
  61. if (p->widx >= MAX_PCM_BUF_SIZE) {
  62. p->widx = 0;
  63. }
  64. GLOBAL_INT_DISABLE();
  65. p->len += (len * opus_channel);
  66. GLOBAL_INT_RESTORE();
  67. }else{
  68. uart_putchar('!');
  69. }
  70. }
  71. //---------------------------------------------------------------------
  72. AT(.com_text.opus)
  73. static s16* opus_get_pcm(void)
  74. {
  75. s16 *buf = NULL;
  76. u32 frame_size = opus_pcm_len();
  77. frame_size = opus_channel * frame_size;
  78. opus_cb_t *p = &opus_pcm_cb;
  79. if (p->len >= frame_size) {
  80. buf = &p->pcm[p->ridx];
  81. p->ridx += frame_size;
  82. if (p->ridx >= MAX_PCM_BUF_SIZE) {
  83. p->ridx = 0;
  84. }
  85. GLOBAL_INT_DISABLE();
  86. p->len -= frame_size;
  87. GLOBAL_INT_RESTORE();
  88. }
  89. return buf;
  90. }
  91. //---------------------------------------------------------------------
  92. //缓存压缩好的数据
  93. AT(.com_text.opus)
  94. void bsp_opus_put_frame(u8 *buf, u16 len)
  95. {
  96. struct opus_mcb_t *p = &bsp_opus_mcb;
  97. if (p->len <= OPUS_CACHE_LEN) {
  98. memcpy(&p->output[p->wptr], buf, len);
  99. p->wptr += len;
  100. if (p->wptr >= OPUS_CACHE_LEN) {
  101. p->wptr = 0;
  102. }
  103. GLOBAL_INT_DISABLE();
  104. p->len += len;
  105. GLOBAL_INT_RESTORE();
  106. }else{
  107. uart_putchar('*');
  108. }
  109. }
  110. //---------------------------------------------------------------------
  111. //
  112. AT(.com_text.opus)
  113. u8 opus_get_frame(u8 *rx_buff, u16 opus_frame_len)
  114. {
  115. struct opus_mcb_t * p = &bsp_opus_mcb;
  116. if (p->len < opus_frame_len) { //1帧
  117. return 0;
  118. }
  119. memcpy(rx_buff, &p->output[p->rptr], opus_frame_len);
  120. p->rptr += opus_frame_len;
  121. if (p->rptr >= OPUS_CACHE_LEN) {
  122. p->rptr = 0;
  123. }
  124. GLOBAL_INT_DISABLE();
  125. p->len -= opus_frame_len;
  126. GLOBAL_INT_RESTORE();
  127. return opus_frame_len;
  128. }
  129. //---------------------------------------------------------------------
  130. //
  131. void bsp_opus_start_init(u8 nr_type)
  132. {
  133. memset(&bsp_opus_mcb, 0, sizeof(struct opus_mcb_t));
  134. memset(&opus_pcm_cb, 0, sizeof(opus_pcm_cb));
  135. opus_enc_sta = 0;
  136. bsp_opus_mcb.nr_type = nr_type;
  137. bsp_opus_mcb.gain = sco_set_mic_gain_after_aec();
  138. }
  139. //---------------------------------------------------------------------
  140. //
  141. void bsp_opus_stop_end(void)
  142. {
  143. if (bsp_opus_mcb.nr_type) {
  144. bsp_opus_mcb.nr_type = 0;
  145. }
  146. }
  147. //---------------------------------------------------------------------
  148. //opus_sdadc_process
  149. AT(.com_text.opus)
  150. WEAK void opus_sdadc_process(u8 *ptr, u32 samples, int ch_mode)
  151. {
  152. if (opus_skip_frame) {
  153. opus_skip_frame_do((s16*)ptr, samples, ch_mode);
  154. return;
  155. }
  156. if(bsp_opus_mcb.nr_type){
  157. opus_nr_process((void *)ptr);
  158. if(bsp_opus_mcb.gain){
  159. mic_post_gain_process_s((s16 *)ptr, bsp_opus_mcb.gain, samples); //数字后增益
  160. }
  161. if(bsp_opus_mcb.nr_type == 2){
  162. ch_mode = 0;
  163. }
  164. }
  165. bsp_opus_put_pcm(ptr, samples, ch_mode);
  166. if (!opus_enc_sta && opus_pcm_cb.len >= (opus_pcm_len()<< ch_mode)) {
  167. opus_kick_start();
  168. }
  169. #if TME_APP_EN
  170. tme_opus_frame_energy_cal(ptr,samples);
  171. #endif
  172. }
  173. //---------------------------------------------------------------------
  174. //
  175. //---------------------------------------------------------------------
  176. //
  177. AT(.com_text.opus)
  178. void opus_enc_process(void)
  179. {
  180. int rlen;
  181. s16 *in = opus_get_pcm();
  182. if (in == NULL) {
  183. return;
  184. }
  185. opus_enc_sta = 1;
  186. rlen = opus_enc_frame(in, opus_packet);
  187. if(rlen < 0 || rlen > (128*opus_channel)) {
  188. uart_putchar('&');
  189. //printf("opus encode failed: %d\n",rlen);
  190. } else {
  191. //printf("opus:%d\n", rlen);
  192. bsp_opus_put_frame(opus_packet, rlen);
  193. }
  194. opus_enc_sta = 0;
  195. }
  196. AT(.text.opus.bsp)
  197. bool bsp_opus_is_encode(void)
  198. {
  199. return (opus_init == 1);
  200. }
  201. u16 opus_enc_data_len_get(void)
  202. {
  203. return bsp_opus_mcb.len;
  204. }
  205. AT(.text.opus.bsp)
  206. void bsp_opus_encode_start(void)
  207. {
  208. if(opus_init == 0 && f_bt.disp_status < BT_STA_INCOMING){
  209. printf("--->bsp_opus_encode_start\n");
  210. bt_tws_user_key(OPUS_ENC_START);
  211. bt_audio_bypass();
  212. delay_5ms(10); //wait audio bypass
  213. if(opus_enc_init(SPR_16000, 1, OPUS_BIT_RATE)){
  214. opus_sysclk = sys_clk_get();
  215. sys_clk_set(SYS_160M);
  216. opus_skip_frame = 125; //丢掉MIC刚启动时不稳定的数据
  217. bsp_opus_start_init(NR_TYPE_SMIC);
  218. audio_path_init(AUDIO_PATH_OPUS);
  219. audio_path_start(AUDIO_PATH_OPUS);
  220. opus_init = 1;
  221. }
  222. }
  223. }
  224. AT(.text.opus.bsp)
  225. void bsp_opus_encode_stop(void)
  226. {
  227. if(opus_init){
  228. printf("--->bsp_opus_encode_stop\n");
  229. opus_init = 0;
  230. opus_skip_frame = 0;
  231. bt_tws_user_key(OPUS_ENC_STOP);
  232. audio_path_exit(AUDIO_PATH_OPUS);
  233. opus_enc_exit();
  234. bsp_opus_stop_end();
  235. sys_clk_set(opus_sysclk);
  236. bt_audio_enable();
  237. delay_5ms(10); //wait audio bypass enable
  238. }
  239. }
  240. AT(.text.opus.bsp)
  241. u8 bsp_opus_get_enc_frame(u8 *buff, u16 len)
  242. {
  243. if(opus_init && (buff != NULL)){
  244. return opus_get_frame(buff,len);
  245. }
  246. return 0;
  247. }
  248. #endif