audio.c 11 KB

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