audio_arm.c 15 KB

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