beeps.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 39
  12. #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
  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. int max = 0xFF;
  29. float sum = 0;
  30. int value = 128;
  31. float place = 0;
  32. uint16_t place_int = 0;
  33. bool repeat = true;
  34. uint8_t * sample;
  35. uint16_t sample_length = 0;
  36. bool notes = false;
  37. float note_frequency = 0;
  38. float note_length = 0;
  39. uint16_t note_position = 0;
  40. float (* notes_pointer)[][2];
  41. uint8_t notes_length;
  42. bool notes_repeat;
  43. uint8_t current_note = 0;
  44. void stop_all_notes() {
  45. voices = 0;
  46. TIMSK3 &= ~_BV(OCIE3A);
  47. notes = false;
  48. playing_notes = false;
  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. TIMSK3 &= ~_BV(OCIE3A);
  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. frequency = freq;
  95. volume = vol;
  96. }
  97. }
  98. void init_notes() {
  99. PLLFRQ = _BV(PDIV2);
  100. PLLCSR = _BV(PLLE);
  101. while(!(PLLCSR & _BV(PLOCK)));
  102. PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
  103. /* Init a fast PWM on Timer4 */
  104. TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
  105. TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
  106. OCR4A = 0;
  107. /* Enable the OC4A output */
  108. DDRC |= _BV(PORTC6);
  109. TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
  110. TCCR3A = 0x0; // Options not needed
  111. TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
  112. OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
  113. playing_notes = false;
  114. }
  115. ISR(TIMER3_COMPA_vect) {
  116. // SINE
  117. // OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]);
  118. // SQUARE
  119. // if (((int)place) >= 1024){
  120. // OCR4A = 0xFF;
  121. // } else {
  122. // OCR4A = 0x00;
  123. // }
  124. // SAWTOOTH
  125. // OCR4A = (int)place / 4;
  126. // TRIANGLE
  127. // if (((int)place) >= 1024) {
  128. // OCR4A = (int)place / 2;
  129. // } else {
  130. // OCR4A = 2048 - (int)place / 2;
  131. // }
  132. // place += frequency;
  133. // if (place >= SINE_LENGTH)
  134. // if (repeat)
  135. // place -= SINE_LENGTH;
  136. // else
  137. // TIMSK3 &= ~_BV(OCIE3A);
  138. // SAMPLE
  139. // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
  140. // place_int++;
  141. // if (place_int >= sample_length)
  142. // if (repeat)
  143. // place_int -= sample_length;
  144. // else
  145. // TIMSK3 &= ~_BV(OCIE3A);
  146. if (notes) {
  147. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
  148. place += note_frequency;
  149. if (place >= SINE_LENGTH)
  150. place -= SINE_LENGTH;
  151. note_position++;
  152. if (note_position >= note_length) {
  153. current_note++;
  154. if (current_note >= notes_length) {
  155. if (notes_repeat) {
  156. current_note = 0;
  157. } else {
  158. TIMSK3 &= ~_BV(OCIE3A);
  159. notes = false;
  160. playing_notes = false;
  161. return;
  162. }
  163. }
  164. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  165. note_length = (*notes_pointer)[current_note][1];
  166. note_position = 0;
  167. }
  168. }
  169. }
  170. void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
  171. notes = true;
  172. notes_pointer = np;
  173. notes_length = n_length;
  174. notes_repeat = n_repeat;
  175. place = 0;
  176. current_note = 0;
  177. note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
  178. note_length = (*notes_pointer)[current_note][1];
  179. // note_frequency = 880.0 / SAMPLE_RATE;
  180. // note_length = 1000;
  181. note_position = 0;
  182. TIMSK3 |= _BV(OCIE3A);
  183. playing_notes = true;
  184. }
  185. void play_sample(uint8_t * s, uint16_t l, bool r) {
  186. place_int = 0;
  187. sample = s;
  188. sample_length = l;
  189. repeat = r;
  190. TIMSK3 |= _BV(OCIE3A);
  191. playing_notes = true;
  192. }
  193. void play_note(double freq, int vol) {
  194. freq = freq / SAMPLE_RATE;
  195. if (freq > 0) {
  196. if (frequency != 0) {
  197. double starting_f = frequency;
  198. if (frequency < freq) {
  199. for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
  200. frequency = f;
  201. }
  202. } else if (frequency > freq) {
  203. for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
  204. frequency = f;
  205. }
  206. }
  207. }
  208. frequency = freq;
  209. volume = vol;
  210. frequencies[voices] = frequency;
  211. volumes[voices] = volume;
  212. voices++;
  213. }
  214. TIMSK3 |= _BV(OCIE3A);
  215. }