audio.c 14 KB

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