package com.domemusictechnologies.aquamarine;


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.floor;

//[/user-imports]


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

public AquaMarine( long moduleID, VoltageObjects voltageObjects )
{
   super( moduleID, voltageObjects, "Aqua Marine String Ensemble", ModuleType.ModuleType_Effect, 1.0 );

   InitializeControls();


   canBeBypassed = true;
   SetSkin( "0d93ba82eb144f27ad83784c7506c520" );
}

void InitializeControls()
{

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

   outputEnsemble = new VoltageAudioJack( "Left / Mono Output", "Left / Mono Output", this, JackType.JackType_AudioOutput );
   AddComponent( outputEnsemble );
   outputEnsemble.SetWantsMouseNotifications( false );
   outputEnsemble.SetPosition( 23, 243 );
   outputEnsemble.SetSize( 25, 25 );
   outputEnsemble.SetSkin( "Jack Round Blue Ring" );

   textLabel7 = new VoltageLabel( "textLabel7", "textLabel7", this, "Aqua Marine" );
   AddComponent( textLabel7 );
   textLabel7.SetWantsMouseNotifications( false );
   textLabel7.SetPosition( 13, -2 );
   textLabel7.SetSize( 46, 34 );
   textLabel7.SetEditable( false, false );
   textLabel7.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel7.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel7.SetColor( new Color( 129, 255, 255, 255 ) );
   textLabel7.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel7.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel7.SetBorderSize( 1 );
   textLabel7.SetMultiLineEdit( true );
   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", 14, false, false );

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

   textLabel12 = new VoltageLabel( "textLabel12", "textLabel12", this, "Mono / Left" );
   AddComponent( textLabel12 );
   textLabel12.SetWantsMouseNotifications( false );
   textLabel12.SetPosition( 0, 267 );
   textLabel12.SetSize( 72, 16 );
   textLabel12.SetEditable( false, false );
   textLabel12.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel12.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel12.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel12.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel12.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel12.SetBorderSize( 1 );
   textLabel12.SetMultiLineEdit( false );
   textLabel12.SetIsNumberEditor( false );
   textLabel12.SetNumberEditorRange( 0, 100 );
   textLabel12.SetNumberEditorInterval( 1 );
   textLabel12.SetNumberEditorUsesMouseWheel( false );
   textLabel12.SetHasCustomTextHoverColor( false );
   textLabel12.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel12.SetFont( "Verdana", 10, true, false );

   textLabel13 = new VoltageLabel( "textLabel13", "textLabel13", this, "Right" );
   AddComponent( textLabel13 );
   textLabel13.SetWantsMouseNotifications( false );
   textLabel13.SetPosition( 0, 316 );
   textLabel13.SetSize( 70, 16 );
   textLabel13.SetEditable( false, false );
   textLabel13.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel13.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel13.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel13.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel13.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel13.SetBorderSize( 1 );
   textLabel13.SetMultiLineEdit( false );
   textLabel13.SetIsNumberEditor( false );
   textLabel13.SetNumberEditorRange( 0, 100 );
   textLabel13.SetNumberEditorInterval( 1 );
   textLabel13.SetNumberEditorUsesMouseWheel( false );
   textLabel13.SetHasCustomTextHoverColor( false );
   textLabel13.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel13.SetFont( "Verdana", 10, true, false );

   outputEnsembleRight = new VoltageAudioJack( "Right Stereo Output", "Right Stereo Output", this, JackType.JackType_AudioOutput );
   AddComponent( outputEnsembleRight );
   outputEnsembleRight.SetWantsMouseNotifications( false );
   outputEnsembleRight.SetPosition( 23, 292 );
   outputEnsembleRight.SetSize( 25, 25 );
   outputEnsembleRight.SetSkin( "Jack Round Blue Ring" );

   textLabel23 = new VoltageLabel( "textLabel23", "textLabel23", this, "Dome Music Technologies" );
   AddComponent( textLabel23 );
   textLabel23.SetWantsMouseNotifications( false );
   textLabel23.SetPosition( 9, 341 );
   textLabel23.SetSize( 54, 20 );
   textLabel23.SetEditable( false, false );
   textLabel23.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel23.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel23.SetColor( new Color( 129, 255, 255, 255 ) );
   textLabel23.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel23.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel23.SetBorderSize( 1 );
   textLabel23.SetMultiLineEdit( true );
   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", 7, false, false );

   textLabel10 = new VoltageLabel( "textLabel10", "textLabel10", this, "Outputs" );
   AddComponent( textLabel10 );
   textLabel10.SetWantsMouseNotifications( false );
   textLabel10.SetPosition( 1, 216 );
   textLabel10.SetSize( 71, 28 );
   textLabel10.SetEditable( false, false );
   textLabel10.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel10.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel10.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel10.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel10.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel10.SetBorderSize( 1 );
   textLabel10.SetMultiLineEdit( false );
   textLabel10.SetIsNumberEditor( false );
   textLabel10.SetNumberEditorRange( 0, 100 );
   textLabel10.SetNumberEditorInterval( 1 );
   textLabel10.SetNumberEditorUsesMouseWheel( false );
   textLabel10.SetHasCustomTextHoverColor( false );
   textLabel10.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel10.SetFont( "Verdana", 14, true, false );

   switchMode = new VoltageSwitch( "Chorus Mode", "Chorus Mode", this, 2 );
   AddComponent( switchMode );
   switchMode.SetWantsMouseNotifications( false );
   switchMode.SetPosition( 25, 135 );
   switchMode.SetSize( 19, 51 );
   switchMode.SetSkin( "3-State Slide Vertical" );

   textLabel11 = new VoltageLabel( "textLabel11", "textLabel11", this, "Ens" );
   AddComponent( textLabel11 );
   textLabel11.SetWantsMouseNotifications( false );
   textLabel11.SetPosition( 44, 138 );
   textLabel11.SetSize( 24, 14 );
   textLabel11.SetEditable( false, false );
   textLabel11.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel11.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel11.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel11.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel11.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel11.SetBorderSize( 1 );
   textLabel11.SetMultiLineEdit( false );
   textLabel11.SetIsNumberEditor( false );
   textLabel11.SetNumberEditorRange( 0, 100 );
   textLabel11.SetNumberEditorInterval( 1 );
   textLabel11.SetNumberEditorUsesMouseWheel( false );
   textLabel11.SetHasCustomTextHoverColor( false );
   textLabel11.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel11.SetFont( "Verdana", 12, true, false );

   textLabel14 = new VoltageLabel( "textLabel14", "textLabel14", this, "Chr" );
   AddComponent( textLabel14 );
   textLabel14.SetWantsMouseNotifications( false );
   textLabel14.SetPosition( 44, 153 );
   textLabel14.SetSize( 24, 15 );
   textLabel14.SetEditable( false, false );
   textLabel14.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel14.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel14.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel14.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel14.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel14.SetBorderSize( 1 );
   textLabel14.SetMultiLineEdit( false );
   textLabel14.SetIsNumberEditor( false );
   textLabel14.SetNumberEditorRange( 0, 100 );
   textLabel14.SetNumberEditorInterval( 1 );
   textLabel14.SetNumberEditorUsesMouseWheel( false );
   textLabel14.SetHasCustomTextHoverColor( false );
   textLabel14.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel14.SetFont( "Verdana", 12, true, false );

   textLabel15 = new VoltageLabel( "textLabel15", "textLabel15", this, "Off" );
   AddComponent( textLabel15 );
   textLabel15.SetWantsMouseNotifications( false );
   textLabel15.SetPosition( 44, 169 );
   textLabel15.SetSize( 22, 14 );
   textLabel15.SetEditable( false, false );
   textLabel15.SetJustificationFlags( VoltageLabel.Justification.Left );
   textLabel15.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel15.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel15.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel15.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel15.SetBorderSize( 1 );
   textLabel15.SetMultiLineEdit( false );
   textLabel15.SetIsNumberEditor( false );
   textLabel15.SetNumberEditorRange( 0, 100 );
   textLabel15.SetNumberEditorInterval( 1 );
   textLabel15.SetNumberEditorUsesMouseWheel( false );
   textLabel15.SetHasCustomTextHoverColor( false );
   textLabel15.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel15.SetFont( "Verdana", 12, true, false );

   textLabel16 = new VoltageLabel( "textLabel16", "textLabel16", this, "Mode" );
   AddComponent( textLabel16 );
   textLabel16.SetWantsMouseNotifications( false );
   textLabel16.SetPosition( 12, 116 );
   textLabel16.SetSize( 46, 18 );
   textLabel16.SetEditable( false, false );
   textLabel16.SetJustificationFlags( VoltageLabel.Justification.HorizCentered );
   textLabel16.SetJustificationFlags( VoltageLabel.Justification.VertCentered );
   textLabel16.SetColor( new Color( 236, 255, 253, 255 ) );
   textLabel16.SetBkColor( new Color( 65, 65, 65, 0 ) );
   textLabel16.SetBorderColor( new Color( 0, 0, 0, 0 ) );
   textLabel16.SetBorderSize( 1 );
   textLabel16.SetMultiLineEdit( false );
   textLabel16.SetIsNumberEditor( false );
   textLabel16.SetNumberEditorRange( 0, 100 );
   textLabel16.SetNumberEditorInterval( 1 );
   textLabel16.SetNumberEditorUsesMouseWheel( false );
   textLabel16.SetHasCustomTextHoverColor( false );
   textLabel16.SetTextHoverColor( new Color( 0, 0, 0, 255 ) );
   textLabel16.SetFont( "Verdana", 14, 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

   CheckConnections();
   
   ReadEnsembleSwitch(switchMode.GetValue());   

   RestartLFOs();
   
   delayWritePos = 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
      {
      }
      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 == switchMode)
         {
            ReadEnsembleSwitch (doubleValue);
            RestartLFOs();
         }        
      }
      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 temp;
   int tempIndex;
   double intIndex;
   double fractIndex;
   double valuePosN;
   double valuePosNplus1;
   double out1;
   double out2;
   double out3;
   double dryValue;
   
   // Read audio input to delay line
   
   dryValue = inputAudio.GetValue();
   delayLine[delayWritePos] = dryValue;
   delayWritePos++;
   if (delayWritePos >= DELAY_LINE_LENGTH)
   {
      delayWritePos = 0;
   }
   
   // Check for rising edge on Sync input
   
   // Advance LFOs
   SlowLFO1.AdvanceSample();
   SlowLFO2.AdvanceSample();
   SlowLFO3.AdvanceSample();

   FastLFO1.AdvanceSample();
   FastLFO2.AdvanceSample();
   FastLFO3.AdvanceSample();
   
   if (outputConnected)
   {
      // ----------------------------- Calculate channel 1 output
   
      temp = SlowLFO1.GetSineValue() * SLOW_LFO_DEPTH;
      temp += FastLFO1.GetSineValue() * FAST_LFO_DEPTH;
   
      intIndex = floor(temp);
      fractIndex = temp - intIndex;
   
      tempIndex = (int)(intIndex);
   
      tempIndex += delayWritePos + DELAY_LINE_MIDPOINT;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }   
      valuePosN = delayLine[tempIndex];
   
      tempIndex++;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }
      valuePosNplus1 = delayLine[tempIndex];
   
      // Linear interpolation between successive values
   
      temp = valuePosN * (1.0 - fractIndex) +
             valuePosNplus1 * fractIndex;

      out1 = temp;

      // ----------------------------- Calculate channel 2 output
   
      temp = SlowLFO2.GetSineValue() * SLOW_LFO_DEPTH;
      temp += FastLFO2.GetSineValue() * FAST_LFO_DEPTH;
   
      intIndex = floor(temp);
      fractIndex = temp - intIndex;
   
      tempIndex = (int)(intIndex);
   
      tempIndex += delayWritePos + DELAY_LINE_MIDPOINT;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }   
      valuePosN = delayLine[tempIndex];
   
      tempIndex++;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }
      valuePosNplus1 = delayLine[tempIndex];
   
      // Linear interpolation between successive values
   
      temp = valuePosN * (1.0 - fractIndex) +
             valuePosNplus1 * fractIndex;

      out2 = temp;

      // ----------------------------- Calculate channel 3 output
   
      temp = SlowLFO3.GetSineValue() * SLOW_LFO_DEPTH;
      temp += FastLFO3.GetSineValue() * FAST_LFO_DEPTH;
   
      intIndex = floor(temp);
      fractIndex = temp - intIndex;
   
      tempIndex = (int)(intIndex);
   
      tempIndex += delayWritePos + DELAY_LINE_MIDPOINT;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }   
      valuePosN = delayLine[tempIndex];
   
      tempIndex++;
      if (tempIndex >= DELAY_LINE_LENGTH)
      {
         tempIndex -= DELAY_LINE_LENGTH;
      }
      valuePosNplus1 = delayLine[tempIndex];
   
      // Linear interpolation between successive values
   
      temp = valuePosN * (1.0 - fractIndex) +
             valuePosNplus1 * fractIndex;

      out3 = temp;
   
      // ----------------------------- Calculate Ensemble Left / Mono and Right outputs
   
      if (stereoOutput)
      {
         switch (ensembleSetting)
         {
            case OFF:
            {
               outputEnsemble.SetValue(dryValue);
               outputEnsembleRight.SetValue(dryValue);
            }
            break;
            
            case CHORUS:
            {
               // Left stereo output
               temp = 0.5 * (out1 + dryValue);
               outputEnsemble.SetValue(temp);

               // Right stereo output
               temp = 0.5 * (out3 + dryValue);
               outputEnsembleRight.SetValue(temp);
            }
            break;
            
            case ENSEMBLE:
            {
               // Left stereo output
               temp = 0.333 * (out1 + out1 + out2);
               outputEnsemble.SetValue(temp);

               // Right stereo output
               temp = 0.333 * (out3 + out3 + out2);
               outputEnsembleRight.SetValue(temp);
            }
            break;
            
         }

      }
      else
      {
         switch (ensembleSetting)
         {
            case OFF:
            {
               outputEnsemble.SetValue(dryValue);
            }
            break;
            
            case CHORUS:
            {
               temp = 0.5 * (out1 + dryValue);
               outputEnsemble.SetValue(temp);
            }
            break;
            
            case ENSEMBLE:
            {
               temp = 0.333 * (out1 + out2 + out3);
               outputEnsemble.SetValue(temp);
            }
            break;
            
         }

      }
      
   }  // End of if (outputConnected)

   //[/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();
   
   outputEnsemble.SetValue(temp);
   outputEnsembleRight.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 textLabel16;
private VoltageLabel textLabel15;
private VoltageLabel textLabel14;
private VoltageLabel textLabel11;
private VoltageSwitch switchMode;
private VoltageLabel textLabel10;
private VoltageLabel textLabel23;
private VoltageAudioJack outputEnsembleRight;
private VoltageLabel textLabel13;
private VoltageLabel textLabel12;
private VoltageLabel textLabel9;
private VoltageLabel textLabel7;
private VoltageAudioJack outputEnsemble;
private VoltageAudioJack inputAudio;


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


// Types

private enum EnsembleSetting
{
   OFF,
   CHORUS,
   ENSEMBLE
}



// Constants

private final double DEGREES_000 = 0.0;
private final double DEGREES_360 = 1.0;
private final double DEGREES_120 = DEGREES_360 / 3.0;
private final double DEGREES_240 = DEGREES_120 * 2.0;

private final double SLOW_LFO_RATE = 1.21;
private final double FAST_LFO_RATE = 5.82;

private final double SLOW_LFO_DEPTH = 20.0 / 5.0;
private final double FAST_LFO_DEPTH = 13.0 / 5.0;

private final int DELAY_LINE_LENGTH = 100;
private final int DELAY_LINE_MIDPOINT = DELAY_LINE_LENGTH / 2;

// Variables

private double[] delayLine = new double[DELAY_LINE_LENGTH];
private int delayWritePos;

private double currentLFO1Rate;
private double currentLFO1Depth;
private double currentLFO2Rate;
private double currentLFO2Depth;

private double previousLFO1Rate;
private double previousLFO1Depth;
private double previousLFO2Rate;
private double previousLFO2Depth;

private boolean outputConnected;
private boolean stereoOutput;

private EnsembleSetting ensembleSetting;

// Objects

protected DigitalOscillator SlowLFO1 = new DigitalOscillator();
protected DigitalOscillator SlowLFO2 = new DigitalOscillator();
protected DigitalOscillator SlowLFO3 = new DigitalOscillator();

protected DigitalOscillator FastLFO1 = new DigitalOscillator();
protected DigitalOscillator FastLFO2 = new DigitalOscillator();
protected DigitalOscillator FastLFO3 = new DigitalOscillator();

// Functions

private void ReadEnsembleSwitch (double ensembleValue)
{
   if (ensembleValue < 0.5)
   {
      ensembleSetting = EnsembleSetting.OFF;
   }
   else if (ensembleValue < 1.5)
   {
      ensembleSetting = EnsembleSetting.CHORUS;
   }
   else
   {
      ensembleSetting = EnsembleSetting.ENSEMBLE;
   }
}

private void RestartLFOs()
{
   SlowLFO1.SetFrequency(SLOW_LFO_RATE);
   SlowLFO2.SetFrequency(SLOW_LFO_RATE);
   SlowLFO3.SetFrequency(SLOW_LFO_RATE);

   SlowLFO1.SetCycle(DEGREES_000);
   SlowLFO2.SetCycle(DEGREES_120);
   SlowLFO3.SetCycle(DEGREES_240);

   FastLFO1.SetFrequency(FAST_LFO_RATE);
   FastLFO2.SetFrequency(FAST_LFO_RATE);
   FastLFO3.SetFrequency(FAST_LFO_RATE);
      
   FastLFO1.SetCycle(DEGREES_000);
   FastLFO2.SetCycle(DEGREES_120);
   FastLFO3.SetCycle(DEGREES_240);
   
}

private void CheckConnections()
{
   stereoOutput = outputEnsembleRight.IsConnected();
   
   outputConnected = stereoOutput ||
                     outputEnsemble.IsConnected();
   
}

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

 