/*
  ==============================================================================

    This file was auto-generated by the Introjucer!

    It contains the basic framework code for a JUCE plugin processor.

  ==============================================================================
*/

#include "PluginProcessor.h"
#include "PluginEditor.h"

//==============================================================================
slCrossfeedAudioProcessor::slCrossfeedAudioProcessor()
{
    addPluginParameter (new slParameter (PARAM_ENABLE,       "Enable",       "",     0.0f,      1.0f, 1.0f,    1.0f, 1.0f));
}

slCrossfeedAudioProcessor::~slCrossfeedAudioProcessor()
{
}

//==============================================================================
void slCrossfeedAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    crossfeed_init (&crossfeed, sampleRate);
    
    scratch.setSize (2, samplesPerBlock);
    
    enableVal.reset (sampleRate, 0.05);
    disableVal.reset (sampleRate, 0.05);
}

void slCrossfeedAudioProcessor::releaseResources()
{
}

void slCrossfeedAudioProcessor::processBlock (AudioSampleBuffer& buffer, MidiBuffer&)
{
    if (slParameter* s = getParameter (PARAM_ENABLE))
    {
        enableVal.setValue (s->getUserValue() > 0.5f ? 1.0f : 0.0f);
        disableVal.setValue (s->getUserValue() > 0.5f ? 0.0f : 1.0f);
    }
    
    scratch.makeCopyOf (buffer, true);
    
    crossfeed_filter_inplace_noninterleaved (&crossfeed, scratch.getWritePointer (0),
                                             scratch.getWritePointer (1), scratch.getNumSamples());
    
    applyGain (buffer, disableVal);
    applyGain (scratch, enableVal);
    
    buffer.addFrom (0, 0, scratch, 0, 0, buffer.getNumSamples());
    buffer.addFrom (1, 0, scratch, 1, 0, buffer.getNumSamples());
    
    outputLevel.trackBuffer (buffer);
}

//==============================================================================
bool slCrossfeedAudioProcessor::hasEditor() const
{
    return true;
}

AudioProcessorEditor* slCrossfeedAudioProcessor::createEditor()
{
    return new slCrossfeedAudioProcessorEditor (*this);
}

//==============================================================================
// This creates new instances of the plugin..
AudioProcessor* JUCE_CALLTYPE createPluginFilter()
{
    return new slCrossfeedAudioProcessor();
}