From c2ebe39f4a99302f83c969df65d5bd105234cadc Mon Sep 17 00:00:00 2001
From: jsiegle <joshs@alleninstitute.org>
Date: Thu, 18 Sep 2014 14:34:19 -0700
Subject: [PATCH] Finally! The noise gate does what it's supposed to

---
 Source/Processors/AudioNode.cpp | 18 ++++++++----------
 Source/Processors/AudioNode.h   |  2 +-
 2 files changed, 9 insertions(+), 11 deletions(-)

diff --git a/Source/Processors/AudioNode.cpp b/Source/Processors/AudioNode.cpp
index 0d274f5a5..a804995f4 100755
--- a/Source/Processors/AudioNode.cpp
+++ b/Source/Processors/AudioNode.cpp
@@ -142,7 +142,7 @@ void AudioNode::setParameter(int parameterIndex, float newValue)
     {
         // noiseGateLevel level
 
-        Expander.setThreshold(newValue); // in microVolts
+        expander.setThreshold(newValue); // in microVolts
 
     }
     else if (parameterIndex == 100)
@@ -359,9 +359,7 @@ void AudioNode::process(AudioSampleBuffer& buffer,
                     }
 
                     // Simple implementation of a "noise gate" on audio output
-                    // Modified from http://musicdsp.org/archive.php?classid=1#272:
-
-                    Expander.process(buffer.getWritePointer(0), // left channel
+                    expander.process(buffer.getWritePointer(0), // left channel
                                        buffer.getWritePointer(1), // right channel
                                        buffer.getNumSamples());
 
@@ -381,14 +379,14 @@ void AudioNode::process(AudioSampleBuffer& buffer,
 Expander::Expander()
 {
   threshold = 1.f;
-  attack = release = envelope_decay = 0.f;
   output = 1.f;
     
-  transfer_A = 0.f;
-  transfer_B = 1.f;
-    
   env = 0.f;
   gain = 1.f;
+
+  setAttack(1.0f);
+  setRelease(1.0f);
+  setRatio(1.2); // ratio > 1.0 will decrease gain below threshold
 }
 
 void Expander::setThreshold(float value)
@@ -432,13 +430,13 @@ void Expander::process(float* leftChan, float* rightChan, int numSamples)
 
       env = det >= env ? det : det + envelope_decay*(env-det);
 
-      transfer_gain = env > threshold ? pow(env, transfer_A) * transfer_B : output;
+      transfer_gain = env < threshold ? pow(env, transfer_A) * transfer_B : output;
 
       gain = transfer_gain < gain ?
                       transfer_gain + attack * (gain - transfer_gain) :
                       transfer_gain + release * (gain - transfer_gain);
 
-      leftChan[i] = leftChan[i] * gain;
+      leftChan[i] = leftChan[i] * gain; 
       rightChan[i] = rightChan[i] * gain;
   }
 }
\ No newline at end of file
diff --git a/Source/Processors/AudioNode.h b/Source/Processors/AudioNode.h
index f153208be..cec80c82b 100755
--- a/Source/Processors/AudioNode.h
+++ b/Source/Processors/AudioNode.h
@@ -139,7 +139,7 @@ private:
 
     bool bufferSwap;
 
-    Compressor compressor;
+    Expander expander;
 
     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(AudioNode);
 
-- 
GitLab