moai/src/moaiext-audiosampler/MOAIAudioSampler.cpp
// Determine the size, in bytes, of a buffer necessary to represent the supplied number
// of seconds of audio data.
Determine the size, in bytes, of a buffer necessary to represent the supplied number
of seconds of audio data.
Determine the size in bytes of a buffer necessary to represent the supplied number of seconds of audio data .
static int MyComputeRecordBufferSize(const AudioStreamBasicDescription *format, AudioQueueRef queue, float seconds)
{
	int packets, frames, bytes;
	frames = (int)ceil(seconds * format->mSampleRate);
	if (format->mBytesPerFrame > 0)
		bytes = frames * format->mBytesPerFrame;
	else {
		UInt32 maxPacketSize;
		if (format->mBytesPerPacket > 0)
			maxPacketSize = format->mBytesPerPacket;	
		else {
			UInt32 propertySize = sizeof(maxPacketSize); 
			OSStatus result = AudioQueueGetProperty(queue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize,
                                                    &propertySize );
            if(result){
                return -1; 
            }
		}
		if (format->mFramesPerPacket > 0)
			packets = frames / format->mFramesPerPacket;
		else
			packets = frames;	
		if (packets == 0)		
			packets = 1;
		bytes = packets * maxPacketSize;
	}
	return bytes;
}
static int MyComputeRecordBufferSize ( const AudioStreamBasicDescription * format , AudioQueueRef queue , float seconds ) { int packets , frames , bytes ; frames = ( int ) ceil ( seconds * format -> mSampleRate ) ; if ( format -> mBytesPerFrame > 0 ) bytes = frames * format -> mBytesPerFrame ; else { UInt32 maxPacketSize ; if ( format -> mBytesPerPacket > 0 ) maxPacketSize = format -> mBytesPerPacket ; else { UInt32 propertySize = sizeof ( maxPacketSize ) ; OSStatus result = AudioQueueGetProperty ( queue , kAudioConverterPropertyMaximumOutputPacketSize , & maxPacketSize , & propertySize ) ; if ( result ) { return -1 ; } } if ( format -> mFramesPerPacket > 0 ) packets = frames / format -> mFramesPerPacket ; else packets = frames ; if ( packets == 0 ) packets = 1 ; bytes = packets * maxPacketSize ; } return bytes ; }
Determine the size, in bytes, of a buffer necessary to represent the supplied number
of seconds of audio data.
Determine the size in bytes of a buffer necessary to represent the supplied number of seconds of audio data .
// constant packet size // couldn't get queue's maximum output packet size // worst-case scenario: 1 frame in a packet // sanity check