bsp_abp.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "include.h"
  2. #if ABP_EN
  3. #define TRACE_EN 0
  4. #define ABP_INTENSITY 15
  5. #define ABP_PCM_BUF_SAMPLES 128
  6. #if BT_TWS_EN
  7. #define ABP_PCM_BUF_LEN ABP_PCM_BUF_SAMPLES
  8. #else
  9. #define ABP_PCM_BUF_LEN (ABP_PCM_BUF_SAMPLES * 2)
  10. #endif // BT_TWS_EN
  11. #if TRACE_EN
  12. #define TRACE(...) printf(__VA_ARGS__)
  13. #else
  14. #define TRACE(...)
  15. #endif
  16. ABP_generation_init_cb_t ABP_cb AT(.music_buff.abp);
  17. static s16 ABP_pcm[ABP_PCM_BUF_LEN] AT(.music_buff.abp);
  18. static volatile u8 ABP_start = 0;
  19. void alg_abp_process(void);
  20. void abp_kick_start(void);
  21. void dac1_fade_in_real(void);
  22. void dac1_fade_out_real(void);
  23. void dac1_vol_set_fade(u32 vol);
  24. void music_effect_abp_start(void);
  25. void music_effect_abp_stop(void);
  26. static void abp_init(void)
  27. {
  28. if (music_effect_get_state(MUSIC_EFFECT_ABP)) {
  29. return;
  30. }
  31. music_effect_abp_start();
  32. dac1_aubuf_init();
  33. aubuf1_dma_init();
  34. dac1_fade_out();
  35. dac1_fade_wait();
  36. dac1_vol_set(0x7fff);
  37. }
  38. #if BT_TWS_EN
  39. void bsp_abp_set_mode(u8 mode)
  40. {
  41. #if ABP_MUSIC_DIS_PINK_EN
  42. if ((mode == ABP_MODE_PINK) && (f_bt.disp_status == BT_STA_PLAYING)) {
  43. return;
  44. }
  45. #endif // ABP_MUSIC_DIS_PINK_EN
  46. sys_cb.abp_mode = mode;
  47. if (bt_tws_is_slave()) {
  48. bt_tws_sync_setting();
  49. } else {
  50. bt_tws_req_alarm_abp(sys_cb.abp_mode);
  51. }
  52. TRACE("abp_mode: %d\n", sys_cb.abp_mode);
  53. }
  54. AT(.com_text.abp.alarm)
  55. void abp_stop_fade_out(void)
  56. {
  57. if (music_effect_get_state(MUSIC_EFFECT_ABP) && ABP_start) {
  58. dac1_fade_out_real();
  59. }
  60. }
  61. AT(.com_text.abp.alarm)
  62. void abp_start_fade_in(void)
  63. {
  64. if (sco_is_connected()) {
  65. return;
  66. }
  67. dac1_fade_in_real();
  68. }
  69. void abp_start(s32 wave_type)
  70. {
  71. if (sco_is_connected()) {
  72. return;
  73. }
  74. if (ABP_start) {
  75. dac1_fade_out_real();
  76. dac1_fade_wait();
  77. } else {
  78. abp_init();
  79. ABP_start = 1;
  80. }
  81. memset(&ABP_cb, 0, sizeof(ABP_generation_init_cb_t));
  82. ABP_cb.wave_type = wave_type; //0:alpha 1:beta 2:pink
  83. ABP_cb.intensity = ABP_INTENSITY;
  84. ABP_cb.plfsr_l = 0x5EEED1F5;
  85. ABP_cb.plfsr_r = 0xE5A756F3;
  86. alpha_beta_pink_generation_init(&ABP_cb);
  87. dac1_spr_set(SPR_48000);
  88. alg_abp_process();
  89. delay_5ms(1); //先让数据推起来
  90. if (!bt_tws_is_slave()) {
  91. bt_tws_req_alarm_abp(0x10);
  92. }
  93. TRACE("abp_start: %d\n", sys_cb.abp_mode);
  94. }
  95. void abp_stop(void)
  96. {
  97. if (!music_effect_get_state(MUSIC_EFFECT_ABP)) {
  98. return;
  99. }
  100. dac1_fade_wait();
  101. ABP_start = 0;
  102. music_effect_abp_stop();
  103. TRACE("abp_stop: %d\n", sys_cb.abp_mode);
  104. }
  105. #else
  106. void bsp_abp_set_mode(u8 mode)
  107. {
  108. #if ABP_MUSIC_DIS_PINK_EN
  109. if ((mode == ABP_MODE_PINK) && (f_bt.disp_status == BT_STA_PLAYING)) {
  110. return;
  111. }
  112. #endif // ABP_MUSIC_DIS_PINK_EN
  113. sys_cb.abp_mode = mode;
  114. if (sys_cb.abp_mode == 0) {
  115. abp_stop();
  116. } else {
  117. abp_start(sys_cb.abp_mode - 1);
  118. }
  119. TRACE("abp_mode: %d\n", sys_cb.abp_mode);
  120. }
  121. AT(.com_text.abp.alarm)
  122. void abp_stop_fade_out(void)
  123. {
  124. }
  125. void abp_start(s32 wave_type)
  126. {
  127. if (sco_is_connected()) {
  128. return;
  129. }
  130. if (ABP_start) {
  131. dac1_fade_out_real();
  132. dac1_fade_wait();
  133. } else {
  134. abp_init();
  135. ABP_start = 1;
  136. }
  137. memset(&ABP_cb, 0, sizeof(ABP_generation_init_cb_t));
  138. ABP_cb.wave_type = wave_type; //0:alpha 1:beta 2:pink
  139. ABP_cb.intensity = ABP_INTENSITY;
  140. ABP_cb.plfsr_l = 0x5EEED1F5;
  141. ABP_cb.plfsr_r = 0xE5A756F3;
  142. alpha_beta_pink_generation_init(&ABP_cb);
  143. dac1_spr_set(SPR_48000);
  144. alg_abp_process();
  145. delay_5ms(1); //先让数据推起来
  146. dac1_fade_in_real();
  147. TRACE("abp_start: %d\n", sys_cb.abp_mode);
  148. }
  149. void abp_stop(void)
  150. {
  151. if (!music_effect_get_state(MUSIC_EFFECT_ABP)) {
  152. return;
  153. }
  154. dac1_fade_out_real();
  155. dac1_fade_wait();
  156. ABP_start = 0;
  157. music_effect_abp_stop();
  158. TRACE("abp_stop: %d\n", sys_cb.abp_mode);
  159. }
  160. #endif // BT_TWS_EN
  161. void bsp_abp_set_vol(u32 vol)
  162. {
  163. if (ABP_start) {
  164. dac1_vol_set_fade(vol);
  165. }
  166. }
  167. bool abp_is_playing(void)
  168. {
  169. return (bool)ABP_start;
  170. }
  171. AT(.com_text.aubuf1)
  172. void aubuf1_dma_done_callback(void)
  173. {
  174. if ((!sco_is_connected()) && ABP_start) {
  175. abp_kick_start();
  176. }
  177. }
  178. AT(.audio_text.process)
  179. void alg_abp_process(void)
  180. {
  181. #if BT_TWS_EN
  182. u32 ch_idx = sys_cb.tws_left_channel ? 0 : 1;
  183. if (ABP_start) {
  184. alpha_beta_pink_generation((s16*)ABP_pcm, ABP_PCM_BUF_SAMPLES, ch_idx);
  185. aubuf1_dma_kick(ABP_pcm, ABP_PCM_BUF_SAMPLES, 1, 0);
  186. }
  187. #else
  188. if (ABP_start) {
  189. alpha_beta_pink_generation_stereo((s16*)ABP_pcm, ABP_PCM_BUF_SAMPLES);
  190. aubuf1_dma_kick(ABP_pcm, ABP_PCM_BUF_SAMPLES, 2, 0);
  191. }
  192. #endif // BT_TWS_EN
  193. }
  194. #endif