From 93d590284873e1f59f7a9adf50992423c0ed12a2 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Thu, 29 Jun 2023 19:53:18 +0200
Subject: [PATCH] Add log envelope

---
 Soft/Drum/Drum.ino    | 20 ++++------
 Soft/Drum/Envelopes.h | 88 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 86 insertions(+), 22 deletions(-)

diff --git a/Soft/Drum/Drum.ino b/Soft/Drum/Drum.ino
index c505656..b1230a8 100644
--- a/Soft/Drum/Drum.ino
+++ b/Soft/Drum/Drum.ino
@@ -20,8 +20,8 @@
 
 int wave[SAMPLES];
 EnvelopeSimpleLinear kick_volume_envelope;
+EnvelopeSimpleLog kick_pitch_envelope;
 
-float pitch_envelope = 0.0;
 float pitch_sigh = 0.0;
 
 double last_trigger = 0;
@@ -34,8 +34,6 @@ int chord_pot;
 float volume_decay;
 
 int inv_pot;
-float pitch_decay;
-float pitch_envelope_amount;
 float pitch_sigh_amount = KICK_SIGH_AMOUNT;
 float pitch_sigh_speed = KICK_SIGH_SPEED;
 float sigh_incrementer = 0.0;
@@ -91,9 +89,10 @@ void loop() {
   osc_freq = KICK_BASE_FREQ + freq_pot/1023.0*KICK_FREQ_RANGE;
   // Bigger decay values make the envelope shorter
   kick_volume_envelope.setSpeed(0.000005 + (float)chord_pot/1023.0/3000.0);
-  pitch_decay = 0.00001 + (float)inv_pot/1023.0/300.0;
+  kick_pitch_envelope.setSpeed(0.00001 + (float)inv_pot/1023.0/800.0);
   // Increase the amount of the pitch envelope if the pitch decay is longer
-  pitch_envelope_amount = 0.5 + (1023.0-(float)inv_pot)/1023.0*1.5;
+  kick_pitch_envelope.setAmount(2.0 + (1023.0-(float)inv_pot)/1023.0*8.5);
+  // kick_pitch_envelope.setAmount(0.0);
 
   //  -------------------push sw , play wave-------------------------------
   readGates();
@@ -113,12 +112,7 @@ void updateKickEnvelopes() {
     }
   }
 
-  // Decrement the pitch envelope
-  pitch_envelope -= pitch_decay;
-  if (pitch_envelope < 0.0001) {
-    pitch_envelope = 0.0;
-  }
-
+  kick_pitch_envelope.update();
   kick_volume_envelope.update();
 }
 
@@ -134,7 +128,7 @@ void on_pwm_wrap() {
   pwm_set_chan_level(slice_num, PWM_CHAN_A, kick_volume_envelope.value*wave[(int)osc_progress]/2+511);
 
   // Advance the oscillator and reset it if it reached the end
-  osc_progress += osc_freq + pitch_envelope + pitch_sigh * pitch_sigh_amount;
+  osc_progress += osc_freq + kick_pitch_envelope.value + pitch_sigh * pitch_sigh_amount;
   if (osc_progress > SAMPLES) {
     osc_progress = 0;
   }
@@ -167,7 +161,7 @@ void readGates() {
 
 void resetEnvelopes() {
   kick_volume_envelope.reset();
-  pitch_envelope = pitch_envelope_amount;
+  kick_pitch_envelope.reset();
   pitch_sigh = 0.0;
   sigh_incrementer = 0.0;
 }
\ No newline at end of file
diff --git a/Soft/Drum/Envelopes.h b/Soft/Drum/Envelopes.h
index 10d7395..defc48f 100644
--- a/Soft/Drum/Envelopes.h
+++ b/Soft/Drum/Envelopes.h
@@ -1,7 +1,7 @@
-// A linear falling
+// ---- A simple linear falling envelope -------
 class EnvelopeSimpleLinear {
   public:
-    float incrementer = 1.0;
+    float counter = 1.0;
     float value;
     float speed = 0.001;
     float amount = 1.0;
@@ -12,15 +12,15 @@ class EnvelopeSimpleLinear {
 };
 
 void EnvelopeSimpleLinear::update() {
-  this->incrementer -= this->speed;
-  if (this->incrementer <= 0.0) {
-    this->incrementer = 0.0;
+  this->counter -= this->speed;
+  if (this->counter <= 0.0) {
+    this->counter = 0.0;
   }
-  this->value = this->incrementer * this->amount;
+  this->value = this->counter * this->amount;
 }
 
 void EnvelopeSimpleLinear::reset() {
-  this->incrementer = 1.0;
+  this->counter = 1.0;
   this->value = 0.0;
 }
 
@@ -28,6 +28,76 @@ void EnvelopeSimpleLinear::setSpeed(float speed) {
   this->speed = speed;
 }
 
-void EnvelopeSimpleLinear::setAmount(float speed) {
+void EnvelopeSimpleLinear::setAmount(float amount) {
   this->amount = amount;
-}
\ No newline at end of file
+}
+
+// ---- A simple logarithmic falling envelope -------
+class EnvelopeSimpleLog {
+  public:
+    float counter = 1.0;
+    float value;
+    float speed = 0.001;
+    float amount = 1.0;
+    void update();
+    void reset();
+    void setAmount(float speed);
+    void setSpeed(float speed);
+};
+
+void EnvelopeSimpleLog::update() {
+  this->counter -= this->speed;
+  if (this->counter <= 0.0) {
+    this->counter = 0.0;
+  }
+  this->value = max(0.0, (log10(0.005 + 1.0 - this->counter)*-1.0))/2.3 * this->amount;
+}
+
+void EnvelopeSimpleLog::reset() {
+  this->counter = 1.0;
+  this->value = 0.0;
+}
+
+void EnvelopeSimpleLog::setSpeed(float speed) {
+  this->speed = speed;
+}
+
+void EnvelopeSimpleLog::setAmount(float amount) {
+  this->amount = amount;
+}
+
+// // ---- A simple logarithmic falling envelope -------
+// class EnvelopeSigh {
+//   public:
+//     float counter = 0.0;
+//     float value;
+//     float speed = 0.001;
+//     float delay = 300;
+//     float amount = 1.0;
+//     void update();
+//     void reset();
+//     void setAmount(float speed);
+//     void setSpeed(float speed);
+//     void setDelay(float speed);
+// };
+
+// void EnvelopeSimpleLog::update() {
+//   this->counter -= this->speed;
+//   if (this->counter <= 0.0) {
+//     this->counter = 0.0;
+//   }
+//   this->value = max(0.0, (log10(0.005 + 1.0 - this->counter)*-1.0))/2.3 * this->amount;
+// }
+
+// void EnvelopeSimpleLog::reset() {
+//   this->counter = 1.0;
+//   this->value = 0.0;
+// }
+
+// void EnvelopeSimpleLog::setSpeed(float speed) {
+//   this->speed = speed;
+// }
+
+// void EnvelopeSimpleLog::setAmount(float amount) {
+//   this->amount = amount;
+// }
\ No newline at end of file
-- 
GitLab