audio_arm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. uint16_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 100
  65. #ifndef DAC_SAMPLE_MAX
  66. # define DAC_SAMPLE_MAX 65535U
  67. #endif
  68. #define START_CHANNEL_1() \
  69. gptStart(&GPTD6, &gpt6cfg1); \
  70. gptStartContinuous(&GPTD6, 2U)
  71. #define START_CHANNEL_2() \
  72. gptStart(&GPTD7, &gpt7cfg1); \
  73. gptStartContinuous(&GPTD7, 2U)
  74. #define STOP_CHANNEL_1() gptStopTimer(&GPTD6)
  75. #define STOP_CHANNEL_2() gptStopTimer(&GPTD7)
  76. #define RESTART_CHANNEL_1() \
  77. STOP_CHANNEL_1(); \
  78. START_CHANNEL_1()
  79. #define RESTART_CHANNEL_2() \
  80. STOP_CHANNEL_2(); \
  81. START_CHANNEL_2()
  82. #define UPDATE_CHANNEL_1_FREQ(freq) \
  83. gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
  84. RESTART_CHANNEL_1()
  85. #define UPDATE_CHANNEL_2_FREQ(freq) \
  86. gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
  87. RESTART_CHANNEL_2()
  88. #define GET_CHANNEL_1_FREQ (uint16_t)(gpt6cfg1.frequency * DAC_BUFFER_SIZE)
  89. #define GET_CHANNEL_2_FREQ (uint16_t)(gpt7cfg1.frequency * DAC_BUFFER_SIZE)
  90. /*
  91. * GPT6 configuration.
  92. */
  93. // static const GPTConfig gpt6cfg1 = {
  94. // .frequency = 1000000U,
  95. // .callback = NULL,
  96. // .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  97. // .dier = 0U
  98. // };
  99. GPTConfig gpt6cfg1 = {.frequency = 440U * DAC_BUFFER_SIZE,
  100. .callback = NULL,
  101. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  102. .dier = 0U};
  103. GPTConfig gpt7cfg1 = {.frequency = 440U * DAC_BUFFER_SIZE,
  104. .callback = NULL,
  105. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  106. .dier = 0U};
  107. GPTConfig gpt8cfg1 = {.frequency = 10,
  108. .callback = gpt_cb8,
  109. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  110. .dier = 0U};
  111. /*
  112. * DAC test buffer (sine wave).
  113. */
  114. // static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
  115. // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
  116. // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
  117. // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
  118. // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
  119. // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
  120. // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
  121. // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
  122. // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
  123. // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
  124. // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
  125. // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
  126. // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
  127. // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
  128. // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
  129. // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
  130. // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
  131. // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
  132. // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
  133. // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
  134. // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
  135. // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
  136. // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16,
  137. // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
  138. // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
  139. // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
  140. // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
  141. // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
  142. // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
  143. // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
  144. // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012
  145. // };
  146. // static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
  147. // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
  148. // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
  149. // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
  150. // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
  151. // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
  152. // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
  153. // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
  154. // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012,
  155. // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
  156. // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
  157. // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
  158. // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
  159. // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
  160. // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
  161. // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
  162. // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
  163. // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
  164. // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
  165. // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
  166. // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
  167. // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
  168. // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
  169. // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
  170. // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
  171. // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
  172. // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
  173. // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
  174. // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
  175. // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
  176. // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16
  177. // };
  178. // squarewave
  179. static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
  180. // First half is max, second half is 0
  181. [0 ... DAC_BUFFER_SIZE / 2 - 1] = DAC_SAMPLE_MAX,
  182. [DAC_BUFFER_SIZE / 2 ... DAC_BUFFER_SIZE - 1] = 0,
  183. };
  184. // squarewave
  185. static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
  186. // opposite of dac_buffer above
  187. [0 ... DAC_BUFFER_SIZE / 2 - 1] = 0,
  188. [DAC_BUFFER_SIZE / 2 ... DAC_BUFFER_SIZE - 1] = DAC_SAMPLE_MAX,
  189. };
  190. /*
  191. * DAC streaming callback.
  192. */
  193. size_t nx = 0, ny = 0, nz = 0;
  194. static void end_cb1(DACDriver *dacp, dacsample_t *buffer, size_t n) {
  195. (void)dacp;
  196. nz++;
  197. if (dac_buffer == buffer) {
  198. nx += n;
  199. } else {
  200. ny += n;
  201. }
  202. if ((nz % 1000) == 0) {
  203. // palTogglePad(GPIOD, GPIOD_LED3);
  204. }
  205. }
  206. /*
  207. * DAC error callback.
  208. */
  209. static void error_cb1(DACDriver *dacp, dacerror_t err) {
  210. (void)dacp;
  211. (void)err;
  212. chSysHalt("DAC failure");
  213. }
  214. static const DACConfig dac1cfg1 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
  215. static const DACConversionGroup dacgrpcfg1 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
  216. static const DACConfig dac1cfg2 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
  217. static const DACConversionGroup dacgrpcfg2 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
  218. void audio_init() {
  219. if (audio_initialized) {
  220. return;
  221. }
  222. // Check EEPROM
  223. #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
  224. if (!eeconfig_is_enabled()) {
  225. eeconfig_init();
  226. }
  227. audio_config.raw = eeconfig_read_audio();
  228. #else // ARM EEPROM
  229. audio_config.enable = true;
  230. # ifdef AUDIO_CLICKY_ON
  231. audio_config.clicky_enable = true;
  232. # endif
  233. #endif // ARM EEPROM
  234. /*
  235. * Starting DAC1 driver, setting up the output pin as analog as suggested
  236. * by the Reference Manual.
  237. */
  238. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  239. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  240. dacStart(&DACD1, &dac1cfg1);
  241. dacStart(&DACD2, &dac1cfg2);
  242. /*
  243. * Starting GPT6/7 driver, it is used for triggering the DAC.
  244. */
  245. START_CHANNEL_1();
  246. START_CHANNEL_2();
  247. /*
  248. * Starting a continuous conversion.
  249. */
  250. dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
  251. dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
  252. audio_initialized = true;
  253. if (audio_config.enable) {
  254. PLAY_SONG(startup_song);
  255. } else {
  256. stop_all_notes();
  257. }
  258. }
  259. void stop_all_notes() {
  260. dprintf("audio stop all notes");
  261. if (!audio_initialized) {
  262. audio_init();
  263. }
  264. voices = 0;
  265. gptStopTimer(&GPTD6);
  266. gptStopTimer(&GPTD7);
  267. gptStopTimer(&GPTD8);
  268. playing_notes = false;
  269. playing_note = false;
  270. frequency = 0;
  271. frequency_alt = 0;
  272. volume = 0;
  273. for (uint8_t i = 0; i < 8; i++) {
  274. frequencies[i] = 0;
  275. volumes[i] = 0;
  276. }
  277. }
  278. void stop_note(float freq) {
  279. dprintf("audio stop note freq=%d", (int)freq);
  280. if (playing_note) {
  281. if (!audio_initialized) {
  282. audio_init();
  283. }
  284. for (int i = 7; i >= 0; i--) {
  285. if (frequencies[i] == freq) {
  286. frequencies[i] = 0;
  287. volumes[i] = 0;
  288. for (int j = i; (j < 7); j++) {
  289. frequencies[j] = frequencies[j + 1];
  290. frequencies[j + 1] = 0;
  291. volumes[j] = volumes[j + 1];
  292. volumes[j + 1] = 0;
  293. }
  294. break;
  295. }
  296. }
  297. voices--;
  298. if (voices < 0) {
  299. voices = 0;
  300. }
  301. if (voice_place >= voices) {
  302. voice_place = 0;
  303. }
  304. if (voices == 0) {
  305. STOP_CHANNEL_1();
  306. STOP_CHANNEL_2();
  307. gptStopTimer(&GPTD8);
  308. frequency = 0;
  309. frequency_alt = 0;
  310. volume = 0;
  311. playing_note = false;
  312. }
  313. }
  314. }
  315. #ifdef VIBRATO_ENABLE
  316. float mod(float a, int b) {
  317. float r = fmod(a, b);
  318. return r < 0 ? r + b : r;
  319. }
  320. float vibrato(float average_freq) {
  321. # ifdef VIBRATO_STRENGTH_ENABLE
  322. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  323. # else
  324. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  325. # endif
  326. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
  327. return vibrated_freq;
  328. }
  329. #endif
  330. static void gpt_cb8(GPTDriver *gptp) {
  331. float freq;
  332. if (playing_note) {
  333. if (voices > 0) {
  334. float freq_alt = 0;
  335. if (voices > 1) {
  336. if (polyphony_rate == 0) {
  337. if (glissando) {
  338. if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440 / frequencies[voices - 2] / 12 / 2)) {
  339. frequency_alt = frequency_alt * pow(2, 440 / frequency_alt / 12 / 2);
  340. } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440 / frequencies[voices - 2] / 12 / 2)) {
  341. frequency_alt = frequency_alt * pow(2, -440 / frequency_alt / 12 / 2);
  342. } else {
  343. frequency_alt = frequencies[voices - 2];
  344. }
  345. } else {
  346. frequency_alt = frequencies[voices - 2];
  347. }
  348. #ifdef VIBRATO_ENABLE
  349. if (vibrato_strength > 0) {
  350. freq_alt = vibrato(frequency_alt);
  351. } else {
  352. freq_alt = frequency_alt;
  353. }
  354. #else
  355. freq_alt = frequency_alt;
  356. #endif
  357. }
  358. if (envelope_index < 65535) {
  359. envelope_index++;
  360. }
  361. freq_alt = voice_envelope(freq_alt);
  362. if (freq_alt < 30.517578125) {
  363. freq_alt = 30.52;
  364. }
  365. if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
  366. UPDATE_CHANNEL_2_FREQ(freq_alt);
  367. } else {
  368. RESTART_CHANNEL_2();
  369. }
  370. // note_timbre;
  371. }
  372. if (polyphony_rate > 0) {
  373. if (voices > 1) {
  374. voice_place %= voices;
  375. if (place++ > (frequencies[voice_place] / polyphony_rate)) {
  376. voice_place = (voice_place + 1) % voices;
  377. place = 0.0;
  378. }
  379. }
  380. #ifdef VIBRATO_ENABLE
  381. if (vibrato_strength > 0) {
  382. freq = vibrato(frequencies[voice_place]);
  383. } else {
  384. freq = frequencies[voice_place];
  385. }
  386. #else
  387. freq = frequencies[voice_place];
  388. #endif
  389. } else {
  390. if (glissando) {
  391. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
  392. frequency = frequency * pow(2, 440 / frequency / 12 / 2);
  393. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
  394. frequency = frequency * pow(2, -440 / frequency / 12 / 2);
  395. } else {
  396. frequency = frequencies[voices - 1];
  397. }
  398. } else {
  399. frequency = frequencies[voices - 1];
  400. }
  401. #ifdef VIBRATO_ENABLE
  402. if (vibrato_strength > 0) {
  403. freq = vibrato(frequency);
  404. } else {
  405. freq = frequency;
  406. }
  407. #else
  408. freq = frequency;
  409. #endif
  410. }
  411. if (envelope_index < 65535) {
  412. envelope_index++;
  413. }
  414. freq = voice_envelope(freq);
  415. if (freq < 30.517578125) {
  416. freq = 30.52;
  417. }
  418. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  419. UPDATE_CHANNEL_1_FREQ(freq);
  420. } else {
  421. RESTART_CHANNEL_1();
  422. }
  423. // note_timbre;
  424. }
  425. }
  426. if (playing_notes) {
  427. if (note_frequency > 0) {
  428. #ifdef VIBRATO_ENABLE
  429. if (vibrato_strength > 0) {
  430. freq = vibrato(note_frequency);
  431. } else {
  432. freq = note_frequency;
  433. }
  434. #else
  435. freq = note_frequency;
  436. #endif
  437. if (envelope_index < 65535) {
  438. envelope_index++;
  439. }
  440. freq = voice_envelope(freq);
  441. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  442. UPDATE_CHANNEL_1_FREQ(freq);
  443. UPDATE_CHANNEL_2_FREQ(freq);
  444. }
  445. // note_timbre;
  446. } else {
  447. // gptStopTimer(&GPTD6);
  448. // gptStopTimer(&GPTD7);
  449. }
  450. note_position++;
  451. bool end_of_note = false;
  452. if (GET_CHANNEL_1_FREQ > 0) {
  453. if (!note_resting)
  454. end_of_note = (note_position >= (note_length * 8 - 1));
  455. else
  456. end_of_note = (note_position >= (note_length * 8));
  457. } else {
  458. end_of_note = (note_position >= (note_length * 8));
  459. }
  460. if (end_of_note) {
  461. current_note++;
  462. if (current_note >= notes_count) {
  463. if (notes_repeat) {
  464. current_note = 0;
  465. } else {
  466. STOP_CHANNEL_1();
  467. STOP_CHANNEL_2();
  468. // gptStopTimer(&GPTD8);
  469. playing_notes = false;
  470. return;
  471. }
  472. }
  473. if (!note_resting) {
  474. note_resting = true;
  475. current_note--;
  476. if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
  477. note_frequency = 0;
  478. note_length = 1;
  479. } else {
  480. note_frequency = (*notes_pointer)[current_note][0];
  481. note_length = 1;
  482. }
  483. } else {
  484. note_resting = false;
  485. envelope_index = 0;
  486. note_frequency = (*notes_pointer)[current_note][0];
  487. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  488. }
  489. note_position = 0;
  490. }
  491. }
  492. if (!audio_config.enable) {
  493. playing_notes = false;
  494. playing_note = false;
  495. }
  496. }
  497. void play_note(float freq, int vol) {
  498. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  499. if (!audio_initialized) {
  500. audio_init();
  501. }
  502. if (audio_config.enable && voices < 8) {
  503. // Cancel notes if notes are playing
  504. if (playing_notes) {
  505. stop_all_notes();
  506. }
  507. playing_note = true;
  508. envelope_index = 0;
  509. if (freq > 0) {
  510. frequencies[voices] = freq;
  511. volumes[voices] = vol;
  512. voices++;
  513. }
  514. gptStart(&GPTD8, &gpt8cfg1);
  515. gptStartContinuous(&GPTD8, 2U);
  516. RESTART_CHANNEL_1();
  517. RESTART_CHANNEL_2();
  518. }
  519. }
  520. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  521. if (!audio_initialized) {
  522. audio_init();
  523. }
  524. if (audio_config.enable) {
  525. // Cancel note if a note is playing
  526. if (playing_note) {
  527. stop_all_notes();
  528. }
  529. playing_notes = true;
  530. notes_pointer = np;
  531. notes_count = n_count;
  532. notes_repeat = n_repeat;
  533. place = 0;
  534. current_note = 0;
  535. note_frequency = (*notes_pointer)[current_note][0];
  536. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  537. note_position = 0;
  538. gptStart(&GPTD8, &gpt8cfg1);
  539. gptStartContinuous(&GPTD8, 2U);
  540. RESTART_CHANNEL_1();
  541. RESTART_CHANNEL_2();
  542. }
  543. }
  544. bool is_playing_notes(void) { return playing_notes; }
  545. bool is_audio_on(void) { return (audio_config.enable != 0); }
  546. void audio_toggle(void) {
  547. audio_config.enable ^= 1;
  548. eeconfig_update_audio(audio_config.raw);
  549. if (audio_config.enable) {
  550. audio_on_user();
  551. }
  552. }
  553. void audio_on(void) {
  554. audio_config.enable = 1;
  555. eeconfig_update_audio(audio_config.raw);
  556. audio_on_user();
  557. }
  558. void audio_off(void) {
  559. stop_all_notes();
  560. audio_config.enable = 0;
  561. eeconfig_update_audio(audio_config.raw);
  562. }
  563. #ifdef VIBRATO_ENABLE
  564. // Vibrato rate functions
  565. void set_vibrato_rate(float rate) { vibrato_rate = rate; }
  566. void increase_vibrato_rate(float change) { vibrato_rate *= change; }
  567. void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
  568. # ifdef VIBRATO_STRENGTH_ENABLE
  569. void set_vibrato_strength(float strength) { vibrato_strength = strength; }
  570. void increase_vibrato_strength(float change) { vibrato_strength *= change; }
  571. void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
  572. # endif /* VIBRATO_STRENGTH_ENABLE */
  573. #endif /* VIBRATO_ENABLE */
  574. // Polyphony functions
  575. void set_polyphony_rate(float rate) { polyphony_rate = rate; }
  576. void enable_polyphony() { polyphony_rate = 5; }
  577. void disable_polyphony() { polyphony_rate = 0; }
  578. void increase_polyphony_rate(float change) { polyphony_rate *= change; }
  579. void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
  580. // Timbre function
  581. void set_timbre(float timbre) { note_timbre = timbre; }
  582. // Tempo functions
  583. void set_tempo(uint8_t tempo) { note_tempo = tempo; }
  584. void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
  585. void increase_tempo(uint8_t tempo_change) {
  586. if (note_tempo - tempo_change < 10) {
  587. note_tempo = 10;
  588. } else {
  589. note_tempo -= tempo_change;
  590. }
  591. }