audio_arm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 <stdio.h>
  20. #include <string.h>
  21. #include "print.h"
  22. #include "keymap.h"
  23. #include "eeconfig.h"
  24. // -----------------------------------------------------------------------------
  25. int voices = 0;
  26. int voice_place = 0;
  27. float frequency = 0;
  28. float frequency_alt = 0;
  29. int volume = 0;
  30. long position = 0;
  31. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  32. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  33. bool sliding = false;
  34. float place = 0;
  35. uint8_t * sample;
  36. uint16_t sample_length = 0;
  37. bool playing_notes = false;
  38. bool playing_note = false;
  39. float note_frequency = 0;
  40. float note_length = 0;
  41. uint8_t note_tempo = TEMPO_DEFAULT;
  42. float note_timbre = TIMBRE_DEFAULT;
  43. uint16_t note_position = 0;
  44. float (* notes_pointer)[][2];
  45. uint16_t notes_count;
  46. bool notes_repeat;
  47. bool note_resting = false;
  48. uint8_t current_note = 0;
  49. uint8_t rest_counter = 0;
  50. #ifdef VIBRATO_ENABLE
  51. float vibrato_counter = 0;
  52. float vibrato_strength = .5;
  53. float vibrato_rate = 0.125;
  54. #endif
  55. float polyphony_rate = 0;
  56. static bool audio_initialized = false;
  57. audio_config_t audio_config;
  58. uint16_t envelope_index = 0;
  59. bool glissando = true;
  60. #ifndef STARTUP_SONG
  61. #define STARTUP_SONG SONG(STARTUP_SOUND)
  62. #endif
  63. float startup_song[][2] = STARTUP_SONG;
  64. static void gpt_cb6(GPTDriver *gptp);
  65. static void gpt_cb7(GPTDriver *gptp);
  66. static void gpt_cb8(GPTDriver *gptp);
  67. /*
  68. * GPT6 configuration.
  69. */
  70. GPTConfig gpt6cfg1 = {
  71. .frequency = 440,
  72. .callback = gpt_cb6,
  73. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  74. .dier = 0U
  75. };
  76. GPTConfig gpt7cfg1 = {
  77. .frequency = 440,
  78. .callback = gpt_cb7,
  79. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  80. .dier = 0U
  81. };
  82. GPTConfig gpt8cfg1 = {
  83. .frequency = 10,
  84. .callback = gpt_cb8,
  85. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  86. .dier = 0U
  87. };
  88. static void gpt_cb6(GPTDriver *gptp) {
  89. palTogglePad(GPIOA, 4);
  90. }
  91. static void gpt_cb7(GPTDriver *gptp) {
  92. palTogglePad(GPIOA, 5);
  93. }
  94. void audio_init()
  95. {
  96. if (audio_initialized)
  97. return;
  98. // Check EEPROM
  99. // if (!eeconfig_is_enabled())
  100. // {
  101. // eeconfig_init();
  102. // }
  103. // audio_config.raw = eeconfig_read_audio();
  104. audio_config.enable = true;
  105. palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
  106. palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
  107. audio_initialized = true;
  108. if (audio_config.enable) {
  109. PLAY_SONG(startup_song);
  110. }
  111. }
  112. void stop_all_notes()
  113. {
  114. dprintf("audio stop all notes");
  115. if (!audio_initialized) {
  116. audio_init();
  117. }
  118. voices = 0;
  119. gptStopTimer(&GPTD6);
  120. gptStopTimer(&GPTD7);
  121. gptStopTimer(&GPTD8);
  122. playing_notes = false;
  123. playing_note = false;
  124. frequency = 0;
  125. frequency_alt = 0;
  126. volume = 0;
  127. for (uint8_t i = 0; i < 8; i++)
  128. {
  129. frequencies[i] = 0;
  130. volumes[i] = 0;
  131. }
  132. }
  133. void stop_note(float freq)
  134. {
  135. dprintf("audio stop note freq=%d", (int)freq);
  136. if (playing_note) {
  137. if (!audio_initialized) {
  138. audio_init();
  139. }
  140. for (int i = 7; i >= 0; i--) {
  141. if (frequencies[i] == freq) {
  142. frequencies[i] = 0;
  143. volumes[i] = 0;
  144. for (int j = i; (j < 7); j++) {
  145. frequencies[j] = frequencies[j+1];
  146. frequencies[j+1] = 0;
  147. volumes[j] = volumes[j+1];
  148. volumes[j+1] = 0;
  149. }
  150. break;
  151. }
  152. }
  153. voices--;
  154. if (voices < 0)
  155. voices = 0;
  156. if (voice_place >= voices) {
  157. voice_place = 0;
  158. }
  159. if (voices == 0) {
  160. gptStopTimer(&GPTD6);
  161. gptStopTimer(&GPTD7);
  162. gptStopTimer(&GPTD8);
  163. frequency = 0;
  164. frequency_alt = 0;
  165. volume = 0;
  166. playing_note = false;
  167. }
  168. }
  169. }
  170. #ifdef VIBRATO_ENABLE
  171. float mod(float a, int b)
  172. {
  173. float r = fmod(a, b);
  174. return r < 0 ? r + b : r;
  175. }
  176. float vibrato(float average_freq) {
  177. #ifdef VIBRATO_STRENGTH_ENABLE
  178. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  179. #else
  180. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  181. #endif
  182. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  183. return vibrated_freq;
  184. }
  185. #endif
  186. static void restart_gpt6(void) {
  187. // gptStopTimer(&GPTD6);
  188. gptStart(&GPTD6, &gpt6cfg1);
  189. gptStartContinuous(&GPTD6, 2U);
  190. }
  191. static void restart_gpt7(void) {
  192. // gptStopTimer(&GPTD7);
  193. gptStart(&GPTD7, &gpt7cfg1);
  194. gptStartContinuous(&GPTD7, 2U);
  195. }
  196. static void gpt_cb8(GPTDriver *gptp) {
  197. float freq;
  198. if (playing_note) {
  199. if (voices > 0) {
  200. float freq_alt = 0;
  201. if (voices > 1) {
  202. if (polyphony_rate == 0) {
  203. if (glissando) {
  204. if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
  205. frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
  206. } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
  207. frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
  208. } else {
  209. frequency_alt = frequencies[voices - 2];
  210. }
  211. } else {
  212. frequency_alt = frequencies[voices - 2];
  213. }
  214. #ifdef VIBRATO_ENABLE
  215. if (vibrato_strength > 0) {
  216. freq_alt = vibrato(frequency_alt);
  217. } else {
  218. freq_alt = frequency_alt;
  219. }
  220. #else
  221. freq_alt = frequency_alt;
  222. #endif
  223. }
  224. if (envelope_index < 65535) {
  225. envelope_index++;
  226. }
  227. freq_alt = voice_envelope(freq_alt);
  228. if (freq_alt < 30.517578125) {
  229. freq_alt = 30.52;
  230. }
  231. if (gpt6cfg1.frequency != (uint16_t)freq_alt) {
  232. gpt6cfg1.frequency = freq_alt;
  233. restart_gpt6();
  234. }
  235. //note_timbre;
  236. } else {
  237. // gptStopTimer(&GPTD6);
  238. }
  239. if (polyphony_rate > 0) {
  240. if (voices > 1) {
  241. voice_place %= voices;
  242. if (place++ > (frequencies[voice_place] / polyphony_rate)) {
  243. voice_place = (voice_place + 1) % voices;
  244. place = 0.0;
  245. }
  246. }
  247. #ifdef VIBRATO_ENABLE
  248. if (vibrato_strength > 0) {
  249. freq = vibrato(frequencies[voice_place]);
  250. } else {
  251. freq = frequencies[voice_place];
  252. }
  253. #else
  254. freq = frequencies[voice_place];
  255. #endif
  256. } else {
  257. if (glissando) {
  258. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  259. frequency = frequency * pow(2, 440/frequency/12/2);
  260. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  261. frequency = frequency * pow(2, -440/frequency/12/2);
  262. } else {
  263. frequency = frequencies[voices - 1];
  264. }
  265. } else {
  266. frequency = frequencies[voices - 1];
  267. }
  268. #ifdef VIBRATO_ENABLE
  269. if (vibrato_strength > 0) {
  270. freq = vibrato(frequency);
  271. } else {
  272. freq = frequency;
  273. }
  274. #else
  275. freq = frequency;
  276. #endif
  277. }
  278. if (envelope_index < 65535) {
  279. envelope_index++;
  280. }
  281. freq = voice_envelope(freq);
  282. if (freq < 30.517578125) {
  283. freq = 30.52;
  284. }
  285. if (gpt7cfg1.frequency != (uint16_t)freq) {
  286. gpt7cfg1.frequency = freq;
  287. restart_gpt7();
  288. }
  289. //note_timbre;
  290. } else {
  291. // gptStopTimer(&GPTD7);
  292. }
  293. }
  294. if (playing_notes) {
  295. if (note_frequency > 0) {
  296. #ifdef VIBRATO_ENABLE
  297. if (vibrato_strength > 0) {
  298. freq = vibrato(note_frequency);
  299. } else {
  300. freq = note_frequency;
  301. }
  302. #else
  303. freq = note_frequency;
  304. #endif
  305. if (envelope_index < 65535) {
  306. envelope_index++;
  307. }
  308. freq = voice_envelope(freq);
  309. if (gpt6cfg1.frequency != (uint16_t)freq) {
  310. gpt6cfg1.frequency = freq;
  311. restart_gpt6();
  312. gpt7cfg1.frequency = freq;
  313. restart_gpt7();
  314. }
  315. //note_timbre;
  316. } else {
  317. // gptStopTimer(&GPTD6);
  318. // gptStopTimer(&GPTD7);
  319. }
  320. note_position++;
  321. bool end_of_note = false;
  322. if (gpt6cfg1.frequency > 0) {
  323. if (!note_resting)
  324. end_of_note = (note_position >= (note_length*16 - 1));
  325. else
  326. end_of_note = (note_position >= (note_length*16));
  327. } else {
  328. end_of_note = (note_position >= (note_length*16));
  329. }
  330. if (end_of_note) {
  331. current_note++;
  332. if (current_note >= notes_count) {
  333. if (notes_repeat) {
  334. current_note = 0;
  335. } else {
  336. gptStopTimer(&GPTD6);
  337. gptStopTimer(&GPTD7);
  338. // gptStopTimer(&GPTD8);
  339. playing_notes = false;
  340. return;
  341. }
  342. }
  343. if (!note_resting) {
  344. note_resting = true;
  345. current_note--;
  346. if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
  347. note_frequency = 0;
  348. note_length = 1;
  349. } else {
  350. note_frequency = (*notes_pointer)[current_note][0];
  351. note_length = 1;
  352. }
  353. } else {
  354. note_resting = false;
  355. envelope_index = 0;
  356. note_frequency = (*notes_pointer)[current_note][0];
  357. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  358. }
  359. note_position = 0;
  360. }
  361. }
  362. if (!audio_config.enable) {
  363. playing_notes = false;
  364. playing_note = false;
  365. }
  366. }
  367. void play_note(float freq, int vol) {
  368. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  369. if (!audio_initialized) {
  370. audio_init();
  371. }
  372. if (audio_config.enable && voices < 8) {
  373. // Cancel notes if notes are playing
  374. if (playing_notes)
  375. stop_all_notes();
  376. playing_note = true;
  377. envelope_index = 0;
  378. if (freq > 0) {
  379. frequencies[voices] = freq;
  380. volumes[voices] = vol;
  381. voices++;
  382. }
  383. gptStart(&GPTD8, &gpt8cfg1);
  384. gptStartContinuous(&GPTD8, 2U);
  385. }
  386. }
  387. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
  388. {
  389. if (!audio_initialized) {
  390. audio_init();
  391. }
  392. if (audio_config.enable) {
  393. // Cancel note if a note is playing
  394. if (playing_note)
  395. stop_all_notes();
  396. playing_notes = true;
  397. notes_pointer = np;
  398. notes_count = n_count;
  399. notes_repeat = n_repeat;
  400. place = 0;
  401. current_note = 0;
  402. note_frequency = (*notes_pointer)[current_note][0];
  403. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  404. note_position = 0;
  405. gptStart(&GPTD8, &gpt8cfg1);
  406. gptStartContinuous(&GPTD8, 2U);
  407. restart_gpt6();
  408. restart_gpt7();
  409. }
  410. }
  411. bool is_playing_notes(void) {
  412. return playing_notes;
  413. }
  414. bool is_audio_on(void) {
  415. return (audio_config.enable != 0);
  416. }
  417. void audio_toggle(void) {
  418. audio_config.enable ^= 1;
  419. eeconfig_update_audio(audio_config.raw);
  420. if (audio_config.enable)
  421. audio_on_user();
  422. }
  423. void audio_on(void) {
  424. audio_config.enable = 1;
  425. eeconfig_update_audio(audio_config.raw);
  426. audio_on_user();
  427. }
  428. void audio_off(void) {
  429. audio_config.enable = 0;
  430. eeconfig_update_audio(audio_config.raw);
  431. }
  432. #ifdef VIBRATO_ENABLE
  433. // Vibrato rate functions
  434. void set_vibrato_rate(float rate) {
  435. vibrato_rate = rate;
  436. }
  437. void increase_vibrato_rate(float change) {
  438. vibrato_rate *= change;
  439. }
  440. void decrease_vibrato_rate(float change) {
  441. vibrato_rate /= change;
  442. }
  443. #ifdef VIBRATO_STRENGTH_ENABLE
  444. void set_vibrato_strength(float strength) {
  445. vibrato_strength = strength;
  446. }
  447. void increase_vibrato_strength(float change) {
  448. vibrato_strength *= change;
  449. }
  450. void decrease_vibrato_strength(float change) {
  451. vibrato_strength /= change;
  452. }
  453. #endif /* VIBRATO_STRENGTH_ENABLE */
  454. #endif /* VIBRATO_ENABLE */
  455. // Polyphony functions
  456. void set_polyphony_rate(float rate) {
  457. polyphony_rate = rate;
  458. }
  459. void enable_polyphony() {
  460. polyphony_rate = 5;
  461. }
  462. void disable_polyphony() {
  463. polyphony_rate = 0;
  464. }
  465. void increase_polyphony_rate(float change) {
  466. polyphony_rate *= change;
  467. }
  468. void decrease_polyphony_rate(float change) {
  469. polyphony_rate /= change;
  470. }
  471. // Timbre function
  472. void set_timbre(float timbre) {
  473. note_timbre = timbre;
  474. }
  475. // Tempo functions
  476. void set_tempo(uint8_t tempo) {
  477. note_tempo = tempo;
  478. }
  479. void decrease_tempo(uint8_t tempo_change) {
  480. note_tempo += tempo_change;
  481. }
  482. void increase_tempo(uint8_t tempo_change) {
  483. if (note_tempo - tempo_change < 10) {
  484. note_tempo = 10;
  485. } else {
  486. note_tempo -= tempo_change;
  487. }
  488. }