Home › Forums › OpenEars › OpenEars detects multiple words during silence › Reply To: OpenEars detects multiple words during silence
January 8, 2014 at 3:23 am
#1019344
nmpettus
Participant
Please forgive my coding I know it needs work. Thanks.
- (void)viewDidLoad
{
[super viewDidLoad];
[self becomeFirstResponder];
// Load a background image
UIImage *backimg = [UIImage imageNamed:@"back.png"];
[backImage setImage:backimg];
self.synthesizer=[[AVSpeechSynthesizer alloc] init];
// Instantiate Language Model
LanguageModelGenerator *lmGenerator = [[LanguageModelGenerator alloc] init];
//=============================================================================
// Build Dynamic Language Model on the fly
//=============================================================================
NSArray *words = [NSArray arrayWithObjects:@"BET", @"SPIN", @"HELP", @"BALANCE", nil];
NSString *name = @"OpenEarsModel";
NSError *err = [lmGenerator generateLanguageModelFromArray:words withFilesNamed:name forAcousticModelAtPath:[AcousticModel pathToModel:@"AcousticModelEnglish"]]; // Change "AcousticModelEnglish" to "AcousticModelSpanish" to create a Spanish language model instead of an English one.
NSDictionary *languageGeneratorResults = nil;
NSString *lmPath = nil;
NSString *dicPath = nil;
if([err code] == noErr) {
languageGeneratorResults = [err userInfo];
lmPath = [languageGeneratorResults objectForKey:@"LMPath"];
dicPath = [languageGeneratorResults objectForKey:@"DictionaryPath"];
NSLog(@"Path = %@",lmPath);
} else {
NSLog(@"Error: %@",[err localizedDescription]);
}
//=============================================================================
// Check settings bundle if user wants Voice Recognition
//=============================================================================
voiceOn = [[[NSUserDefaults standardUserDefaults] objectForKey:@"voiceOn"] boolValue];
NSLog(@"Voice Recognition = %@", voiceOn ? @"YES" : @"NO");
if (voiceOn == TRUE)
{
[self.openEarsEventsObserver setDelegate:self];
[self.pocketsphinxController startListeningWithLanguageModelAtPath:lmPath dictionaryAtPath:dicPath acousticModelAtPath:[AcousticModel pathToModel:@"AcousticModelEnglish"] languageModelIsJSGF:NO]; // Change "AcousticModelEnglish" to "AcousticModelSpanish" to perform Spanish recognition instead of English.
verbosePocketSphinx=YES;
}
//=============================================================================
// Check if user wants tiltSpin capability using CoreMotion
//=============================================================================
// Check the settings bundle for save game option
tiltSpin = [[[NSUserDefaults standardUserDefaults] objectForKey:@"tiltSpin"] boolValue];
NSLog(@"Tilt Spin Gesture = %@", tiltSpin ? @"YES" : @"NO");
if (tiltSpin==YES) {
self.motionManager = [[CMMotionManager alloc] init];
state = -1;
if ([self.motionManager isAccelerometerAvailable]){
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[self.motionManager startAccelerometerUpdatesToQueue:queue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
CMAcceleration acc = accelerometerData.acceleration;
[self addAcceleration: acc];
CGFloat x = self->oldX;
CGFloat y = self->oldY;
CGFloat z = self->oldZ;
// NSLog(@"x, y, z = %f %f %f",x,y,z);
CGFloat accu = 0.2; // feel free to experiment with this value
if (((fabs(x) < accu && fabs(y) < 0.5 && z < -0.5)||(fabs(x) > 0.5 && fabs(y) > 0.5 && z > -0.5))&& hasSpun==0) {
if (state == -1 || state == 1) {
state = 0;
[self slotSpin:self];
} else {
if (state == -1 || state == 0) {
state = 1;
// NSLog (@"Hey, put me back down on the table!");
}
}
}
}];
} else {
NSLog(@"Accelerometer is not available.");
}
}
//========================================================================================
// Load reel stop array from plist
//========================================================================================
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"slotreels" ofType:@"plist"];
reelArray = [NSArray arrayWithContentsOfFile:plistPath];
wait = .4;
//========================================================================================
// Check the settings bundle for save game option
gameSave = [[[NSUserDefaults standardUserDefaults] objectForKey:@"saveGame"] boolValue];
NSLog(@"Save Game = %@", gameSave ? @"YES" : @"NO");
if (gameSave==YES) {
// Retrieve previous score
amtBal = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PrevAmt"] intValue ];
NSLog(@"Amount Balance = %d",amtBal);
}
else {
amtBal=0;
}
// Correct negative amount balance
if (amtBal<0) {
amtBal=0;
}
// Set Up defaults
hasSpun=0;
amtScore=0;
if (amtBal==0) {
amtBal=100;
// TEST VALUES
// if (amtBal==0) {
// amtBal=10;
// Give instructions the first time game is started
[self slotHelp:self];
AVSpeechUtterance *utterance= [AVSpeechUtterance speechUtteranceWithString:@"starting the game with one hundred dollars!"];
utterance.rate = AVSpeechUtteranceMaximumSpeechRate/6; // Tell it to me slower
utterance.pitchMultiplier=1.1;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
self.synthesizer.delegate=self;
if (voiceOn) {
[self.pocketsphinxController suspendRecognition];
}
[self.synthesizer speakUtterance:utterance];
} else {NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: amtBal]];
result= [NSString stringWithFormat:@"resuming a saved game with %@ dollars!",numberString];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:result];
utterance.rate = AVSpeechUtteranceMaximumSpeechRate/6;
utterance.pitchMultiplier=1.1;
utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
self.synthesizer.delegate=self;
if (voiceOn) {
[self.pocketsphinxController suspendRecognition];
}
[self.synthesizer speakUtterance:utterance];
}
// Give the current bet
// Make the game speak the plural correctly
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithInt: amtBet]];
if (amtBet>1||amtBet==0) {
result= [NSString stringWithFormat:@" Your bet is %@ dollars", numberString];
AVSpeechUtterance* utterance = [AVSpeechUtterance speechUtteranceWithString:result];
utterance.rate = AVSpeechUtteranceMaximumSpeechRate/6;
self.synthesizer.delegate=self;
if (voiceOn) {
[self.pocketsphinxController suspendRecognition];
}
[self.synthesizer speakUtterance:utterance];
}
else {
result= [NSString stringWithFormat:@" Your bet is %@ dollar", numberString];
AVSpeechUtterance* utterance = [AVSpeechUtterance speechUtteranceWithString:result];
utterance.rate = AVSpeechUtteranceMaximumSpeechRate/6;
self.synthesizer.delegate=self;
if (voiceOn) {
[self.pocketsphinxController suspendRecognition];
}
[self.synthesizer speakUtterance:utterance];
}
// Play the background casino ambience sounds - low volume in infinite loop
if (!voiceOn) {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"casino"
ofType:@"mp3"]];
audioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audioPlayer1.delegate=self;
[audioPlayer1 prepareToPlay];
[audioPlayer1 setVolume:.15];
[audioPlayer1 setNumberOfLoops: -1];
[audioPlayer1 play];
}
}