KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Revision:
11:7e11404adca0
Parent:
10:79134dbb339d
Child:
12:5b498285d121
--- a/main.cpp	Tue Jun 06 11:00:12 2017 +0000
+++ b/main.cpp	Tue Jun 06 12:25:19 2017 +0000
@@ -16,7 +16,7 @@
 #define PI_F            (3.1415926f)
 #define SAMPLING_RATE   (96000)
 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
-#define UPDATE_RATE     (1000)
+#define UPDATE_RATE     (32000)
 
 AnalogOut Dac1(PA_5);
 
@@ -34,7 +34,8 @@
 AnalogIn Ain10(PC_5);
 AnalogIn Ain11(PA_6);
 
-class EnvelopeAR {
+class EnvelopeAR
+{
 public:
     EnvelopeAR(int _attack, int _release, float _v0, float _v1, float _v2, float _attackTauRatio=0.36f, float _releaseTauRatio=0.36f) :
         amplitude(_v0),
@@ -43,8 +44,7 @@
         v2(_v2),
         vLast(_v0),
         attackTauRatio(_attackTauRatio),
-        releaseTauRatio(_releaseTauRatio)
-    {
+        releaseTauRatio(_releaseTauRatio) {
         setAttack(_attack);
         setRelease(_release);
     }
@@ -55,44 +55,69 @@
         attack = _attack;
         tau0 = attack * attackTauRatio;
     }
-    int getAttack() { return attack; }
+    int getAttack() {
+        return attack;
+    }
 
     void setRelease(int _release) {
         release = _release;
         tau1 = release * releaseTauRatio;
     }
-    int getRelease() { return release; }
+    int getRelease() {
+        return release;
+    }
 
     void setAttackTauRatio(float _attackTauRatio) {
         attackTauRatio = _attackTauRatio;
         tau0 = attack * attackTauRatio;
     }
-    float getAttackTauRatio() { return attackTauRatio;  }
+    float getAttackTauRatio() {
+        return attackTauRatio;
+    }
 
-    void setReleaseTauRatio(float _releaseTauRatio) { 
+    void setReleaseTauRatio(float _releaseTauRatio) {
         releaseTauRatio = _releaseTauRatio;
         tau1 = release * releaseTauRatio;
     }
-    float getReleaseTauRatio() { return releaseTauRatio; }
-    
-    float getTau0() { return tau0; }
-    float getTau1() { return tau1; }
+    float getReleaseTauRatio() {
+        return releaseTauRatio;
+    }
+
+    float getTau0() {
+        return tau0;
+    }
+    float getTau1() {
+        return tau1;
+    }
 
-    void setV0(float _v0) { v0 = _v0; }
-    float getV0() { return v0; }
-    void setV1(float _v1) { v1 = _v1; }
-    float getV1() { return v1; }
-    void setV2(float _v2) { v2 = _v2; }
-    float getV2() { return v2; }
+    void setV0(float _v0) {
+        v0 = _v0;
+    }
+    float getV0() {
+        return v0;
+    }
+    void setV1(float _v1) {
+        v1 = _v1;
+    }
+    float getV1() {
+        return v1;
+    }
+    void setV2(float _v2) {
+        v2 = _v2;
+    }
+    float getV2() {
+        return v2;
+    }
 
-    float getAmplitude() { return amplitude; }
+    float getAmplitude() {
+        return amplitude;
+    }
     float getAmplitude(int tick) {
         if (tick < attack) {
             // attackの処理
             amplitude = v0 + (v1 - v0) * (1 - expf(-(float)tick / tau0));
             vLast = amplitude;
-        }
-        else {
+        } else {
             // releaseの処理
             amplitude = (vLast - v2) * (expf(-(float)(tick - attack) / tau1)) + v2;
         }
@@ -113,7 +138,8 @@
     float releaseTauRatio;
 };
 
-class EnvelopeParam {
+class EnvelopeParam
+{
 public:
     int attack;
     int release;
@@ -123,7 +149,7 @@
     float attackTauRatio;
     float releaseTauRatio;
 };
-    
+
 EnvelopeAR envelopeFrequency(5, 300, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
 EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
 
@@ -171,10 +197,8 @@
     ticks++;
     if (ticks >= SAMPLING_RATE / UPDATE_RATE) {
         ticks = 0;
-        
+
         // set envelope parameters
-        //envelopeLength = 60 * UPDATE_RATE / bpm;
-        
         envelopeAmplitude.setAttack(amplitudeParam.attack);
         envelopeAmplitude.setRelease(amplitudeParam.release);
         envelopeAmplitude.setV0(amplitudeParam.v0);
@@ -182,7 +206,7 @@
         envelopeAmplitude.setV2(amplitudeParam.v2);
         envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
         envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
-                
+
         envelopeFrequency.setAttack(frequencyParam.attack);
         envelopeFrequency.setRelease(frequencyParam.release);
         envelopeFrequency.setV0(frequencyParam.v0);
@@ -190,7 +214,7 @@
         envelopeFrequency.setV2(frequencyParam.v2);
         envelopeFrequency.setAttackTauRatio(frequencyParam.attackTauRatio);
         envelopeFrequency.setReleaseTauRatio(frequencyParam.releaseTauRatio);
-        
+
         generateEnvelope();
     }
     generateWave();
@@ -200,7 +224,7 @@
 {
     bpm = Ain0.read() * 180.0f + 60.0f;
     envelopeLength = 60 * UPDATE_RATE / bpm;
-    
+
     amplitudeParam.attack = Ain1.read() * envelopeLength;
     amplitudeParam.release = Ain2.read() * envelopeLength;
     amplitudeParam.v0 = Ain3.read();
@@ -208,39 +232,39 @@
     amplitudeParam.v2 = 0.0f;
     amplitudeParam.attackTauRatio = 0.36f;
     amplitudeParam.releaseTauRatio = Ain4.read() + 0.01f;
-    
+
     frequencyParam.attack = Ain8.read() * envelopeLength * 0.1f;
     frequencyParam.release = Ain11.read() * envelopeLength + 1;
     frequencyParam.v0 = Ain5.read() * 4000.0f;
     frequencyParam.v1 = Ain6.read() * 400.0f;
     frequencyParam.v2 = Ain7.read() * 400.0f;
     frequencyParam.attackTauRatio = Ain9.read() + 0.01f;
-    frequencyParam.releaseTauRatio = Ain10.read() + 0.01f;    
+    frequencyParam.releaseTauRatio = Ain10.read() + 0.01f;
 }
 
 int main()
 {
     printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
-    
+
     frequency = 1000.0f;
     phiDelta = 2.0f * frequency / SAMPLING_RATE;
     amplitude = 1.0f;
-    
+
     ticks = 0;
     envelopeTicks = 0;
-    
+
     bpm = 120.0f;
     setParams();
 
     Ticker samplingTicker;
     samplingTicker.attach(&update, SAMPLING_PERIOD);
-        
+
     for (;;) {
         setParams();
-        
-        #if UART_TRACE
+
+#if UART_TRACE
         printf("%.1f\t%d\t", bpm, envelopeLength);
-        
+
         printf("%d\t%d\t", amplitudeParam.attack, amplitudeParam.release);
         printf("%.2f\t%.2f\t%.2f\t", amplitudeParam.v0, amplitudeParam.v1, amplitudeParam.v2);
         printf("%.2f\t%.2f\t", amplitudeParam.attackTauRatio, amplitudeParam.releaseTauRatio);
@@ -248,8 +272,8 @@
         printf("%d\t%d\t", frequencyParam.attack, frequencyParam.release);
         printf("%.2f\t%.2f\t%.2f\t", frequencyParam.v0, frequencyParam.v1, frequencyParam.v2);
         printf("%.2f\t%.2f\r\n", frequencyParam.attackTauRatio, frequencyParam.releaseTauRatio);
-        #endif
-        
-        Thread::wait(100);    
+#endif
+
+        Thread::wait(100);
     }
 }