audio.c 22 KB

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