forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
speech to text microphone input examples
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
27 changes: 27 additions & 0 deletions
27
examples/speech_to_text_microphone_input/transcribe-mic-to-console.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
examples/speech_to_text_microphone_input/transcribe-mic-to-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |