You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

805 lines
25 KiB

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