tone_player.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. #include "system/includes.h"
  2. #include "media/includes.h"
  3. #include "tone_player.h"
  4. #include "audio_config.h"
  5. #include "app_main.h"
  6. #include "btstack/avctp_user.h"
  7. #include "aec_user.h"
  8. /* #include "audio_digital_vol.h" */
  9. #include "audio_codec_clock.h"
  10. #include "board_config.h"
  11. #include "audio_digital_vol.h"
  12. #if TCFG_APP_FM_EMITTER_EN
  13. #include "fm_emitter/fm_emitter_manage.h"
  14. #endif
  15. #if TCFG_AUDIO_MUSIC_SIDETONE_ENABLE //播歌时侧耳监听功能
  16. #include "audio_dec_mic2pcm.h"
  17. #endif
  18. #ifdef CONFIG_LITE_AUDIO
  19. #include "tone_player_api.h"
  20. #endif /*CONFIG_LITE_AUDIO*/
  21. #define LOG_TAG_CONST APP_TONE
  22. #define LOG_TAG "[APP-TONE]"
  23. #define LOG_ERROR_ENABLE
  24. #define LOG_INFO_ENABLE
  25. #define LOG_DUMP_ENABLE
  26. #include "debug.h"
  27. #define TONE_LIST_MAX_NUM 4
  28. #if TCFG_USER_TWS_ENABLE
  29. #include "media/bt_audio_timestamp.h"
  30. #include "audio_syncts.h"
  31. #include "bt_tws.h"
  32. #define msecs_to_bt_time(m) (((m + 1)* 1000) / 625)
  33. #define TWS_TONE_ALIGN_TIME 0
  34. #define TWS_TONE_ALIGN_MIX 1
  35. #define TONE_DEC_NOT_START 0
  36. #define TONE_DEC_WAIT_ALIGN_TIME 1
  37. #define TONE_DEC_WAIT_MIX 2
  38. #define TONE_DEC_CONFIRM 3
  39. #define TWS_TONE_CONFIRM_TIME 250 /*TWS提示音音频同步确认时间(也是音频解码主从确认超时时间)*/
  40. #endif
  41. #if TCFG_WAV_TONE_MIX_ENABLE
  42. #define TONE_FILE_DEC_MIX 1 // 提示音叠加播放
  43. #else
  44. #define TONE_FILE_DEC_MIX 0
  45. #endif
  46. //软件数字音量(调节解码输出数据的音量),选择此音量类型需要把audio_digital_vol.h中BG_DVOL_FADE_ENABLE 这个宏置0
  47. #define VOL_TYPE_DIGITAL 0
  48. #define VOL_TYPE_NULL 1 //不使用音量
  49. #define SYS_VOL_TYPE VOL_TYPE_NULL
  50. static OS_MUTEX tone_mutex;
  51. struct tone_file_handle {
  52. u8 start;
  53. u8 idx;
  54. u8 repeat_begin;
  55. u8 remain;
  56. u8 tws;
  57. u16 loop;
  58. u32 magic;
  59. void *file;
  60. const char **list;
  61. enum audio_channel channel;
  62. struct audio_decoder decoder;
  63. struct audio_mixer_ch mix_ch;
  64. u8 ch_num;
  65. u16 target_sample_rate;
  66. #if TCFG_USER_TWS_ENABLE
  67. u32 wait_time;
  68. u8 tws_align_step;
  69. u8 ts_start;
  70. void *audio_sync;
  71. void *syncts;
  72. void *ts_handle;
  73. u32 time_base;
  74. #endif
  75. u32 mix_ch_event_params[3];
  76. struct audio_src_handle *hw_src;
  77. u32 clk_before_dec;
  78. u8 dec_mix;
  79. #if (SYS_VOL_TYPE == VOL_TYPE_DIGITAL)
  80. dvol_handle *dvol;
  81. #endif
  82. };
  83. struct tone_dec_handle {
  84. u8 r_index;
  85. u8 w_index;
  86. u8 list_cnt;
  87. u8 preemption;
  88. const char **list[4];
  89. struct audio_res_wait wait;
  90. u8 dec_mix;
  91. const char *user_evt_owner;
  92. void (*user_evt_handler)(void *priv);
  93. void *priv;
  94. };
  95. extern struct audio_mixer mixer;
  96. extern struct audio_dac_hdl dac_hdl;
  97. extern struct audio_decoder_task decode_task;
  98. static struct tone_file_handle *file_dec;
  99. static char *single_file[2] = {NULL};
  100. struct tone_dec_handle *tone_dec;
  101. int tone_file_dec_start();
  102. u16 get_source_sample_rate();
  103. extern void audio_mix_ch_event_handler(void *priv, int event);
  104. extern int bt_audio_sync_nettime_select(u8 basetime);
  105. extern u32 bt_audio_sync_lat_time(void);
  106. static void file_decoder_syncts_free(struct tone_file_handle *dec);
  107. void tone_event_to_user(u8 event, const char *name);
  108. void tone_event_clear()
  109. {
  110. struct sys_event e = {0};
  111. e.type = SYS_DEVICE_EVENT;
  112. e.arg = (void *)DEVICE_EVENT_FROM_TONE;
  113. sys_event_clear(&e);
  114. }
  115. void tone_set_user_event_handler(struct tone_dec_handle *dec, void (*user_evt_handler)(void *priv), void *priv)
  116. {
  117. printf("tone_set_user_event_handler:%d\n", *(u8 *)priv);
  118. dec->user_evt_owner = os_current_task();
  119. dec->user_evt_handler = user_evt_handler;
  120. dec->priv = priv;
  121. }
  122. __attribute__((weak)) void audio_pwm_set_resume(void (*resume)(void *))
  123. {
  124. return;
  125. }
  126. int tone_event_handler(struct tone_dec_handle *dec, u8 end_flag)
  127. {
  128. int argv[4];
  129. if (!dec->user_evt_handler) {
  130. /* log_info("user_evt_handler null\n"); */
  131. return -1;
  132. }
  133. if (strcmp(os_current_task(), dec->user_evt_owner) == 0) {
  134. dec->user_evt_handler(dec->priv);
  135. return 0;
  136. }
  137. /* dec->user_evt_handler(dec->priv); */
  138. argv[0] = (int)dec->user_evt_handler;
  139. argv[1] = 1;
  140. argv[2] = (int)dec->priv;
  141. argv[3] = (int)end_flag;//是否是被打断 关闭,0正常关闭,1被打断关闭, 模式切换时,由file_dec->end_flag, 决定该值
  142. return os_taskq_post_type(dec->user_evt_owner, Q_CALLBACK, 4, argv);
  143. /* return 0; */
  144. }
  145. __attribute__((weak))
  146. int audio_dac_stop(struct audio_dac_hdl *p)
  147. {
  148. return 0;
  149. }
  150. __attribute__((weak))
  151. int audio_dac_write(struct audio_dac_hdl *dac, void *data, int len)
  152. {
  153. return len;
  154. }
  155. __attribute__((weak))
  156. int audio_dac_get_sample_rate(struct audio_dac_hdl *p)
  157. {
  158. return 16000;
  159. }
  160. __attribute__((weak))
  161. void audio_dac_clear(struct audio_dac_hdl *dac)
  162. {
  163. }
  164. static u8 tone_dec_idle_query()
  165. {
  166. if (file_dec) {
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. REGISTER_LP_TARGET(tone_dec_lp_target) = {
  172. .name = "tone_dec",
  173. .is_idle = tone_dec_idle_query,
  174. };
  175. #if TCFG_USER_TWS_ENABLE
  176. #define bt_time_before(t1, t2) \
  177. (((t1 < t2) && ((t2 - t1) & 0x7ffffff) < 0xffff) || \
  178. ((t1 > t2) && ((t1 - t2) & 0x7ffffff) > 0xffff))
  179. #define TWS_FUNC_ID_TONE_ALIGN \
  180. (((u8)('T' + 'O' + 'N' + 'E') << (2 * 8)) | \
  181. ((u8)('P' + 'L' + 'A' + 'Y' + 'E' + 'R') << (1 * 8)) | \
  182. ((u8)('S' + 'Y' + 'N' + 'C') << (0 * 8)))
  183. struct tws_tone_align {
  184. u8 confirm;
  185. u8 type;
  186. union {
  187. int time;
  188. int position;
  189. };
  190. };
  191. struct tws_tone_align tws_tone = {0};
  192. /*static u8 tws_tone_align = 0;*/
  193. /*static int tws_tone_align_time = 0;*/
  194. static void tws_tone_play_rx_align_data(void *data, u16 len, bool rx)
  195. {
  196. local_irq_disable();
  197. memcpy(&tws_tone, data, sizeof(tws_tone));
  198. tws_tone.confirm = 1;
  199. local_irq_enable();
  200. y_printf("tone tws confirm rx : %d\n", tws_tone.time);
  201. }
  202. REGISTER_TWS_FUNC_STUB(tone_play_align) = {
  203. .func_id = TWS_FUNC_ID_TONE_ALIGN,
  204. .func = tws_tone_play_rx_align_data,
  205. };
  206. static void tone_mixer_ch_event_handler(void *priv, int event)
  207. {
  208. struct tone_file_handle *dec = (struct tone_file_handle *)priv;
  209. switch (event) {
  210. case MIXER_EVENT_CH_OPEN:
  211. break;
  212. case MIXER_EVENT_CH_CLOSE:
  213. case MIXER_EVENT_CH_RESET:
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. #endif
  220. void tone_event_to_user(u8 event, const char *name)
  221. {
  222. struct sys_event e;
  223. e.type = SYS_DEVICE_EVENT;
  224. e.arg = (void *)DEVICE_EVENT_FROM_TONE;
  225. e.u.dev.event = event;
  226. e.u.dev.value = (int)name;
  227. sys_event_notify(&e);
  228. }
  229. static char *get_file_ext_name(char *name)
  230. {
  231. int len = strlen(name);
  232. char *ext = (char *)name;
  233. while (len--) {
  234. if (*ext++ == '.') {
  235. break;
  236. }
  237. }
  238. return ext;
  239. }
  240. struct audio_dec_input tone_input;
  241. static void tone_file_dec_release()
  242. {
  243. #if (SYS_VOL_TYPE == VOL_TYPE_DIGITAL)
  244. if ((tone_input.coding_type == AUDIO_CODING_WAV) && (tone_dec->preemption == 0)) {
  245. audio_digital_vol_bg_fade(0);
  246. }
  247. audio_digital_vol_close(file_dec->dvol);
  248. file_dec->dvol = NULL;
  249. #endif
  250. free(file_dec);
  251. file_dec = NULL;
  252. }
  253. int tone_list_play_start(const char **list, u8 preemption, u8 tws);
  254. static int tone_file_list_clean(u8 decoding)
  255. {
  256. int i = 0;
  257. if (!tone_dec) {
  258. return 0;
  259. }
  260. for (i = 0; i < TONE_LIST_MAX_NUM; i++) {
  261. if (tone_dec->list[i]) {
  262. if (decoding && i == tone_dec->r_index) {
  263. continue;
  264. }
  265. free(tone_dec->list[i]);
  266. tone_dec->list[i] = NULL;
  267. }
  268. }
  269. if (decoding) {
  270. tone_dec->w_index = tone_dec->r_index + 1;
  271. if (tone_dec->w_index >= TONE_LIST_MAX_NUM) {
  272. tone_dec->w_index = 0;
  273. }
  274. tone_dec->list_cnt = 1;
  275. } else {
  276. tone_dec->list_cnt = 0;
  277. }
  278. return 0;
  279. }
  280. void tone_dec_release()
  281. {
  282. if (!tone_dec) {
  283. os_mutex_post(&tone_mutex);
  284. return;
  285. }
  286. tone_event_handler(tone_dec, 0);
  287. audio_decoder_task_del_wait(&decode_task, &tone_dec->wait);
  288. tone_file_list_clean(0);
  289. free(tone_dec);
  290. tone_dec = NULL;
  291. os_mutex_post(&tone_mutex);
  292. }
  293. void tone_dec_end_handler(int event, const char *name)
  294. {
  295. const char **list;
  296. list = tone_dec->list[tone_dec->r_index];
  297. if (++tone_dec->r_index >= TONE_LIST_MAX_NUM) {
  298. tone_dec->r_index = 0;
  299. }
  300. if (--tone_dec->list_cnt > 0) {
  301. tone_list_play_start(tone_dec->list[tone_dec->r_index], tone_dec->preemption, 1);
  302. } else {
  303. tone_dec_release();
  304. }
  305. tone_event_to_user(event, name);
  306. }
  307. static int tone_file_list_repeat(struct audio_decoder *decoder)
  308. {
  309. int err = 0;
  310. file_dec->idx++;
  311. if (!file_dec->list[file_dec->idx]) {
  312. log_info("repeat end 1:idx end");
  313. return 0;
  314. }
  315. if (IS_REPEAT_END(file_dec->list[file_dec->idx])) {
  316. //log_info("repeat_loop:%d",file_dec->loop);
  317. if (file_dec->loop) {
  318. file_dec->loop--;
  319. file_dec->idx = file_dec->repeat_begin;
  320. } else {
  321. file_dec->idx++;
  322. if (!file_dec->list[file_dec->idx]) {
  323. log_info("repeat end 2:idx end");
  324. return 0;
  325. }
  326. }
  327. }
  328. if (IS_REPEAT_BEGIN(file_dec->list[file_dec->idx])) {
  329. if (!file_dec->loop) {
  330. file_dec->loop = TONE_REPEAT_COUNT(file_dec->list[file_dec->idx]);
  331. log_info("repeat begin:%d", file_dec->loop);
  332. }
  333. file_dec->idx++;
  334. file_dec->repeat_begin = file_dec->idx;
  335. }
  336. log_info("repeat idx:%d,%s", file_dec->idx, file_dec->list[file_dec->idx]);
  337. file_dec->file = fopen(file_dec->list[file_dec->idx], "r");
  338. if (!file_dec->file) {
  339. log_error("repeat end:fopen repeat file faild");
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. static int tone_audio_res_close(u8 rpt)
  345. {
  346. if (file_dec->start) {
  347. audio_decoder_close(&file_dec->decoder);
  348. }
  349. if (file_dec->start) {
  350. file_dec->start = 0;
  351. }
  352. return 0;
  353. }
  354. static void tone_dec_event_handler(struct audio_decoder *decoder, int argc, int *argv)
  355. {
  356. int repeat = 0;
  357. switch (argv[0]) {
  358. case AUDIO_DEC_EVENT_END:
  359. case AUDIO_DEC_EVENT_ERR:
  360. if (argv[1] != file_dec->magic) {
  361. log_error("file_dec magic no match:%d-%d", argv[1], file_dec->magic);
  362. break;
  363. }
  364. repeat = tone_file_list_repeat(decoder);
  365. log_info("AUDIO_DEC_EVENT_END,err=%x,repeat=%d\n", argv[0], repeat);
  366. if (repeat) {
  367. tone_audio_res_close(repeat);
  368. tone_file_dec_start();
  369. } else {
  370. tone_file_list_stop(0);
  371. }
  372. break;
  373. default:
  374. return;
  375. }
  376. }
  377. int tone_get_status()
  378. {
  379. return tone_dec ? TONE_START : TONE_STOP;
  380. }
  381. int tone_get_dec_status()
  382. {
  383. if (tone_dec && file_dec && (file_dec->decoder.state != DEC_STA_WAIT_STOP)) {
  384. return TONE_START;
  385. }
  386. return TONE_STOP;
  387. }
  388. int tone_dec_wait_stop(u32 timeout_ms)
  389. {
  390. u32 to_cnt = 0;
  391. while (tone_get_dec_status()) {
  392. /* putchar('t'); */
  393. os_time_dly(1);
  394. if (timeout_ms) {
  395. to_cnt += 10;
  396. if (to_cnt >= timeout_ms) {
  397. break;
  398. }
  399. }
  400. }
  401. return tone_get_dec_status();
  402. }
  403. static int tone_fread(struct audio_decoder *decoder, void *buf, u32 len)
  404. {
  405. int rlen = 0;
  406. if (!file_dec->file) {
  407. return 0;
  408. }
  409. rlen = fread(file_dec->file, buf, len);
  410. if (rlen < len) {
  411. fclose(file_dec->file);
  412. file_dec->file = NULL;
  413. }
  414. return rlen;
  415. }
  416. static int tone_fseek(struct audio_decoder *decoder, u32 offset, int seek_mode)
  417. {
  418. if (!file_dec->file) {
  419. return 0;
  420. }
  421. return fseek(file_dec->file, offset, seek_mode);
  422. }
  423. static int tone_flen(struct audio_decoder *decoder)
  424. {
  425. void *tone_file = NULL;
  426. int len = 0;
  427. if (file_dec->file) {
  428. len = flen(file_dec->file);
  429. return len;
  430. }
  431. tone_file = fopen(file_dec->list[file_dec->idx], "r");
  432. if (tone_file) {
  433. len = flen(tone_file);
  434. fclose(tone_file);
  435. tone_file = NULL;
  436. }
  437. return len;
  438. }
  439. static int tone_fclose(void *file)
  440. {
  441. if (file_dec->file) {
  442. fclose(file_dec->file);
  443. file_dec->file = NULL;
  444. }
  445. file_dec->idx = 0;
  446. return 0;
  447. }
  448. struct tone_format {
  449. const char *fmt;
  450. u32 coding_type;
  451. };
  452. const struct tone_format tone_fmt_support_list[] = {
  453. {"wtg", AUDIO_CODING_G729},
  454. {"msbc", AUDIO_CODING_MSBC},
  455. {"sbc", AUDIO_CODING_SBC},
  456. {"mty", AUDIO_CODING_MTY},
  457. {"aac", AUDIO_CODING_AAC},
  458. #if TCFG_DEC_OPUS_ENABLE
  459. {"opus", AUDIO_CODING_OPUS},
  460. #endif/*TCFG_DEC_WTGV2_ENABLE*/
  461. #if TCFG_DEC_SPEEX_ENABLE
  462. {"speex", AUDIO_CODING_SPEEX},
  463. #endif/*TCFG_DEC_WTGV2_ENABLE*/
  464. #if TCFG_DEC_WTGV2_ENABLE
  465. {"wts", AUDIO_CODING_WTGV2},
  466. #endif/*TCFG_DEC_WTGV2_ENABLE*/
  467. #if TCFG_DEC_MP3_ENABLE
  468. {"mp3", AUDIO_CODING_MP3},
  469. #endif/*TCFG_DEC_MP3_ENABLE*/
  470. #if (TCFG_WAV_TONE_MIX_ENABLE || TCFG_DEC_WAV_ENABLE)
  471. {"wav", AUDIO_CODING_WAV},
  472. #endif/*TCFG_WAV_TONE_MIX_ENABLE*/
  473. };
  474. struct audio_dec_input tone_input = {
  475. .coding_type = AUDIO_CODING_G729,
  476. .data_type = AUDIO_INPUT_FILE,
  477. .ops = {
  478. .file = {
  479. .fread = tone_fread,
  480. .fseek = tone_fseek,
  481. .flen = tone_flen,
  482. }
  483. }
  484. };
  485. static u32 tone_file_format_match(char *fmt)
  486. {
  487. int list_num = ARRAY_SIZE(tone_fmt_support_list);
  488. int i = 0;
  489. if (fmt == NULL) {
  490. return AUDIO_CODING_UNKNOW;
  491. }
  492. for (i = 0; i < list_num; i++) {
  493. if (ASCII_StrCmpNoCase(fmt, tone_fmt_support_list[i].fmt, 4) == 0) {
  494. return tone_fmt_support_list[i].coding_type;
  495. }
  496. }
  497. return AUDIO_CODING_UNKNOW;
  498. }
  499. static int tone_dec_probe_handler(struct audio_decoder *decoder)
  500. {
  501. struct tone_file_handle *dec = container_of(decoder, struct tone_file_handle, decoder);
  502. int err = 0;
  503. #if TCFG_USER_TWS_ENABLE
  504. if (dec->tws_align_step == 0 && dec->ts_handle) {
  505. if (!tws_file_timestamp_available(dec->ts_handle)) {
  506. audio_decoder_suspend(decoder, 0);
  507. return -EINVAL;
  508. }
  509. dec->tws_align_step = 1;
  510. }
  511. #endif
  512. return 0;
  513. }
  514. static int tone_final_output_handler(struct tone_file_handle *dec, s16 *data, int len)
  515. {
  516. #if 1
  517. #if (SYS_VOL_TYPE == VOL_TYPE_DIGITAL)
  518. if (file_dec->dvol) {
  519. audio_digital_vol_run(file_dec->dvol, data, len);
  520. }
  521. #endif
  522. u32 wlen = audio_pwm_write(data, len);
  523. if (wlen != len) {
  524. /* putchar('W'); */
  525. }
  526. return wlen;
  527. #else
  528. put_buf(data, len);
  529. return len;
  530. #endif
  531. }
  532. static int tone_output_after_syncts_filter(void *priv, void *data, int len)
  533. {
  534. struct tone_file_handle *dec = (struct tone_file_handle *)priv;
  535. int wlen = 0;
  536. if (dec->remain == 0) {
  537. //get_sine_data(&tmp_cnnt, data, len/2, 1);
  538. }
  539. wlen = tone_final_output_handler(dec, data, len);
  540. ret:
  541. dec->remain = wlen < len ? 1 : 0;
  542. return wlen;
  543. }
  544. static int tone_dec_output_handler(struct audio_decoder *decoder, s16 *data, int len, void *priv)
  545. {
  546. int wlen = 0;
  547. int remain_len = len;
  548. struct tone_file_handle *dec = container_of(decoder, struct tone_file_handle, decoder);
  549. return tone_output_after_syncts_filter(dec, data, len);
  550. }
  551. static int tone_dec_post_handler(struct audio_decoder *decoder)
  552. {
  553. return 0;
  554. }
  555. const struct audio_dec_handler tone_dec_handler = {
  556. .dec_probe = tone_dec_probe_handler,
  557. .dec_output = tone_dec_output_handler,
  558. .dec_post = tone_dec_post_handler,
  559. };
  560. static void tone_dec_set_output_channel(struct tone_file_handle *dec)
  561. {
  562. int state;
  563. enum audio_channel channel;
  564. /* dec->channel = AUDIO_CH_LR; */
  565. dec->channel = AUDIO_CH_L;
  566. audio_decoder_set_output_channel(&dec->decoder, dec->channel);
  567. dec->ch_num = 1;
  568. }
  569. static int file_decoder_syncts_setup(struct tone_file_handle *dec)
  570. {
  571. int err = 0;
  572. #if TCFG_USER_TWS_ENABLE
  573. if (dec->hw_src) {
  574. audio_hw_src_close(dec->hw_src);
  575. free(dec->hw_src);
  576. dec->hw_src = NULL;
  577. }
  578. struct audio_syncts_params params = {0};
  579. params.nch = dec->ch_num;
  580. #if defined(TCFG_AUDIO_OUTPUT_IIS) && TCFG_AUDIO_OUTPUT_IIS
  581. params.pcm_device = PCM_OUTSIDE_DAC;
  582. params.rout_sample_rate = TCFG_IIS_SAMPLE_RATE;
  583. #else
  584. params.pcm_device = PCM_INSIDE_DAC;
  585. /* params.rout_sample_rate = dec->target_sample_rate; */
  586. params.rout_sample_rate = 32000;
  587. #endif
  588. params.network = AUDIO_NETWORK_BT2_1;
  589. params.rin_sample_rate = dec->decoder.fmt.sample_rate;
  590. params.priv = dec;
  591. params.factor = TIME_US_FACTOR;
  592. params.output = tone_output_after_syncts_filter;
  593. u8 base = 3;
  594. int state = tws_api_get_tws_state();
  595. if (state & TWS_STA_SIBLING_CONNECTED) {
  596. base = dec->dec_mix ? 3 : 1;
  597. }
  598. bt_audio_sync_nettime_select(base);//3 - 优先选择远端主机为网络时钟
  599. dec->ts_start = 0;
  600. dec->ts_handle = file_audio_timestamp_create(0,
  601. dec->decoder.fmt.sample_rate,
  602. bt_audio_sync_lat_time(),
  603. TWS_TONE_CONFIRM_TIME,
  604. TIME_US_FACTOR);
  605. err = audio_syncts_open(&dec->syncts, &params);
  606. if (!err) {
  607. dec->mix_ch_event_params[0] = (u32)&dec->mix_ch;
  608. dec->mix_ch_event_params[1] = (u32)dec->syncts;
  609. audio_mixer_ch_set_event_handler(&dec->mix_ch, (void *)dec->mix_ch_event_params, audio_mix_ch_event_handler);
  610. } else {
  611. log_e("tone audio syncts open err\n");
  612. }
  613. #endif
  614. return err;
  615. }
  616. static void file_decoder_syncts_free(struct tone_file_handle *dec)
  617. {
  618. #if TCFG_USER_TWS_ENABLE
  619. if (dec->ts_handle) {
  620. file_audio_timestamp_close(dec->ts_handle);
  621. dec->ts_handle = NULL;
  622. }
  623. if (dec->syncts) {
  624. audio_syncts_close(dec->syncts);
  625. dec->syncts = NULL;
  626. }
  627. #endif
  628. }
  629. void tone_play_resume_handler(void *priv)
  630. {
  631. if (file_dec) {
  632. /* putchar('Z'); */
  633. audio_decoder_resume(&file_dec->decoder);
  634. }
  635. }
  636. int tone_file_dec_start()
  637. {
  638. int err;
  639. struct audio_fmt *fmt;
  640. u8 file_name[16];
  641. if (!file_dec || !file_dec->file) {
  642. return -EINVAL;
  643. }
  644. if (file_dec->start) {
  645. return 0;
  646. }
  647. fget_name(file_dec->file, file_name, 16);
  648. tone_input.coding_type = tone_file_format_match(get_file_ext_name((char *)file_name));
  649. if (tone_input.coding_type == AUDIO_CODING_UNKNOW) {
  650. log_e("unknow tone file format\n");
  651. return -EINVAL;
  652. }
  653. #if 0
  654. if (tone_input.coding_type == AUDIO_CODING_AAC) {
  655. file_dec->clk_before_dec = clk_get("sys");
  656. if (get_call_status() == BT_CALL_HANGUP) {
  657. puts("aac tone play:48M\n");
  658. clk_set_sys_lock(48 * 1000000L, 1);
  659. } else {
  660. puts("aac tone play:64M\n");
  661. clk_set_sys_lock(64 * 1000000L, 1);
  662. }
  663. } else if (1) {//(tone_input.coding_type == AUDIO_CODING_WAV) || (tone_input.coding_type == AUDIO_CODING_MP3)) {
  664. /*当前时钟小于wav/mp3提示音播放需要得时钟,则自动提高主频*/
  665. file_dec->clk_before_dec = clk_get("sys");
  666. u32 wav_tone_play_clk = 96 * 1000000L;
  667. if (wav_tone_play_clk < file_dec->clk_before_dec) {
  668. wav_tone_play_clk = file_dec->clk_before_dec;
  669. }
  670. printf("wav/mp3 tone play clk:%d->%d\n", file_dec->clk_before_dec, wav_tone_play_clk);
  671. clk_set_sys_lock(wav_tone_play_clk, 1);
  672. }
  673. #else
  674. audio_codec_clock_set(AUDIO_TONE_MODE, tone_input.coding_type, tone_dec->wait.preemption);
  675. #endif
  676. err = audio_decoder_open(&file_dec->decoder, &tone_input, &decode_task);
  677. if (err) {
  678. return err;
  679. }
  680. audio_decoder_set_handler(&file_dec->decoder, &tone_dec_handler);
  681. /*用于处理DEC_EVENT与当前解码的匹配*/
  682. file_dec->magic = rand32();
  683. audio_decoder_set_event_handler(&file_dec->decoder, tone_dec_event_handler, file_dec->magic);
  684. err = audio_decoder_get_fmt(&file_dec->decoder, &fmt);
  685. if (err) {
  686. printf("tone file get fmt err \n");
  687. goto __err1;
  688. }
  689. tone_dec_set_output_channel(file_dec);
  690. audio_pwm_set_resume((void (*)(void *))tone_play_resume_handler);
  691. /* audio_pwm_start(); */
  692. set_state(2);
  693. file_dec->target_sample_rate = fmt->sample_rate;
  694. __dec_start:
  695. #if TCFG_USER_TWS_ENABLE
  696. /* if (file_dec->tws) { */
  697. /* file_decoder_syncts_setup(file_dec); */
  698. /* } */
  699. /* file_dec->tws_align_step = 0; */
  700. #endif
  701. /* sys_hi_timer_add(NULL,tone_play_resume_handler,3); */
  702. #if (SYS_VOL_TYPE == VOL_TYPE_DIGITAL)
  703. if ((tone_input.coding_type == AUDIO_CODING_WAV) && (tone_dec->preemption == 0)) {
  704. /* audio_digital_vol_bg_fade(1); */
  705. }
  706. file_dec->dvol = audio_digital_vol_open(SYS_DEFAULT_TONE_VOL, SYS_MAX_VOL, 20);
  707. #endif/*VOL_TYPE_DIGITAL*/
  708. err = audio_decoder_start(&file_dec->decoder);
  709. if (err) {
  710. goto __err2;
  711. }
  712. file_dec->start = 1;
  713. return 0;
  714. __err2:
  715. #if TCFG_APP_FM_EMITTER_EN
  716. #else
  717. #endif
  718. __err1:
  719. audio_decoder_close(&file_dec->decoder);
  720. tone_file_dec_release();
  721. return err;
  722. }
  723. static int tone_wait_res_handler(struct audio_res_wait *wait, int event)
  724. {
  725. int err = 0;
  726. if (event == AUDIO_RES_GET) {
  727. printf(">>> %s %d\n", __func__, __LINE__);
  728. err = tone_file_dec_start();
  729. printf(">>> %s %d\n", __func__, __LINE__);
  730. } else if (event == AUDIO_RES_PUT) {
  731. printf(">>> %s %d\n", __func__, __LINE__);
  732. tone_file_list_stop(0);
  733. printf(">>> %s %d\n", __func__, __LINE__);
  734. }
  735. return err;
  736. }
  737. int tone_file_list_stop(u8 no_end)
  738. {
  739. const char *name = NULL;
  740. log_info("tone_file_list_stop\n");
  741. os_mutex_pend(&tone_mutex, 0);
  742. if (!file_dec) {
  743. log_info("tone_file_list_stop out 0\n");
  744. os_mutex_post(&tone_mutex);
  745. return 0;
  746. }
  747. tone_audio_res_close(0);
  748. /* extern void audio_pwm_close(void); */
  749. /* audio_pwm_close(); */
  750. #if 0
  751. if ((tone_input.coding_type == AUDIO_CODING_AAC) || (tone_input.coding_type == AUDIO_CODING_WAV)) {
  752. printf("tone_play end,clk restore:%d", file_dec->clk_before_dec);
  753. clk_set_sys_lock(file_dec->clk_before_dec, 2);
  754. }
  755. #else
  756. audio_codec_clock_del(AUDIO_TONE_MODE);
  757. #endif
  758. if (file_dec->list[file_dec->idx]) {
  759. name = (const char *)file_dec->list[file_dec->idx];
  760. } else if (file_dec->idx) {
  761. name = (const char *)file_dec->list[file_dec->idx - 1];
  762. }
  763. if (file_dec->file) {
  764. fclose(file_dec->file);
  765. }
  766. tone_file_dec_release();
  767. tone_dec_end_handler(AUDIO_DEC_EVENT_END, name);
  768. log_info("tone_file_list_stop out 1\n");
  769. /* audio_pwm_stop(); */
  770. set_state(0);
  771. return 0;
  772. }
  773. /*static const u8 pcm_wav_header[] = {
  774. 'R', 'I', 'F', 'F', //rid
  775. 0xff, 0xff, 0xff, 0xff, //file length
  776. 'W', 'A', 'V', 'E', //wid
  777. 'f', 'm', 't', ' ', //fid
  778. 0x14, 0x00, 0x00, 0x00, //format size
  779. 0x01, 0x00, //format tag
  780. 0x01, 0x00, //channel num
  781. 0x80, 0x3e, 0x00, 0x00, //sr 16K
  782. 0x00, 0x7d, 0x00, 0x00, //avgbyte
  783. 0x02, 0x00, //blockalign
  784. 0x10, 0x00, //persample
  785. 0x02, 0x00,
  786. 0x00, 0x00,
  787. 'f', 'a', 'c', 't', //f2id
  788. 0x40, 0x00, 0x00, 0x00, //flen
  789. 0xff, 0xff, 0xff, 0xff, //datalen
  790. 'd', 'a', 't', 'a', //"data"
  791. 0xff, 0xff, 0xff, 0xff, //sameple size
  792. };*/
  793. int audio_decoder_find_coding_type(struct audio_decoder_task *task, u32 coding_type);
  794. int tone_list_play_start(const char **list, u8 preemption, u8 tws)
  795. {
  796. int err;
  797. u8 file_name[16];
  798. char *format = NULL;
  799. FILE *file = NULL;
  800. int index = 0;
  801. if (IS_REPEAT_BEGIN(list[0])) {
  802. index = 1;
  803. }
  804. printf(">>> %s %d\n", __func__, __LINE__);
  805. file = fopen(list[index], "r");
  806. if (!file) {
  807. return -EINVAL;
  808. }
  809. printf(">>> %s %d\n", __func__, __LINE__);
  810. fget_name(file, file_name, 16);
  811. format = get_file_ext_name((char *)file_name);
  812. file_dec = zalloc(sizeof(*file_dec));
  813. file_dec->list = list;
  814. file_dec->idx = index;
  815. file_dec->file = file;
  816. file_dec->tws = tws;
  817. if (index == 1) {
  818. file_dec->loop = TONE_REPEAT_COUNT(list[0]);
  819. }
  820. #if 0
  821. if (!preemption) {
  822. file_dec->dec_mix = 1;
  823. tone_dec->wait.protect = 1;
  824. }
  825. #endif
  826. tone_dec->wait.priority = 3;
  827. tone_dec->wait.preemption = preemption;
  828. printf(">>> %s %d\n", __func__, __LINE__);
  829. /*AAC提示音默认打断播放*/
  830. if (ASCII_StrCmpNoCase(format, "aac", 3) == 0) {
  831. printf("aac tone,preemption = 1\n");
  832. tone_dec->wait.preemption = 1;
  833. tone_dec->wait.format = AUDIO_CODING_AAC;
  834. } else {
  835. #if TONE_FILE_DEC_MIX
  836. fadsf
  837. if (tone_dec->wait.preemption == 0) {
  838. /*支持叠加的提示音解码文件格式*/
  839. if ((ASCII_StrCmpNoCase(format, "wav", 3) == 0) || \
  840. (ASCII_StrCmpNoCase(format, "mp3", 3) == 0) || \
  841. (ASCII_StrCmpNoCase(format, "wtg", 3) == 0)) {
  842. // 叠加播放
  843. file_dec->dec_mix = 1;
  844. /*tone_dec->wait.protect = 1;*/ //提示音播放不用来做低优先级的背景音
  845. tone_dec->wait.preemption = 0;
  846. }
  847. } else {
  848. file_dec->dec_mix = 0;
  849. }
  850. #endif
  851. }
  852. tone_dec->wait.handler = tone_wait_res_handler;
  853. printf(">>> %s %d\n", __func__, __LINE__);
  854. err = audio_decoder_task_add_wait(&decode_task, &tone_dec->wait);
  855. printf(">>> %s %d\n", __func__, __LINE__);
  856. #if 0
  857. if (!err && file_dec->start == 0) {
  858. /*decoder中有该解码器,则强制使用打断方式,防止overlay冲突*/
  859. if (audio_decoder_find_coding_type(&decode_task, tone_file_format_match(format))) {
  860. tone_dec->wait.preemption = 1;
  861. err = audio_decoder_task_add_wait(&decode_task, &tone_dec->wait);
  862. } else {
  863. err = tone_file_dec_start();
  864. }
  865. }
  866. #endif
  867. printf(">>> %s %d\n", __func__, __LINE__);
  868. if (err) {
  869. if (tone_dec) {
  870. audio_decoder_task_del_wait(&decode_task, &tone_dec->wait);
  871. }
  872. } else {
  873. if (file_dec->dec_mix && !file_dec->start) {
  874. err = tone_file_dec_start();
  875. }
  876. }
  877. printf(">>> %s %d\n", __func__, __LINE__);
  878. return err;
  879. }
  880. static int __tone_file_list_play(const char **list, u8 preemption, u8 tws)
  881. {
  882. int i = 0;
  883. int err = 0;
  884. if (!list) {
  885. return -EINVAL;
  886. }
  887. printf(">>> %s %d\n", __func__, __LINE__);
  888. if (tone_dec == NULL) {
  889. tone_dec = zalloc(sizeof(*tone_dec));
  890. if (tone_dec == NULL) {
  891. log_error("tone dec zalloc failed");
  892. return -ENOMEM;
  893. }
  894. }
  895. printf(">>> %s %d\n", __func__, __LINE__);
  896. while (list[i] != NULL) {
  897. i++;
  898. }
  899. printf(">>> %s %d\n", __func__, __LINE__);
  900. char **p = malloc(4 * (i + 1));
  901. memcpy(p, list, 4 * (i + 1));
  902. tone_dec->list[tone_dec->w_index++] = (const char **)p;
  903. if (tone_dec->w_index >= TONE_LIST_MAX_NUM) {
  904. tone_dec->w_index = 0;
  905. }
  906. printf(">>> %s %d\n", __func__, __LINE__);
  907. tone_dec->list_cnt++;
  908. tone_dec->preemption = preemption;
  909. if (tone_dec->list_cnt == 1) {
  910. err = tone_list_play_start(tone_dec->list[tone_dec->r_index], tone_dec->preemption, tws);
  911. if (err == -EINVAL) {
  912. free(p);
  913. free(tone_dec);
  914. tone_dec = NULL;
  915. }
  916. return err;
  917. } else {
  918. puts("tone_file_add_tail\n");
  919. }
  920. printf(">>> %s %d\n", __func__, __LINE__);
  921. return 0;
  922. }
  923. int tone_file_list_play(const char **list, u8 preemption)
  924. {
  925. return __tone_file_list_play(list, preemption, 1);
  926. }
  927. int tone_file_list_play_with_callback(const char **list, u8 preemption, void (*user_evt_handler)(void *priv), void *priv)
  928. {
  929. int i = 0;
  930. int err = 0;
  931. putchar('A');
  932. if (!list) {
  933. return -EINVAL;
  934. }
  935. putchar('B');
  936. if (tone_dec == NULL) {
  937. tone_dec = zalloc(sizeof(*tone_dec));
  938. if (tone_dec == NULL) {
  939. log_error("tone dec zalloc failed");
  940. return -ENOMEM;
  941. }
  942. }
  943. putchar('C');
  944. while (list[i] != NULL) {
  945. i++;
  946. }
  947. char **p = malloc(4 * (i + 1));
  948. memcpy(p, list, 4 * (i + 1));
  949. tone_dec->list[tone_dec->w_index++] = (const char **)p;
  950. if (tone_dec->w_index >= TONE_LIST_MAX_NUM) {
  951. tone_dec->w_index = 0;
  952. }
  953. putchar('D');
  954. if (user_evt_handler) {
  955. tone_set_user_event_handler(tone_dec, user_evt_handler, priv);
  956. }
  957. tone_dec->list_cnt++;
  958. tone_dec->preemption = preemption;
  959. if (tone_dec->list_cnt == 1) {
  960. err = tone_list_play_start(tone_dec->list[tone_dec->r_index], tone_dec->preemption, 1);
  961. if (err == -EINVAL) {
  962. free(p);
  963. free(tone_dec);
  964. tone_dec = NULL;
  965. }
  966. return err;
  967. } else {
  968. puts("tone_file_add_tail\n");
  969. }
  970. return 0;
  971. }
  972. static void tone_stop(u8 force);
  973. int tone_play(const char *name, u8 preemption)
  974. {
  975. if (tone_dec) {
  976. log_info("tone dec busy now,tone stop first");
  977. tone_stop(0);
  978. }
  979. single_file[0] = (char *)name;
  980. single_file[1] = NULL;
  981. return tone_file_list_play((const char **)single_file, preemption);
  982. }
  983. int tone_play_no_tws(const char *name, u8 preemption)
  984. {
  985. if (tone_dec) {
  986. log_info("tone dec busy now,tone stop first");
  987. tone_stop(0);
  988. }
  989. single_file[0] = (char *)name;
  990. single_file[1] = NULL;
  991. return __tone_file_list_play((const char **)single_file, preemption, 0);
  992. }
  993. int tone_play_with_callback(const char *name, u8 preemption, void (*user_evt_handler)(void *priv), void *priv)
  994. {
  995. if (tone_dec) {
  996. tone_event_clear();
  997. log_info("tone dec busy now,tone stop first");
  998. tone_stop(0);
  999. }
  1000. single_file[0] = (char *)name;
  1001. single_file[1] = NULL;
  1002. return tone_file_list_play_with_callback((const char **)single_file, preemption, user_evt_handler, priv);
  1003. }
  1004. int tone_play_add(const char *name, u8 preemption)
  1005. {
  1006. if (tone_dec) {
  1007. log_info("tone dec busy now,tone file add next");
  1008. //tone_stop(0);
  1009. }
  1010. single_file[0] = (char *)name;
  1011. single_file[1] = NULL;
  1012. return tone_file_list_play((const char **)single_file, preemption);
  1013. }
  1014. const char *get_playing_tone_name(u8 index)
  1015. {
  1016. if (tone_dec) {
  1017. const char **list = tone_dec->list[tone_dec->r_index + index];
  1018. if (list) {
  1019. return list[0];
  1020. }
  1021. }
  1022. return 0;
  1023. }
  1024. static void tone_stop(u8 force)
  1025. {
  1026. if (tone_dec == NULL) {
  1027. return;
  1028. }
  1029. if (force) {
  1030. tone_file_list_clean(1);
  1031. }
  1032. tone_file_list_stop(0);
  1033. tone_dec_release();
  1034. }
  1035. static u8 audio_tone_idle_query()
  1036. {
  1037. if (tone_dec) {
  1038. return 0;
  1039. }
  1040. return 1;
  1041. }
  1042. REGISTER_LP_TARGET(audio_tone_lp_target) = {
  1043. .name = "audio_tone",
  1044. .is_idle = audio_tone_idle_query,
  1045. };
  1046. #if 0 //(USE_DMA_TONE)
  1047. static volatile u8 tone_play_falg = 0; //用于播放提示音后,作相应的动作
  1048. void set_tone_play_falg(u8 flag)
  1049. {
  1050. tone_play_falg = flag;
  1051. }
  1052. u8 get_tone_play_flag(void)
  1053. {
  1054. return tone_play_falg;
  1055. }
  1056. int tone_play_index_for_dma(u8 index, u8 flag)
  1057. {
  1058. set_tone_play_falg(flag);
  1059. return tone_play_index(index, 1);
  1060. }
  1061. static u32 dma_tone_arg_before = 0; //记录下按键的arg值
  1062. void set_dma_tone_arg_before(u32 arg)
  1063. {
  1064. dma_tone_arg_before = arg;
  1065. }
  1066. u32 get_dma_tone_arg_before(void)
  1067. {
  1068. return dma_tone_arg_before;
  1069. }
  1070. void clear_dma_tone_arg_before(void)
  1071. {
  1072. dma_tone_arg_before = 0;
  1073. }
  1074. #endif
  1075. static volatile s32 tone_play_end_cmd = TONE_PLAY_END_CB_CMD_NONE; //用于播放提示音后,作相应的动作
  1076. void tone_play_end_cb_cmd_set(int cmd)
  1077. {
  1078. tone_play_end_cmd = cmd;
  1079. }
  1080. s32 tone_play_end_cb_cmd_get(void)
  1081. {
  1082. return tone_play_end_cmd;
  1083. }
  1084. int tone_play_index_with_cb_cmd(u8 index, int flag)
  1085. {
  1086. tone_play_end_cb_cmd_set(flag);
  1087. return tone_play_index(index, 1);
  1088. }
  1089. static u32 tone_arg_storage = 0; //记录下按键的arg值
  1090. void tone_arg_store(u32 arg)
  1091. {
  1092. tone_arg_storage = arg;
  1093. }
  1094. u32 tone_arg_restore(void)
  1095. {
  1096. return tone_arg_storage;
  1097. }
  1098. void tone_arg_storage_clean(void)
  1099. {
  1100. tone_arg_storage = 0;
  1101. }
  1102. int tone_play_init(void)
  1103. {
  1104. os_mutex_create(&tone_mutex);
  1105. return 0;
  1106. }
  1107. int tone_play_stop(void)
  1108. {
  1109. log_info("tone_play_stop");
  1110. tone_stop(1);
  1111. return 0;
  1112. }
  1113. /*
  1114. *@brief:提示音比较,确认目标提示音和正在播放的提示音是否一致
  1115. *@return: 0 匹配
  1116. * 非0 不匹配或者当前没有提示音播放
  1117. *@note:通过提示音名字比较
  1118. */
  1119. int tone_name_compare(const char *name)
  1120. {
  1121. if (tone_dec) {
  1122. if (file_dec) {
  1123. printf("file_name:%s,cmp_name:%s\n", file_dec->list[file_dec->idx], name);
  1124. return strcmp(name, file_dec->list[file_dec->idx]);
  1125. }
  1126. }
  1127. printf("tone_dec idle now\n");
  1128. return -1;
  1129. }