2
0

audio.c 25 KB

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