audio.c 13 KB

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