bsp_piano.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "include.h"
  2. // 1/8 4/4 3/4 2/4 1/4 4/4 3/4 2/4
  3. //Rhythm = [ 0.062 0.094 0.125 0.125 0.187 0.250];
  4. // Do Re Mi Fa Sol La Si
  5. //Fin = [261.63 293.67 329.63 349.23 391.99 440 493.88 0 0 0];
  6. #define PIANO_BASEKEYDLY (int)(48000*0.125)
  7. #define TONE_DELAY(x) (int)(48*x)
  8. #define TONE_FREQUENCY(a, b) (u32)((a << 16) | (u16)((1 << 15) / (48000 / b)))
  9. struct tone_tbl_t {
  10. u32 res;
  11. u32 last_time; //持续时间
  12. };
  13. #if WARNING_PIANO_EN
  14. extern const u16 dac_dvol_table[16 + 1];
  15. //频率: 380Hz 间隔40mS 380Hz
  16. //持续时间:103mS 103mS
  17. //窗函数: fade out fade out
  18. //窗持续: 1/2 1
  19. AT(.piano_com.tbl)
  20. static const struct tone_tbl_t tone_disconnected_tbl[3] = {
  21. {TONE_FREQUENCY(0x7ff, 380), TONE_DELAY(103)},
  22. {0, TONE_DELAY(40)}, //静音
  23. {TONE_FREQUENCY(0x7ff, 380), TONE_DELAY(103)},
  24. };
  25. //频率: 760Hz
  26. //持续时间:100mS
  27. //窗函数: fade out
  28. //窗持续: 1/2
  29. AT(.piano_com.tbl)
  30. static const struct tone_tbl_t tone_connected_tbl[1] = {
  31. {TONE_FREQUENCY(0x7ff, 760), TONE_DELAY(100)},
  32. };
  33. //频率: 501Hz
  34. //持续时间:100mS
  35. //窗函数: fade out
  36. //窗持续: 1/2
  37. AT(.piano_com.tbl)
  38. static const struct tone_tbl_t tone_pair_tbl[1] = {
  39. {TONE_FREQUENCY(0x7ff, 501), TONE_DELAY(100)},
  40. };
  41. //频率: 200Hz 间隔20mS 302Hz 间隔20mS 400Hz 间隔20mS 501Hz
  42. //持续时间:160mS 160mS 157mS 157mS
  43. //窗函数: fade out fade out fade out fade out
  44. //窗持续: 1/2 1/2 1/2 1
  45. AT(.piano_com.tbl)
  46. static const struct tone_tbl_t tone_power_on_tbl[7] = {
  47. {TONE_FREQUENCY(0x7ff, 200), TONE_DELAY(160)},
  48. {0, TONE_DELAY(20)}, //静音
  49. {TONE_FREQUENCY(0x7ff, 302), TONE_DELAY(160)},
  50. {0, TONE_DELAY(20)}, //静音
  51. {TONE_FREQUENCY(0x7ff, 400), TONE_DELAY(157)},
  52. {0, TONE_DELAY(20)}, //静音
  53. {TONE_FREQUENCY(0x7ff, 501), TONE_DELAY(157)},
  54. };
  55. //频率: 501Hz 间隔20mS 400Hz 间隔20mS 302Hz 间隔20mS 200Hz
  56. //持续时间:160mS 160mS 157mS 157mS
  57. //窗函数: fade out fade out fade out fade out
  58. //窗持续: 1/2 1/2 1/2 1
  59. AT(.piano_com.tbl)
  60. static const struct tone_tbl_t tone_power_off_tbl[7] = {
  61. {TONE_FREQUENCY(0x7ff, 501), TONE_DELAY(160)},
  62. {0, TONE_DELAY(20)}, //静音
  63. {TONE_FREQUENCY(0x7ff, 400), TONE_DELAY(160)},
  64. {0, TONE_DELAY(20)}, //静音
  65. {TONE_FREQUENCY(0x7ff, 302), TONE_DELAY(157)},
  66. {0, TONE_DELAY(20)}, //静音
  67. {TONE_FREQUENCY(0x7ff, 200), TONE_DELAY(157)},
  68. };
  69. //频率: 450Hz
  70. //持续时间:1365mS
  71. //窗函数: sine
  72. //窗调制: 15Hz
  73. AT(.piano_com.tbl)
  74. static const struct tone_tbl_t tone_ring_tbl[2] = {
  75. {TONE_FREQUENCY(0x800a, 450), (TONE_DELAY(1364)+24)},
  76. {0, TONE_DELAY(20)}, //静音
  77. };
  78. ////频率: 2600Hz
  79. ////持续时间:100mS
  80. ////窗函数: fade out
  81. ////窗调制: 1
  82. //AT(.piano_com.tbl)
  83. //static const struct tone_tbl_t tone_maxvol_tbl[1] = {
  84. // {TONE_FREQUENCY(0x7ff, 2600), TONE_DELAY(100)},
  85. //};
  86. //频率: 210Hz 间隔50mS 260Hz 间隔50mS 400Hz
  87. //持续时间:145mS 437mS 145mS
  88. //窗函数: fade out fade out fade out
  89. //窗调制: 1 1 1
  90. AT(.piano_com.tbl)
  91. const struct tone_tbl_t tone_maxvol_tbl[5] = {
  92. {TONE_FREQUENCY(0x7ff, 210), TONE_DELAY(145)},
  93. {0, TONE_DELAY(50)}, //静音
  94. {TONE_FREQUENCY(0x7ff, 260), TONE_DELAY(437)},
  95. {0, TONE_DELAY(50)}, //静音
  96. {TONE_FREQUENCY(0x7ff, 400), TONE_DELAY(145)},
  97. };
  98. //频率: 848Hz 间隔50mS 639Hz
  99. //持续时间:207mS 208mS
  100. //窗函数: fade out fade out
  101. //窗调制: 1 1
  102. AT(.piano_com.tbl)
  103. static const struct tone_tbl_t tone_low_battery_tbl[3] = {
  104. {TONE_FREQUENCY(0x7ff, 848), TONE_DELAY(207)},
  105. {0, TONE_DELAY(50)}, //静音
  106. {TONE_FREQUENCY(0x7ff, 639), TONE_DELAY(208)},
  107. };
  108. AT(.piano_com.tbl)
  109. static const struct tone_tbl_t tone_redialing_tbl[1] = {
  110. {TONE_FREQUENCY(0x7ff, 524), TONE_DELAY(100)},
  111. };
  112. AT(.piano_com.tbl)
  113. static const struct tone_tbl_t tone_bt_mode_tbl[3] = {
  114. {TONE_FREQUENCY(0x7ff, 392), TONE_DELAY(100)},
  115. {0, TONE_DELAY(53)}, //静音
  116. {TONE_FREQUENCY(0x7ff, 586), TONE_DELAY(98)},
  117. };
  118. //piano提示音
  119. AT(.piano_com.tbl)
  120. static const u32 piano_bt_mode_tbl[1] = {
  121. 0x00236521
  122. };
  123. AT(.piano_com.tbl)
  124. static const u32 piano_camera_mode_tbl[1] = {
  125. 0x00030201
  126. };
  127. AT(.piano_com.tbl)
  128. static const u32 piano_aux_mode_tbl[1] = {
  129. 0x00131203
  130. };
  131. AT(.piano_com.tbl)
  132. static const u32 piano_power_on_tbl[1] = {
  133. 0x07050301
  134. };
  135. AT(.piano_com.tbl)
  136. static const u32 piano_power_off_tbl[1] = {
  137. 0x01030507
  138. };
  139. AT(.text.piano.table)
  140. const struct warning_tone_tbl_t warning_tone_tbl[] = {
  141. [TONE_POWER_ON] = {sizeof(tone_power_on_tbl) / sizeof(struct tone_tbl_t), tone_power_on_tbl }, //1
  142. [TONE_POWER_OFF] = {sizeof(tone_power_off_tbl) / sizeof(struct tone_tbl_t), tone_power_off_tbl }, //2
  143. [TONE_PAIR] = {sizeof(tone_pair_tbl) / sizeof(struct tone_tbl_t), tone_pair_tbl }, //3
  144. [TONE_BT_DISCONNECT] = {sizeof(tone_disconnected_tbl) / sizeof(struct tone_tbl_t), tone_disconnected_tbl }, //4
  145. [TONE_BT_CONNECT] = {sizeof(tone_connected_tbl) / sizeof(struct tone_tbl_t), tone_connected_tbl }, //5
  146. [TONE_BT_RING] = {sizeof(tone_ring_tbl) / sizeof(struct tone_tbl_t), tone_ring_tbl }, //6
  147. [TONE_MAX_VOL] = {sizeof(tone_maxvol_tbl) / sizeof(struct tone_tbl_t), tone_maxvol_tbl }, //7
  148. [TONE_LOW_BATTERY] = {sizeof(tone_low_battery_tbl) / sizeof(struct tone_tbl_t), tone_low_battery_tbl }, //8
  149. [TONE_REDIALING] = {sizeof(tone_redialing_tbl) / sizeof(struct tone_tbl_t), tone_redialing_tbl }, //9
  150. [TONE_BT_MODE] = {sizeof(tone_bt_mode_tbl) / sizeof(struct tone_tbl_t), tone_bt_mode_tbl }, //10
  151. };
  152. AT(.text.piano.table)
  153. const struct warning_piano_tbl_t warning_piano_tbl[] = {
  154. [PIANO_POWER_ON] = {sizeof(piano_power_on_tbl) / 4, piano_power_on_tbl},
  155. [PIANO_POWER_OFF] = {sizeof(piano_power_off_tbl) / 4, piano_power_off_tbl},
  156. [PIANO_BT_MODE] = {sizeof(piano_bt_mode_tbl) / 4, piano_bt_mode_tbl},
  157. [PIANO_CAMERA_MODE] = {sizeof(piano_camera_mode_tbl) / 4, piano_camera_mode_tbl},
  158. [PIANO_AUX_MODE] = {sizeof(piano_aux_mode_tbl) / 4, piano_aux_mode_tbl},
  159. };
  160. AT(.text.piano)
  161. void piano_play_process(void)
  162. {
  163. u16 msg;
  164. WDT_CLR();
  165. msg = msg_dequeue();
  166. if ((msg != NO_MSG) && ((msg & KEY_TYPE_MASK) != KEY_HOLD)) {
  167. msg_enqueue(msg); //还原未处理的消息
  168. }
  169. }
  170. //type: 0-->Piano, 1--> Tone
  171. AT(.text.piano)
  172. void bsp_piano_warning_play(uint type, uint index)
  173. {
  174. void *res;
  175. u8 index_max;
  176. #if ABP_EN
  177. if (abp_is_playing()) {
  178. #if ABP_PLAY_DIS_WAV_EN
  179. return;
  180. #else
  181. abp_stop();
  182. dac1_aubuf_clr();
  183. #endif // ABP_PLAY_DIS_WAV_EN
  184. }
  185. #endif // ABP_EN
  186. if (type == WARNING_PIANO) {
  187. index_max = sizeof(warning_piano_tbl)/sizeof(warning_piano_tbl[0]);
  188. if(index > index_max){
  189. index = PIANO_POWER_ON;
  190. }
  191. res = (void *)&warning_piano_tbl[index];
  192. } else {
  193. index_max = sizeof(warning_tone_tbl)/sizeof(warning_tone_tbl[0]);
  194. if(index > index_max){
  195. index = TONE_MAX_VOL;
  196. }
  197. res = (void *)&warning_tone_tbl[index];
  198. }
  199. if (type == WARNING_TONE) {
  200. while (tone_is_playing()) {
  201. bt_thread_check_trigger();
  202. piano_play_process();
  203. WDT_CLR();
  204. }
  205. }
  206. u8 dac_sta = dac_get_pwr_sta();
  207. func_bt_set_dac(1);
  208. #if WARNING_SYSVOL_ADJ_EN
  209. bsp_res_sysvol_adjust();
  210. #endif
  211. #if FUNC_AUX_EN
  212. tone_play_kick(type, res, true);
  213. while (tone_is_playing()) {
  214. piano_play_process();
  215. }
  216. tone_play_end();
  217. #else
  218. tone_play_kick(type, res, true);
  219. while (tone_is_playing()) {
  220. bt_thread_check_trigger();
  221. piano_play_process();
  222. }
  223. tone_play_end();
  224. #endif // FUNC_AUX_EN
  225. #if WARNING_SYSVOL_ADJ_EN
  226. bsp_res_sysvol_resume();
  227. #endif
  228. #if ABP_EN && !ABP_PLAY_DIS_WAV_EN
  229. if (sys_cb.abp_mode) {
  230. bsp_abp_set_mode(sys_cb.abp_mode);
  231. }
  232. #endif
  233. func_bt_set_dac(dac_sta);
  234. }
  235. #else
  236. void bsp_piano_warning_play(uint type, uint index) {}
  237. #endif