<commit_before>/* 
 * 1st-order up/down-sampling filter built up from a 2-path allpass structure
 * Copyright © 2010, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTHalfband3.h"

#define thisTTClass			TTHalfband3
#define thisTTClassName		"halfband.3"
#define thisTTClassTags		"audio, processor, filter, lowpass, highpass, resampling"

#ifdef TT_PLATFORM_WIN
#include <Algorithm>
#endif

TT_AUDIO_CONSTRUCTOR,
	mF0(NULL),
	mDelay(NULL),
	mR0(NULL)	
{
	TTUInt16	initialMaxNumChannels = arguments;
	TTErr		err;

	addAttributeWithSetter(Mode, kTypeSymbol);		
	addUpdate(MaxNumChannels);

	err = TTObjectInstantiate(TT("allpass.1b"), (TTObjectPtr*)&mF0, initialMaxNumChannels);
	err = TTObjectInstantiate(TT("allpass.1a"), (TTObjectPtr*)&mR0, initialMaxNumChannels);
	err = TTObjectInstantiate(TT("allpass.1a"), (TTObjectPtr*)&mDelay, initialMaxNumChannels);

	setAttributeValue(kTTSym_maxNumChannels,	initialMaxNumChannels);
	setAttributeValue(TT("mode"), TT("lowpass"));
	
	// this coefficient gives w0 (stopband edge) at 0.48 * F_nyquist, with an attentuation of -85dB
	mF0->setAttributeValue(TT("alpha"), 0.334654061320571);
	mR0->setAttributeValue(TT("alpha"), 0.334654061320571);
	
	// this coefficient gives w0 (stopband edge) at 0.45 * F_nyquist, with an attentuation of -60dB
	//mF0->setAttributeValue(TT("alpha"), 0.341748648258737);
	//mR0->setAttributeValue(TT("alpha"), 0.341748648258737);
	
	mDelay->setAttributeValue(TT("alpha"), 0.0);
}


TTHalfband3::~TTHalfband3()
{
	TTObjectRelease((TTObjectPtr*)&mF0);
	TTObjectRelease((TTObjectPtr*)&mR0);
	TTObjectRelease((TTObjectPtr*)&mDelay);
}


TTErr TTHalfband3::updateMaxNumChannels(const TTValue& oldMaxNumChannels)
{
	// TODO: update internal filters

	mX1.resize(maxNumChannels);
	mX1.assign(maxNumChannels, 0.0);

	return kTTErrNone;
}


TTErr TTHalfband3::setMode(const TTValue& newValue)
{
	TTSymbolPtr newMode = newValue;

	if (newMode == mMode)
		return kTTErrNone;
	
	if (newMode == TT("upsample")) {
		mMode = TT("upsample");
		setCalculateMethod(calculateUpsample);
		setProcessMethod(processUpsample);
	}
	else if (newMode == TT("downsample")) { // downsample
		mMode = TT("downsample");
		setCalculateMethod(calculateDownsample);
		setProcessMethod(processDownsample);
	}
	else if (newMode == TT("highpass")) {
		mMode = TT("highpass");
		setCalculateMethod(calculateHighpass);
		setProcessMethod(processHighpass);<commit_msg>TTHalfband3: maxNumChannels is properly updated. fixes #927.<commit_after>/* 
 * 1st-order up/down-sampling filter built up from a 2-path allpass structure
 * Copyright © 2010, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTHalfband3.h"

#define thisTTClass			TTHalfband3
#define thisTTClassName		"halfband.3"
#define thisTTClassTags		"audio, processor, filter, lowpass, highpass, resampling"

#ifdef TT_PLATFORM_WIN
#include <Algorithm>
#endif

TT_AUDIO_CONSTRUCTOR,
	mF0(NULL),
	mDelay(NULL),
	mR0(NULL)	
{
	TTUInt16	initialMaxNumChannels = arguments;
	TTErr		err;

	addAttributeWithSetter(Mode, kTypeSymbol);		
	addUpdate(MaxNumChannels);

	err = TTObjectInstantiate(TT("allpass.1b"), (TTObjectPtr*)&mF0, initialMaxNumChannels);
	err = TTObjectInstantiate(TT("allpass.1a"), (TTObjectPtr*)&mR0, initialMaxNumChannels);
	err = TTObjectInstantiate(TT("allpass.1a"), (TTObjectPtr*)&mDelay, initialMaxNumChannels);

	setAttributeValue(kTTSym_maxNumChannels,	initialMaxNumChannels);
	setAttributeValue(TT("mode"), TT("lowpass"));
	
	// this coefficient gives w0 (stopband edge) at 0.48 * F_nyquist, with an attentuation of -85dB
	mF0->setAttributeValue(TT("alpha"), 0.334654061320571);
	mR0->setAttributeValue(TT("alpha"), 0.334654061320571);
	
	// this coefficient gives w0 (stopband edge) at 0.45 * F_nyquist, with an attentuation of -60dB
	//mF0->setAttributeValue(TT("alpha"), 0.341748648258737);
	//mR0->setAttributeValue(TT("alpha"), 0.341748648258737);
	
	mDelay->setAttributeValue(TT("alpha"), 0.0);
}


TTHalfband3::~TTHalfband3()
{
	TTObjectRelease((TTObjectPtr*)&mF0);
	TTObjectRelease((TTObjectPtr*)&mR0);
	TTObjectRelease((TTObjectPtr*)&mDelay);
}


TTErr TTHalfband3::updateMaxNumChannels(const TTValue& oldMaxNumChannels)
{
	// update internal filters
	mF0->setAttributeValue(kTTSym_maxNumChannels, maxNumChannels);
	mR0->setAttributeValue(kTTSym_maxNumChannels, maxNumChannels);
	
	// update ourselves
	mX1.resize(maxNumChannels);
	mX1.assign(maxNumChannels, 0.0);

	return kTTErrNone;
}


TTErr TTHalfband3::setMode(const TTValue& newValue)
{
	TTSymbolPtr newMode = newValue;

	if (newMode == mMode)
		return kTTErrNone;
	
	if (newMode == TT("upsample")) {
		mMode = TT("upsample");
		setCalculateMethod(calculateUpsample);
		setProcessMethod(processUpsample);
	}
	else if (newMode == TT("downsample")) { // downsample
		mMode = TT("downsample");
		setCalculateMethod(calculateDownsample);
		setProcessMethod(processDownsample);
	}
	else if (newMode == TT("highpass")) {
		mMode = TT("highpass");
		setCalculateMethod(calculateHighpass);
		setProcessMethod(processHighpass);
{'fraction_non_alphanumeric': 0.13365477338476375, 'fraction_numerical': 0.04358727097396336, 'mean_word_length': 6.314527503526093, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1778109', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1860, 'n_words': 348}