audio.c 9.5 KB

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