//-----------------------------------------------------------------------
    Real LinearControllerFunction::calculate(Real source) {
        Real input = getAdjustedInput(source*mFrequency);

        std::vector<Real>::iterator ifirst = std::lower_bound(mKeys.begin(), mKeys.end(), input);
        size_t idx = ifirst - mKeys.begin() - 1;

        assert(ifirst != mKeys.end());

        Real alpha = (input - mKeys[idx])/(mKeys[idx + 1] - mKeys[idx]);
        return mValues[idx] + alpha * (mValues[idx + 1] - mValues[idx]);
    }
    //-----------------------------------------------------------------------
    Real WaveformControllerFunction::calculate(Real source)
    {
        Real input = getAdjustedInput(source * mFrequency);
        Real output = 0;
        // For simplicity, factor input down to {0,1)
        // Use looped subtract rather than divide / round
        while (input >= 1.0)
            input -= 1.0;
        while (input < 0.0)
            input += 1.0;

        // Calculate output in -1..1 range
        switch (mWaveType)
        {
        case WFT_SINE:
            output = Math::Sin(Radian(input * Math::TWO_PI));
            break;
        case WFT_TRIANGLE:
            if (input < 0.25)
                output = input * 4;
            else if (input >= 0.25 && input < 0.75)
                output = 1.0f - ((input - 0.25f) * 4.0f);
            else
                output = ((input - 0.75f) * 4.0f) - 1.0f;

            break;
        case WFT_SQUARE:
            if (input <= 0.5f)
                output = 1.0f;
            else
                output = -1.0f;
            break;
        case WFT_SAWTOOTH:
            output = (input * 2.0f) - 1.0f;
            break;
        case WFT_INVERSE_SAWTOOTH:
            output = -((input * 2.0f) - 1.0f);
            break;
		case WFT_PWM:
			if( input <= mDutyCycle )
				output = 1.0f;
			else
				output = -1.0f;
			break;
        }

        // Scale output into 0..1 range and then by base + amplitude
        return mBase + ((output + 1.0f) * 0.5f * mAmplitude);


    }
    //-----------------------------------------------------------------------
    Real ScaleControllerFunction::calculate(Real source)
    {
        return getAdjustedInput(source * mScale);

    }
	//-----------------------------------------------------------------------
	Real PassthroughControllerFunction::calculate(Real source)
	{
		return getAdjustedInput(source);

	}