Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleSpeech sample code for iOS #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Don't save SpeechKit in repo -- have developer add it from distribution
ATTSpeechKit/

.svn

# Mac ignores
.DS_Store

# Rails ignores
*.log
tmp/

# Xcode ignores
build/
*.pbxuser
*.mode1v3
*.mode2v3
*.swp
*~.nib
*.pbxuser
*.perspective
*.perspectivev3

# Xcode 4.0 ignores
xcuserdata/
*.xcuserstate
19 changes: 19 additions & 0 deletions Speech/iOS/SimpleSpeech/Classes/SimpleSpeechAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SimpleSpeechAppDelegate.h
// 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 <UIKit/UIApplication.h>

@class SimpleSpeechViewController;

@interface SimpleSpeechAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SimpleSpeechViewController *viewController;

@end

46 changes: 46 additions & 0 deletions Speech/iOS/SimpleSpeech/Classes/SimpleSpeechAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SimpleSpeechAppDelegate.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 "SimpleSpeechAppDelegate.h"
#import "SimpleSpeechViewController.h"

@implementation SimpleSpeechAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
// Override point for customization after application launch.

// Hook up the UI from Interface Builder.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;
}

- (void) applicationDidBecomeActive: (UIApplication*) application
{
// Since the app has come to the foreground, (re-)initialize SpeechKit.
[viewController prepareSpeech];
}

- (void) dealloc
{
[viewController release];
[window release];
[super dealloc];
}


@end
25 changes: 25 additions & 0 deletions Speech/iOS/SimpleSpeech/Classes/SimpleSpeechViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SimpleSpeechViewController.h
// 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 <UIKit/UIViewController.h>
#import "ATTSpeechKit.h"


@interface SimpleSpeechViewController : UIViewController <ATTSpeechServiceDelegate>

@property (retain, nonatomic) IBOutlet UITextView* textView;
@property (retain, nonatomic) IBOutlet UIButton* talkButton;

// Initialize SpeechKit for this app.
- (void) prepareSpeech;

// Message sent by "Press to Talk" button in UI
- (IBAction) listen: (id) sender;

@end

159 changes: 159 additions & 0 deletions Speech/iOS/SimpleSpeech/Classes/SimpleSpeechViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
38 changes: 38 additions & 0 deletions Speech/iOS/SimpleSpeech/Classes/SpeechAuth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SpeechAuth.h
//
// 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 <Foundation/NSObject.h>

@class NSString, NSError;

/**
* Type of block called when SpeechAuth gets credential or fails.
* token will be the OAuth bearer token.
* If there's an problem authenticating, token will be nil and
* error will contain the error.
* TO DO: document the keys in error.userInfo
**/
typedef void (^SpeechAuthBlock)(NSString* token, NSError* error);

/**
* Fetches OAuth client credentials, calling a block when done.
**/
@interface SpeechAuth : NSObject {
}

/** Creates a SpeechAuth object with the given credentials. **/
+ (SpeechAuth*) authenticatorForService: (NSURL*) oauth_url
withId: (NSString*) client_id
secret: (NSString*) client_secret;

/*! Beging fetching the credentials. Will call block when done. !*/
- (void) fetchTo: (SpeechAuthBlock) block;

/*! Stop fetching. Once stopped, loading may not resume. !*/
- (void) cancel;

@end
Loading