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?
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");
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);
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];
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);
}];
Chris source
share