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.

808 lines
22 KiB

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