beeps.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "beeps.h"
  8. #include "keymap_common.h"
  9. #include "wave.h"
  10. #define PI 3.14159265
  11. #define SAMPLE_DIVIDER 70
  12. #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/256)
  13. // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
  14. void delay_us(int count) {
  15. while(count--) {
  16. _delay_us(1);
  17. }
  18. }
  19. int voices = 0;
  20. double frequency = 0;
  21. int volume = 0;
  22. long position = 0;
  23. double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  24. int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  25. bool sliding = false;
  26. #define RANGE 1000
  27. volatile int i=0; //elements of the wave
  28. // uint8_t sine[128];
  29. // uint8_t tri[128];
  30. // uint8_t squ[128];
  31. // uint8_t* sine_start;
  32. // uint8_t* sine_end;
  33. // uint8_t* tri_start;
  34. // uint8_t* tri_end;
  35. // uint8_t* s_start;
  36. // uint8_t* s_end;
  37. // uint8_t* s_ptr;
  38. void beeps() {
  39. play_notes();
  40. }
  41. void send_freq(double freq, int vol) {
  42. int duty = (((double)F_CPU) / freq);
  43. ICR3 = duty; // Set max to the period
  44. OCR3A = duty >> (0x10 - vol); // Set compare to half the period
  45. }
  46. void stop_all_notes() {
  47. voices = 0;
  48. TIMSK0 &= ~_BV(OCIE0A);
  49. frequency = 0;
  50. volume = 0;
  51. for (int i = 0; i < 8; i++) {
  52. frequencies[i] = 0;
  53. volumes[i] = 0;
  54. }
  55. }
  56. void stop_note(double freq) {
  57. freq = freq / SAMPLE_RATE;
  58. for (int i = 7; i >= 0; i--) {
  59. if (frequencies[i] == freq) {
  60. frequencies[i] = 0;
  61. volumes[i] = 0;
  62. for (int j = i; (j < 7); j++) {
  63. frequencies[j] = frequencies[j+1];
  64. frequencies[j+1] = 0;
  65. volumes[j] = volumes[j+1];
  66. volumes[j+1] = 0;
  67. }
  68. }
  69. }
  70. voices--;
  71. if (voices < 0)
  72. voices = 0;
  73. if (voices == 0) {
  74. TIMSK0 &= ~_BV(OCIE0A);
  75. frequency = 0;
  76. volume = 0;
  77. } else {
  78. double freq = frequencies[voices - 1];
  79. int vol = volumes[voices - 1];
  80. double starting_f = frequency;
  81. if (frequency < freq) {
  82. sliding = true;
  83. for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
  84. frequency = f;
  85. }
  86. sliding = false;
  87. } else if (frequency > freq) {
  88. sliding = true;
  89. for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
  90. frequency = f;
  91. }
  92. sliding = false;
  93. }
  94. // send_freq(freq, vol);
  95. frequency = freq;
  96. volume = vol;
  97. }
  98. }
  99. void init_notes() {
  100. // for(int i = 0; i < 128; i++) {
  101. // sine[i] = sin(i * PI / 64) * 128 + 128;
  102. // tri[i] = 256 - abs((i - 64) * 4);
  103. // }
  104. // sine_start = &sine;
  105. // sine_end = &sine + 128;
  106. // tri_start = &tri;
  107. // tri_end = &tri + 128;
  108. // new
  109. PLLFRQ = _BV(PDIV2);
  110. PLLCSR = _BV(PLLE);
  111. while(!(PLLCSR & _BV(PLOCK)));
  112. PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
  113. /* Init a fast PWM on Timer4 */
  114. TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
  115. TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
  116. OCR4A = 0;
  117. /* Enable the OC4A output */
  118. DDRC |= _BV(PORTC6);
  119. /* First disable the timer overflow interrupt while we're configuring */
  120. // TIMSK0 &= ~(1<<TOIE0);
  121. // /* Configure timer4 in normal mode (pure counting, no PWM etc.) */
  122. // TCCR0A &= ~((1<<WGM01) | (1<<WGM00));
  123. // TCCR0B &= ~(1<<WGM02);
  124. // /* Disable Compare Match A interrupt enable (only want overflow) */
  125. // TIMSK0 &= ~(1<<OCIE0A);
  126. // TCCR0B |= (1<<CS01); // Set bits
  127. // TCCR0B &= ~(1<<CS00) | ~(1<<CS02); // Clear bit
  128. // /* Save value globally for later reload in ISR */
  129. // tcnt0 = 45 - 1;
  130. // /* Finally load end enable the timer */
  131. // TCNT0 = tcnt0;
  132. // TIMSK0 |= (1<<TOIE0);
  133. }
  134. int max = 0xFF;
  135. float sum = 0;
  136. int value = 128;
  137. float place = 0;
  138. ISR(TIMER0_COMPA_vect) {
  139. // value = *(sine_start+=(long)frequencies[0]);
  140. // OCR4A = value;
  141. // if (sine_start >= sine_end) {
  142. // sine_start = &sine[(sine_start - sine_end) % 128];
  143. // }
  144. // OCR4A = pgm_read_byte(sine_start);
  145. // // sine_start = &sine[(sine_start - &sine[0] + (int)frequencies[0]) % 128];
  146. // sine_start += (int)frequencies[0];
  147. // if (sine_start >= sine_end) {
  148. // sine_start = &sine[(sine_start - sine_end) % 128];
  149. // }
  150. // OCR4A = pgm_read_byte(s_ptr);
  151. // s_ptr = s_start + (uint8_t)place;
  152. // OCR4A = pgm_read_byte(s_ptr);
  153. // SINE
  154. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]);
  155. // SQUARE
  156. // if (((int)place) >= 1024){
  157. // OCR4A = 0xFF;
  158. // } else {
  159. // OCR4A = 0x00;
  160. // }
  161. // SAWTOOTH
  162. // OCR4A = (int)place / 4;
  163. // TRIANGLE
  164. // if (((int)place) >= 1024) {
  165. // OCR4A = (int)place / 2;
  166. // } else {
  167. // OCR4A = 2048 - (int)place / 2;
  168. // }
  169. place += frequency;
  170. if (place >= SINE_LENGTH)
  171. place -= SINE_LENGTH;
  172. }
  173. ISR(TIMER0_COMPB_vect)
  174. {
  175. /* Disable the interrupt */
  176. TIMSK0 &= ~_BV(OCIE0B);
  177. }
  178. void play_note(double freq, int vol) {
  179. freq = freq / SAMPLE_RATE;
  180. if (freq > 0) {
  181. // TCCR3A = (1 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (1 << WGM30);
  182. // TCCR3B = (0 << WGM33) | (1 << WGM32) | (0 << CS32) | (0 << CS31) | (1 << CS30);
  183. // TIMSK3 = 0x0;
  184. if (frequency != 0) {
  185. double starting_f = frequency;
  186. if (frequency < freq) {
  187. for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
  188. frequency = f;
  189. }
  190. } else if (frequency > freq) {
  191. for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
  192. frequency = f;
  193. }
  194. }
  195. }
  196. // send_freq(freq, vol);
  197. frequency = freq;
  198. volume = vol;
  199. frequencies[voices] = frequency;
  200. volumes[voices] = volume;
  201. voices++;
  202. // position = 0;
  203. // TCNT0 = 0;
  204. }
  205. // ICR3 = 0xFFFF;
  206. // for (int i = 0; i < 10000; i++) {
  207. // OCR3A = round((sin(i*freq)*.5)+.5)*0xFFFF;
  208. // // _delay_us(50);
  209. // }
  210. // TCCR3A = 0;
  211. // TCCR3B = 0;
  212. TIMSK0 &= ~_BV(OCIE0A) | ~_BV(OCIE0B);
  213. TCCR0A = _BV(WGM01);
  214. TCCR0B = _BV(CS01);
  215. OCR0A = SAMPLE_DIVIDER - 1;
  216. OCR0B = 1;
  217. TIMSK0 |= _BV(OCIE0A);
  218. // sei();
  219. }
  220. // void note(int x, float length) {
  221. // DDRC |= (1<<6);
  222. // int t = (int)(440*pow(2,-x/12.0)); // starting note
  223. // for (int y = 0; y < length*1000/t; y++) { // note length
  224. // PORTC |= (1<<6);
  225. // delay_us(t);
  226. // PORTC &= ~(1<<6);
  227. // delay_us(t);
  228. // }
  229. // PORTC &= ~(1<<6);
  230. // }
  231. // void true_note(float x, float y, float length) {
  232. // for (uint32_t i = 0; i < length * 50; i++) {
  233. // uint32_t v = (uint32_t) (round(sin(PI*2*i*640000*pow(2, x/12.0))*.5+1 + sin(PI*2*i*640000*pow(2, y/12.0))*.5+1) / 2 * pow(2, 8));
  234. // for (int u = 0; u < 8; u++) {
  235. // if (v & (1 << u) && !(PORTC&(1<<6)))
  236. // PORTC |= (1<<6);
  237. // else if (PORTC&(1<<6))
  238. // PORTC &= ~(1<<6);
  239. // }
  240. // }
  241. // PORTC &= ~(1<<6);
  242. // }