package com.domemusictechnologies.kleinerkiffer;


import voltage.controllers.*;
import voltage.core.*;
import voltage.core.Jack.JackType;
import voltage.sources.*;
import voltage.utility.*;
import voltage.processors.*;
import voltage.effects.*;
import java.awt.*;

//[user-imports]   Add your own imports here

import static java.lang.Math.pow;   // Raise to power method, used for V/oct calc
import static java.lang.Math.exp;   // e to the power x method, used for RC time constant calc


//[/user-imports]


public class KleinerKiffer extends VoltageModule
//[user-inheritance]
//[/user-inheritance]
{

public KleinerKiffer( long moduleID, VoltageObjects voltageObjects )
{
   super( moduleID, voltageObjects, "Kleiner-Kiffer Phaser", ModuleType.ModuleType_Effect, 1.0 );

   InitializeControls();


   canBeBypassed = true;
   SetSkin( "a9c4583e92894bc5ad1d720caaf4772f" );
}

void InitializeControls()
{

   inputAudio = new VoltageAudioJack( "Audio Input", "Audio Input", this, JackType.JackType_AudioInput );
   AddComponent( inputAudio );
   inputAudio.SetWantsMouseNotifications( false );
   inputAudio.SetPosition( 24, 57 );
   inputAudio.SetSize( 25, 25 );
   inputAudio.SetSkin( "Jack Round Yellow Ring" );

   inputCV = new VoltageAudioJack( "CV Input", "CV Input", this, JackType.JackType_AudioInput );
   AddComponent( inputCV );
   inputCV.SetWantsMouseNotifications( false );
   inputCV.SetPosition( 24, 302 );
   inputCV.SetSize( 25, 25 );
   inputCV.SetSkin( "Jack Round Plum Ring" );

   outputAudio = new VoltageAudioJack( "Audio Output", "Audio Output", this, JackType.JackType_AudioOutput );
   AddComponent( outputAudio );
   outputAudio.SetWantsMouseNotifications( false );
   outputAudio.SetPosition( 24, 105 );
   outputAudio.SetSize( 25, 25 );
   outputAudio.SetSkin( "Jack Round Orange Ring" );

   textLabel1 = new VoltageLabel( "textLabel1", "textLabel1", this, "Input" );
   AddComponent( textLabel1 );
   textLabel1.SetWantsMouseNotifications( false );
   textLabel1.SetPosition( 12, 35 );
   textLabel1.SetSize( 50, 22 );
   textLabel1.SetEditable( false, false );
   textLabel1.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel1.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel1.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel1.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel1.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel1.SetBorderSize( 1 );
   textLabel1.SetMultiLineEdit( false );
   textLabel1.SetIsNumberEditor( false );
   textLabel1.SetNumberEditorRange( 0, 100 );
   textLabel1.SetNumberEditorInterval( 1 );
   textLabel1.SetNumberEditorUsesMouseWheel( false );
   textLabel1.SetHasCustomTextHoverColor( false );
   textLabel1.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel1.SetFont( "Verdana", 14, true, false );

   textLabel2 = new VoltageLabel( "textLabel2", "textLabel2", this, "Output" );
   AddComponent( textLabel2 );
   textLabel2.SetWantsMouseNotifications( false );
   textLabel2.SetPosition( 11, 85 );
   textLabel2.SetSize( 50, 22 );
   textLabel2.SetEditable( false, false );
   textLabel2.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel2.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel2.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel2.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel2.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel2.SetBorderSize( 1 );
   textLabel2.SetMultiLineEdit( false );
   textLabel2.SetIsNumberEditor( false );
   textLabel2.SetNumberEditorRange( 0, 100 );
   textLabel2.SetNumberEditorInterval( 1 );
   textLabel2.SetNumberEditorUsesMouseWheel( false );
   textLabel2.SetHasCustomTextHoverColor( false );
   textLabel2.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel2.SetFont( "Verdana", 14, true, false );

   textLabel5 = new VoltageLabel( "textLabel5", "textLabel5", this, "Ext CV" );
   AddComponent( textLabel5 );
   textLabel5.SetWantsMouseNotifications( false );
   textLabel5.SetPosition( 8, 283 );
   textLabel5.SetSize( 57, 20 );
   textLabel5.SetEditable( false, false );
   textLabel5.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel5.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel5.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel5.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel5.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel5.SetBorderSize( 1 );
   textLabel5.SetMultiLineEdit( false );
   textLabel5.SetIsNumberEditor( false );
   textLabel5.SetNumberEditorRange( 0, 100 );
   textLabel5.SetNumberEditorInterval( 1 );
   textLabel5.SetNumberEditorUsesMouseWheel( false );
   textLabel5.SetHasCustomTextHoverColor( false );
   textLabel5.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel5.SetFont( "Verdana", 14, true, false );

   knobLFORate = new VoltageKnob( "LFO Rate", "LFO Rate", this, 0.04, 12, 1 );
   AddComponent( knobLFORate );
   knobLFORate.SetWantsMouseNotifications( false );
   knobLFORate.SetPosition( 10, 223 );
   knobLFORate.SetSize( 50, 50 );
   knobLFORate.SetSkin( "TR Large" );
   knobLFORate.SetRange( 0.04, 12, 1, false, 0 );
   knobLFORate.SetKnobParams( 215, 145 );
   knobLFORate.DisplayValueInPercent( false );
   knobLFORate.SetKnobAdjustsRing( true );
   knobLFORate.SetMidpointValue( 1 );

   textLabel23 = new VoltageLabel( "textLabel23", "textLabel23", this, "LFO Rate" );
   AddComponent( textLabel23 );
   textLabel23.SetWantsMouseNotifications( false );
   textLabel23.SetPosition( 5, 201 );
   textLabel23.SetSize( 60, 22 );
   textLabel23.SetEditable( false, false );
   textLabel23.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel23.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel23.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel23.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel23.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel23.SetBorderSize( 1 );
   textLabel23.SetMultiLineEdit( false );
   textLabel23.SetIsNumberEditor( false );
   textLabel23.SetNumberEditorRange( 0, 100 );
   textLabel23.SetNumberEditorInterval( 1 );
   textLabel23.SetNumberEditorUsesMouseWheel( false );
   textLabel23.SetHasCustomTextHoverColor( false );
   textLabel23.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel23.SetFont( "Verdana", 14, true, false );

   switchColor = new VoltageSwitch( "Color Switch", "Color Switch", this, 1 );
   AddComponent( switchColor );
   switchColor.SetWantsMouseNotifications( false );
   switchColor.SetPosition( 28, 154 );
   switchColor.SetSize( 15, 39 );
   switchColor.SetSkin( "3-State Slide Vertical" );

   textLabel3 = new VoltageLabel( "textLabel3", "textLabel3", this, "HI" );
   AddComponent( textLabel3 );
   textLabel3.SetWantsMouseNotifications( false );
   textLabel3.SetPosition( 43, 165 );
   textLabel3.SetSize( 20, 16 );
   textLabel3.SetEditable( false, false );
   textLabel3.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel3.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel3.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel3.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel3.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel3.SetBorderSize( 1 );
   textLabel3.SetMultiLineEdit( false );
   textLabel3.SetIsNumberEditor( false );
   textLabel3.SetNumberEditorRange( 0, 100 );
   textLabel3.SetNumberEditorInterval( 1 );
   textLabel3.SetNumberEditorUsesMouseWheel( false );
   textLabel3.SetHasCustomTextHoverColor( false );
   textLabel3.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel3.SetFont( "Verdana", 12, true, false );

   textLabel4 = new VoltageLabel( "textLabel4", "textLabel4", this, "Color" );
   AddComponent( textLabel4 );
   textLabel4.SetWantsMouseNotifications( false );
   textLabel4.SetPosition( 11, 135 );
   textLabel4.SetSize( 50, 22 );
   textLabel4.SetEditable( false, false );
   textLabel4.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel4.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel4.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel4.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel4.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel4.SetBorderSize( 1 );
   textLabel4.SetMultiLineEdit( false );
   textLabel4.SetIsNumberEditor( false );
   textLabel4.SetNumberEditorRange( 0, 100 );
   textLabel4.SetNumberEditorInterval( 1 );
   textLabel4.SetNumberEditorUsesMouseWheel( false );
   textLabel4.SetHasCustomTextHoverColor( false );
   textLabel4.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel4.SetFont( "Verdana", 14, true, false );

   textLabel6 = new VoltageLabel( "textLabel6", "textLabel6", this, "LO" );
   AddComponent( textLabel6 );
   textLabel6.SetWantsMouseNotifications( false );
   textLabel6.SetPosition( 43, 177 );
   textLabel6.SetSize( 20, 17 );
   textLabel6.SetEditable( false, false );
   textLabel6.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel6.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel6.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel6.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel6.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel6.SetBorderSize( 1 );
   textLabel6.SetMultiLineEdit( false );
   textLabel6.SetIsNumberEditor( false );
   textLabel6.SetNumberEditorRange( 0, 100 );
   textLabel6.SetNumberEditorInterval( 1 );
   textLabel6.SetNumberEditorUsesMouseWheel( false );
   textLabel6.SetHasCustomTextHoverColor( false );
   textLabel6.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel6.SetFont( "Verdana", 12, true, false );

   textLabel27 = new VoltageLabel( "textLabel27", "textLabel27", this, "Kleiner Kiffer" );
   AddComponent( textLabel27 );
   textLabel27.SetWantsMouseNotifications( false );
   textLabel27.SetPosition( 14, 0 );
   textLabel27.SetSize( 43, 31 );
   textLabel27.SetEditable( false, false );
   textLabel27.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel27.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel27.SetColor( new Color( 251, 42, 0, 255 ) );
   textLabel27.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel27.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel27.SetBorderSize( 1 );
   textLabel27.SetMultiLineEdit( true );
   textLabel27.SetIsNumberEditor( false );
   textLabel27.SetNumberEditorRange( 0, 100 );
   textLabel27.SetNumberEditorInterval( 1 );
   textLabel27.SetNumberEditorUsesMouseWheel( false );
   textLabel27.SetHasCustomTextHoverColor( false );
   textLabel27.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel27.SetFont( "Verdana", 14, false, false );

   textLabel28 = new VoltageLabel( "textLabel28", "textLabel28", this, "Dome Music Technologies" );
   AddComponent( textLabel28 );
   textLabel28.SetWantsMouseNotifications( false );
   textLabel28.SetPosition( 12, 342 );
   textLabel28.SetSize( 48, 19 );
   textLabel28.SetEditable( false, false );
   textLabel28.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel28.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel28.SetColor( new Color( 251, 42, 0, 255 ) );
   textLabel28.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel28.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel28.SetBorderSize( 1 );
   textLabel28.SetMultiLineEdit( true );
   textLabel28.SetIsNumberEditor( false );
   textLabel28.SetNumberEditorRange( 0, 100 );
   textLabel28.SetNumberEditorInterval( 1 );
   textLabel28.SetNumberEditorUsesMouseWheel( false );
   textLabel28.SetHasCustomTextHoverColor( false );
   textLabel28.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel28.SetFont( "Verdana", 7, false, false );

   textLabel7 = new VoltageLabel( "textLabel7", "textLabel7", this, "Off" );
   AddComponent( textLabel7 );
   textLabel7.SetWantsMouseNotifications( false );
   textLabel7.SetPosition( 43, 154 );
   textLabel7.SetSize( 21, 14 );
   textLabel7.SetEditable( false, false );
   textLabel7.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel7.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel7.SetColor( new Color( 0, 0, 0, 255 ) );
   textLabel7.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel7.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel7.SetBorderSize( 1 );
   textLabel7.SetMultiLineEdit( false );
   textLabel7.SetIsNumberEditor( false );
   textLabel7.SetNumberEditorRange( 0, 100 );
   textLabel7.SetNumberEditorInterval( 1 );
   textLabel7.SetNumberEditorUsesMouseWheel( false );
   textLabel7.SetHasCustomTextHoverColor( false );
   textLabel7.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel7.SetFont( "Verdana", 12, true, false );
}



//-------------------------------------------------------------------------------
//  public void Initialize()

//  Initialize will get called shortly after your module's constructor runs. You can use it to
//  do any initialization that the auto-generated code doesn't handle.
//-------------------------------------------------------------------------------
@Override
public void Initialize()
{
   //[user-Initialize]   Add your own initialization code here

   CheckColor(switchColor.GetValue());
   CheckConnections();
   SetLFORate();
   
   previousFeedback = 0.0;
   
   stage1Capacitor = 0.0;
   stage2Capacitor = 0.0;
   stage3Capacitor = 0.0;
   stage4Capacitor = 0.0;

   //[/user-Initialize]
}


//-------------------------------------------------------------------------------
//  public void Destroy()

//  Destroy will get called just before your module gets deleted. You can use it to perform any
//  cleanup that's not handled automatically by Java.
//-------------------------------------------------------------------------------
@Override
public void Destroy()
{
   super.Destroy();
   //[user-Destroy]   Add your own module-getting-deleted code here



   //[/user-Destroy]
}


//-------------------------------------------------------------------------------
//  public boolean Notify( VoltageComponent component, ModuleNotifications notification, double doubleValue, long longValue, int x, int y, Object object )

//  Notify will get called when various events occur - control values changing, timers firing, etc.
//-------------------------------------------------------------------------------
@Override
public boolean Notify( VoltageComponent component, ModuleNotifications notification, double doubleValue, long longValue, int x, int y, Object object )
{
   //[user-Notify]   Add your own notification handling code between this line and the notify-close comment
   switch( notification )
   {
      case Knob_Changed:   // doubleValue is the new VoltageKnob value
      {
         if (component == knobLFORate)
         {
            SetLFORate();
         }
      }
      break;
   
      case Slider_Changed:   // doubleValue is the new slider value
      {
      }
      break;
   
      case Button_Changed:   // doubleValue is the new button/toggle button value
      {
      }
      break;
   
      case Switch_Changed:   // doubleValue is the new switch value
      {
         if (component == switchColor)
         {
            CheckColor(doubleValue);
         }
      }
      break;
   
      case Jack_Connected:   // longValue is the new cable ID
      {
         CheckConnections();
      }
      break;
   
      case Jack_Disconnected:   // All cables have been disconnected from this jack
      {
         CheckConnections();
      }
      break;
   
      case GUI_Update_Timer:   // Called every 50ms (by default) if turned on
      {
      }
      break;
   
      case Object_MouseMove:   // called when mouse is over an object that receives mouse notifications. 'object' parameter is a VoltageMouseKeyFlags object.
      {
      }
      break;
   
      case Object_MouseLeave:  // called when mouse leaves an object that receives mouse notifications. 'object' parameter is a VoltageMouseKeyFlags object.
      {
      }
      break;
   
      case Object_LeftButtonDown:   // called when user left-clicks on an object that receives mouse notifications. 'object' parameter is a VoltageMouseKeyFlags object.
      {
      }
      break;
   
      case Object_LeftButtonUp:   // called when user releases left mouse button on an object that receives mouse notifications. 'object' parameter is a VoltageMouseKeyFlags object.
      {
      }
      break;
   
      case Object_RightButtonDown:   // called when user releases right mouse button on an object that receives mouse notifications. 'object' parameter is a VoltageMouseKeyFlags object.
      {
      }
      break;
   
      case Object_RightButtonUp:   // called when user right-clicks on an object that receives mouse notifications
      {
      }
      break;
   
      case Object_LeftButtonDoubleClick: // called when user left-button double-clicks on an object that receives mouse notifications
      {
      }
      break;
   
      // Less common notifications:
   
      case Named_Timer:   // object contains a String with the name of the timer that has fired
      {
      }
      break;
   
      case Canvas_Painting:   // About to paint canvas.  object is a java.awt.Rectangle with painting boundaries
      {
      }
      break;
   
      case Canvas_Painted:   // Canvas painting is complete
      {
      }
      break;
   
      case Control_DragStart:    // A user has started dragging on a control that has been marked as draggable
      {
      }
      break;
   
      case Control_DragOn:       // This control has been dragged over during a drag operation. object contains the dragged object
      {
      }
      break;
   
      case Control_DragOff:      // This control has been dragged over during a drag operation. object contains the dragged object
      {
      }
      break;
   
      case Control_DragEnd:      // A user has ended their drag on a control that has been marked as draggable
      {
      }
      break;
   
      case Label_Changed:        // The text of an editable text control has changed
      {
      }
      break;
   
      case SoundPlayback_Start:   // A sound has begun playback
      {
      }
      break;
   
      case SoundPlayback_End:     // A sound has ended playback
      {
      }
      break;
   
      case Scrollbar_Position:    // longValue is the new scrollbar position
      {
      }
      break;
   
      case PolyVoices_Changed:    // longValue is the new number of poly voices
      {
      }
      break;
   
      case File_Dropped:     // 'object' is a String containing the file path
      {
      }
      break;
   
      case Preset_Loading_Start:   // called when preset loading begins
      {
      }
      break;
   
      case Preset_Loading_Finish:  // called when preset loading finishes
      {
      }
      break;
   
      case Variation_Loading_Start:    // sent when a variation is about to load
      {
      }
      break;
   
      case Variation_Loading_Finish:   // sent when a variation has just finished loading
      {
      }
      break;
   
      case Tempo_Changed:     // doubleValue is the new tempo
      {
      }
      break;
   
      case Randomized:     // called when the module's controls get randomized
      {
      }
      break;
   
      case VariationListChanged:   // sent when a variation gets added, deleted, or renamed, or the variations list gets reordered
      {
      }
      break;
   
      case Key_Press:     // sent when module has keyboard focus and a key is pressed; object is a VoltageKeyPressInfo object
      {
      }
      break;
   
      case Reset:    // sent when the module has been reset to default settings
      {
      }
      break;
   
      case Keyboard_NoteOn:   // sent when a note has been pressed on a VoltageKeyboard object. longValue is the note value ( 0-127 )
      {
      }
      break;
   
      case Keyboard_NoteOff:   // sent when a note has been released on a VoltageKeyboard object. longValue is the note value ( 0-127 )
      {
      }
      break;
   
      case Curve_Changed:   // sent when user has edited a curve's value. 'object' will be a VoltageCurve.CurveChangeNotification object.
      {
      }
      break;
   }



   return false;
   //[/user-Notify]
}


//-------------------------------------------------------------------------------
//  public void ProcessSample()

//  ProcessSample is called once per sample. Usually it's where you read
//  from input jacks, process audio, and write it to your output jacks.
//  Since ProcesssSample gets called 48,000 times per second, offload CPU-intensive operations
//  to other threads when possible and avoid calling native functions.
//-------------------------------------------------------------------------------
@Override
public void ProcessSample()
{
   //[user-ProcessSample]   Add your own process-sampling code here
   double sumOfAllCVs;
   double drySignal;
   double wetSignal;
   double timeFactorRC;
   double resistorVoltage;
   double attenuatedCV;
   double voltsPerOctaveMultiplier;
   double finalOutputValue;
   
   drySignal = inputAudio.GetValue();
   
   wetSignal = drySignal + (previousFeedback * FeedbackAmount);
   
   if (CVinConnected)
   {
      attenuatedCV = inputCV.GetValue() * CVrangeMultiplier;
   }
   else
   {
      TriangleLFO.AdvanceSample();
      attenuatedCV = TriangleLFO.GetTriangleValue() * CVrangeMultiplier;
   }
   
   sumOfAllCVs = FREQ_KNOB_SETTING;
   sumOfAllCVs += attenuatedCV;
   
   voltsPerOctaveMultiplier = pow (2.0, sumOfAllCVs);
   
   timeFactorRC = exp (CENTRE_FREQ * voltsPerOctaveMultiplier);
      
   // Perform Stage 1 Allpass unconditionally
   resistorVoltage = wetSignal - stage1Capacitor;
   stage1Capacitor = wetSignal - (resistorVoltage * timeFactorRC);
   wetSignal = stage1Capacitor + stage1Capacitor - wetSignal;
   
   // Perform Stage 2 Allpass unconditionally
   resistorVoltage = wetSignal - stage2Capacitor;
   stage2Capacitor = wetSignal - (resistorVoltage * timeFactorRC);
   wetSignal = stage2Capacitor + stage2Capacitor - wetSignal;
   
   // Perform Stage 3 Allpass
   resistorVoltage = wetSignal - stage3Capacitor;
   stage3Capacitor = wetSignal - (resistorVoltage * timeFactorRC);
   wetSignal = stage3Capacitor + stage3Capacitor - wetSignal;
   
   // Perform Stage 4 Allpass
   resistorVoltage = wetSignal - stage4Capacitor;
   stage4Capacitor = wetSignal - (resistorVoltage * timeFactorRC);
   wetSignal = stage4Capacitor + stage4Capacitor - wetSignal;
   
   // Assign wet signal as feedback value for next iteration
   previousFeedback = wetSignal;
   
   finalOutputValue = (OutMixWet * wetSignal) +
                      (OutMixDry * drySignal);

   if (BypassSelected)
   {
      outputAudio.SetValue(drySignal);
   }
   else
   {                  
      outputAudio.SetValue(finalOutputValue);
   }
   //[/user-ProcessSample]
}


//-------------------------------------------------------------------------------
//  public void ProcessBypassedSample()

//  ProcessBypassedSample gets called instead of ProcessSample when you've checked the "Can Be Bypassed" box
//  and a user has bypassed your module. If your module processes data from input jacks and then sends it to
//  output jack(s), make ProcessBypassedSample just send the data from the input jacks to the output jacks without
//  processing it.
//-------------------------------------------------------------------------------
@Override
public void ProcessBypassedSample()
{
   //[user-ProcessBypassedSample]   Add your own code here

   double temp;
   
   temp = inputAudio.GetValue();
   outputAudio.SetValue(temp);

   //[/user-ProcessBypassedSample]
}


//-------------------------------------------------------------------------------
//  public String GetTooltipText( VoltageComponent component )

//  Gets called when a tooltip is about to display for a control. Override it if
//  you want to change what the tooltip displays - if you want a knob to work in logarithmic fashion,
//  for instance, you can translate the knob's current value to a log-based string and display it here.
//-------------------------------------------------------------------------------
@Override
public String GetTooltipText( VoltageComponent component )
{
   //[user-GetTooltipText]   Add your own code here



   return super.GetTooltipText( component );
   //[/user-GetTooltipText]
}


//-------------------------------------------------------------------------------
//  public void EditComponentValue( VoltageComponent component, double newValue, String newText )

//  Gets called after a user clicks on a tooltip and types in a new value for a control. Override this if
//  you've changed the default tooltip display (translating a linear value to logarithmic, for instance)
//  in GetTooltipText().
//-------------------------------------------------------------------------------
@Override
public void EditComponentValue( VoltageComponent component, double newValue, String newText )
{
   //[user-EditComponentValue]   Add your own code here



   //[/user-EditComponentValue]
   super.EditComponentValue( component, newValue, newText );
}


//-------------------------------------------------------------------------------
//  public void OnUndoRedo( String undoType, double newValue, Object optionalObject )

//  If you've created custom undo events via calls to CreateUndoEvent, you'll need to
//  process them in this function when they get triggered by undo/redo actions.
//-------------------------------------------------------------------------------
@Override
public void OnUndoRedo( String undoType, double newValue, Object optionalObject )
{
   //[user-OnUndoRedo]   Add your own code here



   //[/user-OnUndoRedo]
}


//-------------------------------------------------------------------------------
//  public byte[] GetStateInformation()

//  Gets called when the module's state gets saved, typically when the user saves a preset with
//  this module in it. Voltage Modular will automatically save the states of knobs, sliders, etc.,
//  but if you have any custom state information you need to save, return it from this function.
//-------------------------------------------------------------------------------
@Override
public byte[] GetStateInformation()
{
   //[user-GetStateInformation]   Add your own code here



   return null;
   //[/user-GetStateInformation]
}


//-------------------------------------------------------------------------------
//  public void SetStateInformation(byte[] stateInfo)

//  Gets called when this module's state is getting restored, typically when a user opens a preset with
//  this module in it. The stateInfo parameter will contain whatever custom data you stored in GetStateInformation().
//-------------------------------------------------------------------------------
@Override
public void SetStateInformation(byte[] stateInfo)
{
   //[user-SetStateInformation]   Add your own code here



   //[/user-SetStateInformation]
}


//-------------------------------------------------------------------------------
//  public byte[] GetStateInformationForVariations()

//  Gets called when a user saves a variation with this module in it.
//  Voltage Modular will automatically save the states of knobs, sliders, etc.,
//  but if you have any custom state information you need to save, return it from this function.
//-------------------------------------------------------------------------------
@Override
public byte[] GetStateInformationForVariations()
{
   //[user-GetStateInformationForVariations]   Add your own code here



   return GetStateInformation();
   //[/user-GetStateInformationForVariations]
}


//-------------------------------------------------------------------------------
//  public void SetStateInformationForVariations(byte[] stateInfo)

//  Gets called when a user loads a variation with this module in it.
//  The stateInfo parameter will contain whatever custom data you stored in GetStateInformationForVariations().
//-------------------------------------------------------------------------------
@Override
public void SetStateInformationForVariations(byte[] stateInfo)
{
   //[user-SetStateInformationForVariations]   Add your own code here
   SetStateInformation(stateInfo);



   //[/user-SetStateInformationForVariations]
}


// Auto-generated variables
private VoltageLabel textLabel7;
private VoltageLabel textLabel28;
private VoltageLabel textLabel27;
private VoltageLabel textLabel6;
private VoltageLabel textLabel4;
private VoltageLabel textLabel3;
private VoltageSwitch switchColor;
private VoltageLabel textLabel23;
private VoltageKnob knobLFORate;
private VoltageLabel textLabel5;
private VoltageLabel textLabel2;
private VoltageLabel textLabel1;
private VoltageAudioJack outputAudio;
private VoltageAudioJack inputCV;
private VoltageAudioJack inputAudio;


//[user-code-and-variables]    Add your own variables and functions here

// Constants

private final double CENTRE_FREQ = -4500.0 / Values.SampleRate;
private final double FREQ_KNOB_SETTING = -0.25;
private final double CV_RANGE_LO = 0.45;
private final double CV_RANGE_HI = 0.45;
private final double OUT_MIX_LO = 0.5;
private final double OUT_MIX_HI = 0.667;
private final double FEEDBACK_LO = 0;
private final double FEEDBACK_HI = 0.8;

// Variables

private double previousFeedback;

private double stage1Capacitor;
private double stage2Capacitor;
private double stage3Capacitor;
private double stage4Capacitor;

private double CVrangeMultiplier;
private double OutMixDry;
private double OutMixWet;
private double FeedbackAmount;

private boolean CVinConnected;
private boolean InputConnected;
private boolean OutputConnected;

private boolean BypassSelected;

// Objects

protected DigitalOscillator TriangleLFO = new DigitalOscillator();

private void CheckConnections()
{
   CVinConnected = inputCV.IsConnected();
   InputConnected = inputAudio.IsConnected();
   OutputConnected = outputAudio.IsConnected();
   
   knobLFORate.SetEnabled(!CVinConnected);
}

private void SetLFORate()
{
   double temp;
   temp = knobLFORate.GetValue();
   TriangleLFO.SetFrequency(temp);
}

private void CheckColor(double colourValue)
{
   if (colourValue < 0.5)
   {
      CVrangeMultiplier = CV_RANGE_LO;
      OutMixWet = OUT_MIX_LO;
      OutMixDry = 1.0 - OUT_MIX_LO;
      FeedbackAmount = FEEDBACK_LO;
      BypassSelected = false;
   }
   else if (colourValue < 1.5)
   {
      CVrangeMultiplier = CV_RANGE_HI;
      OutMixWet = OUT_MIX_HI;
      OutMixDry = 1.0 - OUT_MIX_HI;
      FeedbackAmount = FEEDBACK_HI;
      BypassSelected = false;
   }
   else
   {
      CVrangeMultiplier = CV_RANGE_HI;
      OutMixWet = OUT_MIX_HI;
      OutMixDry = 1.0 - OUT_MIX_HI;
      FeedbackAmount = FEEDBACK_HI;
      BypassSelected = true;
   }
}



//[/user-code-and-variables]
}

 