<commit_before>/* 
 * TTBlue Audio Signal Class
 * Copyright © 2008, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTDSP.h"

#define thisTTClass			TTAudioSignal
#define thisTTClassName		"audiosignal"
#define thisTTClassTags		"audio, processor, dynamics, envelope"


/****************************************************************************************************/

TT_OBJECT_CONSTRUCTOR,
	mIsLocallyOwned(false),
	mMaxNumChannels(0),
	mVectorSize(0),
	mNumChannels(0),
	mBitdepth(0),
	mSampleRate(0),
	mSampleVectors(NULL)
{
	TTUInt16	initialMaxNumChannels = arguments;
	
	addAttributeWithSetter(	VectorSize, kTypeUInt16);
	addAttributeWithSetter(	NumChannels, kTypeUInt16);
	addAttributeWithSetter(	MaxNumChannels, kTypeUInt16);
	addAttribute(			Bitdepth, kTypeUInt8);
	addAttributeProperty(	Bitdepth, readOnly, kTTVal1);
	
	addMessage(clear);
	
	addMessage(alloc);
	addMessageWithArguments(allocWithNewVectorSize);
	addMessageWithArguments(setVector32);
	addMessageWithArguments(getVector32);
	addMessageWithArguments(setVector64);
	addMessageWithArguments(getVector64);
	
	addMessageProperty(alloc,					hidden, YES);
	addMessageProperty(allocWithNewVectorSize,	hidden, YES);
	addMessageProperty(setVector32,				hidden, YES);
	addMessageProperty(getVector32,				hidden, YES);
	addMessageProperty(setVector64,				hidden, YES);
	addMessageProperty(getVector64,				hidden, YES);
	
	setAttributeValue(kTTSym_maxNumChannels, initialMaxNumChannels);
	setAttributeValue(kTTSym_numChannels, initialMaxNumChannels);
}


TTAudioSignal::~TTAudioSignal()
{
	chuck();
}


void TTAudioSignal::chuck()
{
	TTUInt32	i;
	
	if (mIsLocallyOwned) {
		for (i=0; i<mMaxNumChannels; i++) {
			TTFree16(mSampleVectors[i]);
			mSampleVectors[i] = NULL;
		}
		mIsLocallyOwned = false;
	}
	TTFree16(mSampleVectors);
	mMaxNumChannels = 0;
	mSampleVectors = NULL;
}


TTErr TTAudioSignal::setMaxNumChannels(const TTValue& newValue)
{
	TTUInt32	i;
	TTBoolean	wasLocallyOwned = mIsLocallyOwned;
	TTUInt16	newMaxNumChannels = newValue;

	TTLimitMin<TTUInt16>(newMaxNumChannels, 1);
	if (TTUInt16(newMaxNumChannels) != mMaxNumChannels) {
		chuck();
		mMaxNumChannels = newMaxNumChannels;
		if (mMaxNumChannels) {
			mSampleVectors = (TTSampleValue**)TTMalloc16(sizeof(TTSampleValue*) * mMaxNumChannels);
			for (i=0; i<mMaxNumChannels; i++)
				mSampleVectors[i] = NULL;
		}
		if (wasLocallyOwned)
			alloc();
	}
	return kTTErrNone;
}


TTUInt16 TTAudioSignal::getMaxNumChannelsAsInt()
{
	return mMaxNumChannels;
}


TTErr TTAudioSignal::setVector(const TTUInt16 channel, const TTUInt16 newVectorSize, const TTSampleValuePtr newVector)
{
	TTUInt32	i;
	
	// could check against maxnumchannels here
	
	mBitdepth = 64;
	mVectorSize = newVectorSize;
	
	if (mIsLocallyOwned) {
		for (i=0; i<mMaxNumChannels; i++) {
			TTFree16(mSampleVectors[i]);
			mSampleVectors[i] = NULL;
		}
		mIsLocallyOwned = false;
	}
	mSampleVectors[channel] = newVector;
	return kTTErrNone;
}

TTErr TTAudioSignal::setVector64(const TTValue& v, TTValue&)
{
	TTUInt16		channel;
	TTUInt16		newVectorSize;
	TTPtr			newVector;
	
	if (v.size() == 3) {
		v.get(0, channel);
		v.get(1, newVectorSize);
		v.get(2, &newVector);
		return setVector(channel, newVectorSize, (TTSampleValue*)(newVector));
	}
	return kTTErrWrongNumValues;
}

TTErr TTAudioSignal::setVector64Copy(const TTUInt16 channel, const TTUInt16 vectorSize, const TTSampleValuePtr newVector)
{   
	if (mBitdepth != 64 || !mIsLocallyOwned || vectorSize != mVectorSize) {
		mBitdepth = 64;
		mVectorSize = vectorSize;
		alloc();
	}
	memcpy(mSampleVectors[channel], newVector, sizeof(TTSampleValue) * mVectorSize);		
	
	return kTTErrNone;
}

/*
	It sucks if someone sets a 32-bit audio vector, since we have translate it into a 64-bit buffer.
	There may be a better way to do this...
	
	For now, we don't simply reference the data passed in.  Instead we allocate our own buffer and copy the data.
	Unfortunately, this is very slow.
	
	Also note that we are relying on the vector size already being set!
	
	If we passed the vs in to this method, we could avoid having to realloc the memory every single time.<commit_msg>TTAudioSignal: update use of TTValue methods.<commit_after>/* 
 * TTBlue Audio Signal Class
 * Copyright © 2008, <NAME>
 * 
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#include "TTDSP.h"

#define thisTTClass			TTAudioSignal
#define thisTTClassName		"audiosignal"
#define thisTTClassTags		"audio, processor, dynamics, envelope"


/****************************************************************************************************/

TT_OBJECT_CONSTRUCTOR,
	mIsLocallyOwned(false),
	mMaxNumChannels(0),
	mVectorSize(0),
	mNumChannels(0),
	mBitdepth(0),
	mSampleRate(0),
	mSampleVectors(NULL)
{
	TTUInt16	initialMaxNumChannels = arguments;
	
	addAttributeWithSetter(	VectorSize, kTypeUInt16);
	addAttributeWithSetter(	NumChannels, kTypeUInt16);
	addAttributeWithSetter(	MaxNumChannels, kTypeUInt16);
	addAttribute(			Bitdepth, kTypeUInt8);
	addAttributeProperty(	Bitdepth, readOnly, kTTVal1);
	
	addMessage(clear);
	
	addMessage(alloc);
	addMessageWithArguments(allocWithNewVectorSize);
	addMessageWithArguments(setVector32);
	addMessageWithArguments(getVector32);
	addMessageWithArguments(setVector64);
	addMessageWithArguments(getVector64);
	
	addMessageProperty(alloc,					hidden, YES);
	addMessageProperty(allocWithNewVectorSize,	hidden, YES);
	addMessageProperty(setVector32,				hidden, YES);
	addMessageProperty(getVector32,				hidden, YES);
	addMessageProperty(setVector64,				hidden, YES);
	addMessageProperty(getVector64,				hidden, YES);
	
	setAttributeValue(kTTSym_maxNumChannels, initialMaxNumChannels);
	setAttributeValue(kTTSym_numChannels, initialMaxNumChannels);
}


TTAudioSignal::~TTAudioSignal()
{
	chuck();
}


void TTAudioSignal::chuck()
{
	TTUInt32	i;
	
	if (mIsLocallyOwned) {
		for (i=0; i<mMaxNumChannels; i++) {
			TTFree16(mSampleVectors[i]);
			mSampleVectors[i] = NULL;
		}
		mIsLocallyOwned = false;
	}
	TTFree16(mSampleVectors);
	mMaxNumChannels = 0;
	mSampleVectors = NULL;
}


TTErr TTAudioSignal::setMaxNumChannels(const TTValue& newValue)
{
	TTUInt32	i;
	TTBoolean	wasLocallyOwned = mIsLocallyOwned;
	TTUInt16	newMaxNumChannels = newValue;

	TTLimitMin<TTUInt16>(newMaxNumChannels, 1);
	if (TTUInt16(newMaxNumChannels) != mMaxNumChannels) {
		chuck();
		mMaxNumChannels = newMaxNumChannels;
		if (mMaxNumChannels) {
			mSampleVectors = (TTSampleValue**)TTMalloc16(sizeof(TTSampleValue*) * mMaxNumChannels);
			for (i=0; i<mMaxNumChannels; i++)
				mSampleVectors[i] = NULL;
		}
		if (wasLocallyOwned)
			alloc();
	}
	return kTTErrNone;
}


TTUInt16 TTAudioSignal::getMaxNumChannelsAsInt()
{
	return mMaxNumChannels;
}


TTErr TTAudioSignal::setVector(const TTUInt16 channel, const TTUInt16 newVectorSize, const TTSampleValuePtr newVector)
{
	TTUInt32	i;
	
	// could check against maxnumchannels here
	
	mBitdepth = 64;
	mVectorSize = newVectorSize;
	
	if (mIsLocallyOwned) {
		for (i=0; i<mMaxNumChannels; i++) {
			TTFree16(mSampleVectors[i]);
			mSampleVectors[i] = NULL;
		}
		mIsLocallyOwned = false;
	}
	mSampleVectors[channel] = newVector;
	return kTTErrNone;
}

TTErr TTAudioSignal::setVector64(const TTValue& v, TTValue&)
{
	TTUInt16		channel;
	TTUInt16		newVectorSize;
	TTPtr			newVector;
	
	if (v.size() == 3) {
		channel = v[0];
		newVectorSize = v[1];
		newVector = v[2];
//		v.get(0, channel);
//		v.get(1, newVectorSize);
//		v.get(2, &newVector);
		return setVector(channel, newVectorSize, (TTSampleValue*)(newVector));
	}
	return kTTErrWrongNumValues;
}

TTErr TTAudioSignal::setVector64Copy(const TTUInt16 channel, const TTUInt16 vectorSize, const TTSampleValuePtr newVector)
{   
	if (mBitdepth != 64 || !mIsLocallyOwned || vectorSize != mVectorSize) {
		mBitdepth = 64;
		mVectorSize = vectorSize;
		alloc();
	}
	memcpy(mSampleVectors[channel], newVector, sizeof(TTSampleValue) * mVectorSize);		
	
	return kTTErrNone;
}

/*
	It sucks if someone sets a 32-bit audio vector, since we have translate it into a 64-bit buffer.
	There may be a better way to do this...
	
	For now, we don't simply reference the data passed in.  Instead we allocate our own buffer and copy the data.
	Unfortunately, this is very slow.
	
	Also note that we are relying on the vector size already being set!
	
	If we passed the vs in to this method, we could avoid having to realloc the memory every single time.
{'fraction_non_alphanumeric': 0.12371134020618557, 'fraction_numerical': 0.02190721649484536, 'mean_word_length': 5.556835637480798, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, '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': '7231337', 'n_tokens_mistral': 3318, 'n_tokens_neox': 2987, 'n_words': 668}