audio.c 26 KB

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