audio_avr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. // Currently we support timers 1 and 3 used at the sime time, channels A-C,
  34. // pins PB5, PB6, PB7, PC4, PC5, and PC6
  35. #if defined(C6_AUDIO)
  36. # define CPIN_AUDIO
  37. # define CPIN_SET_DIRECTION DDRC |= _BV(PORTC6);
  38. # define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  39. # define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  40. # define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  41. # define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  42. # define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  43. # define TIMER_3_PERIOD ICR3
  44. # define TIMER_3_DUTY_CYCLE OCR3A
  45. # define TIMER3_AUDIO_vect TIMER3_COMPA_vect
  46. #endif
  47. #if defined(C5_AUDIO)
  48. # define CPIN_AUDIO
  49. # define CPIN_SET_DIRECTION DDRC |= _BV(PORTC5);
  50. # define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3B1) | (0 << COM3B0) | (1 << WGM31) | (0 << WGM30);
  51. # define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3B)
  52. # define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3B)
  53. # define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3B1);
  54. # define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3B1) | _BV(COM3B0));
  55. # define TIMER_3_PERIOD ICR3
  56. # define TIMER_3_DUTY_CYCLE OCR3B
  57. # define TIMER3_AUDIO_vect TIMER3_COMPB_vect
  58. #endif
  59. #if defined(C4_AUDIO)
  60. # define CPIN_AUDIO
  61. # define CPIN_SET_DIRECTION DDRC |= _BV(PORTC4);
  62. # define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3C1) | (0 << COM3C0) | (1 << WGM31) | (0 << WGM30);
  63. # define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3C)
  64. # define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3C)
  65. # define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3C1);
  66. # define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3C1) | _BV(COM3C0));
  67. # define TIMER_3_PERIOD ICR3
  68. # define TIMER_3_DUTY_CYCLE OCR3C
  69. # define TIMER3_AUDIO_vect TIMER3_COMPC_vect
  70. #endif
  71. #if defined(B5_AUDIO)
  72. # define BPIN_AUDIO
  73. # define BPIN_SET_DIRECTION DDRB |= _BV(PORTB5);
  74. # define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
  75. # define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
  76. # define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
  77. # define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
  78. # define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
  79. # define TIMER_1_PERIOD ICR1
  80. # define TIMER_1_DUTY_CYCLE OCR1A
  81. # define TIMER1_AUDIO_vect TIMER1_COMPA_vect
  82. #endif
  83. #if defined(B6_AUDIO)
  84. # define BPIN_AUDIO
  85. # define BPIN_SET_DIRECTION DDRB |= _BV(PORTB6);
  86. # define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10);
  87. # define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1B)
  88. # define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1B)
  89. # define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1B1);
  90. # define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1B1) | _BV(COM1B0));
  91. # define TIMER_1_PERIOD ICR1
  92. # define TIMER_1_DUTY_CYCLE OCR1B
  93. # define TIMER1_AUDIO_vect TIMER1_COMPB_vect
  94. #endif
  95. #if defined(B7_AUDIO)
  96. # define BPIN_AUDIO
  97. # define BPIN_SET_DIRECTION DDRB |= _BV(PORTB7);
  98. # define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1C1) | (0 << COM1C0) | (1 << WGM11) | (0 << WGM10);
  99. # define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1C)
  100. # define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1C)
  101. # define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1C1);
  102. # define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1C1) | _BV(COM1C0));
  103. # define TIMER_1_PERIOD ICR1
  104. # define TIMER_1_DUTY_CYCLE OCR1C
  105. # define TIMER1_AUDIO_vect TIMER1_COMPC_vect
  106. #endif
  107. #if !defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
  108. # error "Audio feature enabled, but no suitable pin selected - see docs/feature_audio.md under the AVR settings for available options."
  109. #endif
  110. // -----------------------------------------------------------------------------
  111. int voices = 0;
  112. int voice_place = 0;
  113. float frequency = 0;
  114. float frequency_alt = 0;
  115. int volume = 0;
  116. long position = 0;
  117. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  118. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  119. bool sliding = false;
  120. float place = 0;
  121. uint8_t* sample;
  122. uint16_t sample_length = 0;
  123. bool playing_notes = false;
  124. bool playing_note = false;
  125. float note_frequency = 0;
  126. float note_length = 0;
  127. uint8_t note_tempo = TEMPO_DEFAULT;
  128. float note_timbre = TIMBRE_DEFAULT;
  129. uint16_t note_position = 0;
  130. float (*notes_pointer)[][2];
  131. uint16_t notes_count;
  132. bool notes_repeat;
  133. bool note_resting = false;
  134. uint16_t current_note = 0;
  135. uint8_t rest_counter = 0;
  136. #ifdef VIBRATO_ENABLE
  137. float vibrato_counter = 0;
  138. float vibrato_strength = .5;
  139. float vibrato_rate = 0.125;
  140. #endif
  141. float polyphony_rate = 0;
  142. static bool audio_initialized = false;
  143. audio_config_t audio_config;
  144. uint16_t envelope_index = 0;
  145. bool glissando = true;
  146. #ifndef STARTUP_SONG
  147. # define STARTUP_SONG SONG(STARTUP_SOUND)
  148. #endif
  149. #ifndef AUDIO_ON_SONG
  150. # define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND)
  151. #endif
  152. #ifndef AUDIO_OFF_SONG
  153. # define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND)
  154. #endif
  155. float startup_song[][2] = STARTUP_SONG;
  156. float audio_on_song[][2] = AUDIO_ON_SONG;
  157. float audio_off_song[][2] = AUDIO_OFF_SONG;
  158. void audio_init() {
  159. // Check EEPROM
  160. if (!eeconfig_is_enabled()) {
  161. eeconfig_init();
  162. }
  163. audio_config.raw = eeconfig_read_audio();
  164. if (!audio_initialized) {
  165. // Set audio ports as output
  166. #ifdef CPIN_AUDIO
  167. CPIN_SET_DIRECTION
  168. DISABLE_AUDIO_COUNTER_3_ISR;
  169. #endif
  170. #ifdef BPIN_AUDIO
  171. BPIN_SET_DIRECTION
  172. DISABLE_AUDIO_COUNTER_1_ISR;
  173. #endif
  174. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B
  175. // Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation
  176. // OC3A -- PC6
  177. // OC3B -- PC5
  178. // OC3C -- PC4
  179. // OC1A -- PB5
  180. // OC1B -- PB6
  181. // OC1C -- PB7
  182. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A)
  183. // OCR3A - PC6
  184. // OCR3B - PC5
  185. // OCR3C - PC4
  186. // OCR1A - PB5
  187. // OCR1B - PB6
  188. // OCR1C - PB7
  189. // Clock Select (CS3n) = 0b010 = Clock / 8
  190. #ifdef CPIN_AUDIO
  191. INIT_AUDIO_COUNTER_3
  192. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  193. TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
  194. TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
  195. #endif
  196. #ifdef BPIN_AUDIO
  197. INIT_AUDIO_COUNTER_1
  198. TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
  199. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
  200. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
  201. #endif
  202. audio_initialized = true;
  203. }
  204. if (audio_config.enable) {
  205. PLAY_SONG(startup_song);
  206. }
  207. }
  208. void stop_all_notes() {
  209. dprintf("audio stop all notes");
  210. if (!audio_initialized) {
  211. audio_init();
  212. }
  213. voices = 0;
  214. #ifdef CPIN_AUDIO
  215. DISABLE_AUDIO_COUNTER_3_ISR;
  216. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  217. #endif
  218. #ifdef BPIN_AUDIO
  219. DISABLE_AUDIO_COUNTER_1_ISR;
  220. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  221. #endif
  222. playing_notes = false;
  223. playing_note = false;
  224. frequency = 0;
  225. frequency_alt = 0;
  226. volume = 0;
  227. for (uint8_t i = 0; i < 8; i++) {
  228. frequencies[i] = 0;
  229. volumes[i] = 0;
  230. }
  231. }
  232. void stop_note(float freq) {
  233. dprintf("audio stop note freq=%d", (int)freq);
  234. if (playing_note) {
  235. if (!audio_initialized) {
  236. audio_init();
  237. }
  238. for (int i = 7; i >= 0; i--) {
  239. if (frequencies[i] == freq) {
  240. frequencies[i] = 0;
  241. volumes[i] = 0;
  242. for (int j = i; (j < 7); j++) {
  243. frequencies[j] = frequencies[j + 1];
  244. frequencies[j + 1] = 0;
  245. volumes[j] = volumes[j + 1];
  246. volumes[j + 1] = 0;
  247. }
  248. break;
  249. }
  250. }
  251. voices--;
  252. if (voices < 0) voices = 0;
  253. if (voice_place >= voices) {
  254. voice_place = 0;
  255. }
  256. if (voices == 0) {
  257. #ifdef CPIN_AUDIO
  258. DISABLE_AUDIO_COUNTER_3_ISR;
  259. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  260. #endif
  261. #ifdef BPIN_AUDIO
  262. DISABLE_AUDIO_COUNTER_1_ISR;
  263. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  264. #endif
  265. frequency = 0;
  266. frequency_alt = 0;
  267. volume = 0;
  268. playing_note = false;
  269. }
  270. }
  271. }
  272. #ifdef VIBRATO_ENABLE
  273. float mod(float a, int b) {
  274. float r = fmod(a, b);
  275. return r < 0 ? r + b : r;
  276. }
  277. float vibrato(float average_freq) {
  278. # ifdef VIBRATO_STRENGTH_ENABLE
  279. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  280. # else
  281. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  282. # endif
  283. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
  284. return vibrated_freq;
  285. }
  286. #endif
  287. #ifdef CPIN_AUDIO
  288. ISR(TIMER3_AUDIO_vect) {
  289. float freq;
  290. if (playing_note) {
  291. if (voices > 0) {
  292. # ifdef BPIN_AUDIO
  293. float freq_alt = 0;
  294. if (voices > 1) {
  295. if (polyphony_rate == 0) {
  296. if (glissando) {
  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 < 65535) {
  318. envelope_index++;
  319. }
  320. freq_alt = voice_envelope(freq_alt);
  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);
  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) {
  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 < 65535) {
  368. envelope_index++;
  369. }
  370. freq = voice_envelope(freq);
  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);
  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 < 65535) {
  390. envelope_index++;
  391. }
  392. freq = voice_envelope(freq);
  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);
  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 = 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 BPIN_AUDIO
  447. ISR(TIMER1_AUDIO_vect) {
  448. # if defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
  449. float freq = 0;
  450. if (playing_note) {
  451. if (voices > 0) {
  452. if (polyphony_rate > 0) {
  453. if (voices > 1) {
  454. voice_place %= voices;
  455. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  456. voice_place = (voice_place + 1) % voices;
  457. place = 0.0;
  458. }
  459. }
  460. # ifdef VIBRATO_ENABLE
  461. if (vibrato_strength > 0) {
  462. freq = vibrato(frequencies[voice_place]);
  463. } else {
  464. freq = frequencies[voice_place];
  465. }
  466. # else
  467. freq = frequencies[voice_place];
  468. # endif
  469. } else {
  470. if (glissando) {
  471. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
  472. frequency = frequency * pow(2, 440 / frequency / 12 / 2);
  473. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
  474. frequency = frequency * pow(2, -440 / frequency / 12 / 2);
  475. } else {
  476. frequency = frequencies[voices - 1];
  477. }
  478. } else {
  479. frequency = frequencies[voices - 1];
  480. }
  481. # ifdef VIBRATO_ENABLE
  482. if (vibrato_strength > 0) {
  483. freq = vibrato(frequency);
  484. } else {
  485. freq = frequency;
  486. }
  487. # else
  488. freq = frequency;
  489. # endif
  490. }
  491. if (envelope_index < 65535) {
  492. envelope_index++;
  493. }
  494. freq = voice_envelope(freq);
  495. if (freq < 30.517578125) {
  496. freq = 30.52;
  497. }
  498. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  499. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  500. }
  501. }
  502. if (playing_notes) {
  503. if (note_frequency > 0) {
  504. # ifdef VIBRATO_ENABLE
  505. if (vibrato_strength > 0) {
  506. freq = vibrato(note_frequency);
  507. } else {
  508. freq = note_frequency;
  509. }
  510. # else
  511. freq = note_frequency;
  512. # endif
  513. if (envelope_index < 65535) {
  514. envelope_index++;
  515. }
  516. freq = voice_envelope(freq);
  517. TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
  518. TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
  519. } else {
  520. TIMER_1_PERIOD = 0;
  521. TIMER_1_DUTY_CYCLE = 0;
  522. }
  523. note_position++;
  524. bool end_of_note = false;
  525. if (TIMER_1_PERIOD > 0) {
  526. if (!note_resting)
  527. end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF - 1));
  528. else
  529. end_of_note = (note_position >= (note_length));
  530. } else {
  531. end_of_note = (note_position >= (note_length));
  532. }
  533. if (end_of_note) {
  534. current_note++;
  535. if (current_note >= notes_count) {
  536. if (notes_repeat) {
  537. current_note = 0;
  538. } else {
  539. DISABLE_AUDIO_COUNTER_1_ISR;
  540. DISABLE_AUDIO_COUNTER_1_OUTPUT;
  541. playing_notes = false;
  542. return;
  543. }
  544. }
  545. if (!note_resting) {
  546. note_resting = true;
  547. current_note--;
  548. if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
  549. note_frequency = 0;
  550. note_length = 1;
  551. } else {
  552. note_frequency = (*notes_pointer)[current_note][0];
  553. note_length = 1;
  554. }
  555. } else {
  556. note_resting = false;
  557. envelope_index = 0;
  558. note_frequency = (*notes_pointer)[current_note][0];
  559. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  560. }
  561. note_position = 0;
  562. }
  563. }
  564. if (!audio_config.enable) {
  565. playing_notes = false;
  566. playing_note = false;
  567. }
  568. # endif
  569. }
  570. #endif
  571. void play_note(float freq, int vol) {
  572. dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
  573. if (!audio_initialized) {
  574. audio_init();
  575. }
  576. if (audio_config.enable && voices < 8) {
  577. #ifdef CPIN_AUDIO
  578. DISABLE_AUDIO_COUNTER_3_ISR;
  579. #endif
  580. #ifdef BPIN_AUDIO
  581. DISABLE_AUDIO_COUNTER_1_ISR;
  582. #endif
  583. // Cancel notes if notes are playing
  584. if (playing_notes) stop_all_notes();
  585. playing_note = true;
  586. envelope_index = 0;
  587. if (freq > 0) {
  588. frequencies[voices] = freq;
  589. volumes[voices] = vol;
  590. voices++;
  591. }
  592. #ifdef CPIN_AUDIO
  593. ENABLE_AUDIO_COUNTER_3_ISR;
  594. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  595. #endif
  596. #ifdef BPIN_AUDIO
  597. # ifdef CPIN_AUDIO
  598. if (voices > 1) {
  599. ENABLE_AUDIO_COUNTER_1_ISR;
  600. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  601. }
  602. # else
  603. ENABLE_AUDIO_COUNTER_1_ISR;
  604. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  605. # endif
  606. #endif
  607. }
  608. }
  609. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
  610. if (!audio_initialized) {
  611. audio_init();
  612. }
  613. if (audio_config.enable) {
  614. #ifdef CPIN_AUDIO
  615. DISABLE_AUDIO_COUNTER_3_ISR;
  616. #endif
  617. #ifdef BPIN_AUDIO
  618. DISABLE_AUDIO_COUNTER_1_ISR;
  619. #endif
  620. // Cancel note if a note is playing
  621. if (playing_note) stop_all_notes();
  622. playing_notes = true;
  623. notes_pointer = np;
  624. notes_count = n_count;
  625. notes_repeat = n_repeat;
  626. place = 0;
  627. current_note = 0;
  628. note_frequency = (*notes_pointer)[current_note][0];
  629. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  630. note_position = 0;
  631. #ifdef CPIN_AUDIO
  632. ENABLE_AUDIO_COUNTER_3_ISR;
  633. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  634. #endif
  635. #ifdef BPIN_AUDIO
  636. # ifndef CPIN_AUDIO
  637. ENABLE_AUDIO_COUNTER_1_ISR;
  638. ENABLE_AUDIO_COUNTER_1_OUTPUT;
  639. # endif
  640. #endif
  641. }
  642. }
  643. bool is_playing_notes(void) { return playing_notes; }
  644. bool is_audio_on(void) { return (audio_config.enable != 0); }
  645. void audio_toggle(void) {
  646. audio_config.enable ^= 1;
  647. eeconfig_update_audio(audio_config.raw);
  648. if (audio_config.enable) audio_on_user();
  649. }
  650. void audio_on(void) {
  651. audio_config.enable = 1;
  652. eeconfig_update_audio(audio_config.raw);
  653. audio_on_user();
  654. PLAY_SONG(audio_on_song);
  655. }
  656. void audio_off(void) {
  657. PLAY_SONG(audio_off_song);
  658. wait_ms(100);
  659. stop_all_notes();
  660. audio_config.enable = 0;
  661. eeconfig_update_audio(audio_config.raw);
  662. }
  663. #ifdef VIBRATO_ENABLE
  664. // Vibrato rate functions
  665. void set_vibrato_rate(float rate) { vibrato_rate = rate; }
  666. void increase_vibrato_rate(float change) { vibrato_rate *= change; }
  667. void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
  668. # ifdef VIBRATO_STRENGTH_ENABLE
  669. void set_vibrato_strength(float strength) { vibrato_strength = strength; }
  670. void increase_vibrato_strength(float change) { vibrato_strength *= change; }
  671. void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
  672. # endif /* VIBRATO_STRENGTH_ENABLE */
  673. #endif /* VIBRATO_ENABLE */
  674. // Polyphony functions
  675. void set_polyphony_rate(float rate) { polyphony_rate = rate; }
  676. void enable_polyphony() { polyphony_rate = 5; }
  677. void disable_polyphony() { polyphony_rate = 0; }
  678. void increase_polyphony_rate(float change) { polyphony_rate *= change; }
  679. void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
  680. // Timbre function
  681. void set_timbre(float timbre) { note_timbre = timbre; }
  682. // Tempo functions
  683. void set_tempo(uint8_t tempo) { note_tempo = tempo; }
  684. void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
  685. void increase_tempo(uint8_t tempo_change) {
  686. if (note_tempo - tempo_change < 10) {
  687. note_tempo = 10;
  688. } else {
  689. note_tempo -= tempo_change;
  690. }
  691. }