You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

702 lines
22 KiB

  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 nz = 0;
  194. static void end_cb1(DACDriver *dacp) {
  195. (void)dacp;
  196. nz++;
  197. if ((nz % 1000) == 0) {
  198. // palTogglePad(GPIOD, GPIOD_LED3);
  199. }
  200. }
  201. /*
  202. * DAC error callback.
  203. */
  204. static void error_cb1(DACDriver *dacp, dacerror_t err) {
  205. (void)dacp;
  206. (void)err;
  207. chSysHalt("DAC failure");
  208. }
  209. static const DACConfig dac1cfg1 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
  210. static const DACConversionGroup dacgrpcfg1 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
  211. static const DACConfig dac1cfg2 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
  212. static const DACConversionGroup dacgrpcfg2 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
  213. void audio_init() {
  214. if (audio_initialized) {
  215. return;
  216. }
  217. // Check EEPROM
  218. #ifdef EEPROM_ENABLE
  219. if (!eeconfig_is_enabled()) {
  220. eeconfig_init();
  221. }
  222. audio_config.raw = eeconfig_read_audio();
  223. #else // ARM EEPROM
  224. audio_config.enable = true;
  225. # ifdef AUDIO_CLICKY_ON
  226. audio_config.clicky_enable = true;
  227. # endif
  228. #endif // ARM EEPROM
  229. /*
  230. * Starting DAC1 driver, setting up the output pin as analog as suggested
  231. * by the Reference Manual.
  232. */
  233. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  234. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  235. dacStart(&DACD1, &dac1cfg1);
  236. dacStart(&DACD2, &dac1cfg2);
  237. /*
  238. * Starting GPT6/7 driver, it is used for triggering the DAC.
  239. */
  240. START_CHANNEL_1();
  241. START_CHANNEL_2();
  242. /*
  243. * Starting a continuous conversion.
  244. */
  245. dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
  246. dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
  247. audio_initialized = true;
  248. if (audio_config.enable) {
  249. PLAY_SONG(startup_song);
  250. } else {
  251. stop_all_notes();
  252. }
  253. }
  254. void stop_all_notes() {
  255. dprintf("audio stop all notes");
  256. if (!audio_initialized) {
  257. audio_init();
  258. }
  259. voices = 0;
  260. gptStopTimer(&GPTD6);
  261. gptStopTimer(&GPTD7);
  262. gptStopTimer(&GPTD8);
  263. playing_notes = false;
  264. playing_note = false;
  265. frequency = 0;
  266. frequency_alt = 0;
  267. volume = 0;
  268. for (uint8_t i = 0; i < 8; i++) {
  269. frequencies[i] = 0;
  270. volumes[i] = 0;
  271. }
  272. }
  273. void stop_note(float freq) {
  274. dprintf("audio stop note freq=%d", (int)freq);
  275. if (playing_note) {
  276. if (!audio_initialized) {
  277. audio_init();
  278. }
  279. for (int i = 7; i >= 0; i--) {
  280. if (frequencies[i] == freq) {
  281. frequencies[i] = 0;
  282. volumes[i] = 0;
  283. for (int j = i; (j < 7); j++) {
  284. frequencies[j] = frequencies[j + 1];
  285. frequencies[j + 1] = 0;
  286. volumes[j] = volumes[j + 1];
  287. volumes[j + 1] = 0;
  288. }
  289. break;
  290. }
  291. }
  292. voices--;
  293. if (voices < 0) {
  294. voices = 0;
  295. }
  296. if (voice_place >= voices) {
  297. voice_place = 0;
  298. }
  299. if (voices == 0) {
  300. STOP_CHANNEL_1();
  301. STOP_CHANNEL_2();
  302. gptStopTimer(&GPTD8);
  303. frequency = 0;
  304. frequency_alt = 0;
  305. volume = 0;
  306. playing_note = false;
  307. }
  308. }
  309. }
  310. #ifdef VIBRATO_ENABLE
  311. float mod(float a, int b) {
  312. float r = fmod(a, b);
  313. return r < 0 ? r + b : r;
  314. }
  315. float vibrato(float average_freq) {
  316. # ifdef VIBRATO_STRENGTH_ENABLE
  317. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  318. # else
  319. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  320. # endif
  321. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
  322. return vibrated_freq;
  323. }
  324. #endif
  325. static void gpt_cb8(GPTDriver *gptp) {
  326. float freq;
  327. if (playing_note) {
  328. if (voices > 0) {
  329. float freq_alt = 0;
  330. if (voices > 1) {
  331. if (polyphony_rate == 0) {
  332. if (glissando) {
  333. if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440 / frequencies[voices - 2] / 12 / 2)) {
  334. frequency_alt = frequency_alt * pow(2, 440 / frequency_alt / 12 / 2);
  335. } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440 / frequencies[voices - 2] / 12 / 2)) {
  336. frequency_alt = frequency_alt * pow(2, -440 / frequency_alt / 12 / 2);
  337. } else {
  338. frequency_alt = frequencies[voices - 2];
  339. }
  340. } else {
  341. frequency_alt = frequencies[voices - 2];
  342. }
  343. #ifdef VIBRATO_ENABLE
  344. if (vibrato_strength > 0) {
  345. freq_alt = vibrato(frequency_alt);
  346. } else {
  347. freq_alt = frequency_alt;
  348. }
  349. #else
  350. freq_alt = frequency_alt;
  351. #endif
  352. }
  353. if (envelope_index < 65535) {
  354. envelope_index++;
  355. }
  356. freq_alt = voice_envelope(freq_alt);
  357. if (freq_alt < 30.517578125) {
  358. freq_alt = 30.52;
  359. }
  360. if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
  361. UPDATE_CHANNEL_2_FREQ(freq_alt);
  362. } else {
  363. RESTART_CHANNEL_2();
  364. }
  365. // note_timbre;
  366. }
  367. if (polyphony_rate > 0) {
  368. if (voices > 1) {
  369. voice_place %= voices;
  370. if (place++ > (frequencies[voice_place] / polyphony_rate)) {
  371. voice_place = (voice_place + 1) % voices;
  372. place = 0.0;
  373. }
  374. }
  375. #ifdef VIBRATO_ENABLE
  376. if (vibrato_strength > 0) {
  377. freq = vibrato(frequencies[voice_place]);
  378. } else {
  379. freq = frequencies[voice_place];
  380. }
  381. #else
  382. freq = frequencies[voice_place];
  383. #endif
  384. } else {
  385. if (glissando) {
  386. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
  387. frequency = frequency * pow(2, 440 / frequency / 12 / 2);
  388. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
  389. frequency = frequency * pow(2, -440 / frequency / 12 / 2);
  390. } else {
  391. frequency = frequencies[voices - 1];
  392. }
  393. } else {
  394. frequency = frequencies[voices - 1];
  395. }
  396. #ifdef VIBRATO_ENABLE
  397. if (vibrato_strength > 0) {
  398. freq = vibrato(frequency);
  399. } else {
  400. freq = frequency;
  401. }
  402. #else
  403. freq = frequency;
  404. #endif
  405. }
  406. if (envelope_index < 65535) {
  407. envelope_index++;
  408. }
  409. freq = voice_envelope(freq);
  410. if (freq < 30.517578125) {
  411. freq = 30.52;
  412. }
  413. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  414. UPDATE_CHANNEL_1_FREQ(freq);
  415. } else {
  416. RESTART_CHANNEL_1();
  417. }
  418. // note_timbre;
  419. }
  420. }
  421. if (playing_notes) {
  422. if (note_frequency > 0) {
  423. #ifdef VIBRATO_ENABLE
  424. if (vibrato_strength > 0) {
  425. freq = vibrato(note_frequency);
  426. } else {
  427. freq = note_frequency;
  428. }
  429. #else
  430. freq = note_frequency;
  431. #endif
  432. if (envelope_index < 65535) {
  433. envelope_index++;
  434. }
  435. freq = voice_envelope(freq);
  436. if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
  437. UPDATE_CHANNEL_1_FREQ(freq);
  438. UPDATE_CHANNEL_2_FREQ(freq);
  439. }
  440. // note_timbre;
  441. } else {
  442. // gptStopTimer(&GPTD6);
  443. // gptStopTimer(&GPTD7);
  444. }
  445. note_position++;
  446. bool end_of_note = false;
  447. if (GET_CHANNEL_1_FREQ > 0) {
  448. if (!note_resting)
  449. end_of_note = (note_position >= (note_length * 8 - 1));
  450. else
  451. end_of_note = (note_position >= (note_length * 8));
  452. } else {
  453. end_of_note = (note_position >= (note_length * 8));
  454. }
  455. if (end_of_note) {
  456. current_note++;
  457. if (current_note >= notes_count) {
  458. if (notes_repeat) {
  459. current_note = 0;
  460. } else {
  461. STOP_CHANNEL_1();
  462. STOP_CHANNEL_2();
  463. // gptStopTimer(&GPTD8);
  464. playing_notes = false;
  465. return;
  466. }
  467. }
  468. if (!note_resting) {
  469. note_resting = true;
  470. current_note--;
  471. if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
  472. note_frequency = 0;
  473. note_length = 1;
  474. } else {
  475. note_frequency = (*notes_pointer)[current_note][0];
  476. note_length = 1;
  477. }
  478. } else {
  479. note_resting = false;
  480. envelope_index = 0;
  481. note_frequency = (*notes_pointer)[current_note][0];
  482. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  483. }
  484. note_position = 0;
  485. }
  486. }
  487. if (!audio_config.enable) {
  488. playing_notes = false;
  489. playing_note = false;
  490. }
  491. }
  492. void play_note(float freq, int vol) {
  493. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  494. if (!audio_initialized) {
  495. audio_init();
  496. }
  497. if (audio_config.enable && voices < 8) {
  498. // Cancel notes if notes are playing
  499. if (playing_notes) {
  500. stop_all_notes();
  501. }
  502. playing_note = true;
  503. envelope_index = 0;
  504. if (freq > 0) {
  505. frequencies[voices] = freq;
  506. volumes[voices] = vol;
  507. voices++;
  508. }
  509. gptStart(&GPTD8, &gpt8cfg1);
  510. gptStartContinuous(&GPTD8, 2U);
  511. RESTART_CHANNEL_1();
  512. RESTART_CHANNEL_2();
  513. }
  514. }
  515. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  516. if (!audio_initialized) {
  517. audio_init();
  518. }
  519. if (audio_config.enable) {
  520. // Cancel note if a note is playing
  521. if (playing_note) {
  522. stop_all_notes();
  523. }
  524. playing_notes = true;
  525. notes_pointer = np;
  526. notes_count = n_count;
  527. notes_repeat = n_repeat;
  528. place = 0;
  529. current_note = 0;
  530. note_frequency = (*notes_pointer)[current_note][0];
  531. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  532. note_position = 0;
  533. gptStart(&GPTD8, &gpt8cfg1);
  534. gptStartContinuous(&GPTD8, 2U);
  535. RESTART_CHANNEL_1();
  536. RESTART_CHANNEL_2();
  537. }
  538. }
  539. bool is_playing_notes(void) { return playing_notes; }
  540. bool is_audio_on(void) { return (audio_config.enable != 0); }
  541. void audio_toggle(void) {
  542. audio_config.enable ^= 1;
  543. eeconfig_update_audio(audio_config.raw);
  544. if (audio_config.enable) {
  545. audio_on_user();
  546. }
  547. }
  548. void audio_on(void) {
  549. audio_config.enable = 1;
  550. eeconfig_update_audio(audio_config.raw);
  551. audio_on_user();
  552. }
  553. void audio_off(void) {
  554. stop_all_notes();
  555. audio_config.enable = 0;
  556. eeconfig_update_audio(audio_config.raw);
  557. }
  558. #ifdef VIBRATO_ENABLE
  559. // Vibrato rate functions
  560. void set_vibrato_rate(float rate) { vibrato_rate = rate; }
  561. void increase_vibrato_rate(float change) { vibrato_rate *= change; }
  562. void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
  563. # ifdef VIBRATO_STRENGTH_ENABLE
  564. void set_vibrato_strength(float strength) { vibrato_strength = strength; }
  565. void increase_vibrato_strength(float change) { vibrato_strength *= change; }
  566. void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
  567. # endif /* VIBRATO_STRENGTH_ENABLE */
  568. #endif /* VIBRATO_ENABLE */
  569. // Polyphony functions
  570. void set_polyphony_rate(float rate) { polyphony_rate = rate; }
  571. void enable_polyphony() { polyphony_rate = 5; }
  572. void disable_polyphony() { polyphony_rate = 0; }
  573. void increase_polyphony_rate(float change) { polyphony_rate *= change; }
  574. void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
  575. // Timbre function
  576. void set_timbre(float timbre) { note_timbre = timbre; }
  577. // Tempo functions
  578. void set_tempo(uint8_t tempo) { note_tempo = tempo; }
  579. void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
  580. void increase_tempo(uint8_t tempo_change) {
  581. if (note_tempo - tempo_change < 10) {
  582. note_tempo = 10;
  583. } else {
  584. note_tempo -= tempo_change;
  585. }
  586. }