audio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /* Copyright 2016-2020 Jack Humbert
  2. * Copyright 2020 JohSchneider
  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 "eeconfig.h"
  18. #include "timer.h"
  19. #include "wait.h"
  20. /* audio system:
  21. *
  22. * audio.[ch] takes care of all overall state, tracking the actively playing
  23. * notes/tones; the notes a SONG consists of;
  24. * ...
  25. * = everything audio-related that is platform agnostic
  26. *
  27. * driver_[avr|chibios]_[dac|pwm] take care of the lower hardware dependent parts,
  28. * specific to each platform and the used subsystem/driver to drive
  29. * the output pins/channels with the calculated frequencies for each
  30. * active tone
  31. * as part of this, the driver has to trigger regular state updates by
  32. * calling 'audio_update_state' through some sort of timer - be it a
  33. * dedicated one or piggybacking on for example the timer used to
  34. * generate a pwm signal/clock.
  35. *
  36. *
  37. * A Note on terminology:
  38. * tone, pitch and frequency are used somewhat interchangeably, in a strict Wikipedia-sense:
  39. * "(Musical) tone, a sound characterized by its duration, pitch (=frequency),
  40. * intensity (=volume), and timbre"
  41. * - intensity/volume is currently not handled at all, although the 'dac_additive' driver could do so
  42. * - timbre is handled globally (TODO: only used with the pwm drivers at the moment)
  43. *
  44. * in musical_note.h a 'note' is the combination of a pitch and a duration
  45. * these are used to create SONG arrays; during playback their frequencies
  46. * are handled as single successive tones, while the durations are
  47. * kept track of in 'audio_update_state'
  48. *
  49. * 'voice' as it is used here, equates to a sort of instrument with its own
  50. * characteristics sound and effects
  51. * the audio system as-is deals only with (possibly multiple) tones of one
  52. * instrument/voice at a time (think: chords). since the number of tones that
  53. * can be reproduced depends on the hardware/driver in use: pwm can only
  54. * reproduce one tone per output/speaker; DACs can reproduce/mix multiple
  55. * when doing additive synthesis.
  56. *
  57. * 'duration' can either be in the beats-per-minute related unit found in
  58. * musical_notes.h, OR in ms; keyboards create SONGs with the former, while
  59. * the internal state of the audio system does its calculations with the later - ms
  60. */
  61. #ifndef AUDIO_TONE_STACKSIZE
  62. # define AUDIO_TONE_STACKSIZE 8
  63. #endif
  64. uint8_t active_tones = 0; // number of tones pushed onto the stack by audio_play_tone - might be more than the hardware is able to reproduce at any single time
  65. musical_tone_t tones[AUDIO_TONE_STACKSIZE]; // stack of currently active tones
  66. bool playing_melody = false; // playing a SONG?
  67. bool playing_note = false; // or (possibly multiple simultaneous) tones
  68. bool state_changed = false; // global flag, which is set if anything changes with the active_tones
  69. // melody/SONG related state variables
  70. float (*notes_pointer)[][2]; // SONG, an array of MUSICAL_NOTEs
  71. uint16_t notes_count; // length of the notes_pointer array
  72. bool notes_repeat; // PLAY_SONG or PLAY_LOOP?
  73. uint16_t melody_current_note_duration = 0; // duration of the currently playing note from the active melody, in ms
  74. uint8_t note_tempo = TEMPO_DEFAULT; // beats-per-minute
  75. uint16_t current_note = 0; // index into the array at notes_pointer
  76. bool note_resting = false; // if a short pause was introduced between two notes with the same frequency while playing a melody
  77. uint16_t last_timestamp = 0;
  78. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  79. # ifndef AUDIO_MAX_SIMULTANEOUS_TONES
  80. # define AUDIO_MAX_SIMULTANEOUS_TONES 3
  81. # endif
  82. uint16_t tone_multiplexing_rate = AUDIO_TONE_MULTIPLEXING_RATE_DEFAULT;
  83. uint8_t tone_multiplexing_index_shift = 0; // offset used on active-tone array access
  84. #endif
  85. // provided and used by voices.c
  86. extern uint8_t note_timbre;
  87. extern bool glissando;
  88. extern bool vibrato;
  89. extern uint16_t voices_timer;
  90. #ifndef STARTUP_SONG
  91. # define STARTUP_SONG SONG(STARTUP_SOUND)
  92. #endif
  93. #ifndef AUDIO_ON_SONG
  94. # define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND)
  95. #endif
  96. #ifndef AUDIO_OFF_SONG
  97. # define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND)
  98. #endif
  99. float startup_song[][2] = STARTUP_SONG;
  100. float audio_on_song[][2] = AUDIO_ON_SONG;
  101. float audio_off_song[][2] = AUDIO_OFF_SONG;
  102. static bool audio_initialized = false;
  103. static bool audio_driver_stopped = true;
  104. audio_config_t audio_config;
  105. void audio_init() {
  106. if (audio_initialized) {
  107. return;
  108. }
  109. // Check EEPROM
  110. #ifdef EEPROM_ENABLE
  111. if (!eeconfig_is_enabled()) {
  112. eeconfig_init();
  113. }
  114. audio_config.raw = eeconfig_read_audio();
  115. #else // EEPROM settings
  116. audio_config.enable = true;
  117. # ifdef AUDIO_CLICKY_ON
  118. audio_config.clicky_enable = true;
  119. # endif
  120. #endif // EEPROM settings
  121. for (uint8_t i = 0; i < AUDIO_TONE_STACKSIZE; i++) {
  122. tones[i] = (musical_tone_t){.time_started = 0, .pitch = -1.0f, .duration = 0};
  123. }
  124. if (!audio_initialized) {
  125. audio_driver_initialize();
  126. audio_initialized = true;
  127. }
  128. stop_all_notes();
  129. }
  130. void audio_startup(void) {
  131. if (audio_config.enable) {
  132. PLAY_SONG(startup_song);
  133. }
  134. last_timestamp = timer_read();
  135. }
  136. void audio_toggle(void) {
  137. if (audio_config.enable) {
  138. stop_all_notes();
  139. }
  140. audio_config.enable ^= 1;
  141. eeconfig_update_audio(audio_config.raw);
  142. if (audio_config.enable) {
  143. audio_on_user();
  144. }
  145. }
  146. void audio_on(void) {
  147. audio_config.enable = 1;
  148. eeconfig_update_audio(audio_config.raw);
  149. audio_on_user();
  150. PLAY_SONG(audio_on_song);
  151. }
  152. void audio_off(void) {
  153. PLAY_SONG(audio_off_song);
  154. wait_ms(100);
  155. audio_stop_all();
  156. audio_config.enable = 0;
  157. eeconfig_update_audio(audio_config.raw);
  158. }
  159. bool audio_is_on(void) { return (audio_config.enable != 0); }
  160. void audio_stop_all() {
  161. if (audio_driver_stopped) {
  162. return;
  163. }
  164. active_tones = 0;
  165. audio_driver_stop();
  166. playing_melody = false;
  167. playing_note = false;
  168. melody_current_note_duration = 0;
  169. for (uint8_t i = 0; i < AUDIO_TONE_STACKSIZE; i++) {
  170. tones[i] = (musical_tone_t){.time_started = 0, .pitch = -1.0f, .duration = 0};
  171. }
  172. audio_driver_stopped = true;
  173. }
  174. void audio_stop_tone(float pitch) {
  175. if (pitch < 0.0f) {
  176. pitch = -1 * pitch;
  177. }
  178. if (playing_note) {
  179. if (!audio_initialized) {
  180. audio_init();
  181. }
  182. bool found = false;
  183. for (int i = AUDIO_TONE_STACKSIZE - 1; i >= 0; i--) {
  184. found = (tones[i].pitch == pitch);
  185. if (found) {
  186. tones[i] = (musical_tone_t){.time_started = 0, .pitch = -1.0f, .duration = 0};
  187. for (int j = i; (j < AUDIO_TONE_STACKSIZE - 1); j++) {
  188. tones[j] = tones[j + 1];
  189. tones[j + 1] = (musical_tone_t){.time_started = 0, .pitch = -1.0f, .duration = 0};
  190. }
  191. break;
  192. }
  193. }
  194. if (!found) {
  195. return;
  196. }
  197. state_changed = true;
  198. active_tones--;
  199. if (active_tones < 0) active_tones = 0;
  200. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  201. if (tone_multiplexing_index_shift >= active_tones) {
  202. tone_multiplexing_index_shift = 0;
  203. }
  204. #endif
  205. if (active_tones == 0) {
  206. audio_driver_stop();
  207. audio_driver_stopped = true;
  208. playing_note = false;
  209. }
  210. }
  211. }
  212. void audio_play_note(float pitch, uint16_t duration) {
  213. if (!audio_config.enable) {
  214. return;
  215. }
  216. if (!audio_initialized) {
  217. audio_init();
  218. }
  219. if (pitch < 0.0f) {
  220. pitch = -1 * pitch;
  221. }
  222. // round-robin: shifting out old tones, keeping only unique ones
  223. // if the new frequency is already amongst the active tones, shift it to the top of the stack
  224. bool found = false;
  225. for (int i = active_tones - 1; i >= 0; i--) {
  226. found = (tones[i].pitch == pitch);
  227. if (found) {
  228. for (int j = i; (j < active_tones - 1); j++) {
  229. tones[j] = tones[j + 1];
  230. tones[j + 1] = (musical_tone_t){.time_started = timer_read(), .pitch = pitch, .duration = duration};
  231. }
  232. return; // since this frequency played already, the hardware was already started
  233. }
  234. }
  235. // frequency/tone is actually new, so we put it on the top of the stack
  236. active_tones++;
  237. if (active_tones > AUDIO_TONE_STACKSIZE) {
  238. active_tones = AUDIO_TONE_STACKSIZE;
  239. // shift out the oldest tone to make room
  240. for (int i = 0; i < active_tones - 1; i++) {
  241. tones[i] = tones[i + 1];
  242. }
  243. }
  244. state_changed = true;
  245. playing_note = true;
  246. tones[active_tones - 1] = (musical_tone_t){.time_started = timer_read(), .pitch = pitch, .duration = duration};
  247. // TODO: needs to be handled per note/tone -> use its timestamp instead?
  248. voices_timer = timer_read(); // reset to zero, for the effects added by voices.c
  249. if (audio_driver_stopped) {
  250. audio_driver_start();
  251. audio_driver_stopped = false;
  252. }
  253. }
  254. void audio_play_tone(float pitch) { audio_play_note(pitch, 0xffff); }
  255. void audio_play_melody(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  256. if (!audio_config.enable) {
  257. audio_stop_all();
  258. return;
  259. }
  260. if (!audio_initialized) {
  261. audio_init();
  262. }
  263. // Cancel note if a note is playing
  264. if (playing_note) audio_stop_all();
  265. playing_melody = true;
  266. note_resting = false;
  267. notes_pointer = np;
  268. notes_count = n_count;
  269. notes_repeat = n_repeat;
  270. current_note = 0; // note in the melody-array/list at note_pointer
  271. // start first note manually, which also starts the audio_driver
  272. // all following/remaining notes are played by 'audio_update_state'
  273. audio_play_note((*notes_pointer)[current_note][0], audio_duration_to_ms((*notes_pointer)[current_note][1]));
  274. last_timestamp = timer_read();
  275. melody_current_note_duration = audio_duration_to_ms((*notes_pointer)[current_note][1]);
  276. }
  277. float click[2][2];
  278. void audio_play_click(uint16_t delay, float pitch, uint16_t duration) {
  279. uint16_t duration_tone = audio_ms_to_duration(duration);
  280. uint16_t duration_delay = audio_ms_to_duration(delay);
  281. if (delay <= 0.0f) {
  282. click[0][0] = pitch;
  283. click[0][1] = duration_tone;
  284. click[1][0] = 0.0f;
  285. click[1][1] = 0.0f;
  286. audio_play_melody(&click, 1, false);
  287. } else {
  288. // first note is a rest/pause
  289. click[0][0] = 0.0f;
  290. click[0][1] = duration_delay;
  291. // second note is the actual click
  292. click[1][0] = pitch;
  293. click[1][1] = duration_tone;
  294. audio_play_melody(&click, 2, false);
  295. }
  296. }
  297. bool audio_is_playing_note(void) { return playing_note; }
  298. bool audio_is_playing_melody(void) { return playing_melody; }
  299. uint8_t audio_get_number_of_active_tones(void) { return active_tones; }
  300. float audio_get_frequency(uint8_t tone_index) {
  301. if (tone_index >= active_tones) {
  302. return 0.0f;
  303. }
  304. return tones[active_tones - tone_index - 1].pitch;
  305. }
  306. float audio_get_processed_frequency(uint8_t tone_index) {
  307. if (tone_index >= active_tones) {
  308. return 0.0f;
  309. }
  310. int8_t index = active_tones - tone_index - 1;
  311. // new tones are stacked on top (= appended at the end), so the most recent/current is MAX-1
  312. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  313. index = index - tone_multiplexing_index_shift;
  314. if (index < 0) // wrap around
  315. index += active_tones;
  316. #endif
  317. if (tones[index].pitch <= 0.0f) {
  318. return 0.0f;
  319. }
  320. return voice_envelope(tones[index].pitch);
  321. }
  322. bool audio_update_state(void) {
  323. if (!playing_note && !playing_melody) {
  324. return false;
  325. }
  326. bool goto_next_note = false;
  327. uint16_t current_time = timer_read();
  328. if (playing_melody) {
  329. goto_next_note = timer_elapsed(last_timestamp) >= melody_current_note_duration;
  330. if (goto_next_note) {
  331. uint16_t delta = timer_elapsed(last_timestamp) - melody_current_note_duration;
  332. last_timestamp = current_time;
  333. uint16_t previous_note = current_note;
  334. current_note++;
  335. voices_timer = timer_read(); // reset to zero, for the effects added by voices.c
  336. if (current_note >= notes_count) {
  337. if (notes_repeat) {
  338. current_note = 0;
  339. } else {
  340. audio_stop_all();
  341. return false;
  342. }
  343. }
  344. if (!note_resting && (*notes_pointer)[previous_note][0] == (*notes_pointer)[current_note][0]) {
  345. note_resting = true;
  346. // special handling for successive notes of the same frequency:
  347. // insert a short pause to separate them audibly
  348. audio_play_note(0.0f, audio_duration_to_ms(2));
  349. current_note = previous_note;
  350. melody_current_note_duration = audio_duration_to_ms(2);
  351. } else {
  352. note_resting = false;
  353. // TODO: handle glissando here (or remember previous and current tone)
  354. /* there would need to be a freq(here we are) -> freq(next note)
  355. * and do slide/glissando in between problem here is to know which
  356. * frequency on the stack relates to what other? e.g. a melody starts
  357. * tones in a sequence, and stops expiring one, so the most recently
  358. * stopped is the starting point for a glissando to the most recently started?
  359. * how to detect and preserve this relation?
  360. * and what about user input, chords, ...?
  361. */
  362. // '- delta': Skip forward in the next note's length if we've over shot
  363. // the last, so the overall length of the song is the same
  364. uint16_t duration = audio_duration_to_ms((*notes_pointer)[current_note][1]);
  365. // Skip forward past any completely missed notes
  366. while (delta > duration && current_note < notes_count - 1) {
  367. delta -= duration;
  368. current_note++;
  369. duration = audio_duration_to_ms((*notes_pointer)[current_note][1]);
  370. }
  371. if (delta < duration) {
  372. duration -= delta;
  373. } else {
  374. // Only way to get here is if it is the last note and
  375. // we have completely missed it. Play it for 1ms...
  376. duration = 1;
  377. }
  378. audio_play_note((*notes_pointer)[current_note][0], duration);
  379. melody_current_note_duration = duration;
  380. }
  381. }
  382. }
  383. if (playing_note) {
  384. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  385. tone_multiplexing_index_shift = (int)(current_time / tone_multiplexing_rate) % MIN(AUDIO_MAX_SIMULTANEOUS_TONES, active_tones);
  386. goto_next_note = true;
  387. #endif
  388. if (vibrato || glissando) {
  389. // force update on each cycle, since vibrato shifts the frequency slightly
  390. goto_next_note = true;
  391. }
  392. // housekeeping: stop notes that have no playtime left
  393. for (int i = 0; i < active_tones; i++) {
  394. if ((tones[i].duration != 0xffff) // indefinitely playing notes, started by 'audio_play_tone'
  395. && (tones[i].duration != 0) // 'uninitialized'
  396. ) {
  397. if (timer_elapsed(tones[i].time_started) >= tones[i].duration) {
  398. audio_stop_tone(tones[i].pitch); // also sets 'state_changed=true'
  399. }
  400. }
  401. }
  402. }
  403. // state-changes have a higher priority, always triggering the hardware to update
  404. if (state_changed) {
  405. state_changed = false;
  406. return true;
  407. }
  408. return goto_next_note;
  409. }
  410. // Tone-multiplexing functions
  411. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  412. void audio_set_tone_multiplexing_rate(uint16_t rate) { tone_multiplexing_rate = rate; }
  413. void audio_enable_tone_multiplexing(void) { tone_multiplexing_rate = AUDIO_TONE_MULTIPLEXING_RATE_DEFAULT; }
  414. void audio_disable_tone_multiplexing(void) { tone_multiplexing_rate = 0; }
  415. void audio_increase_tone_multiplexing_rate(uint16_t change) {
  416. if ((0xffff - change) > tone_multiplexing_rate) {
  417. tone_multiplexing_rate += change;
  418. }
  419. }
  420. void audio_decrease_tone_multiplexing_rate(uint16_t change) {
  421. if (change <= tone_multiplexing_rate) {
  422. tone_multiplexing_rate -= change;
  423. }
  424. }
  425. #endif
  426. // Tempo functions
  427. void audio_set_tempo(uint8_t tempo) {
  428. if (tempo < 10) note_tempo = 10;
  429. // else if (tempo > 250)
  430. // note_tempo = 250;
  431. else
  432. note_tempo = tempo;
  433. }
  434. void audio_increase_tempo(uint8_t tempo_change) {
  435. if (tempo_change > 255 - note_tempo)
  436. note_tempo = 255;
  437. else
  438. note_tempo += tempo_change;
  439. }
  440. void audio_decrease_tempo(uint8_t tempo_change) {
  441. if (tempo_change >= note_tempo - 10)
  442. note_tempo = 10;
  443. else
  444. note_tempo -= tempo_change;
  445. }
  446. // TODO in the int-math version are some bugs; songs sometimes abruptly end - maybe an issue with the timer/system-tick wrapping around?
  447. uint16_t audio_duration_to_ms(uint16_t duration_bpm) {
  448. #if defined(__AVR__)
  449. // doing int-math saves us some bytes in the overall firmware size, but the intermediate result is less accurate before being cast to/returned as uint
  450. return ((uint32_t)duration_bpm * 60 * 1000) / (64 * note_tempo);
  451. // NOTE: beware of uint16_t overflows when note_tempo is low and/or the duration is long
  452. #else
  453. return ((float)duration_bpm * 60) / (64 * note_tempo) * 1000;
  454. #endif
  455. }
  456. uint16_t audio_ms_to_duration(uint16_t duration_ms) {
  457. #if defined(__AVR__)
  458. return ((uint32_t)duration_ms * 64 * note_tempo) / 60 / 1000;
  459. #else
  460. return ((float)duration_ms * 64 * note_tempo) / 60 / 1000;
  461. #endif
  462. }