audio.c 15 KB

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