audio.c 11 KB

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