0

I am currently learning to use Audio Queue Services in iOS. For this, I followed Audio Queue Services Programming Guide - Recording Audio. This works.

Now I would like to update a UI element (a power meter) when the callback function receives the input data from the microphone. This is where I run into a problem.

I am not sure what is a good way to call from the callback function into the view/view controller for updating the power meter. I tried this: To the custom struct to manage state, which is described in the guide, I added a pointer to my view controller:

struct AQRecorderState {
    AudioStreamBasicDescription  mDataFormat;                   // 2
    AudioQueueRef                mQueue;                        // 3
    AudioQueueBufferRef          mBuffers[kNumberBuffers];      // 4
    AudioFileID                  mAudioFile;                    // 5
    UInt32                       bufferByteSize;                // 6
    SInt64                       mCurrentPacket;                // 7
    bool                         mIsRunning;                    // 8
    void*                        mViewController; // <- I've added this line
};

I initialize this pointer when I start recording. This is done in a method of the view controller, which gets called when the start button receives a TouchUp event:

- (IBAction)startAudioRecording:(id)sender {

    struct AQRecorderState* aqData = createAQRecorderState();

    void* pViewControllerAsVoidPointer = (__bridge void*) self;
    aqData->mViewController = pViewControllerAsVoidPointer;

//...

The method createAQRecorderState(), which allocates the struct, looks as follows:

struct AQRecorderState* createAQRecorderState() {
    struct AQRecorderState* pAqData = (struct AQRecorderState*) malloc(sizeof(AQRecorderState));

    initializeAQRecorderState(*pAqData); // sets members of the struct; does not set pointer to view controller

    return pAqData;
}

AFAIK, the very instance of the struct that I create when starting recording gets delivered to later calls to the callback function. For this reason, I've added the following code to my callback function in order to then call to the view controller:

void HandleInputBuffer(
                       void                                 *aqData,
                       AudioQueueRef                        inAQ,
                       AudioQueueBufferRef                  inBuffer,
                       const AudioTimeStamp                 *inStartTime,
                       UInt32                               inNumPackets,
                       const AudioStreamPacketDescription   *inPacketDesc
                       ) {

//...

NSLog(@"interesting part start");

void* pViewControllerAsVoidPtr = pAqData->mViewController;
NSLog(@"localized pointer to void");

ViewController* pViewController = (__bridge ViewController*) pViewControllerAsVoidPtr;  //EXC_BAD_ACCESS
NSLog(@"localized pointer to ViewController");

//...
}

However, I get a EXC_BAD_ACCESS (code=1, address=0xffffffff) in the callback function above in the marked line when I do the bridged pointer conversion. I've then checked this with the debugger and in fact pViewControllerAsVoidPtr does not hold the address that I stored in pAqData->mViewController during the execution of the method startAudioRecording:.

Why does that pointer get changed? Or alternatively: How to correctly call from my callback function into the view controller for updating the UI?

Daniel S.
  • 6,458
  • 4
  • 35
  • 78
  • unless it has been updated, the SpeakHere example was pre-ARC. Have you converted it to ARC? I assume that's the example project you're using as it uses Audio Queue to control a level meter. – Max MacLeod Nov 29 '12 at 10:38
  • I do use ARC in my project. Compatibility of the code with ARC is a good idea that I will follow. I also have to admit that I do not fully understand bridging pointer conversions. However, I did not use an example project. I used a freshly created project and copied most code from the guide that I linked to. – Daniel S. Nov 29 '12 at 10:56
  • @MaxMacLeod, btw a good idea to look at the SpeakHere example. If they use Audio Queues and have a level meter, then I should be able to see there how it's done. – Daniel S. Nov 29 '12 at 11:05
  • big time! That does exactly what you need. Btw AudioQueue may well not be for you depending on what you're doing. AudioUnit can be a lot easier to work with. Also if you're using SpeakHere, it's non-ARC so just set the compiler flag for those classes in your project. Check my answers. I cover a lot of issues with AudioQueue and AudioUnit related to SpeakHere. – Max MacLeod Nov 29 '12 at 11:10

1 Answers1

0

I found my mistake. It's in how I do the step Creating a Recording Audio Queue. I do it exactly as copy-pasted from the guide:

AudioQueueNewInput (                              // 1
    &aqData.mDataFormat,                          // 2
    HandleInputBuffer,                            // 3
    &aqData, //<------------------ HERE           // 4
    NULL,                                         // 5
    kCFRunLoopCommonModes,                        // 6
    0,                                            // 7
    &aqData.mQueue                                // 8
);

The problem was that in the guide, the variable aqData is a struct and therefor needs to be referenced (leading & sign) to convert it to a pointer, whereas my variable aqData already is a pointer to a struct and therefor the referencing (leading & sign) changes it to a "pointer to pointer to struct". Hence I passed the address of the pointer instead of the address of the struct to the function AudioQueueNewInput(...), which is wrong, and of course the memory at this other location has different content.

The compiler does not complain about this wrong type, because it expects a (void*), which can be any pointer, including (struct**), a pointer to a pointer to a struct.

Daniel S.
  • 6,458
  • 4
  • 35
  • 78