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 float note_tempo = TEMPO_DEFAULT;
61 float note_timbre = TIMBRE_DEFAULT;
62 uint16_t note_position = 0;
63 float (* notes_pointer)[][2];
67 bool note_resting = false;
70 uint8_t current_note = 0;
71 uint8_t rest_counter = 0;
73 audio_config_t audio_config;
76 void audio_toggle(void) {
77 audio_config.enable ^= 1;
78 eeconfig_write_audio(audio_config.raw);
82 audio_config.enable = 1;
83 eeconfig_write_audio(audio_config.raw);
86 void audio_off(void) {
87 audio_config.enable = 0;
88 eeconfig_write_audio(audio_config.raw);
92 void stop_all_notes() {
95 TIMSK3 &= ~_BV(OCIE3A);
97 TIMSK3 &= ~_BV(OCIE3A);
98 TCCR3A &= ~_BV(COM3A1);
105 for (int i = 0; i < 8; i++) {
111 void stop_note(double freq) {
114 freq = freq / SAMPLE_RATE;
116 for (int i = 7; i >= 0; i--) {
117 if (frequencies[i] == freq) {
120 for (int j = i; (j < 7); j++) {
121 frequencies[j] = frequencies[j+1];
122 frequencies[j+1] = 0;
123 volumes[j] = volumes[j+1];
131 if (voice_place >= voices) {
136 TIMSK3 &= ~_BV(OCIE3A);
138 TIMSK3 &= ~_BV(OCIE3A);
139 TCCR3A &= ~_BV(COM3A1);
150 /* check signature */
151 if (!eeconfig_is_enabled()) {
154 audio_config.raw = eeconfig_read_audio();
159 while(!(PLLCSR & _BV(PLOCK)));
160 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
162 /* Init a fast PWM on Timer4 */
163 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
164 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
167 /* Enable the OC4A output */
170 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
172 TCCR3A = 0x0; // Options not needed
173 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
174 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
178 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
180 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
181 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
186 ISR(TIMER3_COMPA_vect) {
191 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
194 // if (((int)place) >= 1024){
195 // OCR4A = 0xFF >> 2;
201 // OCR4A = (int)place / 4;
204 // if (((int)place) >= 1024) {
205 // OCR4A = (int)place / 2;
207 // OCR4A = 2048 - (int)place / 2;
212 if (place >= SINE_LENGTH)
213 place -= SINE_LENGTH;
217 for (int i = 0; i < voices; i++) {
219 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
222 // if (((int)places[i]) >= 1024){
228 places[i] += frequencies[i];
230 if (places[i] >= SINE_LENGTH)
231 places[i] -= SINE_LENGTH;
236 if (frequencies[voice_place] > 0) {
237 // if (frequencies[voice_place] > 880.0) {
238 // if (note_flipper == 100) {
246 // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period
247 // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period
251 voice_place %= voices;
252 if (place > (frequencies[voice_place] / 50)) {
253 voice_place = (voice_place + 1) % voices;
256 freq = frequencies[voice_place];
258 if (frequency != 0) {
259 if (frequency < frequencies[voices - 1]) {
260 frequency = frequency * 1.01454533494;
261 } else if (frequency > frequencies[voices - 1]) {
262 frequency = frequency * 0.98566319864;
265 frequency = frequencies[voices - 1];
269 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
270 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
271 //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period
273 // if (duty_counter > (frequencies[voice_place] / 500)) {
274 // duty_place = (duty_place % 3) + 1;
283 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
287 // if (place_int >= sample_length)
289 // place_int -= sample_length;
291 // TIMSK3 &= ~_BV(OCIE3A);
296 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
298 place += note_frequency;
299 if (place >= SINE_LENGTH)
300 place -= SINE_LENGTH;
302 if (note_frequency > 0) {
303 ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period
304 OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
313 bool end_of_note = false;
315 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
317 end_of_note = (note_position >= (note_length * 0x7FF));
320 if (current_note >= notes_count) {
325 TIMSK3 &= ~_BV(OCIE3A);
327 TIMSK3 &= ~_BV(OCIE3A);
328 TCCR3A &= ~_BV(COM3A1);
334 if (!note_resting && (notes_rest > 0)) {
337 note_length = notes_rest;
340 note_resting = false;
342 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
343 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
345 note_frequency = (*notes_pointer)[current_note][0];
346 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
354 if (!audio_config.enable) {
360 void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) {
362 if (audio_config.enable) {
364 // Cancel note if a note is playing
370 notes_count = n_count;
371 notes_repeat = n_repeat;
377 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
378 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
380 note_frequency = (*notes_pointer)[current_note][0];
381 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
387 TIMSK3 |= _BV(OCIE3A);
389 TIMSK3 |= _BV(OCIE3A);
390 TCCR3A |= _BV(COM3A1);
397 void play_sample(uint8_t * s, uint16_t l, bool r) {
399 if (audio_config.enable) {
408 TIMSK3 |= _BV(OCIE3A);
416 void play_note(double freq, int vol) {
418 if (audio_config.enable && voices < 8) {
420 // Cancel notes if notes are playing
425 freq = freq / SAMPLE_RATE;
428 frequencies[voices] = freq;
429 volumes[voices] = vol;
434 TIMSK3 |= _BV(OCIE3A);
436 TIMSK3 |= _BV(OCIE3A);
437 TCCR3A |= _BV(COM3A1);
444 void set_timbre(float timbre)
446 note_timbre = timbre;
449 void set_tempo(float tempo)
454 void decrease_tempo(uint8_t tempo_change)
456 note_tempo += (float) tempo_change;
459 void increase_tempo(uint8_t tempo_change)
461 if (note_tempo - (float) tempo_change < 10)
467 note_tempo -= (float) tempo_change;
471 //------------------------------------------------------------------------------
472 // Override these functions in your keymap file to play different tunes on
473 // startup and bootloader jump
474 __attribute__ ((weak))
475 void play_startup_tone()
479 __attribute__ ((weak))
480 void play_goodbye_tone()
483 //------------------------------------------------------------------------------