audio.c 10 KB

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