KIK 01 Prototype 05

Dependencies:   AverageMCP3008 mbed-rtos mbed mcp3008

Fork of KIK01_Proto03 by Ryo Od

Revision:
5:846772a77d33
Parent:
4:9f53a82fc1b6
Child:
6:897d6392b408
--- a/main.cpp	Sun Jun 04 07:49:50 2017 +0000
+++ b/main.cpp	Sun Jun 04 08:59:53 2017 +0000
@@ -13,19 +13,24 @@
 #define TITLE_STR2      ("20170604")
 
 #define PI_F            (3.1415926f)
-#define SAMPLING_RATE   (96000)
+#define SAMPLING_RATE   (48000)
 #define SAMPLING_PERIOD (1.0f/SAMPLING_RATE)
 
 #define FREQUENCY_ATTACK    (5)
 #define FREQUENCY_RELEASE   (300)
 //#define AMPLITUDE_ATTACK    (50)
-#define AMPLITUDE_RELEASE   (200)
+//#define AMPLITUDE_RELEASE   (200)
 
 AnalogOut Dac1(PA_5);
 
-AnalogIn AinBpm(PA_0);
-AnalogIn AinAmplitudeAttack(PA_1);
-AnalogIn AinFrequencyF0(PA_4);
+AnalogIn Ain0(PA_0);
+AnalogIn Ain1(PA_1);
+AnalogIn Ain2(PA_4);
+AnalogIn Ain3(PB_0);
+AnalogIn Ain4(PC_1);
+AnalogIn Ain5(PC_0);
+AnalogIn Ain6(PC_2);
+AnalogIn Ain7(PC_3);
 
 class EnvelopeAR {
 public:
@@ -56,11 +61,20 @@
     }
     int getRelease() { return release; }
 
-    void setAttackTauRatio(float _attackTauRatio) { attackTauRatio = _attackTauRatio; }
+    void setAttackTauRatio(float _attackTauRatio) {
+        attackTauRatio = _attackTauRatio;
+        tau0 = attack * attackTauRatio;
+    }
     float getAttackTauRatio() { return attackTauRatio;  }
 
-    void setReleaseTauRatio(float _releaseTauRatio) { releaseTauRatio = _releaseTauRatio; }
+    void setReleaseTauRatio(float _releaseTauRatio) { 
+        releaseTauRatio = _releaseTauRatio;
+        tau1 = release * releaseTauRatio;
+    }
     float getReleaseTauRatio() { return releaseTauRatio; }
+    
+    float getTau0() { return tau0; }
+    float getTau1() { return tau1; }
 
     void setV0(float _v0) { v0 = _v0; }
     float getV0() { return v0; }
@@ -97,9 +111,23 @@
     float releaseTauRatio;
 };
 
+class EnvelopeParam {
+public:
+    int attack;
+    int release;
+    float v0;
+    float v1;
+    float v2;
+    float attackTauRatio;
+    float releaseTauRatio;
+};
+    
 EnvelopeAR envelopeFrequency(
     FREQUENCY_ATTACK, FREQUENCY_RELEASE, 880.0f, 120.0f, 40.0f, 0.36f, 0.1f);
-EnvelopeAR envelopeAmplitude(50, AMPLITUDE_RELEASE, 0.95f, 1.0f, 0.0f);
+EnvelopeAR envelopeAmplitude(50, 200, 0.99f, 1.0f, 0.0f);
+
+volatile EnvelopeParam frequencyParam;
+volatile EnvelopeParam amplitudeParam;
 
 volatile int ticks;
 volatile int envelopeTicks;
@@ -108,10 +136,8 @@
 volatile float phiDelta;
 volatile float amplitude;
 
-float bpm;
-int envelopeLength;
-int amplitudeAttack;
-float frequencyF0;
+volatile float bpm;
+volatile int envelopeLength;
 
 void generateWave()
 {
@@ -144,11 +170,41 @@
     ticks++;
     if (ticks == SAMPLING_RATE / 1000) {
         ticks = 0;
+        
+        // set envelope parameters
+        envelopeLength = 60 * 1000 / bpm;
+        
+        envelopeAmplitude.setAttack(amplitudeParam.attack);
+        envelopeAmplitude.setRelease(amplitudeParam.release);
+        envelopeAmplitude.setV0(amplitudeParam.v0);
+        envelopeAmplitude.setV1(amplitudeParam.v1);
+        envelopeAmplitude.setV2(amplitudeParam.v2);
+        envelopeAmplitude.setAttackTauRatio(amplitudeParam.attackTauRatio);
+        envelopeAmplitude.setReleaseTauRatio(amplitudeParam.releaseTauRatio);
+                
+        envelopeFrequency.setV0(frequencyParam.v0);
+        
         generateEnvelope();
     }
     generateWave();
 }
 
+void setParams()
+{
+    bpm = Ain0.read() * 180.0f + 60.0f;
+    
+    amplitudeParam.attack = Ain1.read() * envelopeLength;
+    amplitudeParam.release = Ain2.read() * envelopeLength;
+    amplitudeParam.v0 = Ain3.read();
+    amplitudeParam.v1 = 1.0f;
+    amplitudeParam.v2 = 0.0f;
+    amplitudeParam.attackTauRatio = 0.36f;
+    amplitudeParam.releaseTauRatio = 0.36f;
+    
+    frequencyParam.v0 = Ain5.read() * 3000.0f + 50.0f;
+    frequencyParam.v1 = frequencyParam.v0;
+}
+
 int main()
 {
     printf("%s %s\r\n", TITLE_STR1, TITLE_STR2);
@@ -160,20 +216,20 @@
     ticks = 0;
     envelopeTicks = 0;
     
+    bpm = 120.0f;
+    setParams();
+
     Ticker samplingTicker;
     samplingTicker.attach(&update, SAMPLING_PERIOD);
         
-    bpm = 120.0f;
     for (;;) {
-        bpm = AinBpm.read() * 180.0f + 60.0f;
-        envelopeLength = 60 * 1000 / bpm;
-        amplitudeAttack = AinAmplitudeAttack.read() * envelopeLength * 0.7f;
-        envelopeAmplitude.setAttack(amplitudeAttack);
-        frequencyF0 = AinFrequencyF0.read() * 2000.0f + 50.0f;
-        envelopeFrequency.setV0(frequencyF0);
-        
-        printf("%f\t%d\t%d\t%f\r\n", bpm, envelopeLength, amplitudeAttack, frequencyF0);
-        
+        setParams();
+        printf("%.1f\t%d\t%d\t%.1f\r\n", 
+            bpm,
+            envelopeLength,
+            amplitudeParam.attack,
+            frequencyParam.v0
+        );
         Thread::wait(500);    
     }
 }