-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHowdyTalkViewController.m
593 lines (414 loc) · 18.1 KB
/
HowdyTalkViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//
// HowdyTalkViewController.m
// Howdy
//
// Created by Edan Lichtenstein on 3/3/14.
// Copyright (c) 2014 Edan Lichtenstein. All rights reserved.
//
#import "HowdyTalkViewController.h"
#import <MessageUI/MessageUI.h>
#import <OpenEars/LanguageModelGenerator.h>
#import <Parse/Parse.h>
#import "LogInViewController.h"
#import "Constants.h"
#import <FAKFontAwesome.h>
#import "UIColor+Colors.h"
//#import <UIView+DebugQuickLook.m>
@interface HowdyTalkViewController () <MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic, strong) AVAudioPlayer *player;
@property (nonatomic, strong) NSTimer *recordingTimer;
@property (strong, nonatomic) IBOutlet UILabel *howdyLabel;
@property (strong, nonatomic) IBOutlet UIView *playBackView;
@property (strong, nonatomic) IBOutlet UIImageView *playBackImageView;
- (IBAction)playBackButtonTapped:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIView *emailVMView;
@property (strong, nonatomic) IBOutlet UIImageView *emailVMImageView;
- (IBAction)emailVMButtonTapped:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UIView *pushVMView;
@property (strong, nonatomic) IBOutlet UIImageView *pushVMImageView;
- (IBAction)pushVMButtonTapped:(UIButton *)sender;
@property (strong, nonatomic) IBOutlet UILabel *recordLabel;
@property (strong, nonatomic) IBOutlet UIImageView *microphoneImageView;
@end
@implementation HowdyTalkViewController
@synthesize pocketsphinxController;
@synthesize openEarsEventsObserver;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self createUI];
PFUser *currentUser = [PFUser currentUser];
NSLog(@"%@", currentUser);
// PFUser *user = [PFUser logInWithUsername:@"Leroy Brown" password:@"Gene"];
// PFUser *user = [PFUser logInWithUsername:@"Edan" password:@"jam"];
if (currentUser) {
// do stuff with the user
} else {
PFUser *user = [PFUser user];
// user.username = @"Leroy Brown";
// user.password = @"Gene";
// user.email = @"[email protected]";
user.username = @"Edan";
user.password = @"jam";
user.email = @"[email protected]";
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Hooray! Let them use the app now.
} else {
NSString *errorString = [error userInfo][@"error"];
// Show the errorString somewhere and let the user try again.
}
}];
// NSString *storyboardName = @"Main";
// UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
// UIViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
// [self.navigationController pushViewController:loginVC animated:YES];
}
[self associateDeviceWithUser];
// Not yet ready for these two below.
// [self setupNavigationBar];
// [self openEars];
}
- (void) viewDidAppear:(BOOL)animated
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Button Actions
- (IBAction)howdyButtonPressed:(UIButton *)sender{
if (sender.selected) {
[self stopRecording];
} else {
[self startRecording];
}
sender.selected = !sender.selected;
}
- (IBAction)playBackButtonTapped:(UIButton *)sender {
// [SimpleAudioPlayer playFile:_recorder.url.description];
NSError* error = nil;
//Makes audio come out of main speaker
OSStatus result;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
result = AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:_recorder.url error:&error];
_player.volume = 1.0f;
_player.numberOfLoops = 0;
_player.delegate = self;
[_player play];
NSLog(@"duration: %f", _player.duration);
}
- (IBAction)emailVMButtonTapped:(UIButton *)sender {
[self emailVoiceMail];
}
- (IBAction)pushVMButtonTapped:(UIButton *)sender {
[self pushVoiceMessage];
}
- (IBAction)longPressed:(UILongPressGestureRecognizer *)gesture {
if(UIGestureRecognizerStateBegan == gesture.state) {
NSLog(@"Gesture state began");
self.recordLabel.layer.borderColor = [UIColor purpleOcean].CGColor;
[self startRecording];
}
if(UIGestureRecognizerStateChanged == gesture.state) {
}
if(UIGestureRecognizerStateEnded == gesture.state) {
NSLog(@"Gesture state ended");
self.recordLabel.layer.borderColor = [UIColor purpleLight].CGColor;
[self stopRecording];
}
}
- (void) recordingTimerUpdate:(id) sender
{
NSLog(@"%f %@", _recorder.currentTime, sender);
self.recordingLengthLabel.text = [NSString stringWithFormat:@"%.2f", _recorder.currentTime];
}
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"did finish playing %d", flag);
}
-(void) associateDeviceWithUser
{
PFInstallation *installation = [PFInstallation currentInstallation];
installation[@"user"] = [PFUser currentUser];
[installation saveInBackground];
}
#pragma mark Recording
-(void) startRecording
{
NSDictionary* recorderSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,
[NSNumber numberWithInt:44100],AVSampleRateKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
nil];
NSError* error = nil;
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *path = [applicationDocumentsDirectory URLByAppendingPathComponent:@"tmp.caf"];
self.recorder = [[AVAudioRecorder alloc] initWithURL:path settings:recorderSettings error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
// UInt32 doChangeDefault = 1;
//AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefault), &doChangeDefault);
self.recorder.delegate = self;
[self.recorder record];
self.recordingTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(recordingTimerUpdate:) userInfo:nil repeats:YES];
[_recordingTimer fire];
self.recordingLengthLabel.backgroundColor = [UIColor redColor];
}
-(void) stopRecording
{
self.recordingLengthLabel.backgroundColor = [UIColor clearColor];
[self.recorder stop];
[self.recordingTimer invalidate];
self.recordingTimer = nil;
NSLog(@"%@", self.recorder.url);
}
#pragma mark - Parse Communication
-(void) pushVoiceMessage
{
NSURL *shareUrl = self.recorder.url;
NSData *dataToSend = [[NSData alloc] initWithContentsOfURL:shareUrl];
PFObject *voiceMail = [PFObject objectWithClassName:@"Voice_Mail"];
[voiceMail setObject:@"Edan" forKey:@"Name"];
[voiceMail setObject:@101 forKey:@"UniqueID"];
[voiceMail setObject:dataToSend forKey:@"Audio_File"];
[voiceMail saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded){
NSLog(@"Voice Mail Uploaded with Object id %@",[voiceMail objectId]);
[self sendPushWithID:[voiceMail objectId]];
}
else{
NSString *errorString = [[error userInfo] objectForKey:@"error"];
NSLog(@"PFObject Sending Error: %@", errorString);
}
}];
}
-(void) sendPushWithID: (NSString *)voiceMailID
{
NSDictionary *pushDataDictionary = [NSDictionary dictionaryWithObjectsAndKeys:voiceMailID, @"VoiceMailID", nil];
NSLog(@"%@", voiceMailID);
PFQuery *userQuery = [PFUser query];
// [userQuery whereKey:@"username" containsString:@"Leroy Brown"];
[userQuery whereKey:@"username" containsString:@"Edan"];
//NSLog(@"%@",[userQuery getFirstObject]);
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"user" matchesQuery:userQuery];
//NSLog(@"%@", [pushQuery getFirstObject]);
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our Installation query
[push setMessage:@"Testing the How to the dY."];
[push setData:pushDataDictionary];
[push sendPushInBackground];
NSLog(@"Sent Push");
}
#pragma mark - Emailing Methods
-(void) emailVoiceMail
{
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *filepath = [documentsDirectory stringByAppendingPathComponent:memo.memoUrl];
// NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filepath];
NSURL *shareUrl = self.recorder.url;
NSData *dataToSend = [[NSData alloc] initWithContentsOfURL:shareUrl];
// [fileURL release];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Voice Mail for you"];
// Set up recipients
NSArray *toRecipients = nil;
NSArray *ccRecipients = nil;
NSArray *bccRecipients = nil;
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
[picker setMessageBody:@"" isHTML:YES];
[picker addAttachmentData:dataToSend mimeType:@"audio/x-caf" fileName: @"MyMessageToYou.caf"];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark - UI setup
-(void) createUI
{
[self setupControllerButtons];
[self drawRecordLabel];
[self drawHowdyLabel];
[self drawTimerLabel];
}
-(void) setupNavigationBar
{
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self;
searchBar.placeholder = @"Find a Contact";
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
}
-(void) setupControllerButtons
{
//Create and draw Icons
FAKFontAwesome *playIcon = [FAKFontAwesome playIconWithSize:50];
FAKFontAwesome *emailIcon = [FAKFontAwesome envelopeOIconWithSize:45];
FAKFontAwesome *iPhoneIcon = [FAKFontAwesome mobileIconWithSize:60];
playIcon.drawingBackgroundColor = [UIColor clearColor];
[playIcon addAttribute:NSForegroundColorAttributeName value:[UIColor purpleMagic]];
self.playBackImageView.image = [playIcon imageWithSize:self.playBackImageView.frame.size];
[self drawCircleInView:self.playBackView withColor:[UIColor purpleMagic]];
emailIcon.drawingBackgroundColor = [UIColor clearColor];
[emailIcon addAttribute:NSForegroundColorAttributeName value:[UIColor purpleLight]];
self.emailVMImageView.image = [emailIcon imageWithSize:self.emailVMImageView.frame.size];
[self drawCircleInView:self.emailVMView withColor:[UIColor purpleLight]];
iPhoneIcon.drawingBackgroundColor = [UIColor clearColor];
[iPhoneIcon addAttribute:NSForegroundColorAttributeName value:[UIColor purpleOcean]];
self.pushVMImageView.image = [iPhoneIcon imageWithSize:self.pushVMImageView.frame.size];
[self drawCircleInView:self.pushVMView withColor:[UIColor purpleOcean]];
}
-(void) drawCircleInView: (UIView *)view withColor:(UIColor *)color
{
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [view bounds].size.width, [view bounds].size.height)];
[circleLayer setPosition:CGPointMake([view bounds].size.width/2.0f,
[view bounds].size.height/2.0f)];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0.0f, 0.0f, [view bounds].size.width, [view bounds].size.height)];
[circleLayer setPath:[path CGPath]];
[circleLayer setStrokeColor:[color CGColor]];
[circleLayer setLineWidth:3.0f];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
[[view layer] addSublayer:circleLayer];
}
-(void)drawRecordLabel
{
FAKFontAwesome *microphoneIcon = [FAKFontAwesome microphoneIconWithSize:60];
[microphoneIcon addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
self.microphoneImageView.image = [microphoneIcon imageWithSize:CGSizeMake(150, 120)];
self.recordLabel.layer.borderColor = [UIColor purpleLight].CGColor;
self.recordLabel.layer.backgroundColor = [UIColor silverCool].CGColor;
self.recordLabel.layer.borderWidth = 5.0;
}
-(void)drawHowdyLabel
{
self.howdyLabel.textColor = [UIColor purpleMagic];
}
-(void)drawTimerLabel
{
self.recordingLengthLabel.textColor = [UIColor silverSimple];
}
#pragma mark - Open Ears
- (PocketsphinxController *)pocketsphinxController {
if (pocketsphinxController == nil) {
pocketsphinxController = [[PocketsphinxController alloc] init];
}
return pocketsphinxController;
}
- (OpenEarsEventsObserver *)openEarsEventsObserver {
if (openEarsEventsObserver == nil) {
openEarsEventsObserver = [[OpenEarsEventsObserver alloc] init];
}
return openEarsEventsObserver;
}
-(void) openEars
{
LanguageModelGenerator *lmGenerator = [[LanguageModelGenerator alloc] init];
NSArray *words = [NSArray arrayWithObjects:@"BEGIN", @"BREAK" @"POSTMAN", @"LEROY BROWN", nil];
NSString *name = @"NameIWantForMyLanguageModelFiles";
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"];
} else {
NSLog(@"Error: %@",[err localizedDescription]);
}
[self.openEarsEventsObserver setDelegate:self];
[self.pocketsphinxController startListeningWithLanguageModelAtPath:lmPath dictionaryAtPath:dicPath acousticModelAtPath:[AcousticModel pathToModel:@"AcousticModelEnglish"] languageModelIsJSGF:NO];
}
#pragma mark Open Ears Delegate Methods
- (void) pocketsphinxDidReceiveHypothesis:(NSString *)howdyCommand recognitionScore:(NSString *)recognitionScore utteranceID:(NSString *)utteranceID {
NSLog(@"The received hypothesis is %@ with a score of %@ and an ID of %@", howdyCommand, recognitionScore, utteranceID);
//self.heardWordList.text = (@"%@ \n", howdyCommand);
if ([howdyCommand isEqualToString:@"BEGIN"])
{
[self startRecording];
}
else if ([howdyCommand isEqualToString:@"END"])
{
[self stopRecording];
}
else if ([howdyCommand isEqualToString:@"POSTMAN"])
{
[self emailVoiceMail];
}
}
- (void) pocketsphinxDidStartCalibration {
NSLog(@"Pocketsphinx calibration has started.");
}
- (void) pocketsphinxDidCompleteCalibration {
NSLog(@"Pocketsphinx calibration is complete.");
}
- (void) pocketsphinxDidStartListening {
NSLog(@"Pocketsphinx is now listening.");
}
- (void) pocketsphinxDidDetectSpeech {
NSLog(@"Pocketsphinx has detected speech.");
}
- (void) pocketsphinxDidDetectFinishedSpeech {
NSLog(@"Pocketsphinx has detected a period of silence, concluding an utterance.");
}
- (void) pocketsphinxDidStopListening {
NSLog(@"Pocketsphinx has stopped listening.");
}
- (void) pocketsphinxDidSuspendRecognition {
NSLog(@"Pocketsphinx has suspended recognition.");
}
- (void) pocketsphinxDidResumeRecognition {
NSLog(@"Pocketsphinx has resumed recognition.");
}
- (void) pocketsphinxDidChangeLanguageModelToFile:(NSString *)newLanguageModelPathAsString andDictionary:(NSString *)newDictionaryPathAsString {
NSLog(@"Pocketsphinx is now using the following language model: \n%@ and the following dictionary: %@",newLanguageModelPathAsString,newDictionaryPathAsString);
}
- (void) pocketSphinxContinuousSetupDidFail { // This can let you know that something went wrong with the recognition loop startup. Turn on OPENEARSLOGGING to learn why.
NSLog(@"Setting up the continuous recognition loop has failed for some reason, please turn on OpenEarsLogging to learn more.");
}
- (void) testRecognitionCompleted {
NSLog(@"A test file that was submitted for recognition is now complete.");
}
@end