audio.c 23 KB

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