4 #include <avr/pgmspace.h>
5 #include <avr/interrupt.h>
9 #include "keymap_common.h"
14 #include "vibrato_lut.h"
19 #define CPU_PRESCALER 8
23 #define SAMPLE_DIVIDER 39
24 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048)
25 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
27 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
28 uint16_t place_int = 0;
32 void delay_us(int count) {
44 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
45 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 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;
69 uint8_t current_note = 0;
70 uint8_t rest_counter = 0;
73 float vibrato_counter = 0;
74 float vibrato_strength = .5;
75 float vibrato_rate = 0.125;
78 float polyphony_rate = 0;
82 audio_config_t audio_config;
84 uint16_t envelope_index = 0;
86 void audio_toggle(void) {
87 audio_config.enable ^= 1;
88 eeconfig_write_audio(audio_config.raw);
92 audio_config.enable = 1;
93 eeconfig_write_audio(audio_config.raw);
96 void audio_off(void) {
97 audio_config.enable = 0;
98 eeconfig_write_audio(audio_config.raw);
101 #ifdef VIBRATO_ENABLE
102 // Vibrato rate functions
104 void set_vibrato_rate(float rate) {
108 void increase_vibrato_rate(float change) {
109 vibrato_rate *= change;
112 void decrease_vibrato_rate(float change) {
113 vibrato_rate /= change;
116 #ifdef VIBRATO_STRENGTH_ENABLE
118 void set_vibrato_strength(float strength) {
119 vibrato_strength = strength;
122 void increase_vibrato_strength(float change) {
123 vibrato_strength *= change;
126 void decrease_vibrato_strength(float change) {
127 vibrato_strength /= change;
134 // Polyphony functions
136 void set_polyphony_rate(float rate) {
137 polyphony_rate = rate;
140 void enable_polyphony() {
144 void disable_polyphony() {
148 void increase_polyphony_rate(float change) {
149 polyphony_rate *= change;
152 void decrease_polyphony_rate(float change) {
153 polyphony_rate /= change;
158 void set_timbre(float timbre) {
159 note_timbre = timbre;
164 void set_tempo(float tempo) {
168 void decrease_tempo(uint8_t tempo_change) {
169 note_tempo += (float) tempo_change;
172 void increase_tempo(uint8_t tempo_change) {
173 if (note_tempo - (float) tempo_change < 10) {
176 note_tempo -= (float) tempo_change;
182 /* check signature */
183 if (!eeconfig_is_enabled()) {
186 audio_config.raw = eeconfig_read_audio();
191 while(!(PLLCSR & _BV(PLOCK)));
192 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
194 /* Init a fast PWM on Timer4 */
195 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
196 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
199 /* Enable the OC4A output */
202 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
204 TCCR3A = 0x0; // Options not needed
205 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
206 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
210 TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs
212 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
213 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
219 void stop_all_notes() {
225 TIMSK3 &= ~_BV(OCIE3A);
227 TIMSK3 &= ~_BV(OCIE3A);
228 TCCR3A &= ~_BV(COM3A1);
235 for (int i = 0; i < 8; i++) {
241 void stop_note(float freq) {
247 freq = freq / SAMPLE_RATE;
249 for (int i = 7; i >= 0; i--) {
250 if (frequencies[i] == freq) {
253 for (int j = i; (j < 7); j++) {
254 frequencies[j] = frequencies[j+1];
255 frequencies[j+1] = 0;
256 volumes[j] = volumes[j+1];
265 if (voice_place >= voices) {
270 TIMSK3 &= ~_BV(OCIE3A);
272 TIMSK3 &= ~_BV(OCIE3A);
273 TCCR3A &= ~_BV(COM3A1);
282 #ifdef VIBRATO_ENABLE
284 float mod(float a, int b)
286 float r = fmod(a, b);
287 return r < 0 ? r + b : r;
290 float vibrato(float average_freq) {
291 #ifdef VIBRATO_STRENGTH_ENABLE
292 float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength);
294 float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter];
296 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
297 return vibrated_freq;
302 float envelope(float f) {
303 uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f));
304 switch (compensated_index) {
307 note_timbre = TIMBRE_12;
311 note_timbre = TIMBRE_12;
314 note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125;
323 ISR(TIMER3_COMPA_vect) {
328 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
331 // if (((int)place) >= 1024){
332 // OCR4A = 0xFF >> 2;
338 // OCR4A = (int)place / 4;
341 // if (((int)place) >= 1024) {
342 // OCR4A = (int)place / 2;
344 // OCR4A = 2048 - (int)place / 2;
349 if (place >= SINE_LENGTH)
350 place -= SINE_LENGTH;
354 for (int i = 0; i < voices; i++) {
356 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
359 // if (((int)places[i]) >= 1024){
365 places[i] += frequencies[i];
367 if (places[i] >= SINE_LENGTH)
368 places[i] -= SINE_LENGTH;
375 if (polyphony_rate > 0) {
377 voice_place %= voices;
378 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
379 voice_place = (voice_place + 1) % voices;
383 #ifdef VIBRATO_ENABLE
384 if (vibrato_strength > 0) {
385 freq = vibrato(frequencies[voice_place]);
390 freq = frequencies[voice_place];
393 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
394 frequency = frequency * pow(2, 440/frequency/12/2);
395 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
396 frequency = frequency * pow(2, -440/frequency/12/2);
398 frequency = frequencies[voices - 1];
402 #ifdef VIBRATO_ENABLE
403 if (vibrato_strength > 0) {
404 freq = vibrato(frequency);
413 if (envelope_index < 65535) {
416 freq = envelope(freq);
418 if (freq < 30.517578125)
420 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
421 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
427 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
431 // if (place_int >= sample_length)
433 // place_int -= sample_length;
435 // TIMSK3 &= ~_BV(OCIE3A);
440 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
442 place += note_frequency;
443 if (place >= SINE_LENGTH)
444 place -= SINE_LENGTH;
446 if (note_frequency > 0) {
449 #ifdef VIBRATO_ENABLE
450 if (vibrato_strength > 0) {
451 freq = vibrato(note_frequency);
456 freq = note_frequency;
459 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
460 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
469 bool end_of_note = false;
471 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
473 end_of_note = (note_position >= (note_length * 0x7FF));
476 if (current_note >= notes_count) {
481 TIMSK3 &= ~_BV(OCIE3A);
483 TIMSK3 &= ~_BV(OCIE3A);
484 TCCR3A &= ~_BV(COM3A1);
490 if (!note_resting && (notes_rest > 0)) {
493 note_length = notes_rest;
496 note_resting = false;
498 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
499 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
501 note_frequency = (*notes_pointer)[current_note][0];
502 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
510 if (!audio_config.enable) {
516 void play_note(float freq, int vol) {
522 if (audio_config.enable && voices < 8) {
523 TIMSK3 &= ~_BV(OCIE3A);
524 // Cancel notes if notes are playing
530 freq = freq / SAMPLE_RATE;
533 frequencies[voices] = freq;
534 volumes[voices] = vol;
539 TIMSK3 |= _BV(OCIE3A);
541 TIMSK3 |= _BV(OCIE3A);
542 TCCR3A |= _BV(COM3A1);
548 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
554 if (audio_config.enable) {
555 TIMSK3 &= ~_BV(OCIE3A);
556 // Cancel note if a note is playing
562 notes_count = n_count;
563 notes_repeat = n_repeat;
569 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
570 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
572 note_frequency = (*notes_pointer)[current_note][0];
573 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
579 TIMSK3 |= _BV(OCIE3A);
581 TIMSK3 |= _BV(OCIE3A);
582 TCCR3A |= _BV(COM3A1);
589 void play_sample(uint8_t * s, uint16_t l, bool r) {
594 if (audio_config.enable) {
595 TIMSK3 &= ~_BV(OCIE3A);
602 TIMSK3 |= _BV(OCIE3A);
607 //------------------------------------------------------------------------------
608 // Override these functions in your keymap file to play different tunes on
609 // startup and bootloader jump
610 __attribute__ ((weak))
611 void play_startup_tone()
617 __attribute__ ((weak))
618 void play_goodbye_tone()
622 //------------------------------------------------------------------------------