-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSimpleSpeechViewController.m
executable file
·159 lines (131 loc) · 5.41 KB
/
SimpleSpeechViewController.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
// SimpleSpeechViewController.m
// SimpleSpeech
//
// Licensed by AT&T under 'Software Development Kit Tools Agreement' 2012.
// TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION: http://developer.att.com/sdk_agreement/
// Copyright 2012 AT&T Intellectual Property. All rights reserved.
// For more information contact [email protected] http://developer.att.com
#import "SimpleSpeechViewController.h"
#import "SpeechAuth.h"
// Replace the URLs below with the appropriate ones for your Speech API account.
#define SPEECH_URL @"https://api.att.com/rest/1/SpeechToText"
#define OAUTH_URL @"http://api.att.com/oauth/token"
#error Add code to unobfuscate your Speech API credentials in the macros below, then delete this line.
#define API_KEY MY_UNOBFUSCATOR(my_obfuscated_api_key)
#define API_SECRET MY_UNOBFUSCATOR(my_obfuscated_api_key)
@interface SimpleSpeechViewController ()
- (void) speechAuthFailed: (NSError*) error;
@end
@implementation SimpleSpeechViewController
@synthesize textView;
@synthesize talkButton;
#pragma mark -
#pragma mark Lifecyle
- (void) dealloc
{
[textView release];
[talkButton release];
[super dealloc];
}
// Initialize SpeechKit for this app.
- (void) prepareSpeech
{
// Access the SpeechKit singleton.
ATTSpeechService* speechService = [ATTSpeechService sharedSpeechService];
// Point to the SpeechToText API.
speechService.recognitionURL = [NSURL URLWithString: SPEECH_URL];
// Hook ourselves up as a delegate so we can get called back with the response.
speechService.delegate = self;
// Use default speech UI.
speechService.showUI = YES;
// Choose the speech recognition package.
speechService.speechContext = @"BusinessSearch";
// Start the OAuth background operation, disabling the Talk button until
// it's done.
talkButton.enabled = NO;
[[SpeechAuth authenticatorForService: [NSURL URLWithString: OAUTH_URL]
withId: API_KEY secret: API_SECRET]
fetchTo: ^(NSString* token, NSError* error) {
if (token) {
speechService.bearerAuthToken = token;
talkButton.enabled = YES;
}
else
[self speechAuthFailed: error];
}];
// Wake the audio components so there is minimal delay on the first request.
[speechService prepare];
}
#pragma mark -
#pragma mark Actions
// Perform the action of the "Push to talk" button
- (IBAction) listen: (id) sender
{
NSLog(@"Starting speech request");
// Start listening via the microphone.
ATTSpeechService* speechService = [ATTSpeechService sharedSpeechService];
[speechService startWithMicrophone];
}
#pragma mark -
#pragma mark Speech Service Delegate Methods
- (void) speechServiceSucceeded: (ATTSpeechService*) speechService
{
NSLog(@"Speech service succeeded");
// Extract the needed data from the SpeechService object:
// For raw bytes, read speechService.responseData.
// For a JSON tree, read speechService.responseDictionary.
// For the n-best ASR strings, use speechService.responseStrings.
// In this example, use the ASR strings.
// There can be 0 strings, 1 empty string, or 1 non-empty string.
// Display the recognized text in the interface is it's non-empty,
// otherwise have the user try again.
NSArray* nbest = speechService.responseStrings;
NSString* recognizedText = @"";
if (nbest != nil && nbest.count > 0)
recognizedText = [nbest objectAtIndex: 0];
if (recognizedText.length) // non-empty?
[self.textView setText: recognizedText];
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"Didn't recognize speech"
message: @"Please try again."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void) speechService: (ATTSpeechService*) speechService
failedWithError: (NSError*) error
{
if ([error.domain isEqualToString: ATTSpeechServiceErrorDomain]
&& (error.code == ATTSpeechServiceErrorCodeCanceledByUser)) {
NSLog(@"Speech service canceled");
// Nothing to do in this case
return;
}
NSLog(@"Speech service had an error: %@", error);
UIAlertView* alert = [[UIAlertView alloc] initWithTitle: @"An error occurred"
message: @"Please try again later."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
#pragma mark -
#pragma mark OAuth
/* The SpeechAuth authentication failed. */
- (void) speechAuthFailed: (NSError*) error
{
NSLog(@"OAuth error: %@", error);
UIAlertView* alert =
[[UIAlertView alloc] initWithTitle: @"Speech Unavailable"
message: @"This app was rejected by the speech service. Contact the developer for an update."
delegate: self
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
@end