<commit_before>/* 
 * TTBlue Cycling Ramp Generator
 * Copyright © 2003, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTPhasor.h"

#define thisTTClass TTPhasor
#define thisTTClassName		"phasor"
#define thisTTClassTags		"audio, generator, oscillator"


TT_AUDIO_CONSTRUCTOR
, mPhase(0.0), step(0.0), linearGain(1.0)
{
	addAttributeWithSetter(			Frequency,	kTypeFloat64);
	addAttributeWithGetterAndSetter(Gain,		kTypeFloat64);
	addAttributeWithSetter(			Phase,		kTypeFloat64);
	// TODO: More Attributes left to add...
	//	linearGain
	//	period in ms
	//	period in samples
	
	addUpdate(SampleRate);

	setAttributeValue(TT("frequency"), 1.0);
	setAttributeValue(TT("gain"), 0.0);
	setProcessMethod(processAudio);
}


TTPhasor::~TTPhasor()
{
	;
}


TTErr TTPhasor::updateSampleRate(const TTValue&)
{
	TTValue	v(mFrequency);
	return setFrequency(v);
}


TTErr TTPhasor::setFrequency(const TTValue& newValue)
{
	mFrequency = newValue;
	if (mFrequency == 0) {
		rampSamples = 0xFFFFFFFF;
		rampMilliseconds = 0;
	}
	else {
		rampSamples = TTUInt32((1.0 / fabs(mFrequency)) * sr);
		rampMilliseconds = 1000.0 * (rampSamples / TTFloat64(sr));
	}
	setStep();
	return kTTErrNone;
}

void TTPhasor::setStep()
{
	step = 1.0 / TTFloat64(rampSamples - 1.0);	// 1.0 is the destination 
	if (mFrequency < 0){
		step = -step;
		TTValue	v(mPhase);
		setPhase(v);
	}
	TTZeroDenormal(step); 
}


TTErr TTPhasor::setPhase(const TTValue& newValue)
{
	mPhase = newValue;
	phaseInternal = mPhase;
	if (mFrequency < 0)
		phaseInternal += 1.0;
	
	return kTTErrNone;
}


TTErr TTPhasor::setGain(const TTValue& newValue)
{
	linearGain = dbToLinear(newValue);
	return kTTErrNone;
}

TTErr TTPhasor::getGain(TTValue& value)
{
	value = linearToDb(linearGain);
	return kTTErrNone;
}


// TODO: add flags so that TTAudioObject can call a process method with a different number audio signals?

TTErr TTPhasor::processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
{
	TTAudioSignal&	out = outputs->getSignal(0);
	TTSampleValue	*outSample;
	TTUInt16		numchannels = out.getNumChannelsAsInt();
	TTUInt16		channel;
	TTUInt16		vs;

	for (channel=0; channel<numchannels; channel++) {
		outSample = out.mSampleVectors[channel];
		vs = out.getVectorSizeAsInt();

		while (vs--) {
			*outSample++ = phaseInternal * linearGain;	
			phaseInternal += step;
			if (phaseInternal >= 1.0)
				phaseInternal -= 1.0;
			else if (phaseInternal < 0.0)
				phaseInternal += 1.0;
		}
	}
	return kTTErrNone;
}
<commit_msg>TTPhasor: negative frequencies now create a ramp from 1 to 0 (instead of 0 to 1)<commit_after>/* 
 * TTBlue Cycling Ramp Generator
 * Copyright © 2003, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTPhasor.h"

#define thisTTClass TTPhasor
#define thisTTClassName		"phasor"
#define thisTTClassTags		"audio, generator, oscillator"


TT_AUDIO_CONSTRUCTOR
, mPhase(0.0), step(0.0), linearGain(1.0)
{
	addAttributeWithSetter(			Frequency,	kTypeFloat64);
	addAttributeWithGetterAndSetter(Gain,		kTypeFloat64);
	addAttributeWithSetter(			Phase,		kTypeFloat64);
	// TODO: More Attributes left to add...
	//	linearGain
	//	period in ms
	//	period in samples
	
	addUpdate(SampleRate);

	setAttributeValue(TT("frequency"), 1.0);
	setAttributeValue(TT("gain"), 0.0);
	setProcessMethod(processAudio);
}


TTPhasor::~TTPhasor()
{
	;
}


TTErr TTPhasor::updateSampleRate(const TTValue&)
{
	TTValue	v(mFrequency);
	return setFrequency(v);
}


TTErr TTPhasor::setFrequency(const TTValue& newValue)
{
	mFrequency = newValue;
	if (mFrequency == 0) {
		rampSamples = 0xFFFFFFFF;
		rampMilliseconds = 0;
	}
	else {
		rampSamples = TTUInt32(sr / fabs(mFrequency));
		// FIXME: we never use rampMilliseconds, so why computing this?
		rampMilliseconds = 1000.0 * (rampSamples / TTFloat64(sr));
	}
	setStep();
	return kTTErrNone;
}

void TTPhasor::setStep()
{
	step = 1.0 / TTFloat64(rampSamples - 1.0);	// 1.0 is the destination 
	if (mFrequency < 0){
		step = -step;
	}
	TTZeroDenormal(step); 
}


TTErr TTPhasor::setPhase(const TTValue& newValue)
{
	mPhase = newValue;
	phaseInternal = mPhase;
	if (mFrequency < 0)
		phaseInternal += 1.0;
	
	return kTTErrNone;
}


TTErr TTPhasor::setGain(const TTValue& newValue)
{
	linearGain = dbToLinear(newValue);
	return kTTErrNone;
}

TTErr TTPhasor::getGain(TTValue& value)
{
	value = linearToDb(linearGain);
	return kTTErrNone;
}


// TODO: add flags so that TTAudioObject can call a process method with a different number audio signals?

TTErr TTPhasor::processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
{
	TTAudioSignal&	out = outputs->getSignal(0);
	TTSampleValue	*outSample;
	TTUInt16		numchannels = out.getNumChannelsAsInt();
	TTUInt16		channel;
	TTUInt16		vs;

	for (channel=0; channel<numchannels; channel++) {
		outSample = out.mSampleVectors[channel];
		vs = out.getVectorSizeAsInt();

		while (vs--) {
			*outSample++ = phaseInternal * linearGain;	
			phaseInternal += step;
			if (phaseInternal >= 1.0)
				phaseInternal -= 1.0;
			else if (phaseInternal < 0.0)
				phaseInternal += 1.0;
		}
	}
	return kTTErrNone;
}

{'fraction_non_alphanumeric': 0.12293054234062797, 'fraction_numerical': 0.023977164605137963, 'mean_word_length': 4.497907949790795, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6428627', 'n_tokens_mistral': 2166, 'n_tokens_neox': 1950, 'n_words': 442}