audio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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_update_audio(audio_config.raw);
  70. }
  71. void audio_on(void) {
  72. audio_config.enable = 1;
  73. eeconfig_update_audio(audio_config.raw);
  74. }
  75. void audio_off(void) {
  76. audio_config.enable = 0;
  77. eeconfig_update_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. ISR(TIMER3_COMPA_vect) {
  242. if (note) {
  243. #ifdef PWM_AUDIO
  244. if (voices == 1) {
  245. // SINE
  246. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
  247. // SQUARE
  248. // if (((int)place) >= 1024){
  249. // OCR4A = 0xFF >> 2;
  250. // } else {
  251. // OCR4A = 0x00;
  252. // }
  253. // SAWTOOTH
  254. // OCR4A = (int)place / 4;
  255. // TRIANGLE
  256. // if (((int)place) >= 1024) {
  257. // OCR4A = (int)place / 2;
  258. // } else {
  259. // OCR4A = 2048 - (int)place / 2;
  260. // }
  261. place += frequency;
  262. if (place >= SINE_LENGTH)
  263. place -= SINE_LENGTH;
  264. } else {
  265. int sum = 0;
  266. for (int i = 0; i < voices; i++) {
  267. // SINE
  268. sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
  269. // SQUARE
  270. // if (((int)places[i]) >= 1024){
  271. // sum += 0xFF >> 2;
  272. // } else {
  273. // sum += 0x00;
  274. // }
  275. places[i] += frequencies[i];
  276. if (places[i] >= SINE_LENGTH)
  277. places[i] -= SINE_LENGTH;
  278. }
  279. OCR4A = sum;
  280. }
  281. #else
  282. if (voices > 0) {
  283. float freq;
  284. if (polyphony_rate > 0) {
  285. if (voices > 1) {
  286. voice_place %= voices;
  287. if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
  288. voice_place = (voice_place + 1) % voices;
  289. place = 0.0;
  290. }
  291. }
  292. #ifdef VIBRATO_ENABLE
  293. if (vibrato_strength > 0) {
  294. freq = vibrato(frequencies[voice_place]);
  295. } else {
  296. #else
  297. {
  298. #endif
  299. freq = frequencies[voice_place];
  300. }
  301. } else {
  302. if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
  303. frequency = frequency * pow(2, 440/frequency/12/2);
  304. } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
  305. frequency = frequency * pow(2, -440/frequency/12/2);
  306. } else {
  307. frequency = frequencies[voices - 1];
  308. }
  309. #ifdef VIBRATO_ENABLE
  310. if (vibrato_strength > 0) {
  311. freq = vibrato(frequency);
  312. } else {
  313. #else
  314. {
  315. #endif
  316. freq = frequency;
  317. }
  318. }
  319. if (envelope_index < 65535) {
  320. envelope_index++;
  321. }
  322. freq = voice_envelope(freq);
  323. if (freq < 30.517578125)
  324. freq = 30.52;
  325. ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  326. OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  327. }
  328. #endif
  329. }
  330. // SAMPLE
  331. // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
  332. // place_int++;
  333. // if (place_int >= sample_length)
  334. // if (repeat)
  335. // place_int -= sample_length;
  336. // else
  337. // TIMSK3 &= ~_BV(OCIE3A);
  338. if (notes) {
  339. #ifdef PWM_AUDIO
  340. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
  341. place += note_frequency;
  342. if (place >= SINE_LENGTH)
  343. place -= SINE_LENGTH;
  344. #else
  345. if (note_frequency > 0) {
  346. float freq;
  347. #ifdef VIBRATO_ENABLE
  348. if (vibrato_strength > 0) {
  349. freq = vibrato(note_frequency);
  350. } else {
  351. #else
  352. {
  353. #endif
  354. freq = note_frequency;
  355. }
  356. if (envelope_index < 65535) {
  357. envelope_index++;
  358. }
  359. freq = voice_envelope(freq);
  360. ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
  361. OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
  362. } else {
  363. ICR3 = 0;
  364. OCR3A = 0;
  365. }
  366. #endif
  367. note_position++;
  368. bool end_of_note = false;
  369. if (ICR3 > 0)
  370. end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
  371. else
  372. end_of_note = (note_position >= (note_length * 0x7FF));
  373. if (end_of_note) {
  374. current_note++;
  375. if (current_note >= notes_count) {
  376. if (notes_repeat) {
  377. current_note = 0;
  378. } else {
  379. #ifdef PWM_AUDIO
  380. TIMSK3 &= ~_BV(OCIE3A);
  381. #else
  382. TIMSK3 &= ~_BV(OCIE3A);
  383. TCCR3A &= ~_BV(COM3A1);
  384. #endif
  385. notes = false;
  386. return;
  387. }
  388. }
  389. if (!note_resting && (notes_rest > 0)) {
  390. note_resting = true;
  391. note_frequency = 0;
  392. note_length = notes_rest;
  393. current_note--;
  394. } else {
  395. note_resting = false;
  396. #ifdef PWM_AUDIO
  397. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  398. note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
  399. #else
  400. envelope_index = 0;
  401. note_frequency = (*notes_pointer)[current_note][0];
  402. note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
  403. #endif
  404. }
  405. note_position = 0;
  406. }
  407. }
  408. if (!audio_config.enable) {
  409. notes = false;
  410. note = false;
  411. }
  412. }
  413. void play_note(float freq, int vol) {
  414. if (!inited) {
  415. audio_init();
  416. }
  417. if (audio_config.enable && voices < 8) {
  418. TIMSK3 &= ~_BV(OCIE3A);
  419. // Cancel notes if notes are playing
  420. if (notes)
  421. stop_all_notes();
  422. note = true;
  423. envelope_index = 0;
  424. #ifdef PWM_AUDIO
  425. freq = freq / SAMPLE_RATE;
  426. #endif
  427. if (freq > 0) {
  428. frequencies[voices] = freq;
  429. volumes[voices] = vol;
  430. voices++;
  431. }
  432. #ifdef PWM_AUDIO
  433. TIMSK3 |= _BV(OCIE3A);
  434. #else
  435. TIMSK3 |= _BV(OCIE3A);
  436. TCCR3A |= _BV(COM3A1);
  437. #endif
  438. }
  439. }
  440. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
  441. if (!inited) {
  442. audio_init();
  443. }
  444. if (audio_config.enable) {
  445. TIMSK3 &= ~_BV(OCIE3A);
  446. // Cancel note if a note is playing
  447. if (note)
  448. stop_all_notes();
  449. notes = true;
  450. notes_pointer = np;
  451. notes_count = n_count;
  452. notes_repeat = n_repeat;
  453. notes_rest = n_rest;
  454. place = 0;
  455. current_note = 0;
  456. #ifdef PWM_AUDIO
  457. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  458. note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
  459. #else
  460. note_frequency = (*notes_pointer)[current_note][0];
  461. note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
  462. #endif
  463. note_position = 0;
  464. #ifdef PWM_AUDIO
  465. TIMSK3 |= _BV(OCIE3A);
  466. #else
  467. TIMSK3 |= _BV(OCIE3A);
  468. TCCR3A |= _BV(COM3A1);
  469. #endif
  470. }
  471. }
  472. #ifdef PWM_AUDIO
  473. void play_sample(uint8_t * s, uint16_t l, bool r) {
  474. if (!inited) {
  475. audio_init();
  476. }
  477. if (audio_config.enable) {
  478. TIMSK3 &= ~_BV(OCIE3A);
  479. stop_all_notes();
  480. place_int = 0;
  481. sample = s;
  482. sample_length = l;
  483. repeat = r;
  484. TIMSK3 |= _BV(OCIE3A);
  485. }
  486. }
  487. #endif
  488. //------------------------------------------------------------------------------
  489. // Override these functions in your keymap file to play different tunes on
  490. // startup and bootloader jump
  491. __attribute__ ((weak))
  492. void play_startup_tone()
  493. {
  494. }
  495. __attribute__ ((weak))
  496. void play_goodbye_tone()
  497. {
  498. }
  499. //------------------------------------------------------------------------------