audio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 <stdio.h>
  17. #include <string.h>
  18. //#include <math.h>
  19. #include <avr/pgmspace.h>
  20. #include <avr/interrupt.h>
  21. #include <avr/io.h>
  22. #include "print.h"
  23. #include "audio.h"
  24. #include "keymap.h"
  25. #include "eeconfig.h"
  26. #define CPU_PRESCALER 8
  27. // -----------------------------------------------------------------------------
  28. // Timer Abstractions
  29. // -----------------------------------------------------------------------------
  30. // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
  31. // Turn on/off 3A interputs, stopping/enabling the ISR calls
  32. #ifdef C6_AUDIO
  33. #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  34. #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  35. #endif
  36. #ifdef B5_AUDIO
  37. #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
  38. #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
  39. #endif
  40. // TCCR3A: Timer/Counter #3 Control Register
  41. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  42. #ifdef C6_AUDIO
  43. #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  44. #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  45. #endif
  46. #ifdef B5_AUDIO
  47. #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
  48. #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
  49. #endif
  50. // Fast PWM Mode Controls
  51. #ifdef C6_AUDIO
  52. #define TIMER_3_PERIOD ICR3
  53. #define TIMER_3_DUTY_CYCLE OCR3A
  54. #endif
  55. #ifdef B5_AUDIO
  56. #define TIMER_1_PERIOD ICR1
  57. #define TIMER_1_DUTY_CYCLE OCR1A
  58. #endif
  59. // -----------------------------------------------------------------------------
  60. int voices = 0;
  61. int voice_place = 0;
  62. float frequency = 0;
  63. float frequency_alt = 0;
  64. int volume = 0;
  65. long position = 0;
  66. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  67. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  68. bool sliding = false;
  69. float place = 0;
  70. uint8_t * sample;
  71. uint16_t sample_length = 0;
  72. bool playing_notes = false;
  73. bool playing_note = false;
  74. float note_frequency = 0;
  75. float note_length = 0;
  76. uint8_t note_tempo = TEMPO_DEFAULT;
  77. float note_timbre = TIMBRE_DEFAULT;
  78. uint16_t note_position = 0;
  79. float (* notes_pointer)[][2];
  80. uint16_t notes_count;
  81. bool notes_repeat;
  82. float notes_rest;
  83. bool note_resting = false;
  84. uint8_t current_note = 0;
  85. uint8_t rest_counter = 0;
  86. #ifdef VIBRATO_ENABLE
  87. float vibrato_counter = 0;
  88. float vibrato_strength = .5;
  89. float vibrato_rate = 0.125;
  90. #endif
  91. float polyphony_rate = 0;
  92. static bool audio_initialized = false;
  93. audio_config_t audio_config;
  94. uint16_t envelope_index = 0;
  95. bool glissando = true;
  96. void audio_init()
  97. {
  98. // Check EEPROM
  99. if (!eeconfig_is_enabled())
  100. {
  101. eeconfig_init();
  102. }
  103. audio_config.raw = eeconfig_read_audio();
  104. // Set port PC6 (OC3A and /OC4A) as output
  105. #ifdef C6_AUDIO
  106. DDRC |= _BV(PORTC6);
  107. #else
  108. DDRC |= _BV(PORTC6);
  109. PORTC &= ~_BV(PORTC6);
  110. #endif
  111. #ifdef B5_AUDIO
  112. DDRB |= _BV(PORTB5);
  113. #else
  114. DDRB |= _BV(PORTB5);
  115. PORTB &= ~_BV(PORTB5);
  116. #endif
  117. #ifdef C6_AUDIO
  118. DISABLE_AUDIO_COUNTER_3_ISR;
  119. #endif
  120. #ifdef B5_AUDIO
  121. DISABLE_AUDIO_COUNTER_1_ISR;
  122. #endif
  123. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  124. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  125. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
  126. // Clock Select (CS3n) = 0b010 = Clock / 8
  127. #ifdef C6_AUDIO
  128. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  129. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  130. #endif
  131. #ifdef B5_AUDIO
  132. TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
  133. TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
  134. #endif
  135. audio_initialized = true;
  136. }
  137. void stop_all_notes()
  138. {
  139. dprintf("audio stop all notes");
  140. if (!audio_initialized) {
  141. audio_init();
  142. }
  143. voices = 0;
  144. #ifdef C6_AUDIO
  145. DISABLE_AUDIO_COUNTER_3_ISR;
  146. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  147. #endif
  148. #ifdef B5_AUDIO
  149. DISABLE_AUDIO_COUNTER_1_ISR;
  150. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  151. #endif
  152. playing_notes = false;
  153. playing_note = false;
  154. frequency = 0;
  155. frequency_alt = 0;
  156. volume = 0;
  157. for (uint8_t i = 0; i < 8; i++)
  158. {
  159. frequencies[i] = 0;
  160. volumes[i] = 0;
  161. }
  162. }
  163. void stop_note(float freq)
  164. {
  165. dprintf("audio stop note freq=%d", (int)freq);
  166. if (playing_note) {
  167. if (!audio_initialized) {
  168. audio_init();
  169. }
  170. for (int i = 7; i >= 0; i--) {
  171. if (frequencies[i] == freq) {
  172. frequencies[i] = 0;
  173. volumes[i] = 0;
  174. for (int j = i; (j < 7); j++) {
  175. frequencies[j] = frequencies[j+1];
  176. frequencies[j+1] = 0;
  177. volumes[j] = volumes[j+1];
  178. volumes[j+1] = 0;
  179. }
  180. break;
  181. }
  182. }
  183. voices--;
  184. if (voices < 0)
  185. voices = 0;
  186. if (voice_place >= voices) {
  187. voice_place = 0;
  188. }
  189. if (voices == 0) {
  190. #ifdef C6_AUDIO
  191. DISABLE_AUDIO_COUNTER_3_ISR;
  192. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  193. #endif
  194. #ifdef B5_AUDIO
  195. DISABLE_AUDIO_COUNTER_1_ISR;
  196. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  197. #endif
  198. frequency = 0;
  199. frequency_alt = 0;
  200. volume = 0;
  201. playing_note = false;
  202. }
  203. }
  204. }
  205. #ifdef VIBRATO_ENABLE
  206. float mod(float a, int b)
  207. {
  208. float r = fmod(a, b);
  209. return r < 0 ? r + b : r;
  210. }
  211. float vibrato(float average_freq) {
  212. #ifdef VIBRATO_STRENGTH_ENABLE
  213. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  214. #else
  215. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  216. #endif
  217. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  218. return vibrated_freq;
  219. }
  220. #endif
  221. #ifdef C6_AUDIO
  222. ISR(TIMER3_COMPA_vect)
  223. {
  224. float freq;
  225. if (playing_note) {
  226. if (voices > 0) {
  227. #ifdef B5_AUDIO
  228. float freq_alt = 0;
  229. if (voices > 1) {
  230. if (polyphony_rate == 0) {
  231. if (glissando) {
  232. if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
  233. frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
  234. } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
  235. frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
  236. } else {
  237. frequency_alt = frequencies[voices - 2];
  238. }
  239. } else {
  240. frequency_alt = frequencies[voices - 2];
  241. }
  242. #ifdef VIBRATO_ENABLE
  243. if (vibrato_strength > 0) {
  244. freq_alt = vibrato(frequency_alt);
  245. } else {
  246. freq_alt = frequency_alt;
  247. }
  248. #else
  249. freq_alt = frequency_alt;
  250. #endif
  251. }
  252. if (envelope_index < 65535) {
  253. envelope_index++;
  254. }
  255. freq_alt = voice_envelope(freq_alt);
  256. if (freq_alt < 30.517578125) {
  257. freq_alt = 30.52;
  258. }
  259. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
  260. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
  261. }
  262. #endif
  263. if (polyphony_rate > 0) {
  264. if (voices > 1) {
  265. voice_place %= voices;
  266. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  267. voice_place = (voice_place + 1) % voices;
  268. place = 0.0;
  269. }
  270. }
  271. #ifdef VIBRATO_ENABLE
  272. if (vibrato_strength > 0) {
  273. freq = vibrato(frequencies[voice_place]);
  274. } else {
  275. freq = frequencies[voice_place];
  276. }
  277. #else
  278. freq = frequencies[voice_place];
  279. #endif
  280. } else {
  281. if (glissando) {
  282. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  283. frequency = frequency * pow(2, 440/frequency/12/2);
  284. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  285. frequency = frequency * pow(2, -440/frequency/12/2);
  286. } else {
  287. frequency = frequencies[voices - 1];
  288. }
  289. } else {
  290. frequency = frequencies[voices - 1];
  291. }
  292. #ifdef VIBRATO_ENABLE
  293. if (vibrato_strength > 0) {
  294. freq = vibrato(frequency);
  295. } else {
  296. freq = frequency;
  297. }
  298. #else
  299. freq = frequency;
  300. #endif
  301. }
  302. if (envelope_index < 65535) {
  303. envelope_index++;
  304. }
  305. freq = voice_envelope(freq);
  306. if (freq < 30.517578125) {
  307. freq = 30.52;
  308. }
  309. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  310. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  311. }
  312. }
  313. if (playing_notes) {
  314. if (note_frequency > 0) {
  315. #ifdef VIBRATO_ENABLE
  316. if (vibrato_strength > 0) {
  317. freq = vibrato(note_frequency);
  318. } else {
  319. freq = note_frequency;
  320. }
  321. #else
  322. freq = note_frequency;
  323. #endif
  324. if (envelope_index < 65535) {
  325. envelope_index++;
  326. }
  327. freq = voice_envelope(freq);
  328. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  329. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  330. } else {
  331. TIMER_3_PERIOD = 0;
  332. TIMER_3_DUTY_CYCLE = 0;
  333. }
  334. note_position++;
  335. bool end_of_note = false;
  336. if (TIMER_3_PERIOD > 0) {
  337. end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
  338. } else {
  339. end_of_note = (note_position >= (note_length * 0x7FF));
  340. }
  341. if (end_of_note) {
  342. current_note++;
  343. if (current_note >= notes_count) {
  344. if (notes_repeat) {
  345. current_note = 0;
  346. } else {
  347. DISABLE_AUDIO_COUNTER_3_ISR;
  348. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  349. playing_notes = false;
  350. return;
  351. }
  352. }
  353. if (!note_resting && (notes_rest > 0)) {
  354. note_resting = true;
  355. note_frequency = 0;
  356. note_length = notes_rest;
  357. current_note--;
  358. } else {
  359. note_resting = false;
  360. envelope_index = 0;
  361. note_frequency = (*notes_pointer)[current_note][0];
  362. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  363. }
  364. note_position = 0;
  365. }
  366. }
  367. if (!audio_config.enable) {
  368. playing_notes = false;
  369. playing_note = false;
  370. }
  371. }
  372. #endif
  373. #ifdef B5_AUDIO
  374. ISR(TIMER1_COMPA_vect)
  375. {
  376. #if defined(B5_AUDIO) && !defined(C6_AUDIO)
  377. float freq = 0;
  378. if (playing_note) {
  379. if (voices > 0) {
  380. if (polyphony_rate > 0) {
  381. if (voices > 1) {
  382. voice_place %= voices;
  383. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  384. voice_place = (voice_place + 1) % voices;
  385. place = 0.0;
  386. }
  387. }
  388. #ifdef VIBRATO_ENABLE
  389. if (vibrato_strength > 0) {
  390. freq = vibrato(frequencies[voice_place]);
  391. } else {
  392. freq = frequencies[voice_place];
  393. }
  394. #else
  395. freq = frequencies[voice_place];
  396. #endif
  397. } else {
  398. if (glissando) {
  399. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  400. frequency = frequency * pow(2, 440/frequency/12/2);
  401. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  402. frequency = frequency * pow(2, -440/frequency/12/2);
  403. } else {
  404. frequency = frequencies[voices - 1];
  405. }
  406. } else {
  407. frequency = frequencies[voices - 1];
  408. }
  409. #ifdef VIBRATO_ENABLE
  410. if (vibrato_strength > 0) {
  411. freq = vibrato(frequency);
  412. } else {
  413. freq = frequency;
  414. }
  415. #else
  416. freq = frequency;
  417. #endif
  418. }
  419. if (envelope_index < 65535) {
  420. envelope_index++;
  421. }
  422. freq = voice_envelope(freq);
  423. if (freq < 30.517578125) {
  424. freq = 30.52;
  425. }
  426. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  427. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  428. }
  429. }
  430. if (playing_notes) {
  431. if (note_frequency > 0) {
  432. #ifdef VIBRATO_ENABLE
  433. if (vibrato_strength > 0) {
  434. freq = vibrato(note_frequency);
  435. } else {
  436. freq = note_frequency;
  437. }
  438. #else
  439. freq = note_frequency;
  440. #endif
  441. if (envelope_index < 65535) {
  442. envelope_index++;
  443. }
  444. freq = voice_envelope(freq);
  445. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  446. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  447. } else {
  448. TIMER_1_PERIOD = 0;
  449. TIMER_1_DUTY_CYCLE = 0;
  450. }
  451. note_position++;
  452. bool end_of_note = false;
  453. if (TIMER_1_PERIOD > 0) {
  454. end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF));
  455. } else {
  456. end_of_note = (note_position >= (note_length * 0x7FF));
  457. }
  458. if (end_of_note) {
  459. current_note++;
  460. if (current_note >= notes_count) {
  461. if (notes_repeat) {
  462. current_note = 0;
  463. } else {
  464. DISABLE_AUDIO_COUNTER_1_ISR;
  465. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  466. playing_notes = false;
  467. return;
  468. }
  469. }
  470. if (!note_resting && (notes_rest > 0)) {
  471. note_resting = true;
  472. note_frequency = 0;
  473. note_length = notes_rest;
  474. current_note--;
  475. } else {
  476. note_resting = false;
  477. envelope_index = 0;
  478. note_frequency = (*notes_pointer)[current_note][0];
  479. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  480. }
  481. note_position = 0;
  482. }
  483. }
  484. if (!audio_config.enable) {
  485. playing_notes = false;
  486. playing_note = false;
  487. }
  488. #endif
  489. }
  490. #endif
  491. void play_note(float freq, int vol) {
  492. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  493. if (!audio_initialized) {
  494. audio_init();
  495. }
  496. if (audio_config.enable && voices < 8) {
  497. #ifdef C6_AUDIO
  498. DISABLE_AUDIO_COUNTER_3_ISR;
  499. #endif
  500. #ifdef B5_AUDIO
  501. DISABLE_AUDIO_COUNTER_1_ISR;
  502. #endif
  503. // Cancel notes if notes are playing
  504. if (playing_notes)
  505. stop_all_notes();
  506. playing_note = true;
  507. envelope_index = 0;
  508. if (freq > 0) {
  509. frequencies[voices] = freq;
  510. volumes[voices] = vol;
  511. voices++;
  512. }
  513. #ifdef C6_AUDIO
  514. ENABLE_AUDIO_COUNTER_3_ISR;
  515. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  516. #endif
  517. #ifdef B5_AUDIO
  518. #ifdef C6_AUDIO
  519. if (voices > 1) {
  520. ENABLE_AUDIO_COUNTER_1_ISR;
  521. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  522. }
  523. #else
  524. ENABLE_AUDIO_COUNTER_1_ISR;
  525. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  526. #endif
  527. #endif
  528. }
  529. }
  530. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  531. {
  532. if (!audio_initialized) {
  533. audio_init();
  534. }
  535. if (audio_config.enable) {
  536. #ifdef C6_AUDIO
  537. DISABLE_AUDIO_COUNTER_3_ISR;
  538. #endif
  539. #ifdef B5_AUDIO
  540. DISABLE_AUDIO_COUNTER_1_ISR;
  541. #endif
  542. // Cancel note if a note is playing
  543. if (playing_note)
  544. stop_all_notes();
  545. playing_notes = true;
  546. notes_pointer = np;
  547. notes_count = n_count;
  548. notes_repeat = n_repeat;
  549. notes_rest = n_rest;
  550. place = 0;
  551. current_note = 0;
  552. note_frequency = (*notes_pointer)[current_note][0];
  553. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  554. note_position = 0;
  555. #ifdef C6_AUDIO
  556. ENABLE_AUDIO_COUNTER_3_ISR;
  557. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  558. #endif
  559. #ifdef B5_AUDIO
  560. #ifndef C6_AUDIO
  561. ENABLE_AUDIO_COUNTER_1_ISR;
  562. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  563. #endif
  564. #endif
  565. }
  566. }
  567. bool is_playing_notes(void) {
  568. return playing_notes;
  569. }
  570. bool is_audio_on(void) {
  571. return (audio_config.enable != 0);
  572. }
  573. void audio_toggle(void) {
  574. audio_config.enable ^= 1;
  575. eeconfig_update_audio(audio_config.raw);
  576. if (audio_config.enable)
  577. audio_on_user();
  578. }
  579. void audio_on(void) {
  580. audio_config.enable = 1;
  581. eeconfig_update_audio(audio_config.raw);
  582. audio_on_user();
  583. }
  584. void audio_off(void) {
  585. audio_config.enable = 0;
  586. eeconfig_update_audio(audio_config.raw);
  587. }
  588. #ifdef VIBRATO_ENABLE
  589. // Vibrato rate functions
  590. void set_vibrato_rate(float rate) {
  591. vibrato_rate = rate;
  592. }
  593. void increase_vibrato_rate(float change) {
  594. vibrato_rate *= change;
  595. }
  596. void decrease_vibrato_rate(float change) {
  597. vibrato_rate /= change;
  598. }
  599. #ifdef VIBRATO_STRENGTH_ENABLE
  600. void set_vibrato_strength(float strength) {
  601. vibrato_strength = strength;
  602. }
  603. void increase_vibrato_strength(float change) {
  604. vibrato_strength *= change;
  605. }
  606. void decrease_vibrato_strength(float change) {
  607. vibrato_strength /= change;
  608. }
  609. #endif /* VIBRATO_STRENGTH_ENABLE */
  610. #endif /* VIBRATO_ENABLE */
  611. // Polyphony functions
  612. void set_polyphony_rate(float rate) {
  613. polyphony_rate = rate;
  614. }
  615. void enable_polyphony() {
  616. polyphony_rate = 5;
  617. }
  618. void disable_polyphony() {
  619. polyphony_rate = 0;
  620. }
  621. void increase_polyphony_rate(float change) {
  622. polyphony_rate *= change;
  623. }
  624. void decrease_polyphony_rate(float change) {
  625. polyphony_rate /= change;
  626. }
  627. // Timbre function
  628. void set_timbre(float timbre) {
  629. note_timbre = timbre;
  630. }
  631. // Tempo functions
  632. void set_tempo(uint8_t tempo) {
  633. note_tempo = tempo;
  634. }
  635. void decrease_tempo(uint8_t tempo_change) {
  636. note_tempo += tempo_change;
  637. }
  638. void increase_tempo(uint8_t tempo_change) {
  639. if (note_tempo - tempo_change < 10) {
  640. note_tempo = 10;
  641. } else {
  642. note_tempo -= tempo_change;
  643. }
  644. }