audio_avr.c 25 KB

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