airwindows/plugins/MacSignedAU/PurestConsole4Channel/PurestConsole4Channel.cpp
Christopher Johnson 6c583d8a64 kCyberCity
2026-05-16 20:25:14 -04:00

301 lines
14 KiB
C++
Executable file

/*
* File: PurestConsole4Channel.cpp
*
* Version: 1.0
*
* Created: 5/7/26
*
* Copyright: Copyright © 2026 Airwindows, Airwindows uses the MIT license
*
* Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
* consideration of your agreement to the following terms, and your use, installation, modification
* or redistribution of this Apple software constitutes acceptance of these terms. If you do
* not agree with these terms, please do not use, install, modify or redistribute this Apple
* software.
*
* In consideration of your agreement to abide by the following terms, and subject to these terms,
* Apple grants you a personal, non-exclusive license, under Apple's copyrights in this
* original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the
* Apple Software, with or without modifications, in source and/or binary forms; provided that if you
* redistribute the Apple Software in its entirety and without modifications, you must retain this
* notice and the following text and disclaimers in all such redistributions of the Apple Software.
* Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to
* endorse or promote products derived from the Apple Software without specific prior written
* permission from Apple. Except as expressly stated in this notice, no other rights or
* licenses, express or implied, are granted by Apple herein, including but not limited to any
* patent rights that may be infringed by your derivative works or by other works in which the
* Apple Software may be incorporated.
*
* The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR
* IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE
* OR IN COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
* REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER
* UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN
* IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*=============================================================================
PurestConsole4Channel.cpp
=============================================================================*/
#include "PurestConsole4Channel.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AUDIOCOMPONENT_ENTRY(AUBaseFactory, PurestConsole4Channel)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::PurestConsole4Channel
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PurestConsole4Channel::PurestConsole4Channel(AudioUnit component)
: AUEffectBase(component)
{
CreateElements();
Globals()->UseIndexedParameters(kNumberOfParameters);
SetParameter(kParam_SMO, kDefaultValue_ParamSMO );
SetParameter(kParam_PAN, kDefaultValue_ParamPAN );
SetParameter(kParam_FAD, kDefaultValue_ParamFAD );
#if AU_DEBUG_DISPATCHER
mDebugDispatcher = new AUDebugDispatcher (this);
#endif
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::GetParameterValueStrings
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::GetParameterValueStrings(AudioUnitScope inScope,
AudioUnitParameterID inParameterID,
CFArrayRef * outStrings)
{
return kAudioUnitErr_InvalidProperty;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::GetParameterInfo
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::GetParameterInfo(AudioUnitScope inScope,
AudioUnitParameterID inParameterID,
AudioUnitParameterInfo &outParameterInfo )
{
ComponentResult result = noErr;
outParameterInfo.flags = kAudioUnitParameterFlag_IsWritable
| kAudioUnitParameterFlag_IsReadable;
if (inScope == kAudioUnitScope_Global) {
switch(inParameterID)
{
case kParam_SMO:
AUBase::FillInParameterName (outParameterInfo, kParameterSMOName, false);
outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
outParameterInfo.minValue = 0.0;
outParameterInfo.maxValue = 1.0;
outParameterInfo.defaultValue = kDefaultValue_ParamSMO;
break;
case kParam_PAN:
AUBase::FillInParameterName (outParameterInfo, kParameterPANName, false);
outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
outParameterInfo.minValue = 0.0;
outParameterInfo.maxValue = 1.0;
outParameterInfo.defaultValue = kDefaultValue_ParamPAN;
break;
case kParam_FAD:
AUBase::FillInParameterName (outParameterInfo, kParameterFADName, false);
outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
outParameterInfo.minValue = 0.0;
outParameterInfo.maxValue = 1.0;
outParameterInfo.defaultValue = kDefaultValue_ParamFAD;
break;
default:
result = kAudioUnitErr_InvalidParameter;
break;
}
} else {
result = kAudioUnitErr_InvalidParameter;
}
return result;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::GetPropertyInfo
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::GetPropertyInfo (AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement,
UInt32 & outDataSize,
Boolean & outWritable)
{
return AUEffectBase::GetPropertyInfo (inID, inScope, inElement, outDataSize, outWritable);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// state that plugin supports only stereo-in/stereo-out processing
UInt32 PurestConsole4Channel::SupportedNumChannels(const AUChannelInfo ** outInfo)
{
if (outInfo != NULL)
{
static AUChannelInfo info;
info.inChannels = 2;
info.outChannels = 2;
*outInfo = &info;
}
return 1;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::GetProperty
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::GetProperty( AudioUnitPropertyID inID,
AudioUnitScope inScope,
AudioUnitElement inElement,
void * outData )
{
return AUEffectBase::GetProperty (inID, inScope, inElement, outData);
}
// PurestConsole4Channel::Initialize
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::Initialize()
{
ComponentResult result = AUEffectBase::Initialize();
if (result == noErr)
Reset(kAudioUnitScope_Global, 0);
return result;
}
#pragma mark ____PurestConsole4ChannelEffectKernel
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::PurestConsole4ChannelKernel::Reset()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ComponentResult PurestConsole4Channel::Reset(AudioUnitScope inScope, AudioUnitElement inElement)
{
for (int x = 0; x < bezier_total; x++) bezier[x] = 0.0;
fpdL = 1.0; while (fpdL < 16386) fpdL = rand()*UINT32_MAX;
fpdR = 1.0; while (fpdR < 16386) fpdR = rand()*UINT32_MAX;
return noErr;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// PurestConsole4Channel::ProcessBufferLists
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OSStatus PurestConsole4Channel::ProcessBufferLists(AudioUnitRenderActionFlags & ioActionFlags,
const AudioBufferList & inBuffer,
AudioBufferList & outBuffer,
UInt32 inFramesToProcess)
{
Float32 * inputL = (Float32*)(inBuffer.mBuffers[0].mData);
Float32 * inputR = (Float32*)(inBuffer.mBuffers[1].mData);
Float32 * outputL = (Float32*)(outBuffer.mBuffers[0].mData);
Float32 * outputR = (Float32*)(outBuffer.mBuffers[1].mData);
UInt32 nSampleFrames = inFramesToProcess;
double overallscale = 1.0;
overallscale /= 44100.0;
overallscale *= GetSampleRate();
double bezierRez = fmax(pow((1.0-GetParameter( kParam_SMO ))*0.25,3.0)/overallscale,0.00001);
int stepped = 999999; if (bezierRez > 0.000001) stepped = (int)(1.0/bezierRez); bezierRez = 1.0/stepped;
double bezierTrim = 1.0-(bezierRez*((double)stepped/(stepped+1.0)));
//manages the overall Bezier control smoothing system plugin-wide and feed all controls
//into bezier[] as just 0-1 values, unprocessed. do it IN the control smoothing engine
while (nSampleFrames-- > 0) {
double inputSampleL = *inputL;
double inputSampleR = *inputR;
if (fabs(inputSampleL)<1.18e-23) inputSampleL = fpdL * 1.18e-17;
if (fabs(inputSampleR)<1.18e-23) inputSampleR = fpdR * 1.18e-17;
//begin Bezier control smoothing engine
bezier[bezier_cycle] += bezierRez;
if (bezier[bezier_cycle] > 1.0) {bezier[bezier_cycle] = 0.0;
bezier[bezierGainL_C] = bezier[bezierGainL_B]; bezier[bezierGainL_B] = bezier[bezierGainL_A];
bezier[bezierGainR_C] = bezier[bezierGainR_B]; bezier[bezierGainR_B] = bezier[bezierGainR_A];
//one of these bucket brigade lines for every smoothed control
//begin expensive control calculations
double gain = GetParameter( kParam_FAD )*2.0;
if (gain > 1.0) gain *= gain;
if (gain < 1.0) gain = 1.0-pow(1.0-gain,2);
gain *= 0.763932022500211;
bezier[bezierGainL_A] = gain*sin(M_PI_2-(GetParameter( kParam_PAN )*M_PI_2));
bezier[bezierGainR_A] = gain*sin(GetParameter( kParam_PAN )*M_PI_2);
//end expensive control calculations
} double lerp = bezier[bezier_cycle]*bezierTrim;
bezier[bezierGainL_Out] = bezier[bezierGainL_B]+(bezier[bezierGainL_C]*(1.0-lerp)*(1.0-lerp))+(bezier[bezierGainL_B]*2.0*(1.0-lerp)*lerp)+(bezier[bezierGainL_A]*lerp*lerp);
bezier[bezierGainR_Out] = bezier[bezierGainR_B]+(bezier[bezierGainR_C]*(1.0-lerp)*(1.0-lerp))+(bezier[bezierGainR_B]*2.0*(1.0-lerp)*lerp)+(bezier[bezierGainR_A]*lerp*lerp);
//end Bezier control smoothing engine
inputSampleL *= bezier[bezierGainL_Out];
inputSampleR *= bezier[bezierGainR_Out];
inputSampleL = fmax(fmin(inputSampleL,M_PI_2),-M_PI_2);
inputSampleR = fmax(fmin(inputSampleR,M_PI_2),-M_PI_2);
long double X = inputSampleL; X *= X; //long double for even
long double temp = inputSampleL * X; //the initial multiplies
inputSampleL -= temp*0.16666666666666666666666666666666666; temp *= X;
inputSampleL += temp*0.00833333333333333333333333333333333; temp *= X;
inputSampleL -= temp*0.00019841269841269841269841269841269; temp *= X;
inputSampleL += temp*0.00000275573192239858906525573192239; temp *= X;
inputSampleL -= temp*0.00000002505210838544171877521083854; temp *= X;
inputSampleL += temp*0.00000000016059043836821614599392377; temp *= X;
inputSampleL -= temp*0.00000000000076471637318198164759011; temp *= X;
inputSampleL += temp*0.00000000000000281145725434552076319; temp *= X;
inputSampleL -= temp*0.00000000000000000822063524662432971; temp *= X;
inputSampleL += temp*0.00000000000000000001957294106339126;
X = inputSampleR; X *= X; //long double for even
temp = inputSampleR * X; //the initial multiplies
inputSampleR -= temp*0.16666666666666666666666666666666666; temp *= X;
inputSampleR += temp*0.00833333333333333333333333333333333; temp *= X;
inputSampleR -= temp*0.00019841269841269841269841269841269; temp *= X;
inputSampleR += temp*0.00000275573192239858906525573192239; temp *= X;
inputSampleR -= temp*0.00000002505210838544171877521083854; temp *= X;
inputSampleR += temp*0.00000000016059043836821614599392377; temp *= X;
inputSampleR -= temp*0.00000000000076471637318198164759011; temp *= X;
inputSampleR += temp*0.00000000000000281145725434552076319; temp *= X;
inputSampleR -= temp*0.00000000000000000822063524662432971; temp *= X;
inputSampleR += temp*0.00000000000000000001957294106339126;
//retain mantissa of a long double increasing power function
//long double probably doesn't handle more than 36 digits or so
//begin 32 bit stereo floating point dither
int expon; frexpf((float)inputSampleL, &expon);
fpdL ^= fpdL << 13; fpdL ^= fpdL >> 17; fpdL ^= fpdL << 5;
inputSampleL += ((double(fpdL)-uint32_t(0x7fffffff)) * 3.553e-44l * pow(2,expon+62));
frexpf((float)inputSampleR, &expon);
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5;
if (fpdL-fpdR < 1073741824 || fpdR-fpdL < 1073741824) {
fpdR ^= fpdR << 13; fpdR ^= fpdR >> 17; fpdR ^= fpdR << 5;}
inputSampleR += ((double(fpdR)-uint32_t(0x7fffffff)) * 3.553e-44l * pow(2,expon+62));
//end 32 bit stereo floating point dither
*outputL = inputSampleL;
*outputR = inputSampleR;
//direct stereo out
inputL += 1;
inputR += 1;
outputL += 1;
outputR += 1;
}
return noErr;
}