summary refs log tree commit diff
path: root/quantum
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-04-19 12:58:13 -0400
committerJack Humbert <jack.humb@gmail.com>2016-04-19 12:58:13 -0400
commita2f31c886ff13c5e7adeccccfe672698c1c7efb9 (patch)
tree1895e4ec947e81ad185bd9f8ff5623fabfeb9f68 /quantum
parent78b3a88154914591d2c3433b123d4b80e70f511d (diff)
getting ready for getters and setters
Diffstat (limited to 'quantum')
-rw-r--r--quantum/audio.c81
-rw-r--r--quantum/audio.h104
2 files changed, 149 insertions, 36 deletions
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;
diff --git a/quantum/audio.h b/quantum/audio.h
index 44cafccd68..f705341d7f 100644
--- a/quantum/audio.h
+++ b/quantum/audio.h
@@ -47,4 +47,108 @@ void decrease_tempo(uint8_t tempo_change);
 void play_goodbye_tone(void);
 void play_startup_tone(void);
 
+#define VIBRATO_LUT (float []) { \
+1.00090714186239, \
+1.00181152169061, \
+1.00270955652027, \
+1.00359767896099, \
+1.00447235162891, \
+1.00533008160601, \
+1.00616743486158, \
+1.00698105056935, \
+1.00776765525194, \
+1.00852407668313, \
+1.0092472574777, \
+1.00993426829815, \
+1.01058232060837, \
+1.01118877890462, \
+1.01175117235612, \
+1.01226720578933, \
+1.01273476995269, \
+1.01315195100182, \
+1.0135170391489, \
+1.01382853642434, \
+1.01408516350345, \
+1.01428586555648, \
+1.0144298170856, \
+1.0145164257189, \
+1.01454533493752, \
+1.0145164257189, \
+1.0144298170856, \
+1.01428586555648, \
+1.01408516350345, \
+1.01382853642434, \
+1.0135170391489, \
+1.01315195100182, \
+1.01273476995269, \
+1.01226720578933, \
+1.01175117235612, \
+1.01118877890462, \
+1.01058232060837, \
+1.00993426829815, \
+1.0092472574777, \
+1.00852407668313, \
+1.00776765525194, \
+1.00698105056935, \
+1.00616743486158, \
+1.00533008160601, \
+1.00447235162891, \
+1.00359767896099, \
+1.00270955652027, \
+1.00181152169061, \
+1.00090714186239, \
+1, \
+0.999093680298157, \
+0.998191753986265, \
+0.997297765337276, \
+0.996415217934032, \
+0.995547561242821, \
+0.99469817754036, \
+0.993870369236874, \
+0.993067346634376, \
+0.992292216155724, \
+0.991547969076588, \
+0.990837470789065, \
+0.990163450622494, \
+0.989528492243954, \
+0.988935024658062, \
+0.988385313823004, \
+0.98788145489731, \
+0.987425365129624, \
+0.987018777401739, \
+0.986663234433381, \
+0.986360083655655, \
+0.986110472758728, \
+0.985915345918143, \
+0.985775440703176, \
+0.985691285669809, \
+0.985663198640188, \
+0.985691285669809, \
+0.985775440703176, \
+0.985915345918143, \
+0.986110472758728, \
+0.986360083655655, \
+0.986663234433381, \
+0.987018777401739, \
+0.987425365129624, \
+0.98788145489731, \
+0.988385313823004, \
+0.988935024658062, \
+0.989528492243954, \
+0.990163450622494, \
+0.990837470789065, \
+0.991547969076588, \
+0.992292216155724, \
+0.993067346634376, \
+0.993870369236874, \
+0.99469817754036, \
+0.99554756124282, \
+0.996415217934032, \
+0.997297765337276, \
+0.998191753986265, \
+0.999093680298157, \
+1, \
+}
+#define VIBRATO_LUT_LENGTH NOTE_ARRAY_SIZE(VIBRATO_LUT)
+
 #endif
\ No newline at end of file