audio.c 23 KB

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