我正在开发一个iOS应用程序,它使用AVAudioEngine进行各种操作,包括将音频录制到文件,使用音频单元对音频应用效果,以及播放应用效果的音频 . 我使用tap也将输出写入文件 . 完成此操作后,它会在播放音频时实时写入文件 .
是否可以设置AVAudioEngine图形,该图形从文件读取,使用音频单元处理声音,并输出到文件,但比实时更快(即,与硬件可以处理的速度一样快)?用于此的用例是输出几分钟的音频并应用效果,我当然不希望等待几分钟来处理它 .
Edit: 这里's the code that I' m用于设置AVAudioEngine的图形,并播放声音文件:
AVAudioEngine* engine = [[AVAudioEngine alloc] init];
AVAudioPlayerNode* player = [[AVAudioPlayerNode alloc] init];
[engine attachNode:player];
self.player = player;
self.engine = engine;
if (!self.distortionEffect) {
self.distortionEffect = [[AVAudioUnitDistortion alloc] init];
[self.engine attachNode:self.distortionEffect];
[self.engine connect:self.player to:self.distortionEffect format:[self.distortionEffect outputFormatForBus:0]];
AVAudioMixerNode* mixer = [self.engine mainMixerNode];
[self.engine connect:self.distortionEffect to:mixer format:[mixer outputFormatForBus:0]];
}
[self.distortionEffect loadFactoryPreset:AVAudioUnitDistortionPresetDrumsBitBrush];
NSError* error;
if (![self.engine startAndReturnError:&error]) {
NSLog(@"error: %@", error);
} else {
NSURL* fileURL = [[NSBundle mainBundle] URLForResource:@"test2" withExtension:@"mp3"];
AVAudioFile* file = [[AVAudioFile alloc] initForReading:fileURL error:&error];
if (error) {
NSLog(@"error: %@", error);
} else {
[self.player scheduleFile:file atTime:nil completionHandler:nil];
[self.player play];
}
}
上面的代码在test2.mp3文件中播放声音,实时应用 AVAudioUnitDistortionPresetDrumsBitBrush
失真预设 .
然后我通过在[self.player play]之后添加这些行来修改上面的代码:
[self.engine stop];
[self renderAudioAndWriteToFile];
我修改了Vladimir提供的renderAudioAndWriteToFile方法,这样它就不会在第一行中分配新的AVAudioEngine,而只使用已经设置的self.engine .
但是,在renderAudioAndWriteToFile中,它正在记录"Can not render audio unit",因为AudioUnitRender返回的状态为 kAudioUnitErr_Uninitialized
.
Edit 2 :我应该提一下,我很高兴转换我发布的AVAudioEngine代码使用C apis,如果这会让事情变得更容易 . 但是,我希望代码生成与AVAudioEngine代码相同的输出(包括使用上面显示的工厂预设) .
1 回答
配置引擎和播放器节点 .
为您的玩家节点调用
play
方法 .暂停引擎 .
使用此method从AVAudioOutputNode(
audioEngine.outputNode
)获取音频设备 .在循环中使用AudioUnitRender从音频单元渲染,并使用Extended Audio File Services将音频缓冲区列表写入文件 .
例:
音频引擎配置
渲染方法
我使用了this酷框架中的一些函数: