Record microphone input with AudioUnit filter applied to the recorded file
I'm trying to make a little project just to see how to record the microphone input into a file with a AudioUnit filter (NewTimePitch).
I use TheAmazingAudioEngine library in order to achieve this.
I've tried to follow exactly how they implement this feature in the TheAmazingSample, but I still don't understand why the file is recorded without the filter. I even tried to create the AEAudioController in the AppDelegate like they do it in the sample project, without success...
If someone can tell me what I'm doing wrong, that would be very cool since I'm stuck on this.
Thanks in advance.
Source: I've uploaded this test project here on github.
Here are the code snippets related to my problem:
From AppDelegate.h and .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;
}
From 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];
}
EDIT: I seem to have the same problem as this issue How to save a recorded audio with reverb effect
Anyone know how to use AEAudioFileWriter ? Should I use an AEBlockChannel in order to do that?
链接地址: http://www.djcxy.com/p/78154.html