<commit_before>/* 
 * TTBlue Signal Crossfader Object
 * Copyright © 2008, <NAME>
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "TTCrossfade.h"


static bool zeroed = false;
static TTSampleValue zeroVector1[2048]; //TODO: make this dynamically sized
static TTSampleValue zeroVector2[2048]; //TODO: make this dynamically sized
static TTSampleValue zeroVector3[2048]; //TODO: make this dynamically sized


TTCrossfade::TTCrossfade(TTUInt8 newMaxNumChannels)
	: TTAudioObject("audio.crossfade", newMaxNumChannels), attrShape(kTTSymEmpty), attrMode(kTTSymEmpty)
{
	registerAttribute(TT("position"),	kTypeFloat64,	&attrPosition);
	registerAttribute(TT("shape"),		kTypeSymbol,	&attrShape,		(TTGetterMethod)NULL, (TTSetterMethod)&TTCrossfade::setShape);
	registerAttribute(TT("mode"),		kTypeSymbol,	&attrMode,		(TTGetterMethod)NULL, (TTSetterMethod)&TTCrossfade::setMode);

	if(!zeroed){
		memset(zeroVector1, 0, sizeof(TTSampleValue) * 2048);
		memset(zeroVector2, 0, sizeof(TTSampleValue) * 2048);
		memset(zeroVector3, 0, sizeof(TTSampleValue) * 2048);
	}
	
	// Set Defaults (the attribute setters will set the process method for us)...
	setAttributeValue(TT("position"),	0.5);
	setAttributeValue(TT("shape"),		TT("equalPower"));
	setAttributeValue(TT("mode"),		TT("lookup"));
}


TTCrossfade::~TTCrossfade()
{
	;
}


TTErr TTCrossfade::setShape(const TTValue& newValue)
{
	attrShape = newValue;
	return setProcessPointers();
}


TTErr TTCrossfade::setMode(const TTValue& newValue)
{
	attrMode = newValue;
	return setProcessPointers();
}


TTErr TTCrossfade::setProcessPointers()
{
	TTErr	err =kTTErrNone;
	
	if(attrShape == TT("equalPower") && attrMode == TT("lookup")){
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processLookupUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processLookup);
	}
	else if(attrShape == TT("equalPower") && attrMode == TT("calculate")){
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processCalcUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processCalc);
	}
	else{
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processLinearUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processLinear);
	}
	return err;
}


#if 0
#pragma mark -
#pragma mark Interleaved Process Methods
#endif

TTErr TTCrossfade::processLinear(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;
	short			numchannels = out.getNumChannels();
	short			channel;
	
	if(in.getNumChannels() != out.getNumChannels()*2)
		return kTTErrGeneric;

	for(channel=0; channel<numchannels; channel++){
		inSampleA = in.sampleVectors[channel];
		inSampleB = in.sampleVectors[numchannels+channel];
		outSample = out.sampleVectors[channel];
		vs = in.getVectorSize();
		
		while(vs--)
			*outSample++ = (*inSampleB++ * attrPosition) + (*inSampleA++ * (1.0 - attrPosition));
	}
	return kTTErrNone;
}


TTErr TTCrossfade::processLookup(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;
	short			numchannels = out.getNumChannels();
	short			channel;
	int				index;
	
	if(in.getNumChannels() != out.getNumChannels()*2)
		return kTTErrGeneric;

	for(channel=0; channel<numchannels; channel++){
		inSampleA = in.sampleVectors[channel];
		inSampleB = in.sampleVectors[numchannels+channel];
		outSample = out.sampleVectors[channel];
		vs = in.getVectorSize();
		
		while(vs--){
			index = (int)(attrPosition * 511.0);
			*outSample++ = (*inSampleB++ * kTTLookupEqualPower[511 - index]) + (*inSampleA++ * kTTLookupEqualPower[index]);
		}
	}
	return kTTErrNone;
}


TTErr TTCrossfade::processCalc(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;<commit_msg>adding a note from nils<commit_after>/* 
 * TTBlue Signal Crossfader Object
 * Copyright © 2008, <NAME>
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "TTCrossfade.h"


static bool zeroed = false;
static TTSampleValue zeroVector1[2048]; //TODO: make this dynamically sized
static TTSampleValue zeroVector2[2048]; //TODO: make this dynamically sized
static TTSampleValue zeroVector3[2048]; //TODO: make this dynamically sized


TTCrossfade::TTCrossfade(TTUInt8 newMaxNumChannels)
	: TTAudioObject("audio.crossfade", newMaxNumChannels), attrShape(kTTSymEmpty), attrMode(kTTSymEmpty)
{
	registerAttribute(TT("position"),	kTypeFloat64,	&attrPosition);
	registerAttribute(TT("shape"),		kTypeSymbol,	&attrShape,		(TTGetterMethod)NULL, (TTSetterMethod)&TTCrossfade::setShape);
	registerAttribute(TT("mode"),		kTypeSymbol,	&attrMode,		(TTGetterMethod)NULL, (TTSetterMethod)&TTCrossfade::setMode);

	if(!zeroed){
		memset(zeroVector1, 0, sizeof(TTSampleValue) * 2048);
		memset(zeroVector2, 0, sizeof(TTSampleValue) * 2048);
		memset(zeroVector3, 0, sizeof(TTSampleValue) * 2048);
	}
	
	// Set Defaults (the attribute setters will set the process method for us)...
	setAttributeValue(TT("position"),	0.5);
	setAttributeValue(TT("shape"),		TT("equalPower"));
	setAttributeValue(TT("mode"),		TT("lookup"));
}


TTCrossfade::~TTCrossfade()
{
	;
}


TTErr TTCrossfade::setShape(const TTValue& newValue)
{
	attrShape = newValue;
	return setProcessPointers();
}


TTErr TTCrossfade::setMode(const TTValue& newValue)
{
	attrMode = newValue;
	return setProcessPointers();
}


TTErr TTCrossfade::setProcessPointers()
{
	TTErr	err =kTTErrNone;
	
	if(attrShape == TT("equalPower") && attrMode == TT("lookup")){
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processLookupUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processLookup);
	}
	else if(attrShape == TT("equalPower") && attrMode == TT("calculate")){
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processCalcUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processCalc);
	}
	else{
		err |= setProcessWithSidechain((TTProcessWithSidechainMethod)&TTCrossfade::processLinearUsingSidechain);
		err |= setProcess((TTProcessMethod)&TTCrossfade::processLinear);
	}
	return err;
}


#if 0
#pragma mark -
#pragma mark Interleaved Process Methods
#endif

TTErr TTCrossfade::processLinear(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;
	short			numchannels = out.getNumChannels();
	short			channel;
	
	if(in.getNumChannels() != out.getNumChannels()*2)
		return kTTErrGeneric;

	for(channel=0; channel<numchannels; channel++){
		inSampleA = in.sampleVectors[channel];
		inSampleB = in.sampleVectors[numchannels+channel];
		outSample = out.sampleVectors[channel];
		vs = in.getVectorSize();
		
		while(vs--)
			*outSample++ = (*inSampleB++ * attrPosition) + (*inSampleA++ * (1.0 - attrPosition));
	}
	return kTTErrNone;
}


TTErr TTCrossfade::processLookup(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;
	short			numchannels = out.getNumChannels();
	short			channel;
	int				index;
	
	if(in.getNumChannels() != out.getNumChannels()*2)
		return kTTErrGeneric;

	for(channel=0; channel<numchannels; channel++){
		inSampleA = in.sampleVectors[channel];
		inSampleB = in.sampleVectors[numchannels+channel];
		outSample = out.sampleVectors[channel];
		vs = in.getVectorSize();
		
		while(vs--){
			index = (int)(attrPosition * 511.0);
			*outSample++ = (*inSampleB++ * kTTLookupEqualPower[511 - index]) + (*inSampleA++ * kTTLookupEqualPower[index]);
		}
	}
	return kTTErrNone;
}


// TODO: Nils says that we should experiment here with (sin*sin) here so that we can sum to 1.0 in the center???
// It's worth experimenting with it....

TTErr TTCrossfade::processCalc(TTAudioSignal& in, TTAudioSignal& out)
{
	short			vs;
	TTSampleValue	*inSampleA,
					*inSampleB,
					*outSample;
{'fraction_non_alphanumeric': 0.13596926127912742, 'fraction_numerical': 0.01412989588497769, 'mean_word_length': 5.855564995751911, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974724', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2756, 'n_words': 537}