4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
15 #define CPU_PRESCALER 8
20 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
21 // Turn on/off 3A interputs, stopping/enabling the ISR calls
22 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
23 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
26 // TCCR3A: Timer/Counter #3 Control Register
27 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
28 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
29 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
32 #define NOTE_PERIOD ICR3
33 #define NOTE_DUTY_CYCLE OCR3A
38 #define SAMPLE_DIVIDER 39
39 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
40 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
42 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
43 uint16_t place_int = 0;
47 void delay_us(int count) {
59 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
60 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
66 uint16_t sample_length = 0;
69 bool playing_notes = false;
70 bool playing_note = false;
71 float note_frequency = 0;
72 float note_length = 0;
73 uint8_t note_tempo = TEMPO_DEFAULT;
74 float note_timbre = TIMBRE_DEFAULT;
75 uint16_t note_position = 0;
76 float (* notes_pointer)[][2];
80 bool note_resting = false;
82 uint8_t current_note = 0;
83 uint8_t rest_counter = 0;
86 float vibrato_counter = 0;
87 float vibrato_strength = .5;
88 float vibrato_rate = 0.125;
91 float polyphony_rate = 0;
93 static bool audio_initialized = false;
95 audio_config_t audio_config;
97 uint16_t envelope_index = 0;
102 if (!eeconfig_is_enabled())
106 audio_config.raw = eeconfig_read_audio();
112 while(!(PLLCSR & _BV(PLOCK)));
113 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
115 /* Init a fast PWM on Timer4 */
116 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
117 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
120 /* Enable the OC4A output */
123 DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
125 TCCR3A = 0x0; // Options not needed
126 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
127 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
131 // Set port PC6 (OC3A and /OC4A) as output
134 DISABLE_AUDIO_COUNTER_3_ISR;
136 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
137 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
138 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
139 // Clock Select (CS3n) = 0b010 = Clock / 8
140 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
141 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
145 audio_initialized = true;
148 void stop_all_notes() {
149 if (!audio_initialized) {
154 DISABLE_AUDIO_COUNTER_3_ISR;
156 DISABLE_AUDIO_COUNTER_3_ISR;
157 DISABLE_AUDIO_COUNTER_3_OUTPUT;
160 playing_notes = false;
161 playing_note = false;
165 for (uint8_t i = 0; i < 8; i++)
172 void stop_note(float freq)
175 if (!audio_initialized) {
179 freq = freq / SAMPLE_RATE;
181 for (int i = 7; i >= 0; i--) {
182 if (frequencies[i] == freq) {
185 for (int j = i; (j < 7); j++) {
186 frequencies[j] = frequencies[j+1];
187 frequencies[j+1] = 0;
188 volumes[j] = volumes[j+1];
197 if (voice_place >= voices) {
202 DISABLE_AUDIO_COUNTER_3_ISR;
204 DISABLE_AUDIO_COUNTER_3_ISR;
205 DISABLE_AUDIO_COUNTER_3_OUTPUT;
209 playing_note = false;
214 #ifdef VIBRATO_ENABLE
216 float mod(float a, int b)
218 float r = fmod(a, b);
219 return r < 0 ? r + b : r;
222 float vibrato(float average_freq) {
223 #ifdef VIBRATO_STRENGTH_ENABLE
224 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
226 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
228 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
229 return vibrated_freq;
234 ISR(TIMER3_COMPA_vect)
240 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
243 // if (((int)place) >= 1024){
244 // OCR4A = 0xFF >> 2;
250 // OCR4A = (int)place / 4;
253 // if (((int)place) >= 1024) {
254 // OCR4A = (int)place / 2;
256 // OCR4A = 2048 - (int)place / 2;
261 if (place >= SINE_LENGTH)
262 place -= SINE_LENGTH;
266 for (int i = 0; i < voices; i++) {
268 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
271 // if (((int)places[i]) >= 1024){
277 places[i] += frequencies[i];
279 if (places[i] >= SINE_LENGTH)
280 places[i] -= SINE_LENGTH;
287 if (polyphony_rate > 0) {
289 voice_place %= voices;
290 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
291 voice_place = (voice_place + 1) % voices;
295 #ifdef VIBRATO_ENABLE
296 if (vibrato_strength > 0) {
297 freq = vibrato(frequencies[voice_place]);
302 freq = frequencies[voice_place];
305 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
306 frequency = frequency * pow(2, 440/frequency/12/2);
307 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
308 frequency = frequency * pow(2, -440/frequency/12/2);
310 frequency = frequencies[voices - 1];
314 #ifdef VIBRATO_ENABLE
315 if (vibrato_strength > 0) {
316 freq = vibrato(frequency);
325 if (envelope_index < 65535) {
328 freq = voice_envelope(freq);
330 if (freq < 30.517578125)
332 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
333 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
339 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
343 // if (place_int >= sample_length)
345 // place_int -= sample_length;
347 // DISABLE_AUDIO_COUNTER_3_ISR;
352 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
354 place += note_frequency;
355 if (place >= SINE_LENGTH)
356 place -= SINE_LENGTH;
358 if (note_frequency > 0) {
361 #ifdef VIBRATO_ENABLE
362 if (vibrato_strength > 0) {
363 freq = vibrato(note_frequency);
368 freq = note_frequency;
371 if (envelope_index < 65535) {
374 freq = voice_envelope(freq);
376 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
377 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
386 bool end_of_note = false;
388 end_of_note = (note_position >= (note_length / NOTE_PERIOD * 0xFFFF));
390 end_of_note = (note_position >= (note_length * 0x7FF));
393 if (current_note >= notes_count) {
398 DISABLE_AUDIO_COUNTER_3_ISR;
400 DISABLE_AUDIO_COUNTER_3_ISR;
401 DISABLE_AUDIO_COUNTER_3_OUTPUT;
403 playing_notes = false;
407 if (!note_resting && (notes_rest > 0)) {
410 note_length = notes_rest;
413 note_resting = false;
415 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
416 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
419 note_frequency = (*notes_pointer)[current_note][0];
420 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
428 if (!audio_config.enable) {
429 playing_notes = false;
430 playing_note = false;
434 void play_note(float freq, int vol) {
436 if (!audio_initialized) {
440 if (audio_config.enable && voices < 8) {
441 DISABLE_AUDIO_COUNTER_3_ISR;
443 // Cancel notes if notes are playing
452 freq = freq / SAMPLE_RATE;
455 frequencies[voices] = freq;
456 volumes[voices] = vol;
461 ENABLE_AUDIO_COUNTER_3_ISR;
463 ENABLE_AUDIO_COUNTER_3_ISR;
464 ENABLE_AUDIO_COUNTER_3_OUTPUT;
470 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
473 if (!audio_initialized) {
477 if (audio_config.enable) {
479 DISABLE_AUDIO_COUNTER_3_ISR;
481 // Cancel note if a note is playing
485 playing_notes = true;
488 notes_count = n_count;
489 notes_repeat = n_repeat;
496 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
497 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
499 note_frequency = (*notes_pointer)[current_note][0];
500 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
506 ENABLE_AUDIO_COUNTER_3_ISR;
508 ENABLE_AUDIO_COUNTER_3_ISR;
509 ENABLE_AUDIO_COUNTER_3_OUTPUT;
516 void play_sample(uint8_t * s, uint16_t l, bool r) {
517 if (!audio_initialized) {
521 if (audio_config.enable) {
522 DISABLE_AUDIO_COUNTER_3_ISR;
529 ENABLE_AUDIO_COUNTER_3_ISR;
535 void audio_toggle(void) {
536 audio_config.enable ^= 1;
537 eeconfig_update_audio(audio_config.raw);
540 void audio_on(void) {
541 audio_config.enable = 1;
542 eeconfig_update_audio(audio_config.raw);
545 void audio_off(void) {
546 audio_config.enable = 0;
547 eeconfig_update_audio(audio_config.raw);
550 #ifdef VIBRATO_ENABLE
552 // Vibrato rate functions
554 void set_vibrato_rate(float rate) {
558 void increase_vibrato_rate(float change) {
559 vibrato_rate *= change;
562 void decrease_vibrato_rate(float change) {
563 vibrato_rate /= change;
566 #ifdef VIBRATO_STRENGTH_ENABLE
568 void set_vibrato_strength(float strength) {
569 vibrato_strength = strength;
572 void increase_vibrato_strength(float change) {
573 vibrato_strength *= change;
576 void decrease_vibrato_strength(float change) {
577 vibrato_strength /= change;
580 #endif /* VIBRATO_STRENGTH_ENABLE */
582 #endif /* VIBRATO_ENABLE */
584 // Polyphony functions
586 void set_polyphony_rate(float rate) {
587 polyphony_rate = rate;
590 void enable_polyphony() {
594 void disable_polyphony() {
598 void increase_polyphony_rate(float change) {
599 polyphony_rate *= change;
602 void decrease_polyphony_rate(float change) {
603 polyphony_rate /= change;
608 void set_timbre(float timbre) {
609 note_timbre = timbre;
614 void set_tempo(uint8_t tempo) {
618 void decrease_tempo(uint8_t tempo_change) {
619 note_tempo += tempo_change;
622 void increase_tempo(uint8_t tempo_change) {
623 if (note_tempo - tempo_change < 10) {
626 note_tempo -= tempo_change;
631 //------------------------------------------------------------------------------
632 // Override these functions in your keymap file to play different tunes on
633 // startup and bootloader jump
634 __attribute__ ((weak))
635 void play_startup_tone()
639 __attribute__ ((weak))
640 void play_goodbye_tone()
643 //------------------------------------------------------------------------------