Custom AVAudioUnit with AVAudioEngine crashes in the service provider block

I have the following code block, which should create an AVAudioNode that generates a sine wave, but it crashes in the line marked with the sign - [AUAudioUnitV2Bridge setOutputProvider:]: unrecognized selector sent to the instance.

Any ideas what could be wrong?

// TEST  
AudioComponentDescription mixerDesc;  
mixerDesc.componentType = kAudioUnitType_Generator;  
mixerDesc.componentSubType = kAudioUnitSubType_ScheduledSoundPlayer;  
mixerDesc.componentManufacturer = kAudioUnitManufacturer_Apple;  
mixerDesc.componentFlags = 0;  
mixerDesc.componentFlagsMask = 0;  

[AVAudioUnit instantiateWithComponentDescription:mixerDesc options:kAudioComponentInstantiation_LoadInProcess completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {  
    NSLog(@"here");  

    // Crashes here  
    audioUnit.AUAudioUnit.outputProvider = ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp, AUAudioFrameCount frameCount, NSInteger inputBusNumber, AudioBufferList *inputData)  
    {  
        const double amplitude = 0.2;  
        static double theta = 0.0;  
        double theta_increment = 2.0 * M_PI * 880.0 / 44100.0;  
        const int channel = 0;  
        Float32 *buffer = (Float32 *)inputData->mBuffers[channel].mData;  

        memset(inputData->mBuffers[channel].mData, 0, inputData->mBuffers[channel].mDataByteSize);  
        memset(inputData->mBuffers[1].mData, 0, inputData->mBuffers[1].mDataByteSize);  

        // Generate the samples  
        for (UInt32 frame = 0; frame < inputBusNumber; frame++)  
        {  
            buffer[frame] = sin(theta) * amplitude;  

            theta += theta_increment;  
            if (theta >= 2.0 * M_PI)  
            {  
                theta -= 2.0 * M_PI;  
            }  
        }  

        return noErr;  
    };  

}]; 

UPDATE

It seems like this will be the right approach, but alas, the status causes errors, callback is never called .:

    AVAudioUnitGenerator *unit = [[AVAudioUnitGenerator alloc] init];

// TEST
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Generator;
desc.componentSubType = kAudioUnitSubType_ScheduledSoundPlayer;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;

[AVAudioUnit instantiateWithComponentDescription:desc options:kAudioComponentInstantiation_LoadInProcess completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
    NSLog(@"here");

    self.audioNode = audioUnit;

    OSStatus status;
     AudioUnit *unit = audioUnit.audioUnit;

    UInt32 numBuses = 1;
    status = AudioUnitSetProperty(unit, kAudioUnitProperty_ElementCount, kAudioUnitScope_Output, 0, &numBuses, sizeof(UInt32));
    NSLog(@"status: %d", status);


    AURenderCallbackStruct rcbs;
    rcbs.inputProc = callback2;
    rcbs.inputProcRefCon = (__bridge void * _Nullable)(self);


    status = AudioUnitSetProperty(unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Output, 0, &rcbs, sizeof(rcbs));
    NSLog(@"status: %d", status);

}];
+6
source share
1 answer

Exactly what the error message says. After creating the required type of audioUnit and ensuring that AUAudioUnit is not equal to zero, this code makes it explicit:

if (![audioUnit.AUAudioUnit respondsToSelector:@selector(outputProvider)]) {
        NSLog(@"selector method not found in this instance of an AUAudioUnit");
}

, .

: , AVAudioUnit API , :   AudioUnit AVAudioUnit; AURenderCallback; AURenderCallbackStruct C; AudioUnitSetProperty kAudioUnitProperty_SetRenderCallback AudioUnit. , , outputProvider.

+2

Source: https://habr.com/ru/post/1015746/


All Articles