audio.c 11 KB

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