audio.c 12 KB

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