30 namespace cinder {
namespace audio {
namespace dsp {
36 static std::unique_ptr<Converter>
create(
size_t sourceSampleRate,
size_t destSampleRate,
size_t sourceNumChannels,
size_t destNumChannels,
size_t sourceMaxFramesPerBlock );
42 virtual std::pair<size_t, size_t>
convert(
const Buffer *sourceBuffer,
Buffer *destBuffer ) = 0;
55 Converter(
size_t sourceSampleRate,
size_t destSampleRate,
size_t sourceNumChannels,
size_t destNumChannels,
size_t sourceMaxFramesPerBlock );
71 template <
typename SourceT,
typename DestT>
72 void convert(
const SourceT *sourceArray, DestT *destArray,
size_t length )
74 for(
size_t i = 0; i <
length; i++ )
75 destArray[i] = static_cast<DestT>( sourceArray[i] );
79 template <
typename SourceT,
typename DestT>
85 for(
size_t ch = 0; ch < numChannels; ch++ )
90 template<
typename FloatT>
93 const FloatT floatNormalizer = (FloatT)1 / (FloatT)8388607;
95 for(
size_t i = 0; i <
length; i++ ) {
96 int32_t sample = (int32_t)( ( (int32_t)sourceArray[2] ) << 16 ) | ( ( (int32_t)(uint8_t)sourceArray[1] ) << 8 ) | ( (int32_t)(uint8_t)sourceArray[0] );
97 destArray[i] = sample * floatNormalizer;
103 template<
typename FloatT>
106 const FloatT intNormalizer = 8388607;
108 for(
size_t i = 0; i <
length; i++ ) {
109 int32_t sample = int32_t( sourceArray[i] * intNormalizer );
110 *(destArray++) = (
char)( sample & 255 );
111 *(destArray++) = (
char)( ( sample >> 8 ) & 255 );
112 *(destArray++) = (
char)( ( sample >> 16 ) & 255 );
118 void interleave(
const T *nonInterleavedSourceArray, T *interleavedDestArray,
size_t numFramesPerChannel,
size_t numChannels,
size_t numCopyFrames )
120 for(
size_t ch = 0; ch < numChannels; ch++ ) {
122 const T *sourceChannel = &nonInterleavedSourceArray[ch * numFramesPerChannel];
123 for(
size_t i = 0; i < numCopyFrames; i++ ) {
124 interleavedDestArray[
x] = sourceChannel[i];
131 template<
typename FloatT>
132 void interleave(
const FloatT *nonInterleavedFloatSourceArray, int16_t *interleavedInt16DestArray,
size_t numFramesPerChannel,
size_t numChannels,
size_t numCopyFrames )
134 const FloatT intNormalizer = 32768;
136 for(
size_t ch = 0; ch < numChannels; ch++ ) {
138 const FloatT *sourceChannel = &nonInterleavedFloatSourceArray[ch * numFramesPerChannel];
139 for(
size_t i = 0; i < numCopyFrames; i++ ) {
140 interleavedInt16DestArray[
x] = int16_t( sourceChannel[i] * intNormalizer );
148 void deinterleave(
const T *interleavedSourceArray, T *nonInterleavedDestArray,
size_t numFramesPerChannel,
size_t numChannels,
size_t numCopyFrames )
150 for(
size_t ch = 0; ch < numChannels; ch++ ) {
152 T *destChannel = &nonInterleavedDestArray[ch * numFramesPerChannel];
153 for(
size_t i = 0; i < numCopyFrames; i++ ) {
154 destChannel[i] = interleavedSourceArray[
x];
161 template<
typename FloatT>
162 void deinterleave(
const int16_t *interleavedInt16SourceArray, FloatT *nonInterleavedFloatDestArray,
size_t numFramesPerChannel,
size_t numChannels,
size_t numCopyFrames )
164 const FloatT floatNormalizer = (FloatT)3.0517578125e-05;
166 for(
size_t ch = 0; ch < numChannels; ch++ ) {
168 FloatT *destChannel = &nonInterleavedFloatDestArray[ch * numFramesPerChannel];
169 for(
size_t i = 0; i < numCopyFrames; i++ ) {
170 destChannel[i] = (FloatT)interleavedInt16SourceArray[x] * floatNormalizer;
207 T *mixed = interleavedDest->
getData();
210 for( i = 0, j = 0; i < numFrames; i++, j += 2 ) {
212 mixed[j + 1] = right[i];
226 const T *mixed = interleavedSource->
getData();
229 for( i = 0, j = 0; i < numFrames; i++, j += 2 ) {
231 right[i] = mixed[j + 1];
T * getChannel(size_t ch)
Returns a pointer offset to the first sample of channel ch.
Definition: Buffer.h:97
void sumBuffers(const Buffer *sourceBuffer, Buffer *destBuffer, size_t numFrames)
Sums numFrames frames of sourceBuffer into destBuffer. Channel up or down mixing is applied if necess...
Definition: Converter.cpp:101
size_t mDestNumChannels
Definition: Converter.h:57
size_t getDestNumChannels() const
Definition: Converter.h:50
void deinterleave(const T *interleavedSourceArray, T *nonInterleavedDestArray, size_t numFramesPerChannel, size_t numChannels, size_t numCopyFrames)
De-interleaves numCopyFrames of interleavedSourceArray, placing the result in nonInterleavedDestArray...
Definition: Converter.h:148
A platform-specific converter that supports samplerate and channel conversion.
Definition: Converter.h:33
void interleaveStereoBuffer(const BufferT< T > *nonInterleavedSource, BufferInterleavedT< T > *interleavedDest)
Interleaves nonInterleavedSource, placing the result in interleavedDest. This method is only slightly...
Definition: Converter.h:198
void deinterleaveStereoBuffer(const BufferInterleavedT< T > *interleavedSource, BufferT< T > *nonInterleavedDest)
De-interleaves interleavedSource, placing the result in nonInterleavedDest. This method is only sligh...
Definition: Converter.h:218
size_t getSourceMaxFramesPerBlock() const
Definition: Converter.h:51
size_t getSourceSampleRate() const
Definition: Converter.h:47
virtual std::pair< size_t, size_t > convert(const Buffer *sourceBuffer, Buffer *destBuffer)=0
void mixBuffers(const Buffer *sourceBuffer, Buffer *destBuffer, size_t numFrames)
Mixes numFrames frames of sourceBuffer to destBuffer's layout, replacing its content. Channel up or down mixing is applied if necessary.
Definition: Converter.cpp:62
size_t mSourceMaxFramesPerBlock
Definition: Converter.h:57
#define min(a, b)
Definition: AppImplMsw.cpp:36
Converter(size_t sourceSampleRate, size_t destSampleRate, size_t sourceNumChannels, size_t destNumChannels, size_t sourceMaxFramesPerBlock)
Definition: Converter.cpp:49
virtual void clear()
Clears the state of the converter, discarding / flushing accumulated samples. Optional for implementa...
Definition: Converter.h:45
size_t mSourceSampleRate
Definition: Converter.h:57
size_t getNumChannels() const
Returns the number of channels in the buffer.
Definition: Buffer.h:50
size_t getDestMaxFramesPerBlock() const
Definition: Converter.h:52
size_t getNumFrames() const
Returns the number of frames in the buffer.
Definition: Buffer.h:48
void convertBuffer(const BufferT< SourceT > *sourceBuffer, BufferT< DestT > *destBuffer)
Converts between two BufferT's of different precision (ex. float to double). The number of frames con...
Definition: Converter.h:80
void convertFloatToInt24(const FloatT *sourceArray, char *destArray, size_t length)
Converts the floating point sourceArray to 24-bit int precision, placing the result in destArray...
Definition: Converter.h:104
size_t getSize() const
Returns the total size of the buffer (frames * channels).
Definition: Buffer.h:52
virtual ~Converter()
Definition: Converter.h:38
GLenum GLint x
Definition: GLee.h:987
GLdouble left
Definition: GLee.h:13559
Audio buffer that stores its channels of type T contiguously (ie. the first sample of channel 1 is di...
Definition: Buffer.h:89
GLuint GLsizei GLsizei * length
Definition: GLee.h:2313
void deinterleaveBuffer(const BufferInterleavedT< T > *interleavedSource, BufferT< T > *nonInterleavedDest)
De-interleaves interleavedSource, placing the result in nonInterleavedDest.
Definition: Converter.h:188
GLdouble GLdouble right
Definition: GLee.h:13559
void interleave(const T *nonInterleavedSourceArray, T *interleavedDestArray, size_t numFramesPerChannel, size_t numChannels, size_t numCopyFrames)
Interleaves numCopyFrames of nonInterleavedSourceArray, placing the result in interleavedDestArray. numFramesPerChannel and numChannels describe the layout of the non-interleaved array.
Definition: Converter.h:118
size_t getSourceNumChannels() const
Definition: Converter.h:49
T * getData()
Returns a pointer to the first sample in the data buffer.
Definition: Buffer.h:56
void convertInt24ToFloat(const char *sourceArray, FloatT *destArray, size_t length)
Converts the 24-bit int sourceArray to floating point precision, placing the result in destArray...
Definition: Converter.h:91
size_t mDestSampleRate
Definition: Converter.h:57
size_t getDestSampleRate() const
Definition: Converter.h:48
static std::unique_ptr< Converter > create(size_t sourceSampleRate, size_t destSampleRate, size_t sourceNumChannels, size_t destNumChannels, size_t sourceMaxFramesPerBlock)
If destSampleRate is 0, it is set to match sourceSampleRate. If destNumChannels is 0...
Definition: Converter.cpp:40
size_t mDestMaxFramesPerBlock
Definition: Converter.h:57
size_t mSourceNumChannels
Definition: Converter.h:57
Audio buffer that stores its channels of type T in one interleaved array (ie. the first sample of cha...
Definition: Buffer.h:171
void interleaveBuffer(const BufferT< T > *nonInterleavedSource, BufferInterleavedT< T > *interleavedDest)
Interleaves nonInterleavedSource, placing the result in interleavedDest.
Definition: Converter.h:178
#define CI_ASSERT(expr)
Definition: CinderAssert.h:75
void convert(const SourceT *sourceArray, DestT *destArray, size_t length)
Converts between two arrays of different precision (ex. float to double). length samples are converte...
Definition: Converter.h:72