2
0

audio.c 15 KB

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