audio.c 25 KB

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