Search code examples
iphoneavaudiorecordervoice-recognition

How to Recognise voice using AVAudioRecorder in iphone


I am using AVAudioRecorder .if I tap on record button,The recording should start/save only after recognising the voice.

- (void)viewDidLoad
{
    recording = NO;
    NSString * filePath = [NSHomeDirectory()
                           stringByAppendingPathComponent:@"Documents/recording.caf"];

     NSDictionary *recordSettings = 
     [[NSDictionary alloc] initWithObjectsAndKeys:
     [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
     [NSNumber numberWithInt: kAudioFormatAppleLossless],    
     AVFormatIDKey,
     [NSNumber numberWithInt: 1],                         
     AVNumberOfChannelsKey,
     [NSNumber numberWithInt: AVAudioQualityMax],         
     AVEncoderAudioQualityKey,nil];
    AVAudioRecorder *newRecorder = [[AVAudioRecorder alloc] 
                                    initWithURL: [NSURL fileURLWithPath:filePath]
                                    settings: recordSettings
                                    error: nil];

    [recordSettings release];
    self.soundRecorder = newRecorder;
    [newRecorder release];
    self.soundRecorder.delegate = self;
    NSLog(@"path is %@",filePath);
    [super viewDidLoad];
}
- (IBAction) record:(id) sender {
    if (recording) {
        [self.soundRecorder stop];
        [recordBtn setTitle:@"Record" forState:UIControlStateNormal];
        recording = NO;     
    } else {
        [self.soundRecorder record];
        [recordBtn setTitle:@"Stop" forState:UIControlStateNormal];
        recording = YES;

    }
}
- (IBAction) play {
    NSString * filePath = [NSHomeDirectory()
                           stringByAppendingPathComponent:@"Documents/recording.caf"];
    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:filePath] error: nil];
    newPlayer.delegate = self;
    NSLog(@"playing file at url %@ %d",[[newPlayer url] description],[newPlayer play]);
}

Please Help me out.


Solution

  • That's a challenging goal you have. iOS doesn't include the smarts to recognize voice specifically, you will have to provide a filter or your own to do that. If you just want VOX type support (i.e. start recording when a given level of audio is detected) it is easily done by monitoring audio levels using the Audio Toolbox Framework.

    If you need to recognize voice specifically you will need a specialized recognition filter to run your audio data through.

    If you had such a filter you could take one of two approaches: a) Just record everything then post-process the resulting audio data to locate the time index at which voice is recognized and just ignore the data up to that point (copy the remaining data to another buffer perhaps) or b) use the Audio Toolbox Framework to monitor the recorded data in real time. Pass the data through your voice finding filter and only start buffering the data when your filter triggers.

    Actual implementation is quite involved and too long to address here, but I have seen sample code in books and online that you could start from. I'm sorry I don't have any links to share at this time but will post any I come across in the near future.