4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
17 #define SAMPLE_DIVIDER 39
18 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
19 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
22 void delay_us(int count) {
34 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
35 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
44 uint16_t place_int = 0;
47 uint16_t sample_length = 0;
52 float note_frequency = 0;
53 float note_length = 0;
54 uint16_t note_position = 0;
55 float (* notes_pointer)[][2];
58 uint8_t current_note = 0;
60 void stop_all_notes() {
63 TIMSK3 &= ~_BV(OCIE3A);
65 TIMSK3 &= ~_BV(OCIE3A);
66 TCCR3A &= ~_BV(COM3A1);
73 for (int i = 0; i < 8; i++) {
79 void stop_note(double freq) {
81 freq = freq / SAMPLE_RATE;
83 for (int i = 7; i >= 0; i--) {
84 if (frequencies[i] == freq) {
87 for (int j = i; (j < 7); j++) {
88 frequencies[j] = frequencies[j+1];
90 volumes[j] = volumes[j+1];
100 TIMSK3 &= ~_BV(OCIE3A);
102 TIMSK3 &= ~_BV(OCIE3A);
103 TCCR3A &= ~_BV(COM3A1);
109 double freq = frequencies[voices - 1];
110 int vol = volumes[voices - 1];
111 double starting_f = frequency;
112 if (frequency < freq) {
114 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
118 } else if (frequency > freq) {
120 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
135 while(!(PLLCSR & _BV(PLOCK)));
136 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
138 /* Init a fast PWM on Timer4 */
139 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
140 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
143 /* Enable the OC4A output */
146 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
148 TCCR3A = 0x0; // Options not needed
149 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
150 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
154 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
156 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
157 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
162 ISR(TIMER3_COMPA_vect) {
168 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
171 // if (((int)place) >= 1024){
172 // OCR4A = 0xFF >> 2;
178 // OCR4A = (int)place / 4;
181 // if (((int)place) >= 1024) {
182 // OCR4A = (int)place / 2;
184 // OCR4A = 2048 - (int)place / 2;
189 if (place >= SINE_LENGTH)
190 place -= SINE_LENGTH;
194 for (int i = 0; i < voices; i++) {
196 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
199 // if (((int)places[i]) >= 1024){
205 places[i] += frequencies[i];
207 if (places[i] >= SINE_LENGTH)
208 places[i] -= SINE_LENGTH;
214 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
215 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
217 voice_place = (voice_place + 1) % voices;
220 ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period
221 OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1; // Set compare to half the period
228 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
232 // if (place_int >= sample_length)
234 // place_int -= sample_length;
236 // TIMSK3 &= ~_BV(OCIE3A);
241 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
243 place += note_frequency;
244 if (place >= SINE_LENGTH)
245 place -= SINE_LENGTH;
247 if (note_frequency > 0) {
248 ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period
249 OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period
258 bool end_of_note = false;
260 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
262 end_of_note = (note_position >= (note_length * 0x7FF));
265 if (current_note >= notes_length) {
270 TIMSK3 &= ~_BV(OCIE3A);
272 TIMSK3 &= ~_BV(OCIE3A);
273 TCCR3A &= ~_BV(COM3A1);
280 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
281 note_length = (*notes_pointer)[current_note][1];
283 note_frequency = (*notes_pointer)[current_note][0];
284 note_length = (*notes_pointer)[current_note][1] / 4;
293 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) {
299 notes_length = n_length;
300 notes_repeat = n_repeat;
305 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
306 note_length = (*notes_pointer)[current_note][1];
308 note_frequency = (*notes_pointer)[current_note][0];
309 note_length = (*notes_pointer)[current_note][1] / 4;
315 TIMSK3 |= _BV(OCIE3A);
317 TIMSK3 |= _BV(OCIE3A);
318 TCCR3A |= _BV(COM3A1);
322 void play_sample(uint8_t * s, uint16_t l, bool r) {
330 TIMSK3 |= _BV(OCIE3A);
335 void play_note(double freq, int vol) {
340 freq = freq / SAMPLE_RATE;
343 if (frequency != 0) {
344 double starting_f = frequency;
345 if (frequency < freq) {
346 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
349 } else if (frequency > freq) {
350 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
358 frequencies[voices] = frequency;
359 volumes[voices] = volume;
364 TIMSK3 |= _BV(OCIE3A);
366 TIMSK3 |= _BV(OCIE3A);
367 TCCR3A |= _BV(COM3A1);