1
votes

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);
2

2 Answers

0
votes

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.

0
votes

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.