Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom AVAudioUnit with AVAudioEngine crashes on set provider block

I have the follow code block that should create an AVAudioNode that produces a sine wave, however it crashes on the line marked with a -[AUAudioUnitV2Bridge setOutputProvider:]: unrecognized selector sent to instance.

Any ideas what might 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 feels like something like this would be the right approach, but alas, status throws errors, callback 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);

}];
like image 230
Chris Avatar asked Oct 30 '22 10:10

Chris


1 Answers

Exactly what the error message says. After instantiating your requested type of audioUnit and making sure the AUAudioUnit isn't nil, this code makes it explicit:

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

The header file does not claim that this method is absolutely required to be implemented in all audio units.

Added later: If you just want to have your code synthesize samples for an AVAudioUnit, and don't care about the specific API or code structure, then: Get the AudioUnit property of your AVAudioUnit instance; create an AURenderCallback function; fill in an AURenderCallbackStruct with this C function; then do an AudioUnitSetProperty for kAudioUnitProperty_SetRenderCallback on the AudioUnit. This seems to not crash even if no outputProvider method is available.

like image 190
hotpaw2 Avatar answered Jan 02 '23 21:01

hotpaw2