Skip to content

Commit

Permalink
update Watson STT and TTS, chapter 3 and following, to support the ne…
Browse files Browse the repository at this point in the history
…w Watson API
  • Loading branch information
Bob Dill committed Nov 6, 2018
1 parent ba30e53 commit a10545b
Show file tree
Hide file tree
Showing 45 changed files with 89,714 additions and 50,760 deletions.
36 changes: 17 additions & 19 deletions Chapter03/Documentation/answers/speech_to_text_complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
var extend = require('extend');
var watson = require('watson-developer-cloud');
var vcapServices = require('vcap_services');
var request = require('request');

// load in the environment data for our application
var config = require('../../env.json');
Expand All @@ -28,24 +29,21 @@ var config = require('../../env.json');
* @param {NodeJS Request Object} req - provides information about the inbound request
* @param {NodeJS Response Object} res - this is how we respond back to the browser
*/
exports.token = function(req, res) {
// the extend function adds additional information into our credentials from within the
// Watson and Bluemix operating environment
var sttConfig = extend(config.speech_to_text, vcapServices.getCredentials('speech_to_text'));
// request authorization to access the service
var sttAuthService = watson.authorization(sttConfig);

// now that we're authenticated, get the token
sttAuthService.getToken({
url: sttConfig.url
}, function(err, token) {
if (err) {
// send an error back if we cannot retrieve the token successfully
console.log('Error retrieving token: ', err);
res.status(500).send('Error retrieving token'+ReferenceError);
return;
}
// if we're successful, then send the new token back to the browser
res.send(token);
exports.stt_token = function(req, res) {
var methodName = 'stt_token';
// The following three lines translate the curl request provided by IBM into a nodeJS request format so that the token can be retrieved by your server code.
var form = { grant_type: 'urn:ibm:params:oauth:grant-type:apikey', apikey: config.speech_to_text.apikey }
var headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' };
var options = { url: 'https://iam.bluemix.net/identity/token', method: 'POST', form: form, headers: headers };

// get the new token
request(options, function (error, response, body)
{
if (!error && response.statusCode == 200)
// send the token back as the 'success' item in the returned json object
{ res.send({success: JSON.parse(body).access_token}); }
else
// send the failure message back as the 'failed' item in the returned json object.
{ console.log(methodName+' error: ', error); res.send({failed: error.message}) }
});
}
32 changes: 20 additions & 12 deletions Chapter03/Documentation/answers/z2c-speech_complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,33 @@
/*
** z2c-speech.js
*/

var stt_token;
/**
* initPage is called by the browser once all files have loaded from the server.
* This is specified in the index.html file via the <body onLoad='initPage'> statement
*/
function initPage ()
{
var methodName = 'initPage';
// define the variables we need to access the microphone and stop icons in the web page
var _mic = $('#microphone'); var _stop = $("#stop");
$.when($.get('/api/speech-to-text/token')).done(
function(_token)
{ if (_token.success != 'undefined')
{stt_token = _token.success; console.log(methodName+' stt_token request succeeded.');}
else
{stt_token = null; console.log(methodName+' token request failed with: ',_token.failed);}
}
);

// start things off by enabling the microphone button and disabling the stop recording button
_mic.addClass("mic_enabled");
_stop.addClass("mic_disabled");

// Identify what to do when the microphone button has been clicked
_mic.on("click", function ()
{
var _className = this.className;
// if the microphone button is enabled, then do the following.
// otherwise, ignore the mouse button click
if(this.className == "mic_enabled")
Expand All @@ -50,18 +61,15 @@ function initPage ()
// I'll leave it to you to make this change, it's pretty simple
// and will make your app run more smoothly
//
$.when($.get('/api/speech-to-text/token')).done(
function (token) {
// the stream is what comes in from the microphone
stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
// it needs the token received from the server
token: token,
// and the outputElement is the html element defined with an id="speech" statement
outputElement: '#speech' // CSS selector or DOM Element
});
// if there's an error in this process, log it to the browser console.
stream.on('error', function(err) { console.log(err); });
// the stream is what comes in from the microphone
stream = WatsonSpeech.SpeechToText.recognizeMicrophone({
// it needs the token received from the server
access_token: stt_token,
// and the outputElement is the html element defined with an id="speech" statement
outputElement: '#speech' // CSS selector or DOM Element
});
// if there's an error in this process, log it to the browser console.
stream.on('error', function(err) { console.log(err); });
}
});

Expand Down
Loading

0 comments on commit a10545b

Please sign in to comment.