Skip to content

Commit

Permalink
speech to text microphone input examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly committed Jul 6, 2017
1 parent 2a99f0c commit ad1776f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ doc/
dist/
.env
.eslintcache
examples/speech_to_text_microphone_input/*.wav
examples/speech_to_text_microphone_input/*.txt
19 changes: 19 additions & 0 deletions examples/speech_to_text_microphone_input/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "watson-mic-example",
"version": "0.0.0",
"description": "IBM Watson Speech to Text microphone input example",
"main": "transcribe-mic-to-console.js",
"scripts": {
"start": "node transcribe-mic-to-console.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"private": true,
"author": "IBM",
"license": "Apache-2.0",
"dependencies": {
"dotenv": "^4.0.0",
"line-in": "^0.1.2",
"watson-developer-cloud": "^2.33.0",
"wav": "^1.0.1"
}
}
5 changes: 5 additions & 0 deletions examples/speech_to_text_microphone_input/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# IBM Watson Speech to Text Microphone Transcription Example

Uses IBM Watson and Node.js to transcribe speech captured from the computer's microphone.

This example requires [arecord](http://alsa-project.org/) (Linux) or [sox](http://sox.sourceforge.net/) (Mac/Windows).
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require('dotenv').config({ silent: true }); // optional, handy for local development
var SpeechToText = require('watson-developer-cloud/speech-to-text/v1');
var LineIn = require('line-in'); // the `mic` package also works - it's more flexible but requires a bit more setup
var wav = require('wav');


var speechToText = new SpeechToText({
// if left unspecified here, the SDK will fall back to the SPEECH_TO_TEXT_USERNAME and SPEECH_TO_TEXT_PASSWORD
// environment properties, and then Bluemix's VCAP_SERVICES environment property
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
});

var lineIn = new LineIn(); // 2-channel 16-bit little-endian signed integer pcm encoded audio @ 44100 Hz

var wavStream = new wav.Writer({
sampleRate: 44100,
channels: 2
});

var recognizeStream = speechToText.createRecognizeStream({'content_type': 'audio/wav'});

lineIn.pipe(wavStream);

wavStream.pipe(recognizeStream);

recognizeStream.pipe(process.stdout);
55 changes: 55 additions & 0 deletions examples/speech_to_text_microphone_input/transcribe-mic-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var fs = require('fs');
require('dotenv').config({ silent: true }); // optional, handy for local development
var SpeechToText = require('watson-developer-cloud/speech-to-text/v1');
var mic = require('mic');
var wav = require('wav');


var speechToText = new SpeechToText({
// if left unspecified here, the SDK will fall back to the SPEECH_TO_TEXT_USERNAME and SPEECH_TO_TEXT_PASSWORD
// environment properties, and then Bluemix's VCAP_SERVICES environment property
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
});

var micInstance = mic({
'rate': '48000',
'channels': '1',
'debug': false
});

var micInputStream = micInstance.getAudioStream();


var wavStream = new wav.FileWriter('./audio.wav', {
sampleRate: 48000,
channels: 1
});

var recognizeStream = speechToText.createRecognizeStream({'content_type': 'audio/wav'});

micInputStream.pipe(wavStream);

wavStream.pipe(recognizeStream);

recognizeStream.pipe(fs.createWriteStream('./transcription.txt'));

// note:
// If you just kill the process with control-c, the .wav file will have an incorrect header, and any in-flight
// transcription will be lost.
// This allows for a graceful termination of the recording, and the process will automatically exit after everything is
// complete.
console.log('Recording, press any key to exit');
process.stdin.setRawMode(true);
// process.stdin.resume();
process.stdin.once('data', function() {
console.log('Cleaning up and exiting...');
process.stdin.setRawMode(false);
micInstance.stop();
recognizeStream.on('end', function() {
process.exit();
})
});


micInstance.start();

0 comments on commit ad1776f

Please sign in to comment.