I'm having trouble getting AVAudioEngine
manual rendering working when processing input. It's easy to get it working when there's no input node and audio comes from a player node.
Here's the code I've got, which can be pasted into a test:
- (void)testManualRendering {
auto engine = [[AVAudioEngine alloc] init];
auto format = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100.0
channels:2];
auto inputBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format
frameCapacity:1024];
auto abl = inputBuffer.mutableAudioBufferList;
XCTAssert(abl != NULL);
XCTAssertEqual(abl->mNumberBuffers, 2);
[engine connect:engine.inputNode to:engine.mainMixerNode format:format];
NSError* error;
[engine enableManualRenderingMode:AVAudioEngineManualRenderingModeOffline
format:format
maximumFrameCount:1024 error:&error];
XCTAssertNil(error);
XCTAssert([format isEqual:engine.manualRenderingFormat]);
NSLog(@"manualRenderingFormat: %@", engine.manualRenderingFormat);
auto success = [engine.inputNode setManualRenderingInputPCMFormat:format
inputBlock:^const AudioBufferList * _Nullable(AVAudioFrameCount inNumberOfFrames) {
XCTAssert(abl->mBuffers[0].mDataByteSize <= inNumberOfFrames * sizeof(float));
XCTAssert(abl->mBuffers[1].mDataByteSize <= inNumberOfFrames * sizeof(float));
return abl;
}];
XCTAssert(success);
[engine startAndReturnError:&error];
XCTAssertNil(error);
auto outputBuffer = [[AVAudioPCMBuffer alloc] initWithPCMFormat:format
frameCapacity:1024];
XCTAssertNotNil(outputBuffer);
XCTAssert(engine.isInManualRenderingMode);
auto status = [engine renderOffline:32 toBuffer:outputBuffer error:&error];
if(status == AVAudioEngineManualRenderingStatusInsufficientDataFromInputNode) {
printf("manual rendering failed: AVAudioEngineManualRenderingStatusInsufficientDataFromInputNode\n");
}
XCTAssertEqual(status, AVAudioEngineManualRenderingStatusSuccess);
if (error) {
NSLog(@"error: %@", error);
}
XCTAssertNil(error);
}
I'm getting AVAudioEngineManualRenderingStatusInsufficientDataFromInputNode
but the input buffer has plenty of data. What am I missing? Having trouble finding example code for this.