0

How to call objective c function in callback function

Callback Function:

static OSStatus inputRenderCallback (
 void *inRefCon, 
 AudioUnitRenderActionFlags *ioActionFlags, 
 const AudioTimeStamp *inTimeStamp, 
 UInt32 inBusNumber, 
 UInt32 inNumberFrames, 
 AudioBufferList *ioData 
) {
 soundStructPtr soundStructPointerArray = (soundStructPtr) inRefCon;
 UInt32 frameTotalForSound = soundStructPointerArray[inBusNumber].frameCount;
 BOOL isStereo = soundStructPointerArray[inBusNumber].isStereo;
 AudioUnitSampleType *dataInLeft;
 AudioUnitSampleType *dataInRight;
 dataInLeft = soundStructPointerArray[inBusNumber].audioDataLeft;
 if (isStereo) dataInRight = soundStructPointerArray[inBusNumber].audioDataRight;
 AudioUnitSampleType *outSamplesChannelLeft;
 AudioUnitSampleType *outSamplesChannelRight;
 outSamplesChannelLeft = (AudioUnitSampleType *) ioData->mBuffers[0].mData;
 if (isStereo) outSamplesChannelRight = (AudioUnitSampleType *) ioData->mBuffers[1].mData;
 UInt32 sampleNumber = soundStructPointerArray[inBusNumber].sampleNumber;
 for (UInt32 frameNumber = 0; frameNumber < inNumberFrames; ++frameNumber) {
 outSamplesChannelLeft[frameNumber] = dataInLeft[sampleNumber];
 if (isStereo) outSamplesChannelRight[frameNumber] = dataInRight[sampleNumber];
 sampleNumber++;
 if (sampleNumber >= frameTotalForSound){
 sampleNumber = 0;
 }
 }
 soundStructPointerArray[inBusNumber].sampleNumber = sampleNumber;
 return noErr;
}

Objective c function:

- (void) stopAUGraph {
 NSLog (@"Stopping audio processing graph");
 Boolean isRunning = false;
 OSStatus result = AUGraphIsRunning (processingGraph, &isRunning);
 if (noErr != result) {[self printErrorMessage: @"AUGraphIsRunning" withStatus: result]; return;}
 if (isRunning) {
 result = AUGraphStop (processingGraph);
 if (noErr != result) {[self printErrorMessage: @"AUGraphStop" withStatus: result]; return;}
 self.playing = NO;
 }
}

now in

if (sampleNumber >= frameTotalForSound){
 sampleNumber = 0;
}

of callback function part how can i call stopAUGraph function

asked Aug 12, 2013 at 4:48

1 Answer 1

2

A. Objects have methods, no functions. Methods are not called, but messages are sent to a receiver, that executes the method.

B. Therefore you need a reference to the instance as the receiver of the message, that leads to the execution of the method. There are two ways to get it:

B.A. Typically a callback has a userInfo pointer, which can hold the reference directly or in a structure.

B.B. If you do not have such a info, you can ask a singleton for it or use a global var. This is worse.

C. After having such a receiver reference, you can use the usual message syntax in the C function, if it is compiled as Objective-C code (filename.m or setting in Xcode):

void function (void *userInfo)
{
 id reference = userInfo; // Using ARC you have to clarify the ownership with castings.
 [reference message];
}

You can use a function call, too, if you get the implementation pointer of the method or by using the rte, for example objc_msgSend(). Getting the implementation pointer is worse, because it turns off the polymorphic mechanism.

answered Aug 12, 2013 at 5:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.