audio_chibios.c 23 KB

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