audio.c 14 KB

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