AudioData conversion from SampleBuffer to interleaved int?


#1

I’m not seeing a method to directly convert a given number of samples from a multi-channel AudioSampleBuffer to an interleaved integer array (memblock, whatever).

What’s the best way?

Bruce


#2

Have a look at the AudioFormatWriter::WriteHelper class - that does this with an AudioData::Pointer.


#3

So, it looks like it puts all the channels data into one big block, one after another, then converts it?

Can I get a sanity check please? (floatBlock and intBlock are MemoryBlocks I keep to do the conversions, converter is an AudioData::converterInstance setup to do non-interleaved floats to interleaved s16 or s32 ints)

[code] int start1, size1, start2, size2;
prepareToRead (numSamples, start1, size1, start2, size2);

// it *will* be a complete number of samples, one for each channel
numSamples                  = size1 + size2;    // that's what we really got

floatBlock.ensureSize ((size1 + size2) * sizeof(float) * numChannels);

const size_t bytes          = numSamples * (audioSampleDepth / 8) * numChannels;
intBlock.ensureSize (bytes, false);

// Put one frame of audio samples off the buffer and into the decklink buffer
size_t firstCopy            = jmin (size1, numSamples);
size_t secondCopy           = jmin (size2, numSamples - size1);

float* dest                 = floatBlock.getData ();

for (int i = 0; i < numChannels; ++i)
{
    memcpy (dest, buffer.getSampleData(i, start1), firstCopy);
    dest                    += firstCopy;
    
    if (secondCopy)
        memcpy (dest, buffer.getSampleData(i, start2), secondCopy);
    
    dest                    += secondCopy;
}
finishedRead (firstCopy + secondCopy);

converter->convertSamples (floatBlock.getData(), intBlock.getData(), firstCopy + secondCopy);

[/code]

Bruce


#4

No… that doesn’t make any sense to me… You shouldn’t need to copy any memory, and would surely want to call convertSamples for each channel (?)


#5

Well, the AudioData class has template parameters for interleaved and non-interleaved samples. If writing interleaved, doing it per channel would be very odd indeed, IMO.

The real question would be what format the input for the converter should be. In an AudioFormatWriter, it looks like that’s sequential arrays for each channel (which I can’t get out of my FIFO AudioSampleBuffer, since it has a far larger capacity, and may be delivering in two batches).

Bruce