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.

658 lines
17 KiB

  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. #include <avr/pgmspace.h>
  20. #include <avr/interrupt.h>
  21. #include <avr/io.h>
  22. #include "print.h"
  23. #include "audio.h"
  24. #include "keymap.h"
  25. #include "eeconfig.h"
  26. #define PI 3.14159265
  27. #define CPU_PRESCALER 8
  28. // Timer Abstractions
  29. // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
  30. // Turn on/off 3A interputs, stopping/enabling the ISR calls
  31. #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
  32. #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
  33. // TCCR3A: Timer/Counter #3 Control Register
  34. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  35. #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
  36. #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
  37. #define NOTE_PERIOD ICR3
  38. #define NOTE_DUTY_CYCLE OCR3A
  39. #ifdef PWM_AUDIO
  40. #include "wave.h"
  41. #define SAMPLE_DIVIDER 39
  42. #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
  43. // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
  44. float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  45. uint16_t place_int = 0;
  46. bool repeat = true;
  47. #endif
  48. void delay_us(int count) {
  49. while(count--) {
  50. _delay_us(1);
  51. }
  52. }
  53. int voices = 0;
  54. int voice_place = 0;
  55. float frequency = 0;
  56. int volume = 0;
  57. long position = 0;
  58. float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  59. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  60. bool sliding = false;
  61. float place = 0;
  62. uint8_t * sample;
  63. uint16_t sample_length = 0;
  64. // float freq = 0;
  65. bool playing_notes = false;
  66. bool playing_note = false;
  67. float note_frequency = 0;
  68. float note_length = 0;
  69. uint8_t note_tempo = TEMPO_DEFAULT;
  70. float note_timbre = TIMBRE_DEFAULT;
  71. uint16_t note_position = 0;
  72. float (* notes_pointer)[][2];
  73. uint16_t notes_count;
  74. bool notes_repeat;
  75. float notes_rest;
  76. bool note_resting = false;
  77. uint8_t current_note = 0;
  78. uint8_t rest_counter = 0;
  79. #ifdef VIBRATO_ENABLE
  80. float vibrato_counter = 0;
  81. float vibrato_strength = .5;
  82. float vibrato_rate = 0.125;
  83. #endif
  84. float polyphony_rate = 0;
  85. static bool audio_initialized = false;
  86. audio_config_t audio_config;
  87. uint16_t envelope_index = 0;
  88. void audio_init() {
  89. // Check EEPROM
  90. if (!eeconfig_is_enabled())
  91. {
  92. eeconfig_init();
  93. }
  94. audio_config.raw = eeconfig_read_audio();
  95. #ifdef PWM_AUDIO
  96. PLLFRQ = _BV(PDIV2);
  97. PLLCSR = _BV(PLLE);
  98. while(!(PLLCSR & _BV(PLOCK)));
  99. PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
  100. /* Init a fast PWM on Timer4 */
  101. TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
  102. TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
  103. OCR4A = 0;
  104. /* Enable the OC4A output */
  105. DDRC |= _BV(PORTC6);
  106. DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
  107. TCCR3A = 0x0; // Options not needed
  108. TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
  109. OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
  110. #else
  111. // Set port PC6 (OC3A and /OC4A) as output
  112. DDRC |= _BV(PORTC6);
  113. DISABLE_AUDIO_COUNTER_3_ISR;
  114. // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
  115. // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
  116. // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
  117. // Clock Select (CS3n) = 0b010 = Clock / 8
  118. TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
  119. TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
  120. #endif
  121. audio_initialized = true;
  122. }
  123. void stop_all_notes() {
  124. if (!audio_initialized) {
  125. audio_init();
  126. }
  127. voices = 0;
  128. #ifdef PWM_AUDIO
  129. DISABLE_AUDIO_COUNTER_3_ISR;
  130. #else
  131. DISABLE_AUDIO_COUNTER_3_ISR;
  132. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  133. #endif
  134. playing_notes = false;
  135. playing_note = false;
  136. frequency = 0;
  137. volume = 0;
  138. for (uint8_t i = 0; i < 8; i++)
  139. {
  140. frequencies[i] = 0;
  141. volumes[i] = 0;
  142. }
  143. }
  144. void stop_note(float freq)
  145. {
  146. if (playing_note) {
  147. if (!audio_initialized) {
  148. audio_init();
  149. }
  150. #ifdef PWM_AUDIO
  151. freq = freq / SAMPLE_RATE;
  152. #endif
  153. for (int i = 7; i >= 0; i--) {
  154. if (frequencies[i] == freq) {
  155. frequencies[i] = 0;
  156. volumes[i] = 0;
  157. for (int j = i; (j < 7); j++) {
  158. frequencies[j] = frequencies[j+1];
  159. frequencies[j+1] = 0;
  160. volumes[j] = volumes[j+1];
  161. volumes[j+1] = 0;
  162. }
  163. break;
  164. }
  165. }
  166. voices--;
  167. if (voices < 0)
  168. voices = 0;
  169. if (voice_place >= voices) {
  170. voice_place = 0;
  171. }
  172. if (voices == 0) {
  173. #ifdef PWM_AUDIO
  174. DISABLE_AUDIO_COUNTER_3_ISR;
  175. #else
  176. DISABLE_AUDIO_COUNTER_3_ISR;
  177. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  178. #endif
  179. frequency = 0;
  180. volume = 0;
  181. playing_note = false;
  182. }
  183. }
  184. }
  185. #ifdef VIBRATO_ENABLE
  186. float mod(float a, int b)
  187. {
  188. float r = fmod(a, b);
  189. return r < 0 ? r + b : r;
  190. }
  191. float vibrato(float average_freq) {
  192. #ifdef VIBRATO_STRENGTH_ENABLE
  193. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  194. #else
  195. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  196. #endif
  197. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
  198. return vibrated_freq;
  199. }
  200. #endif
  201. ISR(TIMER3_COMPA_vect)
  202. {
  203. if (playing_note) {
  204. #ifdef PWM_AUDIO
  205. if (voices == 1) {
  206. // SINE
  207. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
  208. // SQUARE
  209. // if (((int)place) >= 1024){
  210. // OCR4A = 0xFF >> 2;
  211. // } else {
  212. // OCR4A = 0x00;
  213. // }
  214. // SAWTOOTH
  215. // OCR4A = (int)place / 4;
  216. // TRIANGLE
  217. // if (((int)place) >= 1024) {
  218. // OCR4A = (int)place / 2;
  219. // } else {
  220. // OCR4A = 2048 - (int)place / 2;
  221. // }
  222. place += frequency;
  223. if (place >= SINE_LENGTH)
  224. place -= SINE_LENGTH;
  225. } else {
  226. int sum = 0;
  227. for (int i = 0; i < voices; i++) {
  228. // SINE
  229. sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
  230. // SQUARE
  231. // if (((int)places[i]) >= 1024){
  232. // sum += 0xFF >> 2;
  233. // } else {
  234. // sum += 0x00;
  235. // }
  236. places[i] += frequencies[i];
  237. if (places[i] >= SINE_LENGTH)
  238. places[i] -= SINE_LENGTH;
  239. }
  240. OCR4A = sum;
  241. }
  242. #else
  243. if (voices > 0) {
  244. float freq;
  245. if (polyphony_rate > 0) {
  246. if (voices > 1) {
  247. voice_place %= voices;
  248. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  249. voice_place = (voice_place + 1) % voices;
  250. place = 0.0;
  251. }
  252. }
  253. #ifdef VIBRATO_ENABLE
  254. if (vibrato_strength > 0) {
  255. freq = vibrato(frequencies[voice_place]);
  256. } else {
  257. #else
  258. {
  259. #endif
  260. freq = frequencies[voice_place];
  261. }
  262. } else {
  263. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  264. frequency = frequency * pow(2, 440/frequency/12/2);
  265. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  266. frequency = frequency * pow(2, -440/frequency/12/2);
  267. } else {
  268. frequency = frequencies[voices - 1];
  269. }
  270. #ifdef VIBRATO_ENABLE
  271. if (vibrato_strength > 0) {
  272. freq = vibrato(frequency);
  273. } else {
  274. #else
  275. {
  276. #endif
  277. freq = frequency;
  278. }
  279. }
  280. if (envelope_index < 65535) {
  281. envelope_index++;
  282. }
  283. freq = voice_envelope(freq);
  284. if (freq < 30.517578125)
  285. freq = 30.52;
  286. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  287. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  288. }
  289. #endif
  290. }
  291. // SAMPLE
  292. // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
  293. // place_int++;
  294. // if (place_int >= sample_length)
  295. // if (repeat)
  296. // place_int -= sample_length;
  297. // else
  298. // DISABLE_AUDIO_COUNTER_3_ISR;
  299. if (playing_notes) {
  300. #ifdef PWM_AUDIO
  301. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
  302. place += note_frequency;
  303. if (place >= SINE_LENGTH)
  304. place -= SINE_LENGTH;
  305. #else
  306. if (note_frequency > 0) {
  307. float freq;
  308. #ifdef VIBRATO_ENABLE
  309. if (vibrato_strength > 0) {
  310. freq = vibrato(note_frequency);
  311. } else {
  312. #else
  313. {
  314. #endif
  315. freq = note_frequency;
  316. }
  317. if (envelope_index < 65535) {
  318. envelope_index++;
  319. }
  320. freq = voice_envelope(freq);
  321. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  322. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  323. } else {
  324. NOTE_PERIOD = 0;
  325. NOTE_DUTY_CYCLE = 0;
  326. }
  327. #endif
  328. note_position++;
  329. bool end_of_note = false;
  330. if (NOTE_PERIOD > 0)
  331. end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
  332. else
  333. end_of_note = (note_position >= (note_length * 0x7FF));
  334. if (end_of_note) {
  335. current_note++;
  336. if (current_note >= notes_count) {
  337. if (notes_repeat) {
  338. current_note = 0;
  339. } else {
  340. #ifdef PWM_AUDIO
  341. DISABLE_AUDIO_COUNTER_3_ISR;
  342. #else
  343. DISABLE_AUDIO_COUNTER_3_ISR;
  344. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  345. #endif
  346. playing_notes = false;
  347. return;
  348. }
  349. }
  350. if (!note_resting && (notes_rest > 0)) {
  351. note_resting = true;
  352. note_frequency = 0;
  353. note_length = notes_rest;
  354. current_note--;
  355. } else {
  356. note_resting = false;
  357. #ifdef PWM_AUDIO
  358. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  359. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  360. #else
  361. envelope_index = 0;
  362. note_frequency = (*notes_pointer)[current_note][0];
  363. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  364. #endif
  365. }
  366. note_position = 0;
  367. }
  368. }
  369. if (!audio_config.enable) {
  370. playing_notes = false;
  371. playing_note = false;
  372. }
  373. }
  374. void play_note(float freq, int vol) {
  375. if (!audio_initialized) {
  376. audio_init();
  377. }
  378. if (audio_config.enable && voices < 8) {
  379. DISABLE_AUDIO_COUNTER_3_ISR;
  380. // Cancel notes if notes are playing
  381. if (playing_notes)
  382. stop_all_notes();
  383. playing_note = true;
  384. envelope_index = 0;
  385. #ifdef PWM_AUDIO
  386. freq = freq / SAMPLE_RATE;
  387. #endif
  388. if (freq > 0) {
  389. frequencies[voices] = freq;
  390. volumes[voices] = vol;
  391. voices++;
  392. }
  393. #ifdef PWM_AUDIO
  394. ENABLE_AUDIO_COUNTER_3_ISR;
  395. #else
  396. ENABLE_AUDIO_COUNTER_3_ISR;
  397. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  398. #endif
  399. }
  400. }
  401. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
  402. {
  403. if (!audio_initialized) {
  404. audio_init();
  405. }
  406. if (audio_config.enable) {
  407. DISABLE_AUDIO_COUNTER_3_ISR;
  408. // Cancel note if a note is playing
  409. if (playing_note)
  410. stop_all_notes();
  411. playing_notes = true;
  412. notes_pointer = np;
  413. notes_count = n_count;
  414. notes_repeat = n_repeat;
  415. notes_rest = n_rest;
  416. place = 0;
  417. current_note = 0;
  418. #ifdef PWM_AUDIO
  419. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  420. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  421. #else
  422. note_frequency = (*notes_pointer)[current_note][0];
  423. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  424. #endif
  425. note_position = 0;
  426. #ifdef PWM_AUDIO
  427. ENABLE_AUDIO_COUNTER_3_ISR;
  428. #else
  429. ENABLE_AUDIO_COUNTER_3_ISR;
  430. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  431. #endif
  432. }
  433. }
  434. #ifdef PWM_AUDIO
  435. void play_sample(uint8_t * s, uint16_t l, bool r) {
  436. if (!audio_initialized) {
  437. audio_init();
  438. }
  439. if (audio_config.enable) {
  440. DISABLE_AUDIO_COUNTER_3_ISR;
  441. stop_all_notes();
  442. place_int = 0;
  443. sample = s;
  444. sample_length = l;
  445. repeat = r;
  446. ENABLE_AUDIO_COUNTER_3_ISR;
  447. }
  448. }
  449. #endif
  450. void audio_toggle(void) {
  451. audio_config.enable ^= 1;
  452. eeconfig_update_audio(audio_config.raw);
  453. }
  454. void audio_on(void) {
  455. audio_config.enable = 1;
  456. eeconfig_update_audio(audio_config.raw);
  457. }
  458. void audio_off(void) {
  459. audio_config.enable = 0;
  460. eeconfig_update_audio(audio_config.raw);
  461. }
  462. #ifdef VIBRATO_ENABLE
  463. // Vibrato rate functions
  464. void set_vibrato_rate(float rate) {
  465. vibrato_rate = rate;
  466. }
  467. void increase_vibrato_rate(float change) {
  468. vibrato_rate *= change;
  469. }
  470. void decrease_vibrato_rate(float change) {
  471. vibrato_rate /= change;
  472. }
  473. #ifdef VIBRATO_STRENGTH_ENABLE
  474. void set_vibrato_strength(float strength) {
  475. vibrato_strength = strength;
  476. }
  477. void increase_vibrato_strength(float change) {
  478. vibrato_strength *= change;
  479. }
  480. void decrease_vibrato_strength(float change) {
  481. vibrato_strength /= change;
  482. }
  483. #endif /* VIBRATO_STRENGTH_ENABLE */
  484. #endif /* VIBRATO_ENABLE */
  485. // Polyphony functions
  486. void set_polyphony_rate(float rate) {
  487. polyphony_rate = rate;
  488. }
  489. void enable_polyphony() {
  490. polyphony_rate = 5;
  491. }
  492. void disable_polyphony() {
  493. polyphony_rate = 0;
  494. }
  495. void increase_polyphony_rate(float change) {
  496. polyphony_rate *= change;
  497. }
  498. void decrease_polyphony_rate(float change) {
  499. polyphony_rate /= change;
  500. }
  501. // Timbre function
  502. void set_timbre(float timbre) {
  503. note_timbre = timbre;
  504. }
  505. // Tempo functions
  506. void set_tempo(uint8_t tempo) {
  507. note_tempo = tempo;
  508. }
  509. void decrease_tempo(uint8_t tempo_change) {
  510. note_tempo += tempo_change;
  511. }
  512. void increase_tempo(uint8_t tempo_change) {
  513. if (note_tempo - tempo_change < 10) {
  514. note_tempo = 10;
  515. } else {
  516. note_tempo -= tempo_change;
  517. }
  518. }
  519. //------------------------------------------------------------------------------
  520. // Override these functions in your keymap file to play different tunes on
  521. // startup and bootloader jump
  522. __attribute__ ((weak))
  523. void play_startup_tone()
  524. {
  525. }
  526. __attribute__ ((weak))
  527. void play_goodbye_tone()
  528. {
  529. }
  530. //------------------------------------------------------------------------------