audio_arm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* Copyright 2016 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "audio.h"
  17. #include "ch.h"
  18. #include "hal.h"
  19. #include <string.h>
  20. #include "print.h"
  21. #include "keymap.h"
  22. #include "eeconfig.h"
  23. // -----------------------------------------------------------------------------
  24. int voices = 0;
  25. int voice_place = 0;
  26. float frequency = 0;
  27. float frequency_alt = 0;
  28. int volume = 0;
  29. long position = 0;
  30. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  31. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  32. bool sliding = false;
  33. float place = 0;
  34. uint8_t * sample;
  35. uint16_t sample_length = 0;
  36. bool playing_notes = false;
  37. bool playing_note = false;
  38. float note_frequency = 0;
  39. float note_length = 0;
  40. uint8_t note_tempo = TEMPO_DEFAULT;
  41. float note_timbre = TIMBRE_DEFAULT;
  42. uint16_t note_position = 0;
  43. float (* notes_pointer)[][2];
  44. uint16_t notes_count;
  45. bool notes_repeat;
  46. bool note_resting = false;
  47. uint8_t current_note = 0;
  48. uint8_t rest_counter = 0;
  49. #ifdef VIBRATO_ENABLE
  50. float vibrato_counter = 0;
  51. float vibrato_strength = .5;
  52. float vibrato_rate = 0.125;
  53. #endif
  54. float polyphony_rate = 0;
  55. static bool audio_initialized = false;
  56. audio_config_t audio_config;
  57. uint16_t envelope_index = 0;
  58. bool glissando = true;
  59. #ifndef STARTUP_SONG
  60. #define STARTUP_SONG SONG(STARTUP_SOUND)
  61. #endif
  62. float startup_song[][2] = STARTUP_SONG;
  63. static void gpt_cb8(GPTDriver *gptp);
  64. #define DAC_BUFFER_SIZE 720
  65. #define START_CHANNEL_1() gptStart(&GPTD6, &gpt6cfg1); \
  66. gptStartContinuous(&GPTD6, 2U)
  67. #define START_CHANNEL_2() gptStart(&GPTD7, &gpt7cfg1); \
  68. gptStartContinuous(&GPTD7, 2U)
  69. #define STOP_CHANNEL_1() gptStopTimer(&GPTD6)
  70. #define STOP_CHANNEL_2() gptStopTimer(&GPTD7)
  71. #define RESTART_CHANNEL_1() STOP_CHANNEL_1(); \
  72. START_CHANNEL_1()
  73. #define RESTART_CHANNEL_2() STOP_CHANNEL_2(); \
  74. START_CHANNEL_2()
  75. #define UPDATE_CHANNEL_1_FREQ(freq) gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
  76. RESTART_CHANNEL_1()
  77. #define UPDATE_CHANNEL_2_FREQ(freq) gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
  78. RESTART_CHANNEL_2()
  79. #define GET_CHANNEL_1_FREQ gpt6cfg1.frequency
  80. #define GET_CHANNEL_2_FREQ gpt7cfg1.frequency
  81. /*
  82. * GPT6 configuration.
  83. */
  84. // static const GPTConfig gpt6cfg1 = {
  85. // .frequency = 1000000U,
  86. // .callback = NULL,
  87. // .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  88. // .dier = 0U
  89. // };
  90. GPTConfig gpt6cfg1 = {
  91. .frequency = 440U*DAC_BUFFER_SIZE,
  92. .callback = NULL,
  93. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  94. .dier = 0U
  95. };
  96. GPTConfig gpt7cfg1 = {
  97. .frequency = 440U*DAC_BUFFER_SIZE,
  98. .callback = NULL,
  99. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  100. .dier = 0U
  101. };
  102. GPTConfig gpt8cfg1 = {
  103. .frequency = 10,
  104. .callback = gpt_cb8,
  105. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  106. .dier = 0U
  107. };
  108. /*
  109. * DAC test buffer (sine wave).
  110. */
  111. // static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
  112. // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
  113. // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
  114. // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
  115. // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
  116. // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
  117. // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
  118. // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
  119. // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
  120. // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
  121. // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
  122. // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
  123. // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
  124. // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
  125. // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
  126. // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
  127. // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
  128. // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
  129. // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
  130. // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
  131. // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
  132. // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
  133. // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16,
  134. // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
  135. // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
  136. // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
  137. // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
  138. // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
  139. // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
  140. // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
  141. // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012
  142. // };
  143. // static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
  144. // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
  145. // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
  146. // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
  147. // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
  148. // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
  149. // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
  150. // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
  151. // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012,
  152. // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
  153. // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
  154. // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
  155. // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
  156. // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
  157. // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
  158. // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
  159. // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
  160. // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
  161. // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
  162. // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
  163. // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
  164. // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
  165. // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
  166. // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
  167. // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
  168. // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
  169. // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
  170. // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
  171. // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
  172. // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
  173. // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16
  174. // };
  175. // squarewave
  176. static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
  177. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  178. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  179. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  180. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  181. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  182. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  183. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  184. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  185. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  186. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  187. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  188. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  189. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  190. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  191. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  192. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  193. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  194. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  195. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  196. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  197. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  198. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  199. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  200. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  201. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  202. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  203. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  204. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  205. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  206. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  207. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  208. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  209. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  211. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  212. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  213. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  214. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  215. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  216. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  217. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  218. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  219. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  220. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  221. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  223. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  224. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  225. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  226. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  227. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  228. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  229. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  231. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  232. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  234. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  235. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  236. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  237. };
  238. // squarewave
  239. static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
  240. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  241. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  242. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  243. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  244. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  245. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  246. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  247. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  248. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  249. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  250. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  251. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  252. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  254. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  255. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  256. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  257. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  258. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  259. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  260. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  261. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  262. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  263. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  264. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  265. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  266. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  267. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  268. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  269. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  270. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  271. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  272. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  273. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  274. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  275. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  276. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  277. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  278. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  279. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  280. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  281. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  282. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  283. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  284. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  285. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  286. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  287. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  288. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  289. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  290. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  291. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  292. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  293. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  294. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  295. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  296. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  297. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  298. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047,
  299. 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047, 2047
  300. };
  301. /*
  302. * DAC streaming callback.
  303. */
  304. size_t nx = 0, ny = 0, nz = 0;
  305. static void end_cb1(DACDriver *dacp, dacsample_t *buffer, size_t n) {
  306. (void)dacp;
  307. nz++;
  308. if (dac_buffer == buffer) {
  309. nx += n;
  310. }
  311. else {
  312. ny += n;
  313. }
  314. if ((nz % 1000) == 0) {
  315. // palTogglePad(GPIOD, GPIOD_LED3);
  316. }
  317. }
  318. /*
  319. * DAC error callback.
  320. */
  321. static void error_cb1(DACDriver *dacp, dacerror_t err) {
  322. (void)dacp;
  323. (void)err;
  324. chSysHalt("DAC failure");
  325. }
  326. static const DACConfig dac1cfg1 = {
  327. .init = 2047U,
  328. .datamode = DAC_DHRM_12BIT_RIGHT
  329. };
  330. static const DACConversionGroup dacgrpcfg1 = {
  331. .num_channels = 1U,
  332. .end_cb = end_cb1,
  333. .error_cb = error_cb1,
  334. .trigger = DAC_TRG(0)
  335. };
  336. static const DACConfig dac1cfg2 = {
  337. .init = 2047U,
  338. .datamode = DAC_DHRM_12BIT_RIGHT
  339. };
  340. static const DACConversionGroup dacgrpcfg2 = {
  341. .num_channels = 1U,
  342. .end_cb = end_cb1,
  343. .error_cb = error_cb1,
  344. .trigger = DAC_TRG(0)
  345. };
  346. void audio_init()
  347. {
  348. if (audio_initialized)
  349. return;
  350. // Check EEPROM
  351. // if (!eeconfig_is_enabled())
  352. // {
  353. // eeconfig_init();
  354. // }
  355. // audio_config.raw = eeconfig_read_audio();
  356. audio_config.enable = true;
  357. /*
  358. * Starting DAC1 driver, setting up the output pin as analog as suggested
  359. * by the Reference Manual.
  360. */
  361. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  362. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  363. dacStart(&DACD1, &dac1cfg1);
  364. dacStart(&DACD2, &dac1cfg2);
  365. /*
  366. * Starting GPT6/7 driver, it is used for triggering the DAC.
  367. */
  368. START_CHANNEL_1();
  369. START_CHANNEL_2();
  370. /*
  371. * Starting a continuous conversion.
  372. */
  373. dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
  374. dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
  375. audio_initialized = true;
  376. if (audio_config.enable) {
  377. PLAY_SONG(startup_song);
  378. }
  379. }
  380. void stop_all_notes()
  381. {
  382. dprintf("audio stop all notes");
  383. if (!audio_initialized) {
  384. audio_init();
  385. }
  386. voices = 0;
  387. gptStopTimer(&GPTD6);
  388. gptStopTimer(&GPTD7);
  389. gptStopTimer(&GPTD8);
  390. playing_notes = false;
  391. playing_note = false;
  392. frequency = 0;
  393. frequency_alt = 0;
  394. volume = 0;
  395. for (uint8_t i = 0; i < 8; i++)
  396. {
  397. frequencies[i] = 0;
  398. volumes[i] = 0;
  399. }
  400. }
  401. void stop_note(float freq)
  402. {
  403. dprintf("audio stop note freq=%d", (int)freq);
  404. if (playing_note) {
  405. if (!audio_initialized) {
  406. audio_init();
  407. }
  408. for (int i = 7; i >= 0; i--) {
  409. if (frequencies[i] == freq) {
  410. frequencies[i] = 0;
  411. volumes[i] = 0;
  412. for (int j = i; (j < 7); j++) {
  413. frequencies[j] = frequencies[j+1];
  414. frequencies[j+1] = 0;
  415. volumes[j] = volumes[j+1];
  416. volumes[j+1] = 0;
  417. }
  418. break;
  419. }
  420. }
  421. voices--;
  422. if (voices < 0)
  423. voices = 0;
  424. if (voice_place >= voices) {
  425. voice_place = 0;
  426. }
  427. if (voices == 0) {
  428. STOP_CHANNEL_1();
  429. STOP_CHANNEL_2();
  430. gptStopTimer(&GPTD8);
  431. frequency = 0;
  432. frequency_alt = 0;
  433. volume = 0;
  434. playing_note = false;
  435. }
  436. }
  437. }
  438. #ifdef VIBRATO_ENABLE
  439. float mod(float a, int b)
  440. {
  441. float r = fmod(a, b);
  442. return r < 0 ? r + b : r;
  443. }
  444. float vibrato(float average_freq) {
  445. #ifdef VIBRATO_STRENGTH_ENABLE
  446. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  447. #else
  448. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  449. #endif
  450. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  451. return vibrated_freq;
  452. }
  453. #endif
  454. static void gpt_cb8(GPTDriver *gptp) {
  455. float freq;
  456. if (playing_note) {
  457. if (voices > 0) {
  458. float freq_alt = 0;
  459. if (voices > 1) {
  460. if (polyphony_rate == 0) {
  461. if (glissando) {
  462. if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
  463. frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
  464. } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
  465. frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
  466. } else {
  467. frequency_alt = frequencies[voices - 2];
  468. }
  469. } else {
  470. frequency_alt = frequencies[voices - 2];
  471. }
  472. #ifdef VIBRATO_ENABLE
  473. if (vibrato_strength > 0) {
  474. freq_alt = vibrato(frequency_alt);
  475. } else {
  476. freq_alt = frequency_alt;
  477. }
  478. #else
  479. freq_alt = frequency_alt;
  480. #endif
  481. }
  482. if (envelope_index < 65535) {
  483. envelope_index++;
  484. }
  485. freq_alt = voice_envelope(freq_alt);
  486. if (freq_alt < 30.517578125) {
  487. freq_alt = 30.52;
  488. }
  489. if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
  490. UPDATE_CHANNEL_2_FREQ(freq_alt);
  491. } else {
  492. RESTART_CHANNEL_2();
  493. }
  494. //note_timbre;
  495. }
  496. if (polyphony_rate > 0) {
  497. if (voices > 1) {
  498. voice_place %= voices;
  499. if (place++ > (frequencies[voice_place] / polyphony_rate)) {
  500. voice_place = (voice_place + 1) % voices;
  501. place = 0.0;
  502. }
  503. }
  504. #ifdef VIBRATO_ENABLE
  505. if (vibrato_strength > 0) {
  506. freq = vibrato(frequencies[voice_place]);
  507. } else {
  508. freq = frequencies[voice_place];
  509. }
  510. #else
  511. freq = frequencies[voice_place];
  512. #endif
  513. } else {
  514. if (glissando) {
  515. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  516. frequency = frequency * pow(2, 440/frequency/12/2);
  517. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  518. frequency = frequency * pow(2, -440/frequency/12/2);
  519. } else {
  520. frequency = frequencies[voices - 1];
  521. }
  522. } else {
  523. frequency = frequencies[voices - 1];
  524. }
  525. #ifdef VIBRATO_ENABLE
  526. if (vibrato_strength > 0) {
  527. freq = vibrato(frequency);
  528. } else {
  529. freq = frequency;
  530. }
  531. #else
  532. freq = frequency;
  533. #endif
  534. }
  535. if (envelope_index < 65535) {
  536. envelope_index++;
  537. }
  538. freq = voice_envelope(freq);
  539. if (freq < 30.517578125) {
  540. freq = 30.52;
  541. }
  542. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  543. UPDATE_CHANNEL_1_FREQ(freq);
  544. } else {
  545. RESTART_CHANNEL_1();
  546. }
  547. //note_timbre;
  548. }
  549. }
  550. if (playing_notes) {
  551. if (note_frequency > 0) {
  552. #ifdef VIBRATO_ENABLE
  553. if (vibrato_strength > 0) {
  554. freq = vibrato(note_frequency);
  555. } else {
  556. freq = note_frequency;
  557. }
  558. #else
  559. freq = note_frequency;
  560. #endif
  561. if (envelope_index < 65535) {
  562. envelope_index++;
  563. }
  564. freq = voice_envelope(freq);
  565. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  566. UPDATE_CHANNEL_1_FREQ(freq);
  567. UPDATE_CHANNEL_2_FREQ(freq);
  568. }
  569. //note_timbre;
  570. } else {
  571. // gptStopTimer(&GPTD6);
  572. // gptStopTimer(&GPTD7);
  573. }
  574. note_position++;
  575. bool end_of_note = false;
  576. if (GET_CHANNEL_1_FREQ > 0) {
  577. if (!note_resting)
  578. end_of_note = (note_position >= (note_length*8 - 1));
  579. else
  580. end_of_note = (note_position >= (note_length*8));
  581. } else {
  582. end_of_note = (note_position >= (note_length*8));
  583. }
  584. if (end_of_note) {
  585. current_note++;
  586. if (current_note >= notes_count) {
  587. if (notes_repeat) {
  588. current_note = 0;
  589. } else {
  590. STOP_CHANNEL_1();
  591. STOP_CHANNEL_2();
  592. // gptStopTimer(&GPTD8);
  593. playing_notes = false;
  594. return;
  595. }
  596. }
  597. if (!note_resting) {
  598. note_resting = true;
  599. current_note--;
  600. if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
  601. note_frequency = 0;
  602. note_length = 1;
  603. } else {
  604. note_frequency = (*notes_pointer)[current_note][0];
  605. note_length = 1;
  606. }
  607. } else {
  608. note_resting = false;
  609. envelope_index = 0;
  610. note_frequency = (*notes_pointer)[current_note][0];
  611. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  612. }
  613. note_position = 0;
  614. }
  615. }
  616. if (!audio_config.enable) {
  617. playing_notes = false;
  618. playing_note = false;
  619. }
  620. }
  621. void play_note(float freq, int vol) {
  622. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  623. if (!audio_initialized) {
  624. audio_init();
  625. }
  626. if (audio_config.enable && voices < 8) {
  627. // Cancel notes if notes are playing
  628. if (playing_notes)
  629. stop_all_notes();
  630. playing_note = true;
  631. envelope_index = 0;
  632. if (freq > 0) {
  633. frequencies[voices] = freq;
  634. volumes[voices] = vol;
  635. voices++;
  636. }
  637. gptStart(&GPTD8, &gpt8cfg1);
  638. gptStartContinuous(&GPTD8, 2U);
  639. RESTART_CHANNEL_1();
  640. RESTART_CHANNEL_2();
  641. }
  642. }
  643. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
  644. {
  645. if (!audio_initialized) {
  646. audio_init();
  647. }
  648. if (audio_config.enable) {
  649. // Cancel note if a note is playing
  650. if (playing_note)
  651. stop_all_notes();
  652. playing_notes = true;
  653. notes_pointer = np;
  654. notes_count = n_count;
  655. notes_repeat = n_repeat;
  656. place = 0;
  657. current_note = 0;
  658. note_frequency = (*notes_pointer)[current_note][0];
  659. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  660. note_position = 0;
  661. gptStart(&GPTD8, &gpt8cfg1);
  662. gptStartContinuous(&GPTD8, 2U);
  663. RESTART_CHANNEL_1();
  664. RESTART_CHANNEL_2();
  665. }
  666. }
  667. bool is_playing_notes(void) {
  668. return playing_notes;
  669. }
  670. bool is_audio_on(void) {
  671. return (audio_config.enable != 0);
  672. }
  673. void audio_toggle(void) {
  674. audio_config.enable ^= 1;
  675. eeconfig_update_audio(audio_config.raw);
  676. if (audio_config.enable)
  677. audio_on_user();
  678. }
  679. void audio_on(void) {
  680. audio_config.enable = 1;
  681. eeconfig_update_audio(audio_config.raw);
  682. audio_on_user();
  683. }
  684. void audio_off(void) {
  685. audio_config.enable = 0;
  686. eeconfig_update_audio(audio_config.raw);
  687. }
  688. #ifdef VIBRATO_ENABLE
  689. // Vibrato rate functions
  690. void set_vibrato_rate(float rate) {
  691. vibrato_rate = rate;
  692. }
  693. void increase_vibrato_rate(float change) {
  694. vibrato_rate *= change;
  695. }
  696. void decrease_vibrato_rate(float change) {
  697. vibrato_rate /= change;
  698. }
  699. #ifdef VIBRATO_STRENGTH_ENABLE
  700. void set_vibrato_strength(float strength) {
  701. vibrato_strength = strength;
  702. }
  703. void increase_vibrato_strength(float change) {
  704. vibrato_strength *= change;
  705. }
  706. void decrease_vibrato_strength(float change) {
  707. vibrato_strength /= change;
  708. }
  709. #endif /* VIBRATO_STRENGTH_ENABLE */
  710. #endif /* VIBRATO_ENABLE */
  711. // Polyphony functions
  712. void set_polyphony_rate(float rate) {
  713. polyphony_rate = rate;
  714. }
  715. void enable_polyphony() {
  716. polyphony_rate = 5;
  717. }
  718. void disable_polyphony() {
  719. polyphony_rate = 0;
  720. }
  721. void increase_polyphony_rate(float change) {
  722. polyphony_rate *= change;
  723. }
  724. void decrease_polyphony_rate(float change) {
  725. polyphony_rate /= change;
  726. }
  727. // Timbre function
  728. void set_timbre(float timbre) {
  729. note_timbre = timbre;
  730. }
  731. // Tempo functions
  732. void set_tempo(uint8_t tempo) {
  733. note_tempo = tempo;
  734. }
  735. void decrease_tempo(uint8_t tempo_change) {
  736. note_tempo += tempo_change;
  737. }
  738. void increase_tempo(uint8_t tempo_change) {
  739. if (note_tempo - tempo_change < 10) {
  740. note_tempo = 10;
  741. } else {
  742. note_tempo -= tempo_change;
  743. }
  744. }