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.

595 lines
16 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. uint16_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. eeconfig_init();
  92. }
  93. audio_config.raw = eeconfig_read_audio();
  94. #ifdef PWM_AUDIO
  95. PLLFRQ = _BV(PDIV2);
  96. PLLCSR = _BV(PLLE);
  97. while (!(PLLCSR & _BV(PLOCK)))
  98. ;
  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. frequencies[i] = 0;
  140. volumes[i] = 0;
  141. }
  142. }
  143. void stop_note(float freq) {
  144. if (playing_note) {
  145. if (!audio_initialized) {
  146. audio_init();
  147. }
  148. #ifdef PWM_AUDIO
  149. freq = freq / SAMPLE_RATE;
  150. #endif
  151. for (int i = 7; i >= 0; i--) {
  152. if (frequencies[i] == freq) {
  153. frequencies[i] = 0;
  154. volumes[i] = 0;
  155. for (int j = i; (j < 7); j++) {
  156. frequencies[j] = frequencies[j + 1];
  157. frequencies[j + 1] = 0;
  158. volumes[j] = volumes[j + 1];
  159. volumes[j + 1] = 0;
  160. }
  161. break;
  162. }
  163. }
  164. voices--;
  165. if (voices < 0) voices = 0;
  166. if (voice_place >= voices) {
  167. voice_place = 0;
  168. }
  169. if (voices == 0) {
  170. #ifdef PWM_AUDIO
  171. DISABLE_AUDIO_COUNTER_3_ISR;
  172. #else
  173. DISABLE_AUDIO_COUNTER_3_ISR;
  174. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  175. #endif
  176. frequency = 0;
  177. volume = 0;
  178. playing_note = false;
  179. }
  180. }
  181. }
  182. #ifdef VIBRATO_ENABLE
  183. float mod(float a, int b) {
  184. float r = fmod(a, b);
  185. return r < 0 ? r + b : r;
  186. }
  187. float vibrato(float average_freq) {
  188. # ifdef VIBRATO_STRENGTH_ENABLE
  189. float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  190. # else
  191. float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
  192. # endif
  193. vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
  194. return vibrated_freq;
  195. }
  196. #endif
  197. ISR(TIMER3_COMPA_vect) {
  198. if (playing_note) {
  199. #ifdef PWM_AUDIO
  200. if (voices == 1) {
  201. // SINE
  202. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
  203. // SQUARE
  204. // if (((int)place) >= 1024){
  205. // OCR4A = 0xFF >> 2;
  206. // } else {
  207. // OCR4A = 0x00;
  208. // }
  209. // SAWTOOTH
  210. // OCR4A = (int)place / 4;
  211. // TRIANGLE
  212. // if (((int)place) >= 1024) {
  213. // OCR4A = (int)place / 2;
  214. // } else {
  215. // OCR4A = 2048 - (int)place / 2;
  216. // }
  217. place += frequency;
  218. if (place >= SINE_LENGTH) place -= SINE_LENGTH;
  219. } else {
  220. int sum = 0;
  221. for (int i = 0; i < voices; i++) {
  222. // SINE
  223. sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
  224. // SQUARE
  225. // if (((int)places[i]) >= 1024){
  226. // sum += 0xFF >> 2;
  227. // } else {
  228. // sum += 0x00;
  229. // }
  230. places[i] += frequencies[i];
  231. if (places[i] >= SINE_LENGTH) places[i] -= SINE_LENGTH;
  232. }
  233. OCR4A = sum;
  234. }
  235. #else
  236. if (voices > 0) {
  237. float freq;
  238. if (polyphony_rate > 0) {
  239. if (voices > 1) {
  240. voice_place %= voices;
  241. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  242. voice_place = (voice_place + 1) % voices;
  243. place = 0.0;
  244. }
  245. }
  246. # ifdef VIBRATO_ENABLE
  247. if (vibrato_strength > 0) {
  248. freq = vibrato(frequencies[voice_place]);
  249. } else {
  250. # else
  251. {
  252. # endif
  253. freq = frequencies[voice_place];
  254. }
  255. } else {
  256. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
  257. frequency = frequency * pow(2, 440 / frequency / 12 / 2);
  258. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
  259. frequency = frequency * pow(2, -440 / frequency / 12 / 2);
  260. } else {
  261. frequency = frequencies[voices - 1];
  262. }
  263. # ifdef VIBRATO_ENABLE
  264. if (vibrato_strength > 0) {
  265. freq = vibrato(frequency);
  266. } else {
  267. # else
  268. {
  269. # endif
  270. freq = frequency;
  271. }
  272. }
  273. if (envelope_index < 65535) {
  274. envelope_index++;
  275. }
  276. freq = voice_envelope(freq);
  277. if (freq < 30.517578125) freq = 30.52;
  278. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  279. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  280. }
  281. #endif
  282. }
  283. // SAMPLE
  284. // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
  285. // place_int++;
  286. // if (place_int >= sample_length)
  287. // if (repeat)
  288. // place_int -= sample_length;
  289. // else
  290. // DISABLE_AUDIO_COUNTER_3_ISR;
  291. if (playing_notes) {
  292. #ifdef PWM_AUDIO
  293. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
  294. place += note_frequency;
  295. if (place >= SINE_LENGTH) place -= SINE_LENGTH;
  296. #else
  297. if (note_frequency > 0) {
  298. float freq;
  299. # ifdef VIBRATO_ENABLE
  300. if (vibrato_strength > 0) {
  301. freq = vibrato(note_frequency);
  302. } else {
  303. # else
  304. {
  305. # endif
  306. freq = note_frequency;
  307. }
  308. if (envelope_index < 65535) {
  309. envelope_index++;
  310. }
  311. freq = voice_envelope(freq);
  312. NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  313. NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  314. } else {
  315. NOTE_PERIOD = 0;
  316. NOTE_DUTY_CYCLE = 0;
  317. }
  318. #endif
  319. note_position++;
  320. bool end_of_note = false;
  321. if (NOTE_PERIOD > 0)
  322. end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
  323. else
  324. end_of_note = (note_position >= (note_length * 0x7FF));
  325. if (end_of_note) {
  326. current_note++;
  327. if (current_note >= notes_count) {
  328. if (notes_repeat) {
  329. current_note = 0;
  330. } else {
  331. #ifdef PWM_AUDIO
  332. DISABLE_AUDIO_COUNTER_3_ISR;
  333. #else
  334. DISABLE_AUDIO_COUNTER_3_ISR;
  335. DISABLE_AUDIO_COUNTER_3_OUTPUT;
  336. #endif
  337. playing_notes = false;
  338. return;
  339. }
  340. }
  341. if (!note_resting && (notes_rest > 0)) {
  342. note_resting = true;
  343. note_frequency = 0;
  344. note_length = notes_rest;
  345. current_note--;
  346. } else {
  347. note_resting = false;
  348. #ifdef PWM_AUDIO
  349. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  350. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  351. #else
  352. envelope_index = 0;
  353. note_frequency = (*notes_pointer)[current_note][0];
  354. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  355. #endif
  356. }
  357. note_position = 0;
  358. }
  359. }
  360. if (!audio_config.enable) {
  361. playing_notes = false;
  362. playing_note = false;
  363. }
  364. }
  365. void play_note(float freq, int vol) {
  366. if (!audio_initialized) {
  367. audio_init();
  368. }
  369. if (audio_config.enable && voices < 8) {
  370. DISABLE_AUDIO_COUNTER_3_ISR;
  371. // Cancel notes if notes are playing
  372. if (playing_notes) stop_all_notes();
  373. playing_note = true;
  374. envelope_index = 0;
  375. #ifdef PWM_AUDIO
  376. freq = freq / SAMPLE_RATE;
  377. #endif
  378. if (freq > 0) {
  379. frequencies[voices] = freq;
  380. volumes[voices] = vol;
  381. voices++;
  382. }
  383. #ifdef PWM_AUDIO
  384. ENABLE_AUDIO_COUNTER_3_ISR;
  385. #else
  386. ENABLE_AUDIO_COUNTER_3_ISR;
  387. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  388. #endif
  389. }
  390. }
  391. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
  392. if (!audio_initialized) {
  393. audio_init();
  394. }
  395. if (audio_config.enable) {
  396. DISABLE_AUDIO_COUNTER_3_ISR;
  397. // Cancel note if a note is playing
  398. if (playing_note) stop_all_notes();
  399. playing_notes = true;
  400. notes_pointer = np;
  401. notes_count = n_count;
  402. notes_repeat = n_repeat;
  403. notes_rest = n_rest;
  404. place = 0;
  405. current_note = 0;
  406. #ifdef PWM_AUDIO
  407. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  408. note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
  409. #else
  410. note_frequency = (*notes_pointer)[current_note][0];
  411. note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
  412. #endif
  413. note_position = 0;
  414. #ifdef PWM_AUDIO
  415. ENABLE_AUDIO_COUNTER_3_ISR;
  416. #else
  417. ENABLE_AUDIO_COUNTER_3_ISR;
  418. ENABLE_AUDIO_COUNTER_3_OUTPUT;
  419. #endif
  420. }
  421. }
  422. #ifdef PWM_AUDIO
  423. void play_sample(uint8_t* s, uint16_t l, bool r) {
  424. if (!audio_initialized) {
  425. audio_init();
  426. }
  427. if (audio_config.enable) {
  428. DISABLE_AUDIO_COUNTER_3_ISR;
  429. stop_all_notes();
  430. place_int = 0;
  431. sample = s;
  432. sample_length = l;
  433. repeat = r;
  434. ENABLE_AUDIO_COUNTER_3_ISR;
  435. }
  436. }
  437. #endif
  438. void audio_toggle(void) {
  439. audio_config.enable ^= 1;
  440. eeconfig_update_audio(audio_config.raw);
  441. }
  442. void audio_on(void) {
  443. audio_config.enable = 1;
  444. eeconfig_update_audio(audio_config.raw);
  445. }
  446. void audio_off(void) {
  447. audio_config.enable = 0;
  448. eeconfig_update_audio(audio_config.raw);
  449. }
  450. #ifdef VIBRATO_ENABLE
  451. // Vibrato rate functions
  452. void set_vibrato_rate(float rate) { vibrato_rate = rate; }
  453. void increase_vibrato_rate(float change) { vibrato_rate *= change; }
  454. void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
  455. # ifdef VIBRATO_STRENGTH_ENABLE
  456. void set_vibrato_strength(float strength) { vibrato_strength = strength; }
  457. void increase_vibrato_strength(float change) { vibrato_strength *= change; }
  458. void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
  459. # endif /* VIBRATO_STRENGTH_ENABLE */
  460. #endif /* VIBRATO_ENABLE */
  461. // Polyphony functions
  462. void set_polyphony_rate(float rate) { polyphony_rate = rate; }
  463. void enable_polyphony() { polyphony_rate = 5; }
  464. void disable_polyphony() { polyphony_rate = 0; }
  465. void increase_polyphony_rate(float change) { polyphony_rate *= change; }
  466. void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
  467. // Timbre function
  468. void set_timbre(float timbre) { note_timbre = timbre; }
  469. // Tempo functions
  470. void set_tempo(uint8_t tempo) { note_tempo = tempo; }
  471. void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
  472. void increase_tempo(uint8_t tempo_change) {
  473. if (note_tempo - tempo_change < 10) {
  474. note_tempo = 10;
  475. } else {
  476. note_tempo -= tempo_change;
  477. }
  478. }
  479. //------------------------------------------------------------------------------
  480. // Override these functions in your keymap file to play different tunes on
  481. // startup and bootloader jump
  482. __attribute__((weak)) void play_startup_tone() {}
  483. __attribute__((weak)) void play_goodbye_tone() {}
  484. //------------------------------------------------------------------------------