From 976d0a327b563ff83d0f7bcee41b2360877bface Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 5 Apr 2016 09:21:44 -0400 Subject: add silent notes --- quantum/audio.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 3a3a1a4910..f29d941d7c 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -247,6 +247,9 @@ ISR(TIMER3_COMPA_vect) { if (note_frequency > 0) { ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period + } else { + ICR3 = 0; + OCR3A = 0; } #endif -- cgit v1.2.3 From ee2ee7f4f049dda385a9db7dddd8e7e91681315b Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Fri, 15 Apr 2016 13:44:07 -0400 Subject: audio note length fixes --- quantum/audio.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index f29d941d7c..50e5505fe0 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -255,7 +255,12 @@ ISR(TIMER3_COMPA_vect) { note_position++; - if (note_position >= note_length) { + bool end_of_note = false; + if (ICR3 > 0) + end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF)); + else + end_of_note = (note_position >= (note_length * 0x7FF)); + if (end_of_note) { current_note++; if (current_note >= notes_length) { if (notes_repeat) { -- cgit v1.2.3 From 0faa18eab996c2cfcc5da0b60b702f52335c5854 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Fri, 15 Apr 2016 23:38:21 -0400 Subject: audio enable stored in eeprom --- quantum/audio.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 50e5505fe0..73985479cc 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -8,6 +8,8 @@ #include "audio.h" #include "keymap_common.h" +#include "eeconfig.h" + #define PI 3.14159265 // #define PWM_AUDIO @@ -57,6 +59,25 @@ uint8_t notes_length; bool notes_repeat; uint8_t current_note = 0; +audio_config_t audio_config; + + +void audio_toggle(void) { + audio_config.enable ^= 1; + eeconfig_write_audio(audio_config.raw); +} + +void audio_on(void) { + audio_config.enable = 1; + eeconfig_write_audio(audio_config.raw); +} + +void audio_off(void) { + audio_config.enable = 0; + eeconfig_write_audio(audio_config.raw); +} + + void stop_all_notes() { voices = 0; #ifdef PWM_AUDIO @@ -129,6 +150,12 @@ void stop_note(double freq) { void init_notes() { + /* check signature */ + if (!eeconfig_is_enabled()) { + eeconfig_init(); + } + audio_config.raw = eeconfig_read_audio(); + #ifdef PWM_AUDIO PLLFRQ = _BV(PDIV2); PLLCSR = _BV(PLLE); @@ -160,7 +187,6 @@ void init_notes() { ISR(TIMER3_COMPA_vect) { - if (note) { #ifdef PWM_AUDIO if (voices == 1) { @@ -288,9 +314,16 @@ ISR(TIMER3_COMPA_vect) { } + if (!audio_config.enable) { + notes = false; + note = false; + } } void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) { + +if (audio_config.enable) { + if (note) stop_all_notes(); notes = true; @@ -319,7 +352,12 @@ void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) { #endif } +} + void play_sample(uint8_t * s, uint16_t l, bool r) { + +if (audio_config.enable) { + stop_all_notes(); place_int = 0; sample = s; @@ -330,9 +368,15 @@ void play_sample(uint8_t * s, uint16_t l, bool r) { TIMSK3 |= _BV(OCIE3A); #else #endif + +} + } void play_note(double freq, int vol) { + +if (audio_config.enable) { + if (notes) stop_all_notes(); note = true; @@ -367,4 +411,6 @@ void play_note(double freq, int vol) { TCCR3A |= _BV(COM3A1); #endif +} + } \ No newline at end of file -- cgit v1.2.3 From 86169833c597fa5f2b5226e1587c48a6692ec017 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sat, 16 Apr 2016 15:47:17 -0400 Subject: updates to preonic default, music mode integrated --- quantum/audio.c | 100 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 45 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 73985479cc..5edcccdbe1 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -32,6 +32,8 @@ int voice_place = 0; double frequency = 0; int volume = 0; long position = 0; +int duty_place = 1; +int duty_counter = 0; double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -98,53 +100,55 @@ void stop_all_notes() { } void stop_note(double freq) { - #ifdef PWM_AUDIO - freq = freq / SAMPLE_RATE; - #endif - for (int i = 7; i >= 0; i--) { - if (frequencies[i] == freq) { - frequencies[i] = 0; - volumes[i] = 0; - for (int j = i; (j < 7); j++) { - frequencies[j] = frequencies[j+1]; - frequencies[j+1] = 0; - volumes[j] = volumes[j+1]; - volumes[j+1] = 0; - } - } - } - voices--; - if (voices < 0) - voices = 0; - if (voices == 0) { + if (note) { #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); + freq = freq / SAMPLE_RATE; #endif - frequency = 0; - volume = 0; - note = false; - } else { - double freq = frequencies[voices - 1]; - int vol = volumes[voices - 1]; - double starting_f = frequency; - if (frequency < freq) { - sliding = true; - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { - frequency = f; + for (int i = 7; i >= 0; i--) { + if (frequencies[i] == freq) { + frequencies[i] = 0; + volumes[i] = 0; + for (int j = i; (j < 7); j++) { + frequencies[j] = frequencies[j+1]; + frequencies[j+1] = 0; + volumes[j] = volumes[j+1]; + volumes[j+1] = 0; + } } - sliding = false; - } else if (frequency > freq) { - sliding = true; - for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { - frequency = f; + } + voices--; + if (voices < 0) + voices = 0; + if (voices == 0) { + #ifdef PWM_AUDIO + TIMSK3 &= ~_BV(OCIE3A); + #else + TIMSK3 &= ~_BV(OCIE3A); + TCCR3A &= ~_BV(COM3A1); + #endif + frequency = 0; + volume = 0; + note = false; + } else { + double freq = frequencies[voices - 1]; + int vol = volumes[voices - 1]; + double starting_f = frequency; + if (frequency < freq) { + sliding = true; + for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { + frequency = f; + } + sliding = false; + } else if (frequency > freq) { + sliding = true; + for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { + frequency = f; + } + sliding = false; } - sliding = false; + frequency = freq; + volume = vol; } - frequency = freq; - volume = vol; } } @@ -239,13 +243,19 @@ ISR(TIMER3_COMPA_vect) { if (frequency > 0) { // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period - if (place > 10) { + voice_place %= voices; + if (place > (frequencies[voice_place] / 500)) { voice_place = (voice_place + 1) % voices; place = 0.0; } ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period - OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1; // Set compare to half the period + OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1 * duty_place; // Set compare to half the period place++; + // if (duty_counter > (frequencies[voice_place] / 500)) { + // duty_place = (duty_place % 3) + 1; + // duty_counter = 0; + // } + // duty_counter++; } #endif } @@ -375,7 +385,7 @@ if (audio_config.enable) { void play_note(double freq, int vol) { -if (audio_config.enable) { +if (audio_config.enable && voices < 8) { if (notes) stop_all_notes(); -- cgit v1.2.3 From c83aa16f1d614c1c10f7597a67ffb9f2ae871951 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sat, 16 Apr 2016 20:26:02 -0400 Subject: fixes audio prescaler to emit correct freq --- quantum/audio.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 5edcccdbe1..470dc8e0c7 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -12,6 +12,8 @@ #define PI 3.14159265 +#define CPU_PRESCALER 8 + // #define PWM_AUDIO #ifdef PWM_AUDIO @@ -244,12 +246,12 @@ ISR(TIMER3_COMPA_vect) { // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period voice_place %= voices; - if (place > (frequencies[voice_place] / 500)) { + if (place > (frequencies[voice_place] / 50)) { voice_place = (voice_place + 1) % voices; place = 0.0; } - ICR3 = (int)(((double)F_CPU) / frequencies[voice_place]); // Set max to the period - OCR3A = (int)(((double)F_CPU) / frequencies[voice_place]) >> 1 * duty_place; // Set compare to half the period + ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period + OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period place++; // if (duty_counter > (frequencies[voice_place] / 500)) { // duty_place = (duty_place % 3) + 1; @@ -281,8 +283,8 @@ ISR(TIMER3_COMPA_vect) { place -= SINE_LENGTH; #else if (note_frequency > 0) { - ICR3 = (int)(((double)F_CPU) / note_frequency); // Set max to the period - OCR3A = (int)(((double)F_CPU) / note_frequency) >> 1; // Set compare to half the period + ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period + OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period } else { ICR3 = 0; OCR3A = 0; -- cgit v1.2.3 From 41cc35425ab32c9a9492006da8b667d01d32dfa6 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sat, 16 Apr 2016 21:31:40 -0400 Subject: rests between notes as an argument --- quantum/audio.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 470dc8e0c7..40d09d62fd 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -61,7 +61,11 @@ uint16_t note_position = 0; float (* notes_pointer)[][2]; uint8_t notes_length; bool notes_repeat; +float notes_rest; +bool note_resting = false; + uint8_t current_note = 0; +uint8_t rest_counter = 0; audio_config_t audio_config; @@ -314,13 +318,21 @@ ISR(TIMER3_COMPA_vect) { return; } } - #ifdef PWM_AUDIO - note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; - note_length = (*notes_pointer)[current_note][1]; - #else - note_frequency = (*notes_pointer)[current_note][0]; - note_length = (*notes_pointer)[current_note][1] / 4; - #endif + if (!note_resting && ((int)notes_rest != 0)) { + note_resting = true; + note_frequency = 0; + note_length = notes_rest; + current_note--; + } else { + note_resting = false; + #ifdef PWM_AUDIO + note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; + note_length = (*notes_pointer)[current_note][1]; + #else + note_frequency = (*notes_pointer)[current_note][0]; + note_length = (*notes_pointer)[current_note][1] / 4; + #endif + } note_position = 0; } @@ -332,7 +344,7 @@ ISR(TIMER3_COMPA_vect) { } } -void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat) { +void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat, float n_rest) { if (audio_config.enable) { @@ -343,6 +355,7 @@ if (audio_config.enable) { notes_pointer = np; notes_length = n_length; notes_repeat = n_repeat; + notes_rest = n_rest; place = 0; current_note = 0; -- cgit v1.2.3 From 45f10b4c4b308226fa1568277654a13853a03ab4 Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sat, 16 Apr 2016 22:10:18 -0500 Subject: Fixed how note arrays are used. --- quantum/audio.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 40d09d62fd..90f3c5a13f 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -181,7 +181,7 @@ void init_notes() { DDRC |= _BV(PORTC6); TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - + TCCR3A = 0x0; // Options not needed TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback @@ -202,14 +202,14 @@ ISR(TIMER3_COMPA_vect) { if (voices == 1) { // SINE OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2; - + // SQUARE // if (((int)place) >= 1024){ // OCR4A = 0xFF >> 2; // } else { // OCR4A = 0x00; // } - + // SAWTOOTH // OCR4A = (int)place / 4; @@ -298,9 +298,9 @@ ISR(TIMER3_COMPA_vect) { note_position++; bool end_of_note = false; - if (ICR3 > 0) + if (ICR3 > 0) end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF)); - else + else end_of_note = (note_position >= (note_length * 0x7FF)); if (end_of_note) { current_note++; @@ -318,7 +318,7 @@ ISR(TIMER3_COMPA_vect) { return; } } - if (!note_resting && ((int)notes_rest != 0)) { + if (!note_resting && (notes_rest > 0)) { note_resting = true; note_frequency = 0; note_length = notes_rest; @@ -412,7 +412,7 @@ if (audio_config.enable && voices < 8) { if (frequency != 0) { double starting_f = frequency; if (frequency < freq) { - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { + for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { frequency = f; } } else if (frequency > freq) { -- cgit v1.2.3 From a67d425f4d5278595e7ab785a0f246b83fb1a09f Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sun, 17 Apr 2016 01:00:39 -0400 Subject: planck default layout updates --- quantum/audio.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 90f3c5a13f..6bd6532a3a 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -350,7 +350,6 @@ if (audio_config.enable) { if (note) stop_all_notes(); - notes = true; notes_pointer = np; notes_length = n_length; @@ -375,6 +374,8 @@ if (audio_config.enable) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif + + notes = true; } } @@ -404,7 +405,6 @@ if (audio_config.enable && voices < 8) { if (notes) stop_all_notes(); - note = true; #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -436,6 +436,7 @@ if (audio_config.enable && voices < 8) { TCCR3A |= _BV(COM3A1); #endif + note = true; } } \ No newline at end of file -- cgit v1.2.3 From 943b5b770955be937a89016680052be56d874c4a Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sun, 17 Apr 2016 00:24:58 -0500 Subject: Adding folding to #if, etc --- quantum/audio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 90f3c5a13f..119bd92298 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -288,7 +288,8 @@ ISR(TIMER3_COMPA_vect) { #else if (note_frequency > 0) { ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period + //OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period + OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 2; // Set compare to half the period } else { ICR3 = 0; OCR3A = 0; -- cgit v1.2.3 From 29e495be2a57d1eb41699909b204c12ac6bc4c0e Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sun, 17 Apr 2016 02:52:38 -0500 Subject: Added trimble and tempo adjustments! --- quantum/audio.c | 56 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 13 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 119bd92298..9b9589f133 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -4,7 +4,7 @@ #include #include #include - +#include "print.h" #include "audio.h" #include "keymap_common.h" @@ -57,9 +57,11 @@ bool notes = false; bool note = false; float note_frequency = 0; float note_length = 0; +float note_tempo = TEMPO_DEFAULT; +float note_timbre = TIMBRE_DEFAULT; uint16_t note_position = 0; float (* notes_pointer)[][2]; -uint8_t notes_length; +uint8_t notes_count; bool notes_repeat; float notes_rest; bool note_resting = false; @@ -255,7 +257,8 @@ ISR(TIMER3_COMPA_vect) { place = 0.0; } ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period + OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period place++; // if (duty_counter > (frequencies[voice_place] / 500)) { // duty_place = (duty_place % 3) + 1; @@ -288,8 +291,7 @@ ISR(TIMER3_COMPA_vect) { #else if (note_frequency > 0) { ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period - //OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 1; // Set compare to half the period - OCR3A = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)) >> 2; // Set compare to half the period + OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period } else { ICR3 = 0; OCR3A = 0; @@ -305,7 +307,7 @@ ISR(TIMER3_COMPA_vect) { end_of_note = (note_position >= (note_length * 0x7FF)); if (end_of_note) { current_note++; - if (current_note >= notes_length) { + if (current_note >= notes_count) { if (notes_repeat) { current_note = 0; } else { @@ -328,10 +330,10 @@ ISR(TIMER3_COMPA_vect) { note_resting = false; #ifdef PWM_AUDIO note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; - note_length = (*notes_pointer)[current_note][1]; + note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); #else note_frequency = (*notes_pointer)[current_note][0]; - note_length = (*notes_pointer)[current_note][1] / 4; + note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); #endif } note_position = 0; @@ -345,7 +347,7 @@ ISR(TIMER3_COMPA_vect) { } } -void play_notes(float (*np)[][2], uint8_t n_length, bool n_repeat, float n_rest) { +void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { if (audio_config.enable) { @@ -354,7 +356,7 @@ if (audio_config.enable) { notes = true; notes_pointer = np; - notes_length = n_length; + notes_count = n_count; notes_repeat = n_repeat; notes_rest = n_rest; @@ -362,10 +364,10 @@ if (audio_config.enable) { current_note = 0; #ifdef PWM_AUDIO note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; - note_length = (*notes_pointer)[current_note][1]; + note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); #else note_frequency = (*notes_pointer)[current_note][0]; - note_length = (*notes_pointer)[current_note][1] / 4; + note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); #endif note_position = 0; @@ -439,4 +441,32 @@ if (audio_config.enable && voices < 8) { } -} \ No newline at end of file +} + +void set_timbre(float timbre) +{ + note_timbre = timbre; +} + +void set_tempo(float tempo) +{ + note_tempo = tempo; +} + +void decrease_tempo(uint8_t tempo_change) +{ + note_tempo += (float) tempo_change; +} + +void increase_tempo(uint8_t tempo_change) +{ + if (note_tempo - (float) tempo_change < 10) + { + note_tempo = 10; + } + else + { + note_tempo -= (float) tempo_change; + } +} + -- cgit v1.2.3 From c940e87235c9fe26f5c7451464af54a493270a68 Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sun, 17 Apr 2016 10:41:07 -0500 Subject: fixed single note play copy/paste bug --- quantum/audio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 9b9589f133..58b9ab76bf 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -257,7 +257,7 @@ ISR(TIMER3_COMPA_vect) { place = 0.0; } ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + OCR3A = (int)((((double)F_CPU) /(frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period place++; // if (duty_counter > (frequencies[voice_place] / 500)) { -- cgit v1.2.3 From ce463ef424c5bd26b84ead7de5f31eab366f98eb Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sun, 17 Apr 2016 18:14:42 -0400 Subject: audio fixes --- quantum/audio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 855e97361b..e0413051a0 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -257,7 +257,7 @@ ISR(TIMER3_COMPA_vect) { place = 0.0; } ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period place++; // if (duty_counter > (frequencies[voice_place] / 500)) { @@ -353,6 +353,7 @@ if (audio_config.enable) { if (note) stop_all_notes(); + notes = true; notes_pointer = np; notes_count = n_count; @@ -378,7 +379,6 @@ if (audio_config.enable) { TCCR3A |= _BV(COM3A1); #endif - notes = true; } } @@ -408,6 +408,7 @@ if (audio_config.enable && voices < 8) { if (notes) stop_all_notes(); + note = true; #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -439,7 +440,6 @@ if (audio_config.enable && voices < 8) { TCCR3A |= _BV(COM3A1); #endif - note = true; } } -- cgit v1.2.3 From 5c98ad59606ee95b82c27bf2525383a9ec88542b Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sun, 17 Apr 2016 20:14:37 -0500 Subject: Added extra songs, LED indicator notes --- quantum/audio.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 3ccd5ab9bc..6023669739 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -351,7 +351,7 @@ void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) if (audio_config.enable) { - if (note) + if (note || notes) stop_all_notes(); notes_pointer = np; @@ -406,7 +406,7 @@ void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - if (notes) + if (note || notes) stop_all_notes(); #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; @@ -471,3 +471,16 @@ void increase_tempo(uint8_t tempo_change) } } +//------------------------------------------------------------------------------ +// Override these functions in your keymap file to play different tunes on +// startup and bootloader jump +__attribute__ ((weak)) +void play_startup_tone() +{ +} + +__attribute__ ((weak)) +void play_goodbye_tone() +{ +} +//------------------------------------------------------------------------------ -- cgit v1.2.3 From 89a78f4a11a8f04777b3407c958dc989b4d7d884 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Sun, 17 Apr 2016 21:26:06 -0400 Subject: audio fixes --- quantum/audio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 5805da46c1..69c58ca6c6 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -351,7 +351,7 @@ void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) if (audio_config.enable) { - if (note || notes) + if (note) stop_all_notes(); notes = true; @@ -406,7 +406,7 @@ void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - if (note || notes) + if (notes) stop_all_notes(); note = true; #ifdef PWM_AUDIO -- cgit v1.2.3 From ef73ab662812232f5e73c8098a059439dcb201fa Mon Sep 17 00:00:00 2001 From: IBNobody Date: Sun, 17 Apr 2016 22:08:05 -0500 Subject: Notes Bugfix --- quantum/audio.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 6023669739..ba85e418ed 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -351,7 +351,8 @@ void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) if (audio_config.enable) { - if (note || notes) + // Cancel note if a note is playing + if (note) stop_all_notes(); notes_pointer = np; @@ -406,7 +407,8 @@ void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - if (note || notes) + // Cancel notes if notes are playing + if (notes) stop_all_notes(); #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; -- cgit v1.2.3 From e49712b5593b887c8af18aeb7196513f1c7b7bcf Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Mon, 18 Apr 2016 21:01:48 -0400 Subject: note sliding - on right now --- quantum/audio.c | 81 +++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 69c58ca6c6..6a5d43013a 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -65,6 +65,7 @@ uint8_t notes_count; bool notes_repeat; float notes_rest; bool note_resting = false; +int note_flipper = 0; uint8_t current_note = 0; uint8_t rest_counter = 0; @@ -127,6 +128,9 @@ void stop_note(double freq) { voices--; if (voices < 0) voices = 0; + if (voice_place >= voices) { + voice_place = 0; + } if (voices == 0) { #ifdef PWM_AUDIO TIMSK3 &= ~_BV(OCIE3A); @@ -137,25 +141,6 @@ void stop_note(double freq) { frequency = 0; volume = 0; note = false; - } else { - double freq = frequencies[voices - 1]; - int vol = volumes[voices - 1]; - double starting_f = frequency; - if (frequency < freq) { - sliding = true; - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { - frequency = f; - } - sliding = false; - } else if (frequency > freq) { - sliding = true; - for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { - frequency = f; - } - sliding = false; - } - frequency = freq; - volume = vol; } } } @@ -248,16 +233,41 @@ ISR(TIMER3_COMPA_vect) { OCR4A = sum; } #else - if (frequency > 0) { + if (frequencies[voice_place] > 0) { + // if (frequencies[voice_place] > 880.0) { + // if (note_flipper == 100) { + // note_flipper = 0; + // return; + // } + // note_flipper++; + // } else { + // note_flipper = 0; + // } // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period - voice_place %= voices; - if (place > (frequencies[voice_place] / 50)) { - voice_place = (voice_place + 1) % voices; - place = 0.0; + + double freq; + if (false) { + voice_place %= voices; + if (place > (frequencies[voice_place] / 50)) { + voice_place = (voice_place + 1) % voices; + place = 0.0; + } + freq = frequencies[voice_place]; + } else { + if (frequency != 0) { + if (frequency < frequencies[voices - 1]) { + frequency = frequency * 1.01454533494; + } else if (frequency > frequencies[voices - 1]) { + frequency = frequency * 0.98566319864; + } + } else { + frequency = frequencies[voices - 1]; + } + freq = frequency; } - ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period + OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period place++; // if (duty_counter > (frequencies[voice_place] / 500)) { @@ -413,23 +423,8 @@ if (audio_config.enable && voices < 8) { freq = freq / SAMPLE_RATE; #endif if (freq > 0) { - if (frequency != 0) { - double starting_f = frequency; - if (frequency < freq) { - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { - frequency = f; - } - } else if (frequency > freq) { - for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { - frequency = f; - } - } - } - frequency = freq; - volume = vol; - - frequencies[voices] = frequency; - volumes[voices] = volume; + frequencies[voices] = freq; + volumes[voices] = vol; voices++; } -- cgit v1.2.3 From a2f31c886ff13c5e7adeccccfe672698c1c7efb9 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 12:58:13 -0400 Subject: getting ready for getters and setters --- quantum/audio.c | 81 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 36 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index d8768f1609..c92cb53731 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -34,8 +34,6 @@ int voice_place = 0; double frequency = 0; int volume = 0; long position = 0; -int duty_place = 1; -int duty_counter = 0; double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -51,7 +49,7 @@ uint16_t place_int = 0; bool repeat = true; uint8_t * sample; uint16_t sample_length = 0; - +double freq = 0; bool notes = false; bool note = false; @@ -65,11 +63,15 @@ uint8_t notes_count; bool notes_repeat; float notes_rest; bool note_resting = false; -int note_flipper = 0; uint8_t current_note = 0; uint8_t rest_counter = 0; +uint8_t vibrato_counter = 0; +float vibrato_strength = 0; + +float polyphony_rate = 0; + audio_config_t audio_config; @@ -182,6 +184,11 @@ void init_notes() { #endif } +float mod(float a, int b) +{ + float r = fmod(a, b); + return r < 0 ? r + b : r; +} ISR(TIMER3_COMPA_vect) { if (note) { @@ -233,48 +240,41 @@ ISR(TIMER3_COMPA_vect) { OCR4A = sum; } #else - if (frequencies[voice_place] > 0) { - // if (frequencies[voice_place] > 880.0) { - // if (note_flipper == 100) { - // note_flipper = 0; - // return; - // } - // note_flipper++; - // } else { - // note_flipper = 0; - // } - // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period - // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period - - double freq; - if (false) { - voice_place %= voices; - if (place > (frequencies[voice_place] / 50)) { - voice_place = (voice_place + 1) % voices; - place = 0.0; + if (voices > 0) { + if (false && polyphony_rate > 0) { + if (voices > 1) { + voice_place %= voices; + if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER / voices)) { + voice_place = (voice_place + 1) % voices; + place = 0.0; + } } - freq = frequencies[voice_place]; + if (vibrato_strength > 0) { + freq = frequencies[voice_place] * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + } else { + freq = frequencies[voice_place]; + } } else { if (frequency != 0) { if (frequency < frequencies[voices - 1]) { - frequency = frequency * 1.01454533494; + frequency = frequency * pow(2, 440/frequencies[voices - 1]/12/4); } else if (frequency > frequencies[voices - 1]) { - frequency = frequency * 0.98566319864; + frequency = frequency * pow(2, -440/frequencies[voices - 1]/12/4); } } else { frequency = frequencies[voices - 1]; } - freq = frequency; + + if (false && vibrato_strength > 0) { + freq = frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1 + 440/frequencies[voices - 1]), VIBRATO_LUT_LENGTH); + } else { + freq = frequency; + } } ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period - //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period - place++; - // if (duty_counter > (frequencies[voice_place] / 500)) { - // duty_place = (duty_place % 3) + 1; - // duty_counter = 0; - // } - // duty_counter++; } #endif } @@ -300,8 +300,17 @@ ISR(TIMER3_COMPA_vect) { place -= SINE_LENGTH; #else if (note_frequency > 0) { - ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + float freq; + + if (false && vibrato_strength > 0) { + freq = note_frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + } else { + freq = note_frequency; + } + + ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period + OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period } else { ICR3 = 0; OCR3A = 0; -- cgit v1.2.3 From fd49dfe5cb686f5966447c6b890800c9cd11d281 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 17:00:45 -0400 Subject: vibrato and polyphony paratmeters --- quantum/audio.c | 160 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 50 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index c92cb53731..8ea1bf6ff0 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -10,10 +10,13 @@ #include "eeconfig.h" +#include "vibrato_lut.h" + #define PI 3.14159265 #define CPU_PRESCALER 8 +// Largely untested PWM audio mode (doesn't sound as good) // #define PWM_AUDIO #ifdef PWM_AUDIO @@ -67,10 +70,11 @@ bool note_resting = false; uint8_t current_note = 0; uint8_t rest_counter = 0; -uint8_t vibrato_counter = 0; -float vibrato_strength = 0; +float vibrato_counter = 0; +float vibrato_strength = .5; +float vibrato_rate = 0.125; -float polyphony_rate = 0; +float polyphony_rate = .5; audio_config_t audio_config; @@ -90,6 +94,81 @@ void audio_off(void) { eeconfig_write_audio(audio_config.raw); } +// Vibrato rate functions + +void set_vibrato_rate(float rate) { + vibrato_rate = rate; +} + +void increase_vibrato_rate(float change) { + vibrato_rate *= change; +} + +void decrease_vibrato_rate(float change) { + vibrato_rate /= change; +} + +#ifdef VIBRATO_STRENGTH_ENABLE + +void set_vibrato_strength(float strength) { + vibrato_strength = strength; +} + +void increase_vibrato_strength(float change) { + vibrato_strength *= change; +} + +void decrease_vibrato_strength(float change) { + vibrato_strength /= change; +} + +#endif + +// Polyphony functions + +void set_polyphony_rate(float rate) { + polyphony_rate = rate; +} + +void enable_polyphony() { + polyphony_rate = 5; +} + +void disable_polyphony() { + polyphony_rate = 0; +} + +void increase_polyphony_rate(float change) { + polyphony_rate *= change; +} + +void decrease_polyphony_rate(float change) { + polyphony_rate /= change; +} + +// Timbre function + +void set_timbre(float timbre) { + note_timbre = timbre; +} + +// Tempo functions + +void set_tempo(float tempo) { + note_tempo = tempo; +} + +void decrease_tempo(uint8_t tempo_change) { + note_tempo += (float) tempo_change; +} + +void increase_tempo(uint8_t tempo_change) { + if (note_tempo - (float) tempo_change < 10) { + note_tempo = 10; + } else { + note_tempo -= (float) tempo_change; + } +} void stop_all_notes() { voices = 0; @@ -112,6 +191,7 @@ void stop_all_notes() { void stop_note(double freq) { if (note) { + cli(); #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -125,6 +205,7 @@ void stop_note(double freq) { volumes[j] = volumes[j+1]; volumes[j+1] = 0; } + break; } } voices--; @@ -144,6 +225,7 @@ void stop_note(double freq) { volume = 0; note = false; } + sei(); } } @@ -190,6 +272,16 @@ float mod(float a, int b) return r < 0 ? r + b : r; } +float vibrato(float average_freq) { + #ifdef VIBRATO_STRENGTH_ENABLE + float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + #else + float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter]; + #endif + vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); + return vibrated_freq; +} + ISR(TIMER3_COMPA_vect) { if (note) { #ifdef PWM_AUDIO @@ -241,34 +333,30 @@ ISR(TIMER3_COMPA_vect) { } #else if (voices > 0) { - if (false && polyphony_rate > 0) { + if (polyphony_rate > 0) { if (voices > 1) { voice_place %= voices; - if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER / voices)) { + if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { voice_place = (voice_place + 1) % voices; place = 0.0; } } if (vibrato_strength > 0) { - freq = frequencies[voice_place] * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); - vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + freq = vibrato(frequencies[voice_place]); } else { freq = frequencies[voice_place]; } } else { - if (frequency != 0) { - if (frequency < frequencies[voices - 1]) { - frequency = frequency * pow(2, 440/frequencies[voices - 1]/12/4); - } else if (frequency > frequencies[voices - 1]) { - frequency = frequency * pow(2, -440/frequencies[voices - 1]/12/4); - } + if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { + frequency = frequency * pow(2, 440/frequency/12/2); + } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { + frequency = frequency * pow(2, -440/frequency/12/2); } else { frequency = frequencies[voices - 1]; } - if (false && vibrato_strength > 0) { - freq = frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); - vibrato_counter = mod((vibrato_counter + 1 + 440/frequencies[voices - 1]), VIBRATO_LUT_LENGTH); + if (vibrato_strength > 0) { + freq = vibrato(frequency); } else { freq = frequency; } @@ -302,9 +390,8 @@ ISR(TIMER3_COMPA_vect) { if (note_frequency > 0) { float freq; - if (false && vibrato_strength > 0) { - freq = note_frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); - vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + if (vibrato_strength > 0) { + freq = vibrato(note_frequency); } else { freq = note_frequency; } @@ -369,7 +456,7 @@ ISR(TIMER3_COMPA_vect) { void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { if (audio_config.enable) { - + cli(); // Cancel note if a note is playing if (note) stop_all_notes(); @@ -398,7 +485,7 @@ if (audio_config.enable) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - + sei(); } } @@ -425,7 +512,7 @@ if (audio_config.enable) { void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - + cli(); // Cancel notes if notes are playing if (notes) stop_all_notes(); @@ -445,36 +532,9 @@ if (audio_config.enable && voices < 8) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - -} - -} - -void set_timbre(float timbre) -{ - note_timbre = timbre; + sei(); } -void set_tempo(float tempo) -{ - note_tempo = tempo; -} - -void decrease_tempo(uint8_t tempo_change) -{ - note_tempo += (float) tempo_change; -} - -void increase_tempo(uint8_t tempo_change) -{ - if (note_tempo - (float) tempo_change < 10) - { - note_tempo = 10; - } - else - { - note_tempo -= (float) tempo_change; - } } //------------------------------------------------------------------------------ -- cgit v1.2.3 From 462601f5e8de3476963c6fef44a88653e19fc3fd Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 21:25:48 -0400 Subject: breaking changes - restructuring audio.c a little --- quantum/audio.c | 108 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 45 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 8ea1bf6ff0..bbdbc824c9 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -74,7 +74,9 @@ float vibrato_counter = 0; float vibrato_strength = .5; float vibrato_rate = 0.125; -float polyphony_rate = .5; +float polyphony_rate = 0; + +bool inited = false; audio_config_t audio_config; @@ -170,7 +172,49 @@ void increase_tempo(uint8_t tempo_change) { } } +void audio_init() { + + /* check signature */ + if (!eeconfig_is_enabled()) { + eeconfig_init(); + } + audio_config.raw = eeconfig_read_audio(); + + #ifdef PWM_AUDIO + PLLFRQ = _BV(PDIV2); + PLLCSR = _BV(PLLE); + while(!(PLLCSR & _BV(PLOCK))); + PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ + + /* Init a fast PWM on Timer4 */ + TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ + TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ + OCR4A = 0; + + /* Enable the OC4A output */ + DDRC |= _BV(PORTC6); + + TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs + + TCCR3A = 0x0; // Options not needed + TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC + OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback + #else + DDRC |= _BV(PORTC6); + + TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs + + TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); + TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); + #endif + + inited = true; +} + void stop_all_notes() { + if (!inited) { + audio_init(); + } voices = 0; #ifdef PWM_AUDIO TIMSK3 &= ~_BV(OCIE3A); @@ -191,7 +235,9 @@ void stop_all_notes() { void stop_note(double freq) { if (note) { - cli(); + if (!inited) { + audio_init(); + } #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -225,47 +271,9 @@ void stop_note(double freq) { volume = 0; note = false; } - sei(); } } -void init_notes() { - - /* check signature */ - if (!eeconfig_is_enabled()) { - eeconfig_init(); - } - audio_config.raw = eeconfig_read_audio(); - - #ifdef PWM_AUDIO - PLLFRQ = _BV(PDIV2); - PLLCSR = _BV(PLLE); - while(!(PLLCSR & _BV(PLOCK))); - PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ - - /* Init a fast PWM on Timer4 */ - TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ - TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ - OCR4A = 0; - - /* Enable the OC4A output */ - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = 0x0; // Options not needed - TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC - OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback - #else - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); - TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); - #endif -} - float mod(float a, int b) { float r = fmod(a, b); @@ -456,7 +464,10 @@ ISR(TIMER3_COMPA_vect) { void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { if (audio_config.enable) { - cli(); + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } // Cancel note if a note is playing if (note) stop_all_notes(); @@ -485,7 +496,6 @@ if (audio_config.enable) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - sei(); } } @@ -493,7 +503,10 @@ if (audio_config.enable) { void play_sample(uint8_t * s, uint16_t l, bool r) { if (audio_config.enable) { - + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } stop_all_notes(); place_int = 0; sample = s; @@ -512,7 +525,10 @@ if (audio_config.enable) { void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - cli(); + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } // Cancel notes if notes are playing if (notes) stop_all_notes(); @@ -532,7 +548,6 @@ if (audio_config.enable && voices < 8) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - sei(); } } @@ -545,8 +560,11 @@ void play_startup_tone() { } + + __attribute__ ((weak)) void play_goodbye_tone() { + } //------------------------------------------------------------------------------ -- cgit v1.2.3 From de4690593cec908b19f97509f45c78534fd5440f Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 20 Apr 2016 01:08:17 -0400 Subject: fixed startup audio with a 500ms delay --- quantum/audio.c | 136 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 57 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index bbdbc824c9..ab3444bc97 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -10,20 +10,23 @@ #include "eeconfig.h" -#include "vibrato_lut.h" +#ifdef VIBRATO_ENABLE + #include "vibrato_lut.h" +#endif #define PI 3.14159265 #define CPU_PRESCALER 8 -// Largely untested PWM audio mode (doesn't sound as good) -// #define PWM_AUDIO - #ifdef PWM_AUDIO #include "wave.h" #define SAMPLE_DIVIDER 39 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048) // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap + + float places[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint16_t place_int = 0; + bool repeat = true; #endif void delay_us(int count) { @@ -34,25 +37,21 @@ void delay_us(int count) { int voices = 0; int voice_place = 0; -double frequency = 0; +float frequency = 0; int volume = 0; long position = 0; -double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; +float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; bool sliding = false; int max = 0xFF; float sum = 0; -int value = 128; float place = 0; -float places[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -uint16_t place_int = 0; -bool repeat = true; uint8_t * sample; uint16_t sample_length = 0; -double freq = 0; +// float freq = 0; bool notes = false; bool note = false; @@ -62,7 +61,7 @@ float note_tempo = TEMPO_DEFAULT; float note_timbre = TIMBRE_DEFAULT; uint16_t note_position = 0; float (* notes_pointer)[][2]; -uint8_t notes_count; +uint16_t notes_count; bool notes_repeat; float notes_rest; bool note_resting = false; @@ -70,9 +69,11 @@ bool note_resting = false; uint8_t current_note = 0; uint8_t rest_counter = 0; +#ifdef VIBRATO_ENABLE float vibrato_counter = 0; float vibrato_strength = .5; float vibrato_rate = 0.125; +#endif float polyphony_rate = 0; @@ -96,6 +97,7 @@ void audio_off(void) { eeconfig_write_audio(audio_config.raw); } +#ifdef VIBRATO_ENABLE // Vibrato rate functions void set_vibrato_rate(float rate) { @@ -126,6 +128,8 @@ void decrease_vibrato_strength(float change) { #endif +#endif + // Polyphony functions void set_polyphony_rate(float rate) { @@ -209,6 +213,7 @@ void audio_init() { #endif inited = true; + _delay_ms(500); } void stop_all_notes() { @@ -233,7 +238,7 @@ void stop_all_notes() { } } -void stop_note(double freq) { +void stop_note(float freq) { if (note) { if (!inited) { audio_init(); @@ -274,6 +279,8 @@ void stop_note(double freq) { } } +#ifdef VIBRATO_ENABLE + float mod(float a, int b) { float r = fmod(a, b); @@ -290,6 +297,8 @@ float vibrato(float average_freq) { return vibrated_freq; } +#endif + ISR(TIMER3_COMPA_vect) { if (note) { #ifdef PWM_AUDIO @@ -341,6 +350,7 @@ ISR(TIMER3_COMPA_vect) { } #else if (voices > 0) { + float freq; if (polyphony_rate > 0) { if (voices > 1) { voice_place %= voices; @@ -349,9 +359,13 @@ ISR(TIMER3_COMPA_vect) { place = 0.0; } } + #ifdef VIBRATO_ENABLE if (vibrato_strength > 0) { freq = vibrato(frequencies[voice_place]); } else { + #else + { + #endif freq = frequencies[voice_place]; } } else { @@ -363,9 +377,14 @@ ISR(TIMER3_COMPA_vect) { frequency = frequencies[voices - 1]; } + + #ifdef VIBRATO_ENABLE if (vibrato_strength > 0) { freq = vibrato(frequency); } else { + #else + { + #endif freq = frequency; } } @@ -398,9 +417,13 @@ ISR(TIMER3_COMPA_vect) { if (note_frequency > 0) { float freq; + #ifdef VIBRATO_ENABLE if (vibrato_strength > 0) { freq = vibrato(note_frequency); } else { + #else + { + #endif freq = note_frequency; } @@ -461,13 +484,45 @@ ISR(TIMER3_COMPA_vect) { } } -void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { +void play_note(float freq, int vol) { -if (audio_config.enable) { + if (!inited) { + audio_init(); + } + +if (audio_config.enable && voices < 8) { TIMSK3 &= ~_BV(OCIE3A); + // Cancel notes if notes are playing + if (notes) + stop_all_notes(); + note = true; + #ifdef PWM_AUDIO + freq = freq / SAMPLE_RATE; + #endif + if (freq > 0) { + frequencies[voices] = freq; + volumes[voices] = vol; + voices++; + } + + #ifdef PWM_AUDIO + TIMSK3 |= _BV(OCIE3A); + #else + TIMSK3 |= _BV(OCIE3A); + TCCR3A |= _BV(COM3A1); + #endif +} + +} + +void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) { + if (!inited) { audio_init(); } + +if (audio_config.enable) { + TIMSK3 &= ~_BV(OCIE3A); // Cancel note if a note is playing if (note) stop_all_notes(); @@ -500,57 +555,24 @@ if (audio_config.enable) { } +#ifdef PWM_AUDIO void play_sample(uint8_t * s, uint16_t l, bool r) { - -if (audio_config.enable) { - TIMSK3 &= ~_BV(OCIE3A); if (!inited) { audio_init(); } - stop_all_notes(); - place_int = 0; - sample = s; - sample_length = l; - repeat = r; - #ifdef PWM_AUDIO - TIMSK3 |= _BV(OCIE3A); - #else - #endif - -} - -} - -void play_note(double freq, int vol) { - -if (audio_config.enable && voices < 8) { - TIMSK3 &= ~_BV(OCIE3A); - if (!inited) { - audio_init(); - } - // Cancel notes if notes are playing - if (notes) + if (audio_config.enable) { + TIMSK3 &= ~_BV(OCIE3A); stop_all_notes(); - note = true; - #ifdef PWM_AUDIO - freq = freq / SAMPLE_RATE; - #endif - if (freq > 0) { - frequencies[voices] = freq; - volumes[voices] = vol; - voices++; - } + place_int = 0; + sample = s; + sample_length = l; + repeat = r; - #ifdef PWM_AUDIO TIMSK3 |= _BV(OCIE3A); - #else - TIMSK3 |= _BV(OCIE3A); - TCCR3A |= _BV(COM3A1); - #endif -} - + } } +#endif //------------------------------------------------------------------------------ // Override these functions in your keymap file to play different tunes on -- cgit v1.2.3 From b0635e58eb33b0ed4f6b4a3846660feae15681ad Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 20 Apr 2016 18:39:36 -0400 Subject: delay isn't the problem here --- quantum/audio.c | 1 - 1 file changed, 1 deletion(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index ab3444bc97..e4f0bf30e8 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -213,7 +213,6 @@ void audio_init() { #endif inited = true; - _delay_ms(500); } void stop_all_notes() { -- cgit v1.2.3 From 2e303b40aed372ea69b79850dae41e4f8ea457f4 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 20 Apr 2016 22:29:01 -0400 Subject: start of envelope function --- quantum/audio.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index e4f0bf30e8..1327887d9a 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -81,6 +81,7 @@ bool inited = false; audio_config_t audio_config; +uint16_t envelope_index = 0; void audio_toggle(void) { audio_config.enable ^= 1; @@ -298,6 +299,26 @@ float vibrato(float average_freq) { #endif +float envelope(float f) { + uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f)); + switch (compensated_index) { + case 0: + note_timbre = TIMBRE_50; + break; + case 20: + note_timbre = TIMBRE_25; + break; + case 32: + note_timbre = TIMBRE_12; + break; + case 40 ... 60: + f = f / 2; + note_timbre = TIMBRE_50; + break; + } + return f; +} + ISR(TIMER3_COMPA_vect) { if (note) { #ifdef PWM_AUDIO @@ -387,6 +408,12 @@ ISR(TIMER3_COMPA_vect) { freq = frequency; } } + + if (envelope_index < 65535) { + envelope_index++; + } + freq = envelope(freq); + ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period } @@ -495,6 +522,7 @@ if (audio_config.enable && voices < 8) { if (notes) stop_all_notes(); note = true; + envelope_index = 0; #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif -- cgit v1.2.3 From 2e60054951ce08e973c735991bd95390c6aa3842 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 20 Apr 2016 23:16:39 -0400 Subject: fade envelope --- quantum/audio.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c index 1327887d9a..df421ef999 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -302,18 +302,19 @@ float vibrato(float average_freq) { float envelope(float f) { uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f)); switch (compensated_index) { - case 0: - note_timbre = TIMBRE_50; - break; - case 20: - note_timbre = TIMBRE_25; - break; - case 32: + case 0 ... 9: + f = f / 4; note_timbre = TIMBRE_12; break; - case 40 ... 60: + case 10 ... 19: f = f / 2; - note_timbre = TIMBRE_50; + note_timbre = TIMBRE_12; + break; + case 20 ... 200: + note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125; + break; + default: + note_timbre = 0; break; } return f; @@ -414,6 +415,8 @@ ISR(TIMER3_COMPA_vect) { } freq = envelope(freq); + if (freq < 30.517578125) + freq = 30.52; ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period } -- cgit v1.2.3 From 73228f5e5d1d4cd31a46e5e93aa893a8f727e3b9 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Thu, 21 Apr 2016 00:37:45 -0400 Subject: restructures audio, begins voicing --- quantum/audio.c | 622 -------------------------------------------------------- 1 file changed, 622 deletions(-) delete mode 100644 quantum/audio.c (limited to 'quantum/audio.c') diff --git a/quantum/audio.c b/quantum/audio.c deleted file mode 100644 index df421ef999..0000000000 --- a/quantum/audio.c +++ /dev/null @@ -1,622 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "print.h" -#include "audio.h" -#include "keymap_common.h" - -#include "eeconfig.h" - -#ifdef VIBRATO_ENABLE - #include "vibrato_lut.h" -#endif - -#define PI 3.14159265 - -#define CPU_PRESCALER 8 - -#ifdef PWM_AUDIO - #include "wave.h" - #define SAMPLE_DIVIDER 39 - #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048) - // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap - - float places[8] = {0, 0, 0, 0, 0, 0, 0, 0}; - uint16_t place_int = 0; - bool repeat = true; -#endif - -void delay_us(int count) { - while(count--) { - _delay_us(1); - } -} - -int voices = 0; -int voice_place = 0; -float frequency = 0; -int volume = 0; -long position = 0; - -float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -bool sliding = false; - -int max = 0xFF; -float sum = 0; -float place = 0; - -uint8_t * sample; -uint16_t sample_length = 0; -// float freq = 0; - -bool notes = false; -bool note = false; -float note_frequency = 0; -float note_length = 0; -float note_tempo = TEMPO_DEFAULT; -float note_timbre = TIMBRE_DEFAULT; -uint16_t note_position = 0; -float (* notes_pointer)[][2]; -uint16_t notes_count; -bool notes_repeat; -float notes_rest; -bool note_resting = false; - -uint8_t current_note = 0; -uint8_t rest_counter = 0; - -#ifdef VIBRATO_ENABLE -float vibrato_counter = 0; -float vibrato_strength = .5; -float vibrato_rate = 0.125; -#endif - -float polyphony_rate = 0; - -bool inited = false; - -audio_config_t audio_config; - -uint16_t envelope_index = 0; - -void audio_toggle(void) { - audio_config.enable ^= 1; - eeconfig_write_audio(audio_config.raw); -} - -void audio_on(void) { - audio_config.enable = 1; - eeconfig_write_audio(audio_config.raw); -} - -void audio_off(void) { - audio_config.enable = 0; - eeconfig_write_audio(audio_config.raw); -} - -#ifdef VIBRATO_ENABLE -// Vibrato rate functions - -void set_vibrato_rate(float rate) { - vibrato_rate = rate; -} - -void increase_vibrato_rate(float change) { - vibrato_rate *= change; -} - -void decrease_vibrato_rate(float change) { - vibrato_rate /= change; -} - -#ifdef VIBRATO_STRENGTH_ENABLE - -void set_vibrato_strength(float strength) { - vibrato_strength = strength; -} - -void increase_vibrato_strength(float change) { - vibrato_strength *= change; -} - -void decrease_vibrato_strength(float change) { - vibrato_strength /= change; -} - -#endif - -#endif - -// Polyphony functions - -void set_polyphony_rate(float rate) { - polyphony_rate = rate; -} - -void enable_polyphony() { - polyphony_rate = 5; -} - -void disable_polyphony() { - polyphony_rate = 0; -} - -void increase_polyphony_rate(float change) { - polyphony_rate *= change; -} - -void decrease_polyphony_rate(float change) { - polyphony_rate /= change; -} - -// Timbre function - -void set_timbre(float timbre) { - note_timbre = timbre; -} - -// Tempo functions - -void set_tempo(float tempo) { - note_tempo = tempo; -} - -void decrease_tempo(uint8_t tempo_change) { - note_tempo += (float) tempo_change; -} - -void increase_tempo(uint8_t tempo_change) { - if (note_tempo - (float) tempo_change < 10) { - note_tempo = 10; - } else { - note_tempo -= (float) tempo_change; - } -} - -void audio_init() { - - /* check signature */ - if (!eeconfig_is_enabled()) { - eeconfig_init(); - } - audio_config.raw = eeconfig_read_audio(); - - #ifdef PWM_AUDIO - PLLFRQ = _BV(PDIV2); - PLLCSR = _BV(PLLE); - while(!(PLLCSR & _BV(PLOCK))); - PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ - - /* Init a fast PWM on Timer4 */ - TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ - TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ - OCR4A = 0; - - /* Enable the OC4A output */ - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = 0x0; // Options not needed - TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC - OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback - #else - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); - TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); - #endif - - inited = true; -} - -void stop_all_notes() { - if (!inited) { - audio_init(); - } - voices = 0; - #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); - #endif - notes = false; - note = false; - frequency = 0; - volume = 0; - - for (int i = 0; i < 8; i++) { - frequencies[i] = 0; - volumes[i] = 0; - } -} - -void stop_note(float freq) { - if (note) { - if (!inited) { - audio_init(); - } - #ifdef PWM_AUDIO - freq = freq / SAMPLE_RATE; - #endif - for (int i = 7; i >= 0; i--) { - if (frequencies[i] == freq) { - frequencies[i] = 0; - volumes[i] = 0; - for (int j = i; (j < 7); j++) { - frequencies[j] = frequencies[j+1]; - frequencies[j+1] = 0; - volumes[j] = volumes[j+1]; - volumes[j+1] = 0; - } - break; - } - } - voices--; - if (voices < 0) - voices = 0; - if (voice_place >= voices) { - voice_place = 0; - } - if (voices == 0) { - #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); - #endif - frequency = 0; - volume = 0; - note = false; - } - } -} - -#ifdef VIBRATO_ENABLE - -float mod(float a, int b) -{ - float r = fmod(a, b); - return r < 0 ? r + b : r; -} - -float vibrato(float average_freq) { - #ifdef VIBRATO_STRENGTH_ENABLE - float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); - #else - float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter]; - #endif - vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); - return vibrated_freq; -} - -#endif - -float envelope(float f) { - uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / f)); - switch (compensated_index) { - case 0 ... 9: - f = f / 4; - note_timbre = TIMBRE_12; - break; - case 10 ... 19: - f = f / 2; - note_timbre = TIMBRE_12; - break; - case 20 ... 200: - note_timbre = .125 - pow(((float)compensated_index - 20) / (200 - 20), 2)*.125; - break; - default: - note_timbre = 0; - break; - } - return f; -} - -ISR(TIMER3_COMPA_vect) { - if (note) { - #ifdef PWM_AUDIO - if (voices == 1) { - // SINE - OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2; - - // SQUARE - // if (((int)place) >= 1024){ - // OCR4A = 0xFF >> 2; - // } else { - // OCR4A = 0x00; - // } - - // SAWTOOTH - // OCR4A = (int)place / 4; - - // TRIANGLE - // if (((int)place) >= 1024) { - // OCR4A = (int)place / 2; - // } else { - // OCR4A = 2048 - (int)place / 2; - // } - - place += frequency; - - if (place >= SINE_LENGTH) - place -= SINE_LENGTH; - - } else { - int sum = 0; - for (int i = 0; i < voices; i++) { - // SINE - sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2; - - // SQUARE - // if (((int)places[i]) >= 1024){ - // sum += 0xFF >> 2; - // } else { - // sum += 0x00; - // } - - places[i] += frequencies[i]; - - if (places[i] >= SINE_LENGTH) - places[i] -= SINE_LENGTH; - } - OCR4A = sum; - } - #else - if (voices > 0) { - float freq; - if (polyphony_rate > 0) { - if (voices > 1) { - voice_place %= voices; - if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { - voice_place = (voice_place + 1) % voices; - place = 0.0; - } - } - #ifdef VIBRATO_ENABLE - if (vibrato_strength > 0) { - freq = vibrato(frequencies[voice_place]); - } else { - #else - { - #endif - freq = frequencies[voice_place]; - } - } else { - if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { - frequency = frequency * pow(2, 440/frequency/12/2); - } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { - frequency = frequency * pow(2, -440/frequency/12/2); - } else { - frequency = frequencies[voices - 1]; - } - - - #ifdef VIBRATO_ENABLE - if (vibrato_strength > 0) { - freq = vibrato(frequency); - } else { - #else - { - #endif - freq = frequency; - } - } - - if (envelope_index < 65535) { - envelope_index++; - } - freq = envelope(freq); - - if (freq < 30.517578125) - freq = 30.52; - ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period - } - #endif - } - - // SAMPLE - // OCR4A = pgm_read_byte(&sample[(uint16_t)place_int]); - - // place_int++; - - // if (place_int >= sample_length) - // if (repeat) - // place_int -= sample_length; - // else - // TIMSK3 &= ~_BV(OCIE3A); - - - if (notes) { - #ifdef PWM_AUDIO - OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0; - - place += note_frequency; - if (place >= SINE_LENGTH) - place -= SINE_LENGTH; - #else - if (note_frequency > 0) { - float freq; - - #ifdef VIBRATO_ENABLE - if (vibrato_strength > 0) { - freq = vibrato(note_frequency); - } else { - #else - { - #endif - freq = note_frequency; - } - - ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period - } else { - ICR3 = 0; - OCR3A = 0; - } - #endif - - - note_position++; - bool end_of_note = false; - if (ICR3 > 0) - end_of_note = (note_position >= (note_length / ICR3 * 0xFFFF)); - else - end_of_note = (note_position >= (note_length * 0x7FF)); - if (end_of_note) { - current_note++; - if (current_note >= notes_count) { - if (notes_repeat) { - current_note = 0; - } else { - #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); - #endif - notes = false; - return; - } - } - if (!note_resting && (notes_rest > 0)) { - note_resting = true; - note_frequency = 0; - note_length = notes_rest; - current_note--; - } else { - note_resting = false; - #ifdef PWM_AUDIO - note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; - note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); - #else - note_frequency = (*notes_pointer)[current_note][0]; - note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); - #endif - } - note_position = 0; - } - - } - - if (!audio_config.enable) { - notes = false; - note = false; - } -} - -void play_note(float freq, int vol) { - - if (!inited) { - audio_init(); - } - -if (audio_config.enable && voices < 8) { - TIMSK3 &= ~_BV(OCIE3A); - // Cancel notes if notes are playing - if (notes) - stop_all_notes(); - note = true; - envelope_index = 0; - #ifdef PWM_AUDIO - freq = freq / SAMPLE_RATE; - #endif - if (freq > 0) { - frequencies[voices] = freq; - volumes[voices] = vol; - voices++; - } - - #ifdef PWM_AUDIO - TIMSK3 |= _BV(OCIE3A); - #else - TIMSK3 |= _BV(OCIE3A); - TCCR3A |= _BV(COM3A1); - #endif -} - -} - -void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) { - - if (!inited) { - audio_init(); - } - -if (audio_config.enable) { - TIMSK3 &= ~_BV(OCIE3A); - // Cancel note if a note is playing - if (note) - stop_all_notes(); - notes = true; - - notes_pointer = np; - notes_count = n_count; - notes_repeat = n_repeat; - notes_rest = n_rest; - - place = 0; - current_note = 0; - #ifdef PWM_AUDIO - note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; - note_length = (*notes_pointer)[current_note][1] * (note_tempo / 100); - #else - note_frequency = (*notes_pointer)[current_note][0]; - note_length = ((*notes_pointer)[current_note][1] / 4) * (note_tempo / 100); - #endif - note_position = 0; - - - #ifdef PWM_AUDIO - TIMSK3 |= _BV(OCIE3A); - #else - TIMSK3 |= _BV(OCIE3A); - TCCR3A |= _BV(COM3A1); - #endif -} - -} - -#ifdef PWM_AUDIO -void play_sample(uint8_t * s, uint16_t l, bool r) { - if (!inited) { - audio_init(); - } - - if (audio_config.enable) { - TIMSK3 &= ~_BV(OCIE3A); - stop_all_notes(); - place_int = 0; - sample = s; - sample_length = l; - repeat = r; - - TIMSK3 |= _BV(OCIE3A); - } -} -#endif - -//------------------------------------------------------------------------------ -// Override these functions in your keymap file to play different tunes on -// startup and bootloader jump -__attribute__ ((weak)) -void play_startup_tone() -{ -} - - - -__attribute__ ((weak)) -void play_goodbye_tone() -{ - -} -//------------------------------------------------------------------------------ -- cgit v1.2.3