Skip to content

Commit 4a58461

Browse files
committed
Added STT Streaming options
1 parent 6feddd0 commit 4a58461

File tree

5 files changed

+98
-39
lines changed

5 files changed

+98
-39
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ Node-RED Watson Nodes for IBM Cloud
77

88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

10+
1011
### New in version 0.6.6
11-
- Mute option for STT Node warning status when running in Streaming mode
12+
- Added Mute option for STT Node warning status when running in Streaming mode
13+
- Discard option for STT Node Streaming listening events.
14+
- Added Auto-Connect mode for STT Node - Will keep connection to service alive,
15+
else connection is restored on demand.
16+
- Fix to Discovery Query Builder Node to return schema options.
1217

1318
### New in version 0.6.5
1419
- Endpoint can now be specified in Natural Language Classifier Node

services/discovery/v1-query-builder.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
// can be obtained from the service.
184184
disQB.hideAll = function() {
185185
$('#credentials-not-found').show();
186-
cv1wm.hideSelectedFields(fields);
186+
//disQB.hideSelectedFields(fields);
187187
$('#something-went-wrong').hide();
188188
$('#node-input-environment').parent().hide();
189189
$('#node-input-collection').parent().hide();
@@ -609,7 +609,7 @@
609609

610610

611611
// This is the on edit prepare function, which will be invoked everytime the dialog is shown.
612-
function oneditprepare() {
612+
function disQBoneditprepare() {
613613
disQB.hideAll();
614614
disQB.restoreHidden();
615615

@@ -620,6 +620,7 @@
620620
$('.credentials').toggle(!service);
621621
// for some reason the getJSON resets the vars so need to restoreHidden again
622622
// so again.
623+
disQB.have_credentials = true;
623624
disQB.restoreHidden();
624625
if (!service) {
625626
// no bound service, so check local settings to see if they are valid
@@ -637,7 +638,7 @@
637638
$('#credentials-check').hide();
638639
})
639640
}
640-
// credentials can be set during the oneditprepare process, so check again.
641+
// credentials can be set during the disQBoneditprepare process, so check again.
641642
if (disQB.have_credentials || disQB.using_local_credentials)
642643
{
643644
$('#credentials-check').hide();
@@ -654,7 +655,7 @@
654655
}
655656

656657
// Save the values in the dyanmic lists to the hidden fields.
657-
function oneditsave(){
658+
function disQoneditsave(){
658659
disQB.environment_selected = $('#node-input-environment').val();
659660
$('#node-input-environmenthidden').val(disQB.environment_selected);
660661

@@ -736,8 +737,8 @@
736737
labelStyle: function() {
737738
return this.name ? "node_label_italic" : "";
738739
},
739-
oneditprepare: oneditprepare,
740-
oneditsave: oneditsave
740+
oneditprepare: disQBoneditprepare,
741+
oneditsave: disQoneditsave
741742
});
742743
})();
743744
</script>

services/discovery/v1-query-builder.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,16 @@ module.exports = function(RED) {
4343
username: sUsername ? sUsername : req.query.un,
4444
password: sPassword ? sPassword : req.query.pwd,
4545
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
46-
version_date: '2017-09-01',
46+
version_date: '2017-11-07',
4747
headers: {
4848
'User-Agent': pkg.name + '-' + pkg.version
4949
}
5050
});
5151

52-
console.log('Getting Discovery Environments');
5352
discovery.getEnvironments({}, function(err, response) {
5453
if (err) {
55-
console.log(err);
5654
res.json(err);
5755
} else {
58-
console.log('Returning Envrionments');
5956
res.json(response.environments ? response.environments : response);
6057
}
6158
});
@@ -67,7 +64,7 @@ module.exports = function(RED) {
6764
username: sUsername ? sUsername : req.query.un,
6865
password: sPassword ? sPassword : req.query.pwd,
6966
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
70-
version_date: '2017-09-01',
67+
version_date: '2017-11-07',
7168
headers: {
7269
'User-Agent': pkg.name + '-' + pkg.version
7370
}
@@ -92,7 +89,7 @@ module.exports = function(RED) {
9289
username: sUsername ? sUsername : req.query.un,
9390
password: sPassword ? sPassword : req.query.pwd,
9491
url: req.query.endpoint ? req.query.endpoint : sEndpoint,
95-
version_date: '2017-09-01',
92+
version_date: '2017-11-07',
9693
headers: {
9794
'User-Agent': pkg.name + '-' + pkg.version
9895
}
@@ -101,7 +98,9 @@ module.exports = function(RED) {
10198
discovery.query({
10299
environment_id: req.query.environment_id,
103100
collection_id: req.query.collection_id,
104-
query: 'text:a,text:ibm',
101+
//query: 'text:a,text:ibm',
102+
// Need a Query that will return some data!
103+
query: 'text:"Trump",text:"IBM",text:"Watson"',
105104
count: 1
106105
},
107106
function(err, response) {

services/speech_to_text/v1.html

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
<label for="node-input-streaming-mute" style="width: 70%;"> Mute in Streaming Mode</label>
9595
</div>
9696

97+
<div class="form-row">
98+
<label>&nbsp;</label>
99+
<input type="checkbox" id="node-input-discard-listening" style="display: inline-block; width: auto; vertical-align: top;">
100+
<label for="node-input-discard-listening" style="width: 70%;"> Discard Listening events from STT</label>
101+
</div>
102+
103+
<div class="form-row">
104+
<label>&nbsp;</label>
105+
<input type="checkbox" id="node-input-auto-connect" style="display: inline-block; width: auto; vertical-align: top;">
106+
<label for="node-input-auto-connect" style="width: 70%;"> Attempt auto connect on connection close</label>
107+
</div>
108+
97109
<div class="form-row">
98110
<label>&nbsp;</label>
99111
<input type="checkbox" id="node-input-speakerlabels"style="display: inline-block; width: auto; vertical-align: top;"></input>
@@ -155,6 +167,7 @@
155167
<code>start</code> or <code>stop</code> or an audio blob. No token is needed
156168
as the node takes care of that step.</p>
157169
<p>>The Mute option allows for the supression of session timeout messages.</p>
170+
<p>>The Discard Listening option allows for listening events from STT to be discarded.</p>
158171
<p>For more information about the Speech To Text service, read the <a href="https://www.ibm.com/watson/services/speech-to-text/">documentation</a>.</p>
159172
</script>
160173

@@ -193,41 +206,61 @@
193206
return self.indexOf(value) === index;
194207
}
195208

209+
stt.showSelectedFields = function(fields) {
210+
for (i = 0; i < fields.length; i++) {
211+
$(fields[i]).parent().show();
212+
}
213+
}
214+
215+
stt.hideSelectedFields = function(fields) {
216+
for (i = 0; i < fields.length; i++) {
217+
$(fields[i]).parent().hide();
218+
}
219+
}
220+
221+
196222
// Function to be used at the start, as don't want to expose any fields, unless the models are
197223
// available. The models can only be fetched if the credentials are available.
198224
stt.hideEverything = function () {
199225
if (!stt.models) {
226+
var fields = [];
200227
$('#credentials-not-found').show();
201-
$('label#node-label-message').parent().hide();
202-
$('input#node-input-alternatives').parent().hide();
203-
$('input#node-input-speakerlabels').parent().hide();
204-
$('input#node-input-smartformatting').parent().hide();
205-
$('select#node-input-lang').parent().hide();
206-
$('select#node-input-band').parent().hide();
207-
$('select#node-input-langcustom').parent().hide();
228+
229+
fields.push('#node-label-message'
230+
+ ', #node-input-alternatives'
231+
+ ', #node-input-speakerlabels'
232+
+ ', #node-input-smartformatting'
233+
+ ', #node-input-lang'
234+
+ ', #node-input-band'
235+
+ ', #node-input-langcustom');
236+
stt.hideSelectedFields(fields);
208237
}
209238
}
210239

211240
// Check if there is a model then can show the fields.
212241
// available. The models can only be fetched if the credentials are available.
213242
stt.VisibilityCheck = function () {
243+
var showFields = [];
244+
var hideFields = [];
214245
if (stt.models) {
215-
$('label#node-label-message').parent().hide();
216-
$('input#node-input-alternatives').parent().show();
217-
$('input#node-input-speakerlabels').parent().show();
218-
$('input#node-input-smartformatting').parent().show();
219-
$('select#node-input-lang').parent().show();
220-
//$('select#node-input-langcustom').parent().show();
221-
$('select#node-input-band').parent().show();
246+
showFields.push('#node-input-alternatives'
247+
+ ', #node-input-speakerlabels'
248+
+ ', #node-input-smartformatting'
249+
+ ', #node-input-lang'
250+
+ ', #node-input-band');
251+
252+
hideFields.push('#node-label-message');
222253
} else {
223-
$('label#node-label-message').parent().hide();
224-
$('input#node-input-alternatives').parent().hide();
225-
$('input#node-input-speakerlabels').parent().hide();
226-
$('input#node-input-smartformatting').parent().hide();
227-
$('select#node-input-lang').parent().hide();
228-
$('select#node-input-langcustom').parent().hide();
229-
$('select#node-input-band').parent().hide();
254+
hideFields.push('#node-label-message'
255+
+ ', #node-input-alternatives'
256+
+ ', #node-input-speakerlabels'
257+
+ ', #node-input-smartformatting'
258+
+ ', #node-input-langcustom'
259+
+ ', #node-input-lang'
260+
+ ', #node-input-band');
230261
}
262+
stt.hideSelectedFields(hideFields);
263+
stt.showSelectedFields(showFields);
231264
}
232265

233266

@@ -372,6 +405,20 @@
372405
}
373406
});
374407

408+
$('#node-input-streaming-mode').change(function () {
409+
var fields = [];
410+
fields.push('#node-input-streaming-mute'
411+
+ ', #node-input-discard-listening'
412+
+ ', #node-input-auto-connect');
413+
414+
var checked = $('#node-input-streaming-mode').prop('checked')
415+
if (checked) {
416+
stt.showSelectedFields(fields);
417+
} else {
418+
stt.hideSelectedFields(fields);
419+
}
420+
});
421+
375422
$('#node-input-flushcache').click(function () {
376423
stt.flushCache();
377424
});
@@ -550,6 +597,7 @@
550597
'payload-response' :{value: false},
551598
'streaming-mode' :{value: false},
552599
'streaming-mute' :{value: true},
600+
'discard-listening' :{value: false},
553601
'default-endpoint' :{value: true},
554602
'service-endpoint' :{value: 'https://stream.watsonplatform.net/speech-to-text/api'}
555603
},

services/speech_to_text/v1.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function (RED) {
2828
payloadutils = require('../../utilities/payload-utils'),
2929
sttV1 = require('watson-developer-cloud/speech-to-text/v1'),
3030
authV1 = require('watson-developer-cloud/authorization/v1'),
31-
muteMode = true;
31+
muteMode = true, discardMode = false, autoConnect = true,
3232
username = '', password = '', sUsername = '', sPassword = '',
3333
endpoint = '',
3434
sEndpoint = 'https://stream.watsonplatform.net/speech-to-text/api',
@@ -135,6 +135,8 @@ module.exports = function (RED) {
135135
var message = '';
136136

137137
muteMode = config['streaming-mute'];
138+
discardMode = config['discard-listening'];
139+
autoConnect = config['auto-connect'];
138140

139141
if (!config.lang) {
140142
message = 'Missing audio language configuration, unable to process speech.';
@@ -412,8 +414,9 @@ module.exports = function (RED) {
412414
} else if (d && d.state && 'listening' === d.state) {
413415
socketListening = true;
414416
// Added for verbose testing
415-
// SC - Web Sockect Test
416-
//node.send(newMsg);
417+
if (!discardMode) {
418+
node.send(newMsg);
419+
}
417420
//resolve();
418421
} else {
419422
node.send(newMsg);
@@ -426,9 +429,12 @@ module.exports = function (RED) {
426429
socketListening = false;
427430
// console.log('STT Socket disconnected');
428431
if (!muteMode) {
432+
var newMsg = {payload : 'STT Connection Close Event'};
429433
payloadutils.reportError(node,newMsg,'STT Socket Connection closed');
430434
}
431-
setTimeout(connectIfNeeded, 1000);
435+
if (autoConnect) {
436+
setTimeout(connectIfNeeded, 1000);
437+
}
432438
});
433439

434440
ws.on('error', (err) => {

0 commit comments

Comments
 (0)