decode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "application/audio_dec_app.h"
  2. #include "app_config.h"
  3. #include "audio_config.h"
  4. #include "app_main.h"
  5. //////////////////////////////////////////////////////////////////////////////
  6. extern struct audio_decoder_task decode_task;
  7. extern struct audio_mixer mixer;
  8. extern struct audio_dac_hdl dac_hdl;
  9. #if AUDIO_DAC_MULTI_CHANNEL_ENABLE
  10. extern struct audio_dac_channel default_dac;
  11. #endif
  12. extern const int audio_dec_app_mix_en;
  13. extern u32 audio_output_channel_num(void);
  14. //////////////////////////////////////////////////////////////////////////////
  15. struct audio_dec_app_audio_state_hdl {
  16. struct audio_mixer *p_mixer;
  17. u32 dec_mix : 1; // 1:叠加模式
  18. u32 flag;
  19. };
  20. //////////////////////////////////////////////////////////////////////////////
  21. const struct audio_dec_format_hdl decode_format_list[] = {
  22. {"wtg", AUDIO_CODING_G729},
  23. {"msbc", AUDIO_CODING_MSBC},
  24. {"msb", AUDIO_CODING_MSBC},
  25. {"sbc", AUDIO_CODING_SBC},
  26. {"mty", AUDIO_CODING_MTY},
  27. {"aac", AUDIO_CODING_AAC},
  28. {"mp3", AUDIO_CODING_MP3},
  29. {"wma", AUDIO_CODING_WMA},
  30. {"wav", AUDIO_CODING_WAV},
  31. #if (defined(TCFG_DEC_MIDI_ENABLE) && TCFG_DEC_MIDI_ENABLE)
  32. //midi 文件播放,需要对应音色文件配合
  33. {"midi", AUDIO_CODING_MIDI},
  34. {"mid", AUDIO_CODING_MIDI},
  35. #endif //TCFG_DEC_MIDI_ENABLE
  36. #if TCFG_DEC_WTGV2_ENABLE
  37. {"wts", AUDIO_CODING_WTGV2},
  38. #endif
  39. {"speex", AUDIO_CODING_SPEEX},
  40. {"opus", AUDIO_CODING_OPUS},
  41. {0, 0},
  42. };
  43. #if defined(CONFIG_MEDIA_DEVELOP_ENABLE)
  44. static struct audio_stream_entry *audio_dec_app_entries[2] = {NULL};
  45. #endif
  46. //////////////////////////////////////////////////////////////////////////////
  47. /*----------------------------------------------------------------------------*/
  48. /**@brief 解码创建参数初始化
  49. @param *dec: 解码句柄
  50. @return 0-正常
  51. @note 弱函数重定义
  52. */
  53. /*----------------------------------------------------------------------------*/
  54. int audio_dec_app_create_param_init(struct audio_dec_app_hdl *dec)
  55. {
  56. dec->p_decode_task = &decode_task;
  57. #if defined(CONFIG_MEDIA_DEVELOP_ENABLE)
  58. if (!audio_dec_app_mix_en) {
  59. #if AUDIO_DAC_MULTI_CHANNEL_ENABLE
  60. audio_dec_app_entries[0] = &default_dac.entry;
  61. #else
  62. audio_dec_app_entries[0] = &dac_hdl.entry;
  63. #endif
  64. dec->entries = audio_dec_app_entries;
  65. } else
  66. #endif
  67. {
  68. dec->p_mixer = &mixer;
  69. }
  70. #if defined(CONFIG_MEDIA_DEVELOP_ENABLE)
  71. u8 dac_connect_mode = app_audio_output_mode_get();
  72. if (dac_connect_mode == DAC_OUTPUT_FRONT_LR_REAR_LR) {
  73. dec->out_ch_num = 4;
  74. } else {
  75. dec->out_ch_num = audio_output_channel_num();
  76. }
  77. #else
  78. u8 dac_connect_mode = audio_dac_get_channel(&dac_hdl);
  79. switch (dac_connect_mode) {
  80. /* case DAC_OUTPUT_DUAL_LR_DIFF: */
  81. case DAC_OUTPUT_LR:
  82. dec->out_ch_num = 2;
  83. break;
  84. /* case DAC_OUTPUT_FRONT_LR_REAR_LR: */
  85. /* dec->out_ch_num = 4; */
  86. /* break; */
  87. default :
  88. dec->out_ch_num = 1;
  89. break;
  90. }
  91. #endif
  92. return 0;
  93. }
  94. /*----------------------------------------------------------------------------*/
  95. /**@brief 文件解码创建参数初始化
  96. @param *file_dec: 文件解码句柄
  97. @return 0-正常
  98. @note 弱函数重定义
  99. */
  100. /*----------------------------------------------------------------------------*/
  101. int audio_dec_file_app_create_param_init(struct audio_dec_file_app_hdl *file_dec)
  102. {
  103. file_dec->format = (struct audio_dec_format_hdl *)decode_format_list;
  104. return 0;
  105. }
  106. /*----------------------------------------------------------------------------*/
  107. /**@brief 解码输出状态设置
  108. @param *dec: 解码句柄
  109. @param flag: 解码标签
  110. @return 0-正常
  111. @note
  112. */
  113. /*----------------------------------------------------------------------------*/
  114. int audio_dec_app_audio_state_switch(struct audio_dec_app_hdl *dec, u32 flag)
  115. {
  116. u8 need_set_audio = 1;
  117. /* if ((dec->dec_mix) && (audio_mixer_get_ch_num(dec->p_mixer) > 1)) { */
  118. /* need_set_audio = 0; */
  119. /* } */
  120. if (app_audio_get_state() == APP_AUDIO_STATE_IDLE) {
  121. need_set_audio = 1;
  122. }
  123. if (need_set_audio) {
  124. if (flag == AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MUSIC) {
  125. app_audio_state_switch(APP_AUDIO_STATE_MUSIC, get_max_sys_vol());
  126. } else {
  127. #ifdef TCFG_WTONT_ONCE_VOL
  128. extern u8 get_tone_once_vol(void);
  129. app_audio_state_switch(APP_AUDIO_STATE_WTONE, get_tone_once_vol());
  130. #else
  131. app_audio_state_switch(APP_AUDIO_STATE_WTONE, get_tone_vol());
  132. #endif
  133. }
  134. }
  135. return 0;
  136. }
  137. /*----------------------------------------------------------------------------*/
  138. /**@brief 解码输出状态退出
  139. @param *p_aud_state: 输出状态
  140. @return 0-正常
  141. @note
  142. */
  143. /*----------------------------------------------------------------------------*/
  144. int audio_dec_app_audio_state_exit(struct audio_dec_app_audio_state_hdl *p_aud_state)
  145. {
  146. u8 need_set_audio = 1;
  147. /* if ((p_aud_state->dec_mix) && (audio_mixer_get_ch_num(p_aud_state->p_mixer) > 1)) { */
  148. /* need_set_audio = 0; */
  149. /* } */
  150. /* if (app_audio_get_state() == APP_AUDIO_STATE_IDLE) { */
  151. /* need_set_audio = 1; */
  152. /* } */
  153. if (need_set_audio) {
  154. if (p_aud_state->flag == AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MUSIC) {
  155. app_audio_state_exit(APP_AUDIO_STATE_MUSIC);
  156. } else {
  157. app_audio_state_exit(APP_AUDIO_STATE_WTONE);
  158. }
  159. }
  160. return 0;
  161. }
  162. /*----------------------------------------------------------------------------*/
  163. /**@brief 文件解码初始化完成
  164. @param *file_dec: 文件解码句柄
  165. @return 0-正常
  166. @note 弱函数重定义
  167. */
  168. /*----------------------------------------------------------------------------*/
  169. int audio_dec_file_app_init_ok(struct audio_dec_file_app_hdl *file_dec)
  170. {
  171. audio_dec_app_audio_state_switch(file_dec->dec, file_dec->flag & AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MASK);
  172. return 0;
  173. }
  174. /*----------------------------------------------------------------------------*/
  175. /**@brief 文件解码结束
  176. @param *file_dec: 文件解码句柄
  177. @return 0-正常
  178. @note 弱函数重定义
  179. */
  180. /*----------------------------------------------------------------------------*/
  181. int audio_dec_file_app_play_end(struct audio_dec_file_app_hdl *file_dec)
  182. {
  183. struct audio_dec_app_audio_state_hdl aud_state = {0};
  184. aud_state.p_mixer = file_dec->dec->p_mixer;
  185. aud_state.dec_mix = file_dec->dec->dec_mix;
  186. aud_state.flag = file_dec->flag & AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MASK;
  187. audio_dec_file_app_close(file_dec);
  188. audio_dec_app_audio_state_exit(&aud_state);
  189. return 0;
  190. }
  191. /*----------------------------------------------------------------------------*/
  192. /**@brief 正弦波解码初始化完成
  193. @param *sine_dec: 正弦波解码句柄
  194. @return 0-正常
  195. @note 弱函数重定义
  196. */
  197. /*----------------------------------------------------------------------------*/
  198. int audio_dec_sine_app_init_ok(struct audio_dec_sine_app_hdl *sine_dec)
  199. {
  200. audio_dec_app_audio_state_switch(sine_dec->dec, sine_dec->flag & AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MASK);
  201. return 0;
  202. }
  203. /*----------------------------------------------------------------------------*/
  204. /**@brief 正弦波解码结束
  205. @param *sine_dec: 正弦波解码句柄
  206. @return 0-正常
  207. @note 弱函数重定义
  208. */
  209. /*----------------------------------------------------------------------------*/
  210. int audio_dec_sine_app_play_end(struct audio_dec_sine_app_hdl *sine_dec)
  211. {
  212. struct audio_dec_app_audio_state_hdl aud_state = {0};
  213. aud_state.p_mixer = sine_dec->dec->p_mixer;
  214. aud_state.dec_mix = sine_dec->dec->dec_mix;
  215. aud_state.flag = sine_dec->flag & AUDIO_DEC_FILE_FLAG_AUDIO_STATE_MASK;
  216. audio_dec_sine_app_close(sine_dec);
  217. audio_dec_app_audio_state_exit(&aud_state);
  218. return 0;
  219. }
  220. #if (!defined(CONFIG_MEDIA_DEVELOP_ENABLE))
  221. void audio_dec_app_output_sr_set(struct audio_dec_app_hdl *dec)
  222. {
  223. /* #if defined(CONFIG_CPU_BR23) */
  224. /* extern u32 audio_output_rate(int input_rate); */
  225. /* dec->src_out_sr = audio_output_rate(dec->src_out_sr); */
  226. /* #endif */
  227. if (dec->src_out_sr == 0) {
  228. dec->src_out_sr = audio_dac_get_sample_rate(&dac_hdl);
  229. if (dec->src_out_sr == 0) {
  230. dec->src_out_sr = 16000;
  231. log_w("src out is zero \n");
  232. }
  233. }
  234. if (dec->sample_rate == 0) {
  235. dec->sample_rate = dec->src_out_sr;
  236. }
  237. }
  238. #endif
  239. //////////////////////////////////////////////////////////////////////////////
  240. // test
  241. #if 0
  242. #include "tone_player.h"
  243. void audio_dec_file_test(void)
  244. {
  245. struct audio_dec_file_app_hdl *hdl;
  246. hdl = audio_dec_file_app_create(TONE_POWER_ON, 1);
  247. if (hdl) {
  248. audio_dec_file_app_open(hdl);
  249. }
  250. os_time_dly(2);
  251. hdl = audio_dec_file_app_create(TONE_POWER_OFF, 1);
  252. if (hdl) {
  253. audio_dec_file_app_open(hdl);
  254. }
  255. os_time_dly(300);
  256. }
  257. static const struct audio_sin_param sine_test[] = {
  258. /*{0, 1000, 0, 100},*/
  259. {200 << 9, 4000, 0, 100},
  260. };
  261. static const struct audio_sin_param sine_test1[] = {
  262. {450 << 9, 24960, 1, 16.667 * 512},
  263. {0, 16000, 0, 100},
  264. };
  265. void audio_dec_sine_test(void)
  266. {
  267. struct audio_dec_sine_app_hdl *hdl;
  268. /* hdl = audio_dec_sine_app_create(SDFILE_RES_ROOT_PATH"tone/vol_max.sin", 1); */
  269. hdl = audio_dec_sine_app_create_by_parm(sine_test1, ARRAY_SIZE(sine_test1), 1);
  270. if (hdl) {
  271. audio_dec_sine_app_open(hdl);
  272. }
  273. os_time_dly(2);
  274. hdl = audio_dec_sine_app_create_by_parm(sine_test, ARRAY_SIZE(sine_test), 1);
  275. if (hdl) {
  276. audio_dec_sine_app_open(hdl);
  277. }
  278. /* os_time_dly(300); */
  279. }
  280. void audio_dec_usb_file_test(void)
  281. {
  282. tone_play_stop();
  283. clk_set("sys", 192 * 1000000L);
  284. struct audio_dec_file_app_hdl *hdl;
  285. /* hdl = audio_dec_file_app_create("storage/udisk/C/1.mp3", 1); */
  286. hdl = audio_dec_file_app_create("storage/udisk/C/1.wav", 1);
  287. if (hdl) {
  288. audio_dec_file_app_open(hdl);
  289. }
  290. os_time_dly(2);
  291. /* hdl = audio_dec_file_app_create("storage/udisk/C/2.mp3", 1); */
  292. hdl = audio_dec_file_app_create("storage/udisk/C/2.wav", 1);
  293. if (hdl) {
  294. audio_dec_file_app_open(hdl);
  295. }
  296. os_time_dly(300);
  297. }
  298. #endif /*test*/