What is SourceDrop?

SourceDrop is the easiest way to share your snippets. Try it yourself!

ARDictationViewController.m ...

download
//
//  ARDictationViewController.m
//  Autoradio
//
//  Created by Michael Hohl on 03.09.12.
//  Copyright (c) 2012 Michael Hohl. All rights reserved.
//

#import "ARDictationViewController.h"
#import "OpenEars/PocketsphinxController.h"
#import "OpenEars/LanguageModelGenerator.h"
#import "OpenEars/OpenEarsEventsObserver.h"

@interface ARDictationViewController ()

@end

@implementation ARDictationViewController
{
    PocketsphinxController *pocketsphinxController;
    LanguageModelGenerator *languageModelGenerator;
    OpenEarsEventsObserver *openEarsEventsObserver;
}

@synthesize parentModuleController;

@dynamic pocketsphinxController;
- (PocketsphinxController *)pocketsphinxController
{
    if (pocketsphinxController == nil) {
        pocketsphinxController = [[PocketsphinxController alloc] init];
    }
    return pocketsphinxController;
}

@dynamic languageModelGenerator;
- (LanguageModelGenerator *)languageModelGenerator
{
    if (languageModelGenerator == nil) {
        languageModelGenerator = [[LanguageModelGenerator alloc] init];
    }
    return languageModelGenerator;
}


- (OpenEarsEventsObserver *)openEarsEventsObserver
{
	if (openEarsEventsObserver == nil) {
		openEarsEventsObserver = [[OpenEarsEventsObserver alloc] init];
	}
	return openEarsEventsObserver;
}

- (void)viewDidLoad
{
    [self.openEarsEventsObserver setDelegate:self];
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [self.openEarsEventsObserver setDelegate:nil];
}

- (void)generateLanguageModel:(id)sender
{
    NSError *error = [self.languageModelGenerator generateLanguageModelFromArray:@[ @"PLAY", @"PAUSE", @"RESUME", @"FORWARD", @"BACKWARD", @"STOP", @"NEXT TRACK", @"LAST TRACK", @"PREVIOUS TRACK", @"SKIP TRACK", @"YES", @"NO" ] withFilesNamed:@"DynamicLanguageModel"];
    NSDictionary *dynamicLanguageGenerationResultsDictionary = nil;
	if([error code] != noErr) {
		NSLog(@"Dynamic language generator reported error %@", [error description]);
	} else {
		dynamicLanguageGenerationResultsDictionary = [error userInfo];
		
		// A useful feature of the fact that generateLanguageModelFromArray:withFilesNamed: always returns an NSError is that when it returns noErr (meaning there was
		// no error, or an [NSError code] of zero), the NSError also contains a userInfo dictionary which contains the path locations of your new files.
		
		// What follows demonstrates how to get the paths for your created dynamic language models out of that userInfo dictionary.
		NSString *lmFile = [dynamicLanguageGenerationResultsDictionary objectForKey:@"LMFile"];
		NSString *dictionaryFile = [dynamicLanguageGenerationResultsDictionary objectForKey:@"DictionaryFile"];
		NSString *lmPath = [dynamicLanguageGenerationResultsDictionary objectForKey:@"LMPath"];
		NSString *dictionaryPath = [dynamicLanguageGenerationResultsDictionary objectForKey:@"DictionaryPath"];
		
		NSLog(@"Dynamic language generator completed successfully, you can find your new files %@\n and \n%@\n at the paths \n%@ \nand \n%@", lmFile,dictionaryFile,lmPath,dictionaryPath);
		
		// pathToDynamicallyGeneratedGrammar/Dictionary aren't OpenEars things, they are just the way I'm controlling being able to switch between the grammars in this sample app.
		self.pathToDynamicallyGeneratedGrammar = lmPath; // We'll set our new .languagemodel file to be the one to get switched to when the words "CHANGE MODEL" are recognized.
		self.pathToDynamicallyGeneratedDictionary = dictionaryPath; // We'll set our new dictionary to be the one to get switched to when the words "CHANGE MODEL" are recognized.
	}
	
}

- (void)startListening:(id)sender
{
    [self.pocketsphinxController startListeningWithLanguageModelAtPath:self.pathToDynamicallyGeneratedGrammar
                                                      dictionaryAtPath:self.pathToDynamicallyGeneratedDictionary
                                                   languageModelIsJSGF:FALSE];
}

- (void) pocketsphinxDidReceiveHypothesis:(NSString *)hypothesis recognitionScore:(NSString *)recognitionScore utteranceID:(NSString *)utteranceID
{
    NSLog(@"Pocketsphinx recognized the following phrase:%@ score:%@", hypothesis, recognitionScore);
}


// An optional delegate method of OpenEarsEventsObserver which informs that there was an interruption to the audio session (e.g. an incoming phone call).
- (void) audioSessionInterruptionDidBegin {
	NSLog(@"AudioSession interruption began."); // Log it.
	LOG(@"Status: AudioSession interruption began."); // Show it in the status box.
	[self.pocketsphinxController stopListening]; // React to it by telling Pocketsphinx to stop listening since it will need to restart its loop after an interruption.
}

// An optional delegate method of OpenEarsEventsObserver which informs that the interruption to the audio session ended.
- (void) audioSessionInterruptionDidEnd {
	NSLog(@"AudioSession interruption ended."); // Log it.
	LOG(@"Status: AudioSession interruption ended."); // Show it in the status box.
    // We're restarting the previously-stopped listening loop.
	[self.pocketsphinxController startListeningWithLanguageModelAtPath:self.pathToDynamicallyGeneratedGrammar dictionaryAtPath:self.pathToDynamicallyGeneratedDictionary languageModelIsJSGF:FALSE];
}

// An optional delegate method of OpenEarsEventsObserver which informs that the audio input became unavailable.
- (void) audioInputDidBecomeUnavailable {
	NSLog(@"The audio input has become unavailable"); // Log it.
	LOG(@"Status: The audio input has become unavailable"); // Show it in the status box.
	[self.pocketsphinxController stopListening]; // React to it by telling Pocketsphinx to stop listening since there is no available input
}

// An optional delegate method of OpenEarsEventsObserver which informs that the unavailable audio input became available again.
- (void) audioInputDidBecomeAvailable {
	NSLog(@"The audio input is available"); // Log it.
	LOG(@"Status: The audio input is available"); // Show it in the status box.
	[self.pocketsphinxController startListeningWithLanguageModelAtPath:self.pathToDynamicallyGeneratedGrammar dictionaryAtPath:self.pathToDynamicallyGeneratedDictionary languageModelIsJSGF:FALSE];
}

@end
Uploaded at 09/03/12 17:01 (UTC)