使用适用于录制文件的AudioUnit过滤器录制麦克风输入
我正在尝试制作一个小项目,以了解如何使用AudioUnit过滤器(NewTimePitch)将麦克风输入记录到文件中。
我使用TheAmazingAudioEngine库来实现此目的。
我试图完全按照他们在TheAmazingSample中实现此功能的方式进行操作,但我仍然不明白为什么在没有过滤器的情况下录制文件。 我甚至试图在AppDelegate中创建AEAudioController,就像他们在示例项目中一样,但没有成功......
如果有人能告诉我我做错了什么,那将会非常酷,因为我坚持这一点。
提前致谢。
来源:我已经在github上传了这个测试项目。
以下是与我的问题相关的代码片段:
从AppDelegate.h和.m
@property (strong, nonatomic) AEAudioController *audioController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.audioController = [[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] inputEnabled:YES];
_audioController.preferredBufferDuration = 0.005;
_audioController.useMeasurementMode = YES;
return YES;
}
从ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Getting AEAudioController from AppDelegate
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.audioController = [appDelegate audioController];
//Starting AEAudioController
NSError *errorAudioSetup = NULL;
BOOL result = [self.audioController start:&errorAudioSetup];
if ( !result ) {
NSLog(@"Error starting audio engine: %@", errorAudioSetup.localizedDescription);
}
// Creating path for recorded file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
audioPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"audioRecording.aiff"];
}
- (IBAction)startRecording:(id)sender {
NSError *error;
//Creating AERecorder
recorder = [[AERecorder alloc] initWithAudioController:self.audioController];
//Starting to record
if (![recorder beginRecordingToFileAtPath:audioPath fileType:kAudioFileAIFFType error:&error]){
NSLog(@"n%@n", error);
}
//Adding recorder to Input/Output receiver
[self.audioController addInputReceiver:recorder];
[self.audioController addOutputReceiver:recorder];
//Creating the AudioUnit filter
AudioComponentDescription pitchComponent = AEAudioComponentDescriptionMake(kAudioUnitManufacturer_Apple, kAudioUnitType_FormatConverter, kAudioUnitSubType_NewTimePitch);
NSError *pitchError;
pitch = [[AEAudioUnitFilter alloc] initWithComponentDescription:pitchComponent audioController:self.audioController error:&pitchError];
//High Voice - max 2400
AudioUnitSetParameter(pitch.audioUnit, kNewTimePitchParam_Pitch, kAudioUnitScope_Global, 0, 1000, 0);
//Low Voice - max -2400
//AudioUnitSetParameter(pitch.audioUnit, kNewTimePitchParam_Pitch, kAudioUnitScope_Global, 0, -1000, 0);
//Adding the AudioUnit filter to AEAudioController
[self.audioController addFilter:pitch];
}
- (IBAction)stopRecording:(id)sender {
//End the record
[recorder finishRecording];
//Removing recorder from Input/Output receiver
[self.audioController removeInputReceiver:recorder];
[self.audioController removeOutputReceiver:recorder];
//Removing the AudioUnit filter from AEAudioController
[self.audioController removeFilter:pitch];
//Deleting recorder
recorder = nil;
}
- (IBAction)playFile:(id)sender {
//Creating the AEAudioFilePlayer
NSError *playerError;
player = [AEAudioFilePlayer audioFilePlayerWithURL:[NSURL URLWithString:audioPath] audioController:self.audioController error:&playerError];
[self.audioController addChannels:@[player]];
//Playing the file. It plays the file with the filter applied on it. But the file in the document directory has no filter applied to it.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
编辑:我似乎有这个问题相同的问题如何保存记录音频与混响效果
任何人都知道如何使用AEAudioFileWriter? 我应该使用AEBlockChannel来做到这一点吗?
链接地址: http://www.djcxy.com/p/78153.html上一篇: Record microphone input with AudioUnit filter applied to the recorded file