1

When I start an audio output process with AudioQueueStart(out.queue, nil), the output callback only fires 3 times (which is the number of allocated buffers).

Here is my Output callback code:

static void AQOutputCallback(void* aqr,
                             AudioQueueRef outQ,
                             AudioQueueBufferRef outQB)
{
  AQCallbackStruct *aqc = (AQCallbackStruct *) aqr;

  NSLog(@"Out");

  // Check if AudioQueue is stopped
  if (!aqc->run) {
    NSLog(@"Stopped");
    return;
  }

  // Processing data

  // Check enqueue error
  int err = AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
  if (err != noErr) NSLog(@"OutputCallback AudioQueueEnqueueBuffer() %d ", err);
  NSLog(@"Enqueued");
}

I think it's due to the lack of buffers, but my output is:

Out
Enqueued
Out
Enqueued
Out
Enqueued

So the first buffer is enqueued before the AudioQueue starts to fill the third one, it should not run out of buffers.

What happens here ?

Edit: Setup code

#define AUDIO_BUFFERS 3

typedef struct AQCallbackStruct {
  AudioStreamBasicDescription mDataFormat;
  AudioQueueRef queue;
  AudioQueueBufferRef mBuffers[AUDIO_BUFFERS];
  unsigned long frameSize;
  BOOL *run;
} AQCallbackStruct;

// In some method
AQCallbackStruct out;

out.mDataFormat
out.mDataFormat.mFormatID = kAudioFormatLinearPCM;
out.mDataFormat.mSampleRate = 44100.0;
out.mDataFormat.mChannelsPerFrame = 2;
out.mDataFormat.mBitsPerChannel = 16;
out.mDataFormat.mBytesPerPacket =
  out.mDataFormat.mBytesPerFrame =
    out.mDataFormat.mChannelsPerFrame * sizeof(short int);
out.mDataFormat.mFramesPerPacket = 1;
out.mDataFormat.mFormatFlags =
    kLinearPCMFormatFlagIsBigEndian
  | kLinearPCMFormatFlagIsSignedInteger
  | kLinearPCMFormatFlagIsPacked;

out.frameSize = 735;

int err;
err = AudioQueueNewOutput(&out.mDataFormat,
                          AQOutputCallback,
                          &out,
                          CFRunLoopGetCurrent(),
                          kCFRunLoopCommonModes,
                          0,
                          &out.queue);

if (err != noErr) NSLog(@"AudioQueueNewOutput() error: %d", err);


for (int i=0; i<AUDIO_BUFFERS; i++) {         
  err = AudioQueueAllocateBuffer(out.queue, out.frameSize, &out.mBuffers[i]);
  if (err != noErr) NSLog(@"Output AudioQueueAllocateBuffer() error: %d", err);
  out.mBuffers[i]->mAudioDataByteSize = out.frameSize;

  err = AudioQueueEnqueueBuffer(out.queue, out.mBuffers[i], 0, NULL);
  if (err != noErr) NSLog(@"Output AudioQueueEnqueueBuffer() error: %d", err);
}

AudioQueueStart(out.queue, nil);
ldiqual
  • 15,015
  • 6
  • 52
  • 90

2 Answers2

0

Please have a look at this page: where to start with audio synthesis on iPhone.

The BleepMachine sample code that's in there is what got me started with this.

Community
  • 1
  • 1
Stefan
  • 152
  • 6
  • 2
    Does it, in any way, solve my problem here ? I'm talking about running out of buffers, not about starting with audio synthesis... – ldiqual Apr 09 '12 at 21:00
  • Could you provide a bit more info then? What is it that you are trying to accomplish, if not audio synth? Could it be that you are not doing the setup correctly; what setup code are you using? – Stefan Apr 09 '12 at 21:10
  • I updated my question with the setup code. I'm just trying to output something to my iPhone speaker, but I don't think its relevant here, as it doesn't work even without sound processing code. – ldiqual Apr 09 '12 at 21:21
  • I looked at the initialization code you added. I see two differences with mine, not sure how relevant they are: (1) In the call to AudioQueueNewOutput, I pass NULL instead of CFRunLoopGetCurrent(), and (2) In the call to AudioQueueAllocateBuffer, in the second parameter (the size) I pass what would be in your code: frameSize*out.mDataFormat.mBytesPerFrame (because what you call 'frameSize' I took to be the number of frames in the buffer) – Stefan Apr 09 '12 at 21:28
  • I tried to put `NULL` instead of `CFRunLoopGetCurrent()`, and `frameSize*out.mDataFormat.mBytesPerFrame` during buffer allocation, but it changes nothing. I'm stuck :( – ldiqual Apr 09 '12 at 22:05
0

I finally found out where the problem was: AudioQueue callback in simulator but not on device

However, the output callback was properly fired in simulator but not on my device, and I still don't know exactly why there are differences in the AudioSession settings.

Community
  • 1
  • 1
ldiqual
  • 15,015
  • 6
  • 52
  • 90