Dave L
Dave L

Reputation: 151

How to get mono file to play in stereo in iPhone app using audio queue services

I'm writing an iPhone app in which I'm playing some mono mp3 files using Audio Queue Services. When playing, I only hear sound on one channel. I've been searching for an example of how to get the files to play on both channels, with no luck. What I'm doing is pretty simple right now. I'm setting up my audio queue like this:

AudioStreamBasicDescription queueASBD;  
AudioQueueRef audioQueue;  
queueASBD.mSampleRate = 44100.0;  
queueASBD.mFormatID = kAudioFormatLinearPCM;  
queueASBD.mFormatFlags = kAudioFormatFlagsNativeEndian | AudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;  
queueASBD.mBytesPerPacket = 4;  
queueASBD.mFramesPerPacket = 1;  
queueASBD.mBytesPerFrame = 4;  
queueASBD.mChannelsPerFrame = 2;  
queueASBD.mBitsPerChannel = 16;  
queueASBD.mReserved = 0;  
AudioQueueNewOutput(&queueASBD, AudioQueueCallback, NULL, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &audioQueue);  

I open the mp3 file like this (error checking and such removed for brevity):

ExtAudioFileRef audioFile;  
ExtAudioFileOpenURL(url, &audioFile);  
ExtAudioFileSetProperty(audioFile, kExtAudioFileProperty_ClientDataFormat, sizeof(queueASBD), &queueASBD);  

And to queue a buffer, I do something like this:

AudioQueueBufferRef buffers; // previously allocated  
AudioBufferList abl;  
UInt32 length = (UInt32)queueASBD.mSampleRate / BUFFERS_PER_SECOND;  
abl.mNumberBuffers = 1;  
abl.mBuffers[0].mDataByteSize = (UInt32)(queueASBD.mSampleRate * queueASBD.mBytesPerPacket / BUFFERS_PER_SECOND);  
abl.mBuffers[0].mNumberChannels = queueASBD.mChannelsPerFrame;  
abl.mBuffers[0].mData = buffer->mAudioData;  
ExtAudioFileRead(audioFile, &length, &abl);  
UInt32 byte_length = length * (UInt32)queueASBD.mBytesPerPacket;  
buffer->mAudioDataByteSize = byte_length;  
AudioQueueEnqueueBuffer(audioQueue, buffer, 0, NULL);  

Is there a way to get the file to play in stereo without totally re-coding (such as by using the Audio Unit APIs)? Could an Audio Converter help here? Is there some other way? Thanks for any help.

Upvotes: 1

Views: 912

Answers (1)

hotpaw2
hotpaw2

Reputation: 70693

Try opening the Audio Queue with only one channel per frame (e.g. mono), and the matching number of bytes per packet and per frame (probably 2).

Upvotes: 1

Related Questions