audio.c 13 KB

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