Inside Macintosh: QuickTime Reference

| Previous | Chapter Contents | Chapter Top | Next |

Working with SoundConverterFillBuffer

In general, working with the SoundConverterFillBuffer function is much like working with SoundConverterConvertBuffer. You still call the SoundConverterOpen, SoundConverterClose, SoundConverterBeginConversion and SoundConverterEndConversion routines just as you always have and you still typically perform the conversion in a loop.

The differences begin with how the buffering is done. SoundConverterFillBuffer will do as much or as little work as is required to satisfy a given request. This means that you can pass in buffers of any size you like and expect that the Sound Converter will never overflow the output buffer.

Moreover, the SoundConverterFillBufferDataProc function will be called as many times as necessary to fulfill a request. This means that the SoundConverterFillBufferDataProc routine is free to provide data in whatever chunk size it likes. Of course with both sides, the buffer sizes will control how many times you need to request data and there is a certain amount of overhead for each call. You will want to balance this against the performance you require. While a call to SoundConverterGetBufferSizes is not required by the SoundConverterFillBuffer function, it is useful as a guide for non-VBR formats.

The following code snippet illustrates a typical use of SoundConverterFillBuffer.

Listing 7  A typical usage of the SoundConverterFillBuffer routine

{
SoundComponentData inputFormat, outputFormat;
SoundConverter sc;
Boolean hasLeftOverData = false;
unsigned long totalBytesWritten = 0;
unsigned long totalFramesWritten = 0;
unsigned long bytesWritten = 0;
unsigned long framesWritten = 0;
unsigned long outputFlags = 0;
unsigned long outputBufferByteSize = 0;
void* outputBuffer = NULL;

GetFormats(&inputFormat, &outputFormat);

SoundConverterOpen(&inputFormat, outputFormat, &sc);

SoundConverterBeginConversion(sc);


while(HasMoreSourceData() || hasLeftOverData)
{
bytesWritten = 0;
framesWritten = 0;
outputFlags = 0;
outputBufferByteSize = 0;
outputBuffer = GetNextOutputBuffer(&outputBufferByteSize);

SoundConverterFillBuffer(sc, MySoundConverterFillBufferDataUPP, NULL, outputBuffer,
   outputBufferByteSize, &bytesWritten, &framesWritten, &outputFlags);

//    advance whatever pointers we need to
totalBytesWritten += bytesWritten;
totalFramesWritten += framesWritten;

//    figure out if we have any data still stuck in the chain
hasLeftOverData = outputFlags & kSoundConverterHasLeftOverData;
}

//    get the left overs
outputBuffer = GetNextOutputBuffer(&outputBufferByteSize);    
SoundConverterEndConversion(sc, outputBuffer, &bytesWritten, &framesWritten);

//    advance whatever pointers we need to
totalBytesWritten += bytesWritten;
totalFramesWritten += framesWritten;

SoundConverterClose(sc);
}

The SoundConverterFillBufferDataProc could look something like this:

pascal Boolean SoundConverterFillBufferDataProc( SoundComponentDataPtr* data,
void* refCon)
{
*data = GetNextSourceBuffer(refCon);

return *data != NULL;
}


© 2000 Apple Computer, Inc.

Inside Macintosh: QuickTime Reference

| Previous | Chapter Contents | Chapter Top | Next |