2
0

beeps.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. void stop_all_notes() {
  29. voices = 0;
  30. TIMSK3 &= ~_BV(OCIE3A);
  31. frequency = 0;
  32. volume = 0;
  33. for (int i = 0; i < 8; i++) {
  34. frequencies[i] = 0;
  35. volumes[i] = 0;
  36. }
  37. }
  38. void stop_note(double freq) {
  39. freq = freq / SAMPLE_RATE;
  40. for (int i = 7; i >= 0; i--) {
  41. if (frequencies[i] == freq) {
  42. frequencies[i] = 0;
  43. volumes[i] = 0;
  44. for (int j = i; (j < 7); j++) {
  45. frequencies[j] = frequencies[j+1];
  46. frequencies[j+1] = 0;
  47. volumes[j] = volumes[j+1];
  48. volumes[j+1] = 0;
  49. }
  50. }
  51. }
  52. voices--;
  53. if (voices < 0)
  54. voices = 0;
  55. if (voices == 0) {
  56. TIMSK3 &= ~_BV(OCIE3A);
  57. frequency = 0;
  58. volume = 0;
  59. } else {
  60. double freq = frequencies[voices - 1];
  61. int vol = volumes[voices - 1];
  62. double starting_f = frequency;
  63. if (frequency < freq) {
  64. sliding = true;
  65. for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
  66. frequency = f;
  67. }
  68. sliding = false;
  69. } else if (frequency > freq) {
  70. sliding = true;
  71. for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
  72. frequency = f;
  73. }
  74. sliding = false;
  75. }
  76. frequency = freq;
  77. volume = vol;
  78. }
  79. }
  80. void init_notes() {
  81. PLLFRQ = _BV(PDIV2);
  82. PLLCSR = _BV(PLLE);
  83. while(!(PLLCSR & _BV(PLOCK)));
  84. PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
  85. /* Init a fast PWM on Timer4 */
  86. TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
  87. TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
  88. OCR4A = 0;
  89. /* Enable the OC4A output */
  90. DDRC |= _BV(PORTC6);
  91. }
  92. int max = 0xFF;
  93. float sum = 0;
  94. int value = 128;
  95. float place = 0;
  96. ISR(TIMER3_COMPA_vect) {
  97. // SINE
  98. OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]);
  99. // SQUARE
  100. // if (((int)place) >= 1024){
  101. // OCR4A = 0xFF;
  102. // } else {
  103. // OCR4A = 0x00;
  104. // }
  105. // SAWTOOTH
  106. // OCR4A = (int)place / 4;
  107. // TRIANGLE
  108. // if (((int)place) >= 1024) {
  109. // OCR4A = (int)place / 2;
  110. // } else {
  111. // OCR4A = 2048 - (int)place / 2;
  112. // }
  113. place += frequency;
  114. if (place >= SINE_LENGTH)
  115. place -= SINE_LENGTH;
  116. }
  117. void play_note(double freq, int vol) {
  118. freq = freq / SAMPLE_RATE;
  119. if (freq > 0) {
  120. if (frequency != 0) {
  121. double starting_f = frequency;
  122. if (frequency < freq) {
  123. for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) {
  124. frequency = f;
  125. }
  126. } else if (frequency > freq) {
  127. for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) {
  128. frequency = f;
  129. }
  130. }
  131. }
  132. frequency = freq;
  133. volume = vol;
  134. frequencies[voices] = frequency;
  135. volumes[voices] = volume;
  136. voices++;
  137. }
  138. TIMSK3 &= ~_BV(OCIE3A);
  139. TCCR3A = 0x0;
  140. TCCR3B = _BV(CS31) | _BV(WGM32);
  141. OCR3A = SAMPLE_DIVIDER - 1;
  142. TIMSK3 |= _BV(OCIE3A);
  143. }