All pastes #518470 Raw Edit

arke

public cpp v1 · immutable
#518470 ·published 2007-05-29 10:03 UTC
rendered paste body
#include "Butterworth.h"Butterworth::Butterworth(){	this->is1 = this->is2 = this->os1 = this->os2 = 0.0f;	this->frequency = this->resonance = 0.0f;	this->set(0.5f, 0.2f);}Butterworth::~Butterworth(){}void Butterworth::set(float frequency, float resonance){	if(fabs(frequency - this->frequency) < kNearZero) {		if(fabs(resonance - this->resonance) < kNearZero) {			// do nothing			return;		}	}	this->frequency = frequency;	this->resonance = resonance;	this->recalculateCoefficients();}void Butterworth::recalculateCoefficients(){	float k = 1.0f / tanf(M_PI * this->frequency);	float cc = this->frequency * this->frequency;	this->a0 = 1.0f / (1.0f + this->resonance * this->frequency + cc);	this->a1 = 2.0f * this->a0;	this->a2 = this->a0;	this->b1 = 2.0f * (1.0f - cc) * this->a0;	this->b2 = (1.0f - this->resonance * this->frequency + cc) * this->a0;}void Butterworth::process(float *input, float *output, unsigned int sampleFrames){	float is1, is2, os1, os2, f, g;	is1 = this->is1;	is2 = this->is2;	os1 = this->os1;	os2 = this->os2;	while(sampleFrames--) {		g = *input;		// out(n) = a0 * in + a1 * in(n-1) + a2 * in(n-2) - b1*out(n-1) - b2*out(n-2)		f = a0 * g + a1 * is1 + a2 * is2 - b1 * os1 - b2 * os2;		os2 = os1;		os1 = f;		is2 = is1;		is1 = g;		*output = f;		output++;		input++;	}	this->is1 = is1;	this->is2 = is2;	this->os1 = os1;	this->os2 = os2;}