Well a decision does need to be made one way or the other… since AudioBuffer has some float only code which causes a problem if you use non-float types (int16)
AudioBuffer (const AudioBuffer& other)
: numChannels (other.numChannels),
size (other.size),
allocatedBytes (other.allocatedBytes)
{
if (allocatedBytes == 0)
{
allocateChannels (other.channels, 0);
}
else
{
allocateData();
if (other.isClear)
{
clear();
}
else
{
for (int i = 0; i < numChannels; ++i)
FloatVectorOperations::copy (channels[i], other.channels[i], size); // <<<<--- This will fail if the AudioBuffer is not a float type
}
}
}
Other than this my code looks like it would work for AudioBuffer<int16>
using:
void convertFloatBufferToShort (AudioBuffer<float>& srcBuffer, AudioBuffer<int16>& destBuffer)
{
for (int i = 0; i < srcBuffer.getNumChannels(); ++i)
{
using DstSampleType = AudioData::Pointer<AudioData::Int16, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst>;
using SrcSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::Const>;
DstSampleType dstData (destBuffer.getWritePointer (i), srcBuffer.getNumChannels());
SrcSampleType srcData (srcBuffer.getReadPointer (i));
dstData.convertSamples (srcData, srcBuffer.getNumSamples());
}
}
void convertShortBufferToFloat (AudioBuffer<int16>& srcBuffer, AudioBuffer<float>& destBuffer)
{
for (int i = 0; i < srcBuffer.getNumChannels(); ++i)
{
using DstSampleType = AudioData::Pointer<AudioData::Float32, AudioData::NativeEndian, AudioData::NonInterleaved, AudioData::NonConst>;
using SrcSampleType = AudioData::Pointer<AudioData::Int16, AudioData:: NativeEndian, AudioData::NonInterleaved, AudioData::Const>;
DstSampleType dstData (destBuffer.getWritePointer (i));
SrcSampleType srcData (srcBuffer.getReadPointer (i), srcBuffer.getNumChannels());
dstData.convertSamples (srcData, srcBuffer.getNumSamples());
}
}
BTW… FloatVectorOperations::copy() just calls:
void JUCE_CALLTYPE FloatVectorOperations::copy (double* dest, const double* src, int num) noexcept
{
memcpy (dest, src, (size_t) num * sizeof (double));
}
so there’s no reason not to have a different version using sizeof (DataType)
Replace:
FloatVectorOperations::copy (channels[i], other.channels[i], size);
with:
memcpy (channels[i], other.channels[i], (size_t) size * sizeof (Type));
Never mind… I only needed a very small subset of AudioBuffer, so I created my own ShortAudioBuffer class,
Rail