Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Add Speech to Text service
Browse files Browse the repository at this point in the history
This commit adds Speech to Text capabilities to the chat application. In particular, it streams microphone audio to the Speech to Text service when the microphone button is touched. Interim transcription results from the service are used to set the text field as they are received. Streaming ends when the microphone button is released by the user.
  • Loading branch information
glennrfisher committed Jul 19, 2016
1 parent 292436c commit 5bfa988
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ChatApp/ChatApp/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ @interface ViewController ()
@property (strong, nonatomic) Messages *messages;
@property (strong, nonatomic) ConversationBridge *conversation;
@property (strong, nonatomic) TextToSpeechBridge *textToSpeech;
@property (strong, nonatomic) SpeechToTextBridge *speechToText;

@end

Expand All @@ -45,9 +46,13 @@ - (void)viewDidLoad
[microphoneButton setImage:microphoneImage forState:UIControlStateNormal];
microphoneButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.inputToolbar.contentView.leftBarButtonItem = microphoneButton;

[microphoneButton addTarget:self action:@selector(didPressMicrophoneButton:) forControlEvents:UIControlEventTouchDown];
[microphoneButton addTarget:self action:@selector(didReleaseMicrophoneButton:) forControlEvents:UIControlEventTouchUpInside];

self.conversation = [[ConversationBridge alloc] init];
self.textToSpeech = [[TextToSpeechBridge alloc] init];
self.speechToText = [[SpeechToTextBridge alloc] init];

[self.conversation startConversation:nil success:^(NSString *response){
[self didReceiveConversationResponse:response];
Expand All @@ -70,6 +75,19 @@ - (void)didReceiveConversationResponse:(NSString *)response
[self.textToSpeech synthesize:response];
}

- (void)didPressMicrophoneButton:(UIButton *)sender
{
[self.speechToText startTranscribing:^(NSString *transcript) {
self.inputToolbar.contentView.textView.text = transcript;
[self.inputToolbar toggleSendButtonEnabled];
}];
}

- (void)didReleaseMicrophoneButton:(UIButton *)sender
{
[self.speechToText stopTranscribing:nil];
}

#pragma mark - Custom menu actions for cells

- (void)didReceiveMenuWillShowNotification:(NSNotification *)notification
Expand Down
29 changes: 29 additions & 0 deletions ChatApp/ChatApp/WatsonBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Foundation
import AVFoundation
import TextToSpeechV1
import ConversationV1
import SpeechToTextV1

class TextToSpeechBridge: NSObject {

Expand Down Expand Up @@ -61,3 +62,31 @@ class ConversationBridge: NSObject {
}
}
}

class SpeechToTextBridge: NSObject {

private var stopStreaming: SpeechToText.StopStreaming?
private let speechToText = SpeechToText(
username: credentials["SpeechToTextUsername"]!,
password: credentials["SpeechToTextPassword"]!
)

func startTranscribing(success: (String -> Void)) {
var settings = TranscriptionSettings(contentType: .L16(rate: 44100, channels: 1))
settings.continuous = false
settings.interimResults = true

let failure = { (error: NSError) in print(error) }
stopStreaming = speechToText.transcribe(settings, failure: failure) { results in
if let transcript = results.last?.alternatives.last?.transcript {
success(transcript)
}
}
}

func stopTranscribing(success: (Void -> Void)? = nil) {
stopStreaming?()
stopStreaming = nil
success?()
}
}

0 comments on commit 5bfa988

Please sign in to comment.