4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
15 #define CPU_PRESCALER 8
21 #define SAMPLE_DIVIDER 39
22 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
23 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
26 void delay_us(int count) {
40 double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
41 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
48 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
50 uint16_t place_int = 0;
53 uint16_t sample_length = 0;
58 float note_frequency = 0;
59 float note_length = 0;
60 uint16_t note_position = 0;
61 float (* notes_pointer)[][2];
65 bool note_resting = false;
67 uint8_t current_note = 0;
68 uint8_t rest_counter = 0;
70 audio_config_t audio_config;
73 void audio_toggle(void) {
74 audio_config.enable ^= 1;
75 eeconfig_write_audio(audio_config.raw);
79 audio_config.enable = 1;
80 eeconfig_write_audio(audio_config.raw);
83 void audio_off(void) {
84 audio_config.enable = 0;
85 eeconfig_write_audio(audio_config.raw);
89 void stop_all_notes() {
92 TIMSK3 &= ~_BV(OCIE3A);
94 TIMSK3 &= ~_BV(OCIE3A);
95 TCCR3A &= ~_BV(COM3A1);
102 for (int i = 0; i < 8; i++) {
108 void stop_note(double freq) {
111 freq = freq / SAMPLE_RATE;
113 for (int i = 7; i >= 0; i--) {
114 if (frequencies[i] == freq) {
117 for (int j = i; (j < 7); j++) {
118 frequencies[j] = frequencies[j+1];
119 frequencies[j+1] = 0;
120 volumes[j] = volumes[j+1];
130 TIMSK3 &= ~_BV(OCIE3A);
132 TIMSK3 &= ~_BV(OCIE3A);
133 TCCR3A &= ~_BV(COM3A1);
139 double freq = frequencies[voices - 1];
140 int vol = volumes[voices - 1];
141 double starting_f = frequency;
142 if (frequency < freq) {
144 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
148 } else if (frequency > freq) {
150 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
163 /* check signature */
164 if (!eeconfig_is_enabled()) {
167 audio_config.raw = eeconfig_read_audio();
172 while(!(PLLCSR & _BV(PLOCK)));
173 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
175 /* Init a fast PWM on Timer4 */
176 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
177 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
180 /* Enable the OC4A output */
183 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
185 TCCR3A = 0x0; // Options not needed
186 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
187 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
191 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
193 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
194 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
199 ISR(TIMER3_COMPA_vect) {
204 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
207 // if (((int)place) >= 1024){
208 // OCR4A = 0xFF >> 2;
214 // OCR4A = (int)place / 4;
217 // if (((int)place) >= 1024) {
218 // OCR4A = (int)place / 2;
220 // OCR4A = 2048 - (int)place / 2;
225 if (place >= SINE_LENGTH)
226 place -= SINE_LENGTH;
230 for (int i = 0; i < voices; i++) {
232 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
235 // if (((int)places[i]) >= 1024){
241 places[i] += frequencies[i];
243 if (places[i] >= SINE_LENGTH)
244 places[i] -= SINE_LENGTH;
250 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
251 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
252 voice_place %= voices;
253 if (place > (frequencies[voice_place] / 50)) {
254 voice_place = (voice_place + 1) % voices;
257 ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period
258 OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period
260 // if (duty_counter > (frequencies[voice_place] / 500)) {
261 // duty_place = (duty_place % 3) + 1;
270 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
274 // if (place_int >= sample_length)
276 // place_int -= sample_length;
278 // TIMSK3 &= ~_BV(OCIE3A);
283 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
285 place += note_frequency;
286 if (place >= SINE_LENGTH)
287 place -= SINE_LENGTH;
289 if (note_frequency > 0) {
290 ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period
291 OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period
300 bool end_of_note = false;
302 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
304 end_of_note = (note_position >= (note_length * 0x7FF));
307 if (current_note >= notes_length) {
312 TIMSK3 &= ~_BV(OCIE3A);
314 TIMSK3 &= ~_BV(OCIE3A);
315 TCCR3A &= ~_BV(COM3A1);
321 if (!note_resting && (notes_rest > 0)) {
324 note_length = notes_rest;
327 note_resting = false;
329 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
330 note_length = (*notes_pointer)[current_note][1];
332 note_frequency = (*notes_pointer)[current_note][0];
333 note_length = (*notes_pointer)[current_note][1] / 4;
341 if (!audio_config.enable) {
347 void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat, float n_rest) {
349 if (audio_config.enable) {
356 notes_length = n_length;
357 notes_repeat = n_repeat;
363 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
364 note_length = (*notes_pointer)[current_note][1];
366 note_frequency = (*notes_pointer)[current_note][0];
367 note_length = (*notes_pointer)[current_note][1] / 4;
373 TIMSK3 |= _BV(OCIE3A);
375 TIMSK3 |= _BV(OCIE3A);
376 TCCR3A |= _BV(COM3A1);
382 void play_sample(uint8_t * s, uint16_t l, bool r) {
384 if (audio_config.enable) {
393 TIMSK3 |= _BV(OCIE3A);
401 void play_note(double freq, int vol) {
403 if (audio_config.enable && voices < 8) {
409 freq = freq / SAMPLE_RATE;
412 if (frequency != 0) {
413 double starting_f = frequency;
414 if (frequency < freq) {
415 for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) {
418 } else if (frequency > freq) {
419 for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) {
427 frequencies[voices] = frequency;
428 volumes[voices] = volume;
433 TIMSK3 |= _BV(OCIE3A);
435 TIMSK3 |= _BV(OCIE3A);
436 TCCR3A |= _BV(COM3A1);