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_update_audio(audio_config.raw);
92 audio_config.enable = 1;
93 eeconfig_update_audio(audio_config.raw);
96 void audio_off(void) {
97 audio_config.enable = 0;
98 eeconfig_update_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 ISR(TIMER3_COMPA_vect) {
307 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
310 // if (((int)place) >= 1024){
311 // OCR4A = 0xFF >> 2;
317 // OCR4A = (int)place / 4;
320 // if (((int)place) >= 1024) {
321 // OCR4A = (int)place / 2;
323 // OCR4A = 2048 - (int)place / 2;
328 if (place >= SINE_LENGTH)
329 place -= SINE_LENGTH;
333 for (int i = 0; i < voices; i++) {
335 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
338 // if (((int)places[i]) >= 1024){
344 places[i] += frequencies[i];
346 if (places[i] >= SINE_LENGTH)
347 places[i] -= SINE_LENGTH;
354 if (polyphony_rate > 0) {
356 voice_place %= voices;
357 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
358 voice_place = (voice_place + 1) % voices;
362 #ifdef VIBRATO_ENABLE
363 if (vibrato_strength > 0) {
364 freq = vibrato(frequencies[voice_place]);
369 freq = frequencies[voice_place];
372 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
373 frequency = frequency * pow(2, 440/frequency/12/2);
374 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
375 frequency = frequency * pow(2, -440/frequency/12/2);
377 frequency = frequencies[voices - 1];
381 #ifdef VIBRATO_ENABLE
382 if (vibrato_strength > 0) {
383 freq = vibrato(frequency);
392 if (envelope_index < 65535) {
395 freq = voice_envelope(freq);
397 if (freq < 30.517578125)
399 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
400 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
406 // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]);
410 // if (place_int >= sample_length)
412 // place_int -= sample_length;
414 // TIMSK3 &= ~_BV(OCIE3A);
419 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
421 place += note_frequency;
422 if (place >= SINE_LENGTH)
423 place -= SINE_LENGTH;
425 if (note_frequency > 0) {
428 #ifdef VIBRATO_ENABLE
429 if (vibrato_strength > 0) {
430 freq = vibrato(note_frequency);
435 freq = note_frequency;
438 if (envelope_index < 65535) {
441 freq = voice_envelope(freq);
443 ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
444 OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
453 bool end_of_note = false;
455 end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF));
457 end_of_note = (note_position >= (note_length * 0x7FF));
460 if (current_note >= notes_count) {
465 TIMSK3 &= ~_BV(OCIE3A);
467 TIMSK3 &= ~_BV(OCIE3A);
468 TCCR3A &= ~_BV(COM3A1);
474 if (!note_resting && (notes_rest > 0)) {
477 note_length = notes_rest;
480 note_resting = false;
482 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
483 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
486 note_frequency = (*notes_pointer)[current_note][0];
487 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
495 if (!audio_config.enable) {
501 void play_note(float freq, int vol) {
507 if (audio_config.enable && voices < 8) {
508 TIMSK3 &= ~_BV(OCIE3A);
509 // Cancel notes if notes are playing
515 freq = freq / SAMPLE_RATE;
518 frequencies[voices] = freq;
519 volumes[voices] = vol;
524 TIMSK3 |= _BV(OCIE3A);
526 TIMSK3 |= _BV(OCIE3A);
527 TCCR3A |= _BV(COM3A1);
533 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
539 if (audio_config.enable) {
540 TIMSK3 &= ~_BV(OCIE3A);
541 // Cancel note if a note is playing
547 notes_count = n_count;
548 notes_repeat = n_repeat;
554 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
555 note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100);
557 note_frequency = (*notes_pointer)[current_note][0];
558 note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100);
564 TIMSK3 |= _BV(OCIE3A);
566 TIMSK3 |= _BV(OCIE3A);
567 TCCR3A |= _BV(COM3A1);
574 void play_sample(uint8_t * s, uint16_t l, bool r) {
579 if (audio_config.enable) {
580 TIMSK3 &= ~_BV(OCIE3A);
587 TIMSK3 |= _BV(OCIE3A);
592 //------------------------------------------------------------------------------
593 // Override these functions in your keymap file to play different tunes on
594 // startup and bootloader jump
595 __attribute__ ((weak))
596 void play_startup_tone()
602 __attribute__ ((weak))
603 void play_goodbye_tone()
607 //------------------------------------------------------------------------------