audio.c 27 KB

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