audio.c 23 KB

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