airtalk_speech_ec7xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "csdk.h"
  2. #include "airtalk_def.h"
  3. #include "airtalk_api.h"
  4. #include "luat_airtalk.h"
  5. #include "luat_multimedia.h"
  6. #define PCM_BLOCK_LEN (prv_speech.one_frame_len)
  7. #define DOWNLOAD_CACHE_MASK (14) //下行数据缓存1<<14Byte (16K)
  8. #define PCM_PLAY_FRAME_LOOP_CNT (4) //4个播放缓冲区循环使用
  9. #define SPEECH_ONE_CACHE_MAX (6400) //16K单声道200ms的数据量
  10. static const uint8_t amr_nb_byte_len[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  11. static const uint8_t amr_wb_byte_len[16] = {17, 23, 32, 36, 40, 46, 50, 58, 60, 5,0, 0, 0, 0, 0, 0};
  12. typedef struct
  13. {
  14. BSP_FifoStruct download_data_fifo;
  15. luat_rtos_task_handle speech_task_handle;
  16. int multimedia_id;
  17. void *audio_handle;
  18. const uint8_t *amr_byte_len;
  19. uint32_t one_frame_len;
  20. uint32_t play_data_cache[SPEECH_ONE_CACHE_MAX];
  21. uint32_t record_data_cache[SPEECH_ONE_CACHE_MAX >> 1];
  22. uint8_t *play_data_buffer; //播放缓冲区
  23. uint8_t *record_data_buffer;
  24. uint16_t decode_frame_cnt;
  25. uint16_t encode_frame_cnt;
  26. uint16_t total_play_frame; //播放缓冲区总共的帧数
  27. uint16_t ref_frame_start_pos; //回声抑制参考起始帧位置
  28. volatile uint16_t record_frame_pos; //已经缓存的录音帧数
  29. volatile uint16_t play_frame_pos; //已经播放的帧数
  30. uint8_t record_enable; //允许录音
  31. uint8_t play_enable; //允许播放
  32. uint8_t audio_sleep_mode;
  33. uint8_t decode_sync_ok;
  34. volatile uint8_t record_buffer_pos;
  35. uint8_t debug_on_off;
  36. uint8_t download_data_buffer[1 << DOWNLOAD_CACHE_MASK]; //测试用的播放缓冲区,正常播放不要使用
  37. }speech_ctrl_t;
  38. static speech_ctrl_t prv_speech;
  39. static __USER_FUNC_IN_RAM__ int airtalk_record_cb(uint8_t id ,luat_i2s_event_t event, uint8_t *rx_data, uint32_t rx_len, void *param)
  40. {
  41. if (!prv_speech.audio_handle) return 0;
  42. switch(event)
  43. {
  44. case LUAT_I2S_EVENT_RX_DONE:
  45. memcpy(&prv_speech.record_data_buffer[SPEECH_ONE_CACHE_MAX * prv_speech.record_buffer_pos + prv_speech.record_frame_pos * prv_speech.one_frame_len], rx_data, rx_len);
  46. prv_speech.record_frame_pos++;
  47. prv_speech.play_frame_pos++;
  48. if (prv_speech.record_frame_pos >= prv_speech.encode_frame_cnt)
  49. {
  50. if (prv_speech.record_enable)
  51. {
  52. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_ENCODE_ONCE, prv_speech.record_buffer_pos, prv_speech.ref_frame_start_pos, 0, 0);
  53. }
  54. prv_speech.record_frame_pos = 0;
  55. prv_speech.record_buffer_pos = !prv_speech.record_buffer_pos;
  56. prv_speech.ref_frame_start_pos += prv_speech.encode_frame_cnt;
  57. if (prv_speech.ref_frame_start_pos >= prv_speech.total_play_frame)
  58. {
  59. prv_speech.ref_frame_start_pos = 0;
  60. }
  61. }
  62. if (prv_speech.play_frame_pos >= prv_speech.decode_frame_cnt)
  63. {
  64. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_DECODE_ONCE, 0, 0, 0, 0);
  65. prv_speech.play_frame_pos = 0;
  66. }
  67. break;
  68. case LUAT_I2S_EVENT_TRANSFER_DONE:
  69. break;
  70. default:
  71. break;
  72. }
  73. return 0;
  74. }
  75. extern void log_on(void);
  76. static void speech_task(void *param)
  77. {
  78. if (!prv_speech.audio_sleep_mode)
  79. {
  80. prv_speech.audio_sleep_mode = LUAT_AUDIO_PM_SHUTDOWN;
  81. }
  82. if (!prv_speech.decode_frame_cnt)
  83. {
  84. prv_speech.decode_frame_cnt = 5;
  85. }
  86. if (!prv_speech.encode_frame_cnt)
  87. {
  88. prv_speech.encode_frame_cnt = 5;
  89. }
  90. prv_speech.play_data_buffer = (uint8_t *)prv_speech.play_data_cache;
  91. prv_speech.record_data_buffer = (uint8_t *)prv_speech.record_data_cache;
  92. luat_audio_conf_t* audio_conf = luat_audio_get_config(prv_speech.multimedia_id);
  93. luat_i2s_conf_t *i2s = luat_i2s_get_config(audio_conf->codec_conf.i2s_id);
  94. OS_InitFifo(&prv_speech.download_data_fifo, prv_speech.download_data_buffer, DOWNLOAD_CACHE_MASK);
  95. uint8_t *ref_input;
  96. luat_event_t event;
  97. uint32_t decode_pos = 0;
  98. uint32_t current_play_cnt = 0; //当前播放缓冲区,用于解码数据存入下一个缓存
  99. uint32_t i;
  100. uint32_t data_pos;
  101. PV_Union u_point;
  102. uint8_t out_len, lost_data, temp_len, need_stop_record, need_stop_play, wait_stop_play,is_amr_wb;
  103. uint8_t amr_buff[64];
  104. need_stop_record = 0;
  105. need_stop_play = 0;
  106. wait_stop_play = 0;
  107. temp_len = 0;
  108. while (1)
  109. {
  110. luat_rtos_event_recv(prv_speech.speech_task_handle, 0, &event, NULL, LUAT_WAIT_FOREVER);
  111. switch(event.id)
  112. {
  113. case AIRTALK_EVENT_AMR_ENCODE_ONCE:
  114. if (!prv_speech.audio_handle)
  115. {
  116. break;
  117. }
  118. if (prv_speech.record_enable)
  119. {
  120. if (prv_speech.debug_on_off)
  121. {
  122. LUAT_DEBUG_PRINT("ref point %d, record cnt %d", event.param2, event.param1);
  123. }
  124. u_point.pu8 = &prv_speech.record_data_buffer[SPEECH_ONE_CACHE_MAX * event.param1];
  125. luat_airtalk_net_save_uplink_head(luat_mcu_tick64_ms());
  126. for(i = 0; i < prv_speech.encode_frame_cnt; i++)
  127. {
  128. ref_input = &prv_speech.play_data_buffer[PCM_BLOCK_LEN * event.param2 + i * PCM_BLOCK_LEN];
  129. //录音时刻对应的放音数据作为回声消除的参考数据输入,可以完美消除回声
  130. luat_audio_inter_amr_encode_with_ref(&u_point.pu16[(i * PCM_BLOCK_LEN) >> 1], amr_buff, &out_len, ref_input);
  131. luat_airtalk_net_save_uplink_data(amr_buff, out_len);
  132. }
  133. luat_airtalk_net_uplink_once();
  134. //如果有停止录音的请求,让MQTT上行一次终止包
  135. if (need_stop_record)
  136. {
  137. LUAT_DEBUG_PRINT("upload stop!");
  138. need_stop_record = 0;
  139. prv_speech.record_enable = 0;
  140. luat_airtalk_callback(LUAT_AIRTALK_CB_RECORD_END, NULL, 0);
  141. luat_airtalk_net_uplink_end();
  142. }
  143. }
  144. break;
  145. case AIRTALK_EVENT_AMR_DECODE_ONCE:
  146. if (!prv_speech.audio_handle)
  147. {
  148. break;
  149. }
  150. current_play_cnt = (current_play_cnt + 1) & 0x3;
  151. decode_pos = (current_play_cnt + 1) & 0x03;
  152. if (prv_speech.debug_on_off)
  153. {
  154. LUAT_DEBUG_PRINT("play pos %u, decode pos %u", current_play_cnt, decode_pos);
  155. }
  156. if (prv_speech.decode_sync_ok)
  157. {
  158. lost_data = 0;
  159. for(i = 0; i < prv_speech.decode_frame_cnt; i++)
  160. {
  161. if (OS_CheckFifoUsedSpace(&prv_speech.download_data_fifo))
  162. {
  163. data_pos = (uint32_t)(prv_speech.download_data_fifo.RPoint & prv_speech.download_data_fifo.Mask);
  164. temp_len = prv_speech.amr_byte_len[(prv_speech.download_data_buffer[data_pos] >> 3) & 0x0f];
  165. OS_ReadFifo(&prv_speech.download_data_fifo, amr_buff, temp_len + 1);
  166. u_point.pu8 = &prv_speech.play_data_buffer[PCM_BLOCK_LEN * prv_speech.decode_frame_cnt * decode_pos + i * PCM_BLOCK_LEN];
  167. luat_audio_inter_amr_coder_decode(prv_speech.audio_handle, u_point.pu16, amr_buff, &out_len);
  168. }
  169. else
  170. {
  171. memset(&prv_speech.play_data_buffer[PCM_BLOCK_LEN * prv_speech.decode_frame_cnt * decode_pos + i * PCM_BLOCK_LEN], 0, PCM_BLOCK_LEN);
  172. lost_data = 1;
  173. }
  174. }
  175. if (lost_data)
  176. {
  177. LUAT_DEBUG_PRINT("lost");
  178. prv_speech.decode_sync_ok = 0;
  179. luat_airtalk_net_force_sync_downlink();
  180. }
  181. }
  182. else
  183. {
  184. if (prv_speech.debug_on_off)
  185. {
  186. LUAT_DEBUG_PRINT("no decode");
  187. }
  188. memset(&prv_speech.play_data_buffer[PCM_BLOCK_LEN * prv_speech.decode_frame_cnt * decode_pos], 0, prv_speech.decode_frame_cnt * PCM_BLOCK_LEN);
  189. }
  190. if (wait_stop_play)
  191. {
  192. wait_stop_play = 0;
  193. prv_speech.play_enable = 0;
  194. LUAT_DEBUG_PRINT("play stop!");
  195. luat_airtalk_callback(LUAT_AIRTALK_CB_PLAY_END, NULL, 0);
  196. }
  197. else if (need_stop_play)
  198. {
  199. LUAT_DEBUG_PRINT("play wait stop!");
  200. wait_stop_play = 1;
  201. need_stop_play = 0;
  202. }
  203. //既没有播放也没有录音,就直接停止audio
  204. if (!prv_speech.record_enable && !prv_speech.play_enable)
  205. {
  206. LUAT_DEBUG_PRINT("audio stop!");
  207. luat_audio_record_stop(prv_speech.multimedia_id);
  208. luat_audio_pm_request(prv_speech.multimedia_id, prv_speech.audio_sleep_mode);
  209. luat_audio_inter_amr_coder_deinit(prv_speech.audio_handle);
  210. luat_i2s_load_old_config(audio_conf->codec_conf.i2s_id);
  211. prv_speech.audio_handle = NULL;
  212. prv_speech.decode_sync_ok = 0;
  213. luat_airtalk_callback(LUAT_AIRTALK_CB_AUDIO_END, NULL, 0);
  214. }
  215. break;
  216. case AIRTALK_EVENT_AMR_START:
  217. if (!prv_speech.audio_handle)
  218. {
  219. is_amr_wb = event.param1?1:0;
  220. LUAT_DEBUG_PRINT("play start %s!", is_amr_wb?"amr-wb":"amr-nb");
  221. prv_speech.one_frame_len = 320 * (is_amr_wb + 1);
  222. luat_audio_pm_request(prv_speech.multimedia_id, LUAT_AUDIO_PM_RESUME);
  223. prv_speech.amr_byte_len = is_amr_wb?amr_wb_byte_len:amr_nb_byte_len;
  224. memset(prv_speech.play_data_buffer, 0, sizeof(prv_speech.play_data_cache));
  225. memset(prv_speech.record_data_buffer, 0, sizeof(prv_speech.record_data_cache));
  226. prv_speech.record_buffer_pos = 0;
  227. prv_speech.record_frame_pos = 0;
  228. prv_speech.play_frame_pos = 0;
  229. prv_speech.ref_frame_start_pos = 0;
  230. prv_speech.total_play_frame = prv_speech.decode_frame_cnt * PCM_PLAY_FRAME_LOOP_CNT;
  231. prv_speech.audio_handle = luat_audio_inter_amr_coder_init(is_amr_wb, 7 + is_amr_wb);
  232. prv_speech.download_data_fifo.RPoint = 0;
  233. prv_speech.download_data_fifo.WPoint = 0;
  234. memset(prv_speech.play_data_cache, 0, sizeof(prv_speech.play_data_cache));
  235. current_play_cnt = 0;
  236. luat_i2s_save_old_config(audio_conf->codec_conf.i2s_id);
  237. i2s->cb_rx_len = PCM_BLOCK_LEN;
  238. i2s->luat_i2s_event_callback = airtalk_record_cb;
  239. luat_audio_record_and_play(prv_speech.multimedia_id, 8000 * (is_amr_wb + 1), prv_speech.play_data_buffer, prv_speech.decode_frame_cnt * PCM_BLOCK_LEN, PCM_PLAY_FRAME_LOOP_CNT);
  240. luat_airtalk_callback(LUAT_AIRTALK_CB_PLAY_START, NULL, 0);
  241. if (prv_speech.record_enable)//已经请求录音了,那么就开始录音了
  242. {
  243. luat_airtalk_net_uplink_start();
  244. luat_airtalk_callback(LUAT_AIRTALK_CB_RECORD_START, NULL, 0);
  245. }
  246. luat_airtalk_callback(LUAT_AIRTALK_CB_AUDIO_START, NULL, 0);
  247. }
  248. break;
  249. case AIRTALK_EVENT_AMR_RECORD_STOP:
  250. if (prv_speech.record_enable)
  251. {
  252. need_stop_record = 1;
  253. LUAT_DEBUG_PRINT("record require stop!");
  254. }
  255. break;
  256. case AIRTALK_EVENT_AMR_PLAY_STOP:
  257. if (prv_speech.play_enable && !need_stop_play)
  258. {
  259. need_stop_play = 1;
  260. LUAT_DEBUG_PRINT("play require stop!");
  261. }
  262. break;
  263. }
  264. }
  265. }
  266. void luat_airtalk_speech_init(void)
  267. {
  268. #if defined (FEATURE_AMR_CP_ENABLE) || defined (FEATURE_VEM_CP_ENABLE)
  269. if (!prv_speech.speech_task_handle)
  270. {
  271. luat_rtos_task_create(&prv_speech.speech_task_handle, 4096, 100, "airtalk_speech", speech_task, NULL, 64);
  272. }
  273. #else
  274. LUAT_DEBUG_PRINT("sdk no audio function, stop!!!");
  275. #endif
  276. }
  277. void luat_airtalk_speech_audio_param_config(int multimedia_id, uint8_t audio_sleep_mode)
  278. {
  279. prv_speech.multimedia_id = multimedia_id;
  280. prv_speech.audio_sleep_mode = audio_sleep_mode;
  281. }
  282. void luat_airtalk_speech_start_play(uint8_t is_16k)
  283. {
  284. if (!prv_speech.audio_handle)
  285. {
  286. prv_speech.play_enable = 1;
  287. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_START, is_16k, 0, 0, 0);
  288. }
  289. }
  290. void luat_airtalk_speech_stop_play()
  291. {
  292. LUAT_DEBUG_PRINT("broadcast play end!");
  293. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_PLAY_STOP, 0, 0, 0, 0);
  294. }
  295. void luat_airtalk_speech_sync_ok(void)
  296. {
  297. prv_speech.decode_sync_ok = 1;
  298. }
  299. int luat_airtalk_speech_set_one_block_frame_cnt(uint8_t decode_frame_cnt, uint8_t encode_frame_cnt)
  300. {
  301. if (prv_speech.audio_handle) return -ERROR_DEVICE_BUSY;
  302. if (decode_frame_cnt > 10) return -ERROR_PARAM_INVALID;
  303. if (decode_frame_cnt < 2) return -ERROR_PARAM_INVALID;
  304. if (encode_frame_cnt < 2) return -ERROR_PARAM_INVALID;
  305. if (encode_frame_cnt > 5) return -ERROR_PARAM_INVALID;
  306. prv_speech.decode_frame_cnt = decode_frame_cnt;
  307. prv_speech.encode_frame_cnt = encode_frame_cnt;
  308. return 0;
  309. }
  310. void luat_airtalk_speech_save_downlink_data(uint8_t *data, uint32_t len)
  311. {
  312. OS_WriteFifo(&prv_speech.download_data_fifo, data, len);
  313. }
  314. void luat_airtalk_speech_record_switch(uint8_t on_off, uint8_t is_16k)
  315. {
  316. if (on_off)
  317. {
  318. if (!prv_speech.audio_handle)
  319. {
  320. prv_speech.record_enable = 1;
  321. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_START, is_16k, 0, 0, 0);
  322. }
  323. else
  324. {
  325. if (!prv_speech.record_enable)
  326. {
  327. prv_speech.record_enable = 1;
  328. luat_airtalk_callback(LUAT_AIRTALK_CB_RECORD_START, NULL, 0);
  329. }
  330. }
  331. }
  332. else
  333. {
  334. luat_rtos_event_send(prv_speech.speech_task_handle, AIRTALK_EVENT_AMR_RECORD_STOP, 0, 0, 0, 0);
  335. }
  336. }
  337. void luat_airtalk_speech_debug_switch(uint8_t on_off)
  338. {
  339. prv_speech.debug_on_off = on_off;
  340. if (on_off) log_on();
  341. }