-
Notifications
You must be signed in to change notification settings - Fork 20
/
wave_conversation.js
494 lines (412 loc) · 15.4 KB
/
wave_conversation.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/**
/************************************************************************
* Control a NeoPixel LED unit and servo motor connected to a Raspberry Pi pin through voice commands
* Must run with root-level protection
* sudo node wave.js
Follow the instructions in XXX to
get the system ready to run this code.
*/
/************************************************************************
* Step #1: Configuring your Bluemix Credentials
************************************************************************
In this step, the audio sample (pipe) is sent to "Watson Speech to Text" to transcribe.
The service converts the audio to text and saves the returned text in "textStream"
*/
var pigpio = require('pigpio')
pigpio.initialize();
var watson = require('watson-developer-cloud');
var config = require('./config'); // gets our username and passwords from the config.js files
var speech_to_text = watson.speech_to_text({
username: config.STTUsername,
password: config.STTPassword,
version: config.version,
customization_id: config.STTCustomizationid // comment out this line if you are not using any customized language model
});
var fs = require('fs');
var exec = require('child_process').exec;
var conversation = watson.conversation({
username: config.ConUsername,
password: config.ConPassword,
version: 'v1',
version_date: '2016-07-11'
});
var text_to_speech = watson.text_to_speech({
username: config.TTSUsername,
password: config.TTSPassword,
version: 'v1'
});
var AudioContext = require('web-audio-api').AudioContext
context = new AudioContext
var _ = require('underscore');
/************************************************************************
* Step #2: Configuring the Microphone
************************************************************************
In this step, we configure your microphone to collect the audio samples as you talk.
See https://www.npmjs.com/package/mic for more information on
microphone input events e.g on error, startcomplete, pause, stopcomplete etc.
*/
// Initiate Microphone Instance to Get audio samples
var mic = require('mic');
var micInstance = mic({ 'rate': '44100', 'channels': '2', 'debug': false, 'exitOnSilence': 6 });
var micInputStream = micInstance.getAudioStream();
micInputStream.on('data', function(data) {
//console.log("Recieved Input Stream: " + data.length);
});
micInputStream.on('error', function(err) {
console.log("Error in Input Stream: " + err);
});
micInputStream.on('silence', function() {
// detect silence.
});
micInstance.start();
console.log("TJ is listening, you may speak now.");
/************************************************************************
* Step #3: Converting your Speech Commands to Text
************************************************************************
In this step, the audio sample is sent (piped) to "Watson Speech to Text" to transcribe.
The service converts the audio to text and saves the returned text in "textStream"
*/
var recognizeparams = {
content_type: 'audio/l16; rate=44100; channels=2',
interim_results: true,
smart_formatting: true
// model: 'en-US_BroadbandModel' // Specify your language model here
};
textStream = micInputStream.pipe(speech_to_text.createRecognizeStream(recognizeparams));
textStream.setEncoding('utf8');
/*********************************************************************
* Step #4: Parsing the Text
*********************************************************************
In this step, we parse the text to look for commands such as "ON" or "OFF".
You can say any variations of "lights on", "turn the lights on", "turn on the lights", etc.
You would be able to create your own customized command, such as "good night" to turn the lights off.
What you need to do is to go to parseText function and modify the text.
*/
/*********************************************************************
* Step #4: Parsing the Text and create a response
*********************************************************************
In this step, we parse the text to look for attention word and send that sentence
to watson conversation to get appropriate response. You can change it to something else if needed.
Once the attention word is detected,the text is sent to Watson conversation for processing. The response is generated by Watson Conversation and is sent back to the module.
*/
var conversationcontext = {} ; // Save information on conversation context/stage for continous conversation
textStream.on('data', function(str) {
console.log(' ===== Speech to Text ===== : ' + str); // print the text once received
var res = str ;
console.log("msg sent to conversation:" ,res);
conversation.message({
workspace_id: config.ConWorkspace,
input: {'text': res},
context: conversationcontext
}, function(err, response) {
if (err) {
console.log('error:', err);
} else {
conversationcontext = response.context ; //update conversation context
conversation_response = response.output.text[0] ;
if (conversation_response != undefined ){
var params = {
text: response.output.text[0],
voice: config.voice,
accept: 'audio/wav'
};
console.log("Result from conversation : " , conversation_response);
var matchedintent = response.intents[0].intent ; // intent with the highest confidence
var intentconfidence = response.intents[0].confidence ;
console.log("intents : " , response.intents) ;
if (intentconfidence > 0.5){
setLEDColor("green", Math.floor(intentconfidence * 255) ) ;
if(matchedintent == "dance"){
speak(conversation_response) ;
dance();
}else if(matchedintent == "wave"){
speak(conversation_response) ;
waveArm("wave") ;
} else if(matchedintent == "see"){
launchVision();
}else if(matchedintent == "off_topic"){
//launchVision();
} else {
speak(conversation_response) ;
}
}else{
setLEDColor("red", Math.floor(255) ) ;
setTimeout(function(){
setLEDColor("white", 255);
}, 800);
}
/*********************************************************************
Step #5: Speak out the response
*********************************************************************
In this step, we text is sent out to Watsons Text to Speech service and result is piped to wave file.
Wave files are then played using alsa (native audio) tool.
*/
}else {
setLEDColor("red", 255);
console.log("The response (output) text from your conversation is empty. Please check your conversation flow \n" + JSON.stringify( response))
}
}
})
});
textStream.on('error', function(err) {
console.log(' === Watson Speech to Text : An Error has occurred ===== \nYou may have exceeded your payload quota.') ; // handle errors
console.log(err + "\n Press <ctrl>+C to exit.") ;
});
function parseText(str){
var containsWaveArm = (str.indexOf("raise") >= 0 || str.indexOf("weave") >= 0 || str.indexOf("wave") >= 0 || str.indexOf("leave") >= 0 ) && ( str.indexOf("arm") >= 0) ;
var introduceYourself = str.indexOf("introduce") >= 0 && str.indexOf("yourself") >= 0 ;
var whatisYourname = str.indexOf("what") >= 0 && str.indexOf("your") >= 0 && str.indexOf("name") >= 0 ;
var canYouDance = str.indexOf("can") >= 0 && str.indexOf("you") >= 0 && str.indexOf("dance") >= 0 ;
if (containsWaveArm) {
speak("Ok, I will wave my arm. Just for you.");
waveArm("wave") ;
}else if (introduceYourself){
speak(" Hi, my name is TJ. I'm an open source project designed to help you access Watson Services in a fun way. You can 3D print me or laser cut me, then use one of my recipes to bring me to life. I can't wait to see what we do together. ");
}else if (whatisYourname){
speak(" My name is TJ. You can call me TJ Bot");
}else if (canYouDance){
dance();
}else{
if (str.length > 10){
speak("sorry, I haven't been taught to understand that.")
}
}
}
/*********************************************************************
* Step #5: Wave Arm
*********************************************************************
*/
var mincycle = 500; var maxcycle = 2300 ;
var dutycycle = mincycle;
var iswaving = false ;
// Setup software PWM on pin 26, GPIO7.
/**
* Wave the arm of your robot X times with an interval
* @return {[type]} [description]
*/
function waveArm(action) {
iswaving = true ;
var Gpio = pigpio.Gpio;
var motor = new Gpio(7, {mode: Gpio.OUTPUT});
//pigpio.terminate();
var times = 8 ;
var interval = 700 ;
if (action == "wave") {
var pulse = setInterval(function() {
motor.servoWrite(maxcycle);
setTimeout(function(){
if (motor != null) {
motor.servoWrite(mincycle);
}
}, interval/3);
if (times-- === 0) {
clearInterval(pulse);
if (!isplaying) {
setTimeout(function(){
micInstance.resume();
iswaving = false ;
setLEDColor("white", 255);
}, 500);
}
return;
}
}, interval);
}else {
motor.servoWrite(maxcycle);
setTimeout(function(){
motor.servoWrite(mincycle);
}, 400);
}
}
/*********************************************************************
* Step #6: Convert Text to Speech and Play
*********************************************************************
*/
var Sound = require('node-aplay');
var soundobject ;
var isspeaking = false ;
//speak("testing speaking")
function speak(textstring){
if (!isspeaking) {
micInstance.pause(); // pause the microphone while playing
var params = {
text: textstring,
voice: config.voice,
accept: 'audio/wav'
};
text_to_speech.synthesize(params).pipe(fs.createWriteStream('output.wav')).on('close', function() {
isspeaking = true ;
soundobject = new Sound("output.wav");
soundobject.play();
soundobject.on('complete', function () {
console.log('Done with playback! for ' + textstring + " iswaving " + iswaving);
if (!iswaving && !isplaying) {
micInstance.resume();
setLEDColor("white", 255)
}
isspeaking = false ;
});
});
}
}
/*********************************************************************
* Piece #7: Play a Song and dance to the rythm!
*********************************************************************
*/
var pcmdata = [] ;
var samplerate ;
var soundfile = "sounds/club.wav"
var threshodld = 0 ;
//decodeSoundFile(soundfile);
function decodeSoundFile(soundfile){
console.log("decoding mp3 file ", soundfile, " ..... ")
fs.readFile(soundfile, function(err, buf) {
if (err) throw err
context.decodeAudioData(buf, function(audioBuffer) {
console.log(audioBuffer.numberOfChannels, audioBuffer.length, audioBuffer.sampleRate, audioBuffer.duration);
pcmdata = (audioBuffer.getChannelData(0)) ;
samplerate = audioBuffer.sampleRate;
findPeaks(pcmdata, samplerate);
playsound(soundfile);
}, function(err) { throw err })
})
}
//dance();
function dance(){
//speak("Sure. I am decoding a sound file that I will dance to. This may take a couple of seconds.") ;
decodeSoundFile(soundfile);
}
var isplaying = false ;
function playsound(soundfile){
micInstance.pause();
isplaying = true ;
music = new Sound(soundfile);
music.play();
music.on('complete', function () {
console.log('Done with music playback! .. resuming mic');
isplaying = false;
setTimeout(function(){
micInstance.resume();
iswaving = false ;
}, 600);
});
}
function findPeaks(pcmdata, samplerate, threshold){
var interval = 0.05 * 1000 ; index = 0 ;
var step = Math.round( samplerate * (interval/1000) );
var max = 0 ; var prevmax = 0 ; var prevdiffthreshold = 0.3 ;
//loop through song in time with sample rate
var samplesound = setInterval(function() {
if (index >= pcmdata.length) {
clearInterval(samplesound);
console.log("finished sampling sound")
iswaving = false ;
setLEDColor("white", 255);
return;
}
for(var i = index; i < index + step ; i++){
max = pcmdata[i] > max ? pcmdata[i].toFixed(1) : max ;
}
// Spot a significant increase? Wave Arm
if(max-prevmax >= prevdiffthreshold){
waveArm("dance");
var colors = Object.keys(colorPalette);
var randIdx = Math.floor(Math.random() * colors.length);
var randColor = colors[randIdx];
setLEDColor( randColor, (max-prevmax) * 255)
}
prevmax = max ; max = 0 ; index += step ;
}, interval,pcmdata);
}
/*********************************************************************
* Piece #8: Vision Recognition
*********************************************************************
*/
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');
var watson = require('watson-developer-cloud');
var fs = require('fs');
var config = require("./config");
var child_process = require('child_process');
var visual_recognition = new VisualRecognitionV3({
api_key: config.VisionKey,
version_date: config.VisionVersion
});
var snapinterval = 20000 ; // take a picture every X milliseconds
var i = 0 ;
/**
* Process Images every X seconds
* @return {null} null
*/
function launchVision(){
var filename = 'photos/pic_'+i+'.jpg';
//var args = ['-vf', '-hf','-w', '960', '-h', '720', '-o', filename, '-t', '1'];
var args = ['-w', '960', '-h', '720', '-o', filename, '-t', '5'];
var spawn = child_process.spawn('raspistill', args);
spawn.on('exit', function(code) {
console.log('A photo is saved as '+filename+ ' with exit code, ' + code);
let timestamp = Date.now();
processImage(filename)
i++;
});
}
/**
* [processImage send the given image file to Watson Vision Recognition for Analysis]
* @param {[type]} imagefile [description]
* @return {[type]} [description]
*/
function processImage(imagefile){
var params = {
images_file: fs.createReadStream(imagefile)
};
var confidencethreshold = 0.5 ;
var resultstring = "The objects I see are " ;
visual_recognition.classify(params, function(err, res) {
if (err){
console.log(err);
} else {
result = res.images[0].classifiers[0].classes
if(result !== null & result.length > 0){
result.forEach(function(obj){
//console.log(obj.class)
console.log(obj)
if (obj.score > confidencethreshold){
resultstring = resultstring + ", " + obj.class
}
})
console.log(resultstring)
speak(resultstring);
}else {
resultstring = "I could not understand that image. Try another?"
console.log(resultstring)
speak(resultstring);
}
}
});
}
var ws281x = require('rpi-ws281x-native');
var NUM_LEDS = 1; // Number of LEDs
ws281x.init(NUM_LEDS);
var color = new Uint32Array(NUM_LEDS); // array that stores colors for leds
var colorPalette = {
"red": 0x00ff00,
"green": 0xff0000,
"blue": 0x0000ff,
"purple": 0x008080,
"yellow": 0xc1ff35,
"magenta": 0x00ffff,
"orange": 0xa5ff00,
"aqua": 0xff00ff,
"white": 0xffffff
}
setLEDColor("white", 255);
function setLEDColor(randColor, brightness){
color[0] = colorPalette[randColor];
ws281x.render(color);
ws281x.setBrightness(brightness);
}
// ---- Stop PWM before exit
process.on('SIGINT', function () {
pigpio.terminate();
ws281x.reset();
process.nextTick(function () { process.exit(0); });
});