diff options
Diffstat (limited to 'quantum/beeps.c')
| -rw-r--r-- | quantum/beeps.c | 265 |
1 files changed, 0 insertions, 265 deletions
diff --git a/quantum/beeps.c b/quantum/beeps.c deleted file mode 100644 index 7586ebc52..000000000 --- a/quantum/beeps.c +++ /dev/null | |||
| @@ -1,265 +0,0 @@ | |||
| 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 | |||
| 8 | #include "beeps.h" | ||
| 9 | #include "keymap_common.h" | ||
| 10 | #include "wave.h" | ||
| 11 | |||
| 12 | #define PI 3.14159265 | ||
| 13 | |||
| 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 | |||
| 18 | void delay_us(int count) { | ||
| 19 | while(count--) { | ||
| 20 | _delay_us(1); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | int voices = 0; | ||
| 25 | double frequency = 0; | ||
| 26 | int volume = 0; | ||
| 27 | long position = 0; | ||
| 28 | |||
| 29 | double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 30 | int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | ||
| 31 | bool sliding = false; | ||
| 32 | #define RANGE 1000 | ||
| 33 | volatile int i=0; //elements of the wave | ||
| 34 | |||
| 35 | int max = 0xFF; | ||
| 36 | float sum = 0; | ||
| 37 | int value = 128; | ||
| 38 | float place = 0; | ||
| 39 | |||
| 40 | uint16_t place_int = 0; | ||
| 41 | bool repeat = true; | ||
| 42 | uint8_t * sample; | ||
| 43 | uint16_t sample_length = 0; | ||
| 44 | |||
| 45 | |||
| 46 | bool notes = false; | ||
| 47 | float note_frequency = 0; | ||
| 48 | float note_length = 0; | ||
| 49 | uint16_t note_position = 0; | ||
| 50 | float (* notes_pointer)[][2]; | ||
| 51 | uint8_t notes_length; | ||
| 52 | bool notes_repeat; | ||
| 53 | uint8_t current_note = 0; | ||
| 54 | |||
| 55 | void stop_all_notes() { | ||
| 56 | voices = 0; | ||
| 57 | TIMSK3 &= ~_BV(OCIE3A); | ||
| 58 | notes = false; | ||
| 59 | playing_notes = false; | ||
| 60 | frequency = 0; | ||
| 61 | volume = 0; | ||
| 62 | |||
| 63 | for (int i = 0; i < 8; i++) { | ||
| 64 | frequencies[i] = 0; | ||
| 65 | volumes[i] = 0; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | void stop_note(double freq) { | ||
| 70 | freq = freq / SAMPLE_RATE; | ||
| 71 | for (int i = 7; i >= 0; i--) { | ||
| 72 | if (frequencies[i] == freq) { | ||
| 73 | frequencies[i] = 0; | ||
| 74 | volumes[i] = 0; | ||
| 75 | for (int j = i; (j < 7); j++) { | ||
| 76 | frequencies[j] = frequencies[j+1]; | ||
| 77 | frequencies[j+1] = 0; | ||
| 78 | volumes[j] = volumes[j+1]; | ||
| 79 | volumes[j+1] = 0; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | voices--; | ||
| 84 | if (voices < 0) | ||
| 85 | voices = 0; | ||
| 86 | if (voices == 0) { | ||
| 87 | TIMSK3 &= ~_BV(OCIE3A); | ||
| 88 | frequency = 0; | ||
| 89 | volume = 0; | ||
| 90 | } else { | ||
| 91 | double freq = frequencies[voices - 1]; | ||
| 92 | int vol = volumes[voices - 1]; | ||
| 93 | double starting_f = frequency; | ||
| 94 | if (frequency < freq) { | ||
| 95 | sliding = true; | ||
| 96 | for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) { | ||
| 97 | frequency = f; | ||
| 98 | } | ||
| 99 | sliding = false; | ||
| 100 | } else if (frequency > freq) { | ||
| 101 | sliding = true; | ||
| 102 | for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) { | ||
| 103 | frequency = f; | ||
| 104 | } | ||
| 105 | sliding = false; | ||
| 106 | } | ||
| 107 | frequency = freq; | ||
| 108 | volume = vol; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | void init_notes() { | ||
| 113 | |||
| 114 | PLLFRQ = _BV(PDIV2); | ||
| 115 | PLLCSR = _BV(PLLE); | ||
| 116 | while(!(PLLCSR & _BV(PLOCK))); | ||
| 117 | PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ | ||
| 118 | |||
| 119 | /* Init a fast PWM on Timer4 */ | ||
| 120 | TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ | ||
| 121 | TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ | ||
| 122 | OCR4A = 0; | ||
| 123 | |||
| 124 | /* Enable the OC4A output */ | ||
| 125 | DDRC |= _BV(PORTC6); | ||
| 126 | |||
| 127 | TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs | ||
| 128 | |||
| 129 | TCCR3A = 0x0; // Options not needed | ||
| 130 | TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC | ||
| 131 | OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback | ||
| 132 | |||
| 133 | playing_notes = false; | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | ISR(TIMER3_COMPA_vect) { | ||
| 139 | |||
| 140 | |||
| 141 | // SINE | ||
| 142 | // OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]); | ||
| 143 | |||
| 144 | // SQUARE | ||
| 145 | // if (((int)place) >= 1024){ | ||
| 146 | // OCR4A = 0xFF; | ||
| 147 | // } else { | ||
| 148 | // OCR4A = 0x00; | ||
| 149 | // } | ||
| 150 | |||
| 151 | // SAWTOOTH | ||
| 152 | // OCR4A = (int)place / 4; | ||
| 153 | |||
| 154 | // TRIANGLE | ||
| 155 | // if (((int)place) >= 1024) { | ||
| 156 | // OCR4A = (int)place / 2; | ||
| 157 | // } else { | ||
| 158 | // OCR4A = 2048 - (int)place / 2; | ||
| 159 | // } | ||
| 160 | |||
| 161 | // place += frequency; | ||
| 162 | |||
| 163 | // if (place >= SINE_LENGTH) | ||
| 164 | // if (repeat) | ||
| 165 | // place -= SINE_LENGTH; | ||
| 166 | // else | ||
| 167 | // TIMSK3 &= ~_BV(OCIE3A); | ||
| 168 | |||
| 169 | // SAMPLE | ||
| 170 | // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]); | ||
| 171 | |||
| 172 | // place_int++; | ||
| 173 | |||
| 174 | // if (place_int >= sample_length) | ||
| 175 | // if (repeat) | ||
| 176 | // place_int -= sample_length; | ||
| 177 | // else | ||
| 178 | // TIMSK3 &= ~_BV(OCIE3A); | ||
| 179 | |||
| 180 | |||
| 181 | if (notes) { | ||
| 182 | OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0; | ||
| 183 | |||
| 184 | place += note_frequency; | ||
| 185 | if (place >= SINE_LENGTH) | ||
| 186 | place -= SINE_LENGTH; | ||
| 187 | note_position++; | ||
| 188 | if (note_position >= note_length) { | ||
| 189 | current_note++; | ||
| 190 | if (current_note >= notes_length) { | ||
| 191 | if (notes_repeat) { | ||
| 192 | current_note = 0; | ||
| 193 | } else { | ||
| 194 | TIMSK3 &= ~_BV(OCIE3A); | ||
| 195 | notes = false; | ||
| 196 | playing_notes = false; | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; | ||
| 201 | note_length = (*notes_pointer)[current_note][1]; | ||
| 202 | note_position = 0; | ||
| 203 | } | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | } | ||
| 208 | |||
| 209 | void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) { | ||
| 210 | notes = true; | ||
| 211 | |||
| 212 | notes_pointer = np; | ||
| 213 | notes_length = n_length; | ||
| 214 | notes_repeat = n_repeat; | ||
| 215 | |||
| 216 | place = 0; | ||
| 217 | current_note = 0; | ||
| 218 | note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; | ||
| 219 | note_length = (*notes_pointer)[current_note][1]; | ||
| 220 | // note_frequency = 880.0 / SAMPLE_RATE; | ||
| 221 | // note_length = 1000; | ||
| 222 | note_position = 0; | ||
| 223 | |||
| 224 | |||
| 225 | TIMSK3 |= _BV(OCIE3A); | ||
| 226 | playing_notes = true; | ||
| 227 | } | ||
| 228 | |||
| 229 | void play_sample(uint8_t * s, uint16_t l, bool r) { | ||
| 230 | place_int = 0; | ||
| 231 | sample = s; | ||
| 232 | sample_length = l; | ||
| 233 | repeat = r; | ||
| 234 | |||
| 235 | TIMSK3 |= _BV(OCIE3A); | ||
| 236 | playing_notes = true; | ||
| 237 | } | ||
| 238 | |||
| 239 | void play_note(double freq, int vol) { | ||
| 240 | |||
| 241 | freq = freq / SAMPLE_RATE; | ||
| 242 | if (freq > 0) { | ||
| 243 | if (frequency != 0) { | ||
| 244 | double starting_f = frequency; | ||
| 245 | if (frequency < freq) { | ||
| 246 | for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 500.0)) { | ||
| 247 | frequency = f; | ||
| 248 | } | ||
| 249 | } else if (frequency > freq) { | ||
| 250 | for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 500.0)) { | ||
| 251 | frequency = f; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | } | ||
| 255 | frequency = freq; | ||
| 256 | volume = vol; | ||
| 257 | |||
| 258 | frequencies[voices] = frequency; | ||
| 259 | volumes[voices] = volume; | ||
| 260 | voices++; | ||
| 261 | } | ||
| 262 | |||
| 263 | TIMSK3 |= _BV(OCIE3A); | ||
| 264 | |||
| 265 | } \ No newline at end of file | ||
