audio.c 26 KB

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