使用应用于录制文件的AudioUnit滤镜录制麦克风输入

时间:2015-02-19 15:31:44

标签: ios core-audio audio-recording audiounit

我正在尝试制作一个小项目,以了解如何使用AudioUnit过滤器(NewTimePitch)将麦克风输入录制到文件中。

我使用TheAmazingAudioEngine库来实现这一目标。

我试图完全按照他们如何在TheAmazingSample中实现此功能,但我仍然不明白为什么在没有过滤器的情况下记录文件。我甚至尝试在AppDelegate中创建AEAudioController,就像他们在示例项目中一样,没有成功......

如果有人能告诉我我做错了什么,那就太酷了,因为我坚持这个。

提前致谢。

来源: 我已上传此测试项目here on 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];
}
编辑:我似乎和这个问题有同样的问题 How to save a recorded audio with reverb effect

任何人都知道如何使用AEAudioFileWriter? 我应该使用AEBlockChannel来做到这一点吗?

0 个答案:

没有答案