0

I'm using AudioQueue to analyze the audio frequency. I saw the SpeakHere example by Apple.

I know to use the callback function to get the audio data in inbuffer->mAudioData. The question is when I get the audio data, I want to update the spectrum on the UI. The UI is code use Objective-C and the SpeakHere code use C++. I don't know how to call the UI method in the callback function and pass the mAudioData.

Here is the callback function:

// AudioQueue callback function, called when an input buffers has been filled. void AQRecorder::MyInputBufferHandler( void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp * inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc) { AQRecorder *aqr = (AQRecorder *)inUserData; aqr->currentQueueBufferRef = inBuffer; try { if (inNumPackets > 0) { // write packets to file XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize, inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData), "AudioFileWritePackets failed"); ***//Here I want to call Objective c Method and pass the value.*** } // if we're not stopping, re-enqueue the buffe so that it gets filled again if (aqr->IsRunning()) XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed"); } catch (CAXException e) { char buf[256]; fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf)); } } 

1 Answer 1

2

If you want to use both C++ and Objective-C in the same file, please make sure the extension of the file is .mm instead of .m or .cpp. This way, the compiler knows it's Objective-C++, and it compiles without a problem.

Somebody has explained it in detail in this blog post: Mixing Objective-C, C++ and Objective-C++

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Vikram Singh ! Thank you very much so fast to replay. Yes, the extension of the file is .mm. The c++ callback function is static method, I was not familiar with c++. I mean is that if I can define a protocol in c++ file(the extension of the file is also .mm) ? And in the UI class we can implementation the method.
I am not very clear with your requirements but yes, you can implement your callback C++ method in your UI objective-c class and then call the objective-c code to update the UI from within your callback C++ function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.