Reputation: 23
We'd like to pipe microphone directly to waton speech to text service , but it seems that we have to go through .wav first ? please take a look at the following codes, In particular I was trying to get the microphone streamed directly to the speechToText service. I believe this is the most common way of using mic, not piping it into a .wav and then stream the .wav file to stt:
var mic;
var SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
var fs = require('fs');
var watson = require('watson-developer-cloud');
var cp = require('child_process');
mic = cp.spawn('arecord', ['--device=plughw:1,0', '--format=S16_LE', '--rate=44100', '--channels=1']); //, '--duration=10'
mic.stderr.pipe(process.stderr);
stt();
function stt() {
console.log("openCMDS");
var speech_to_text = new SpeechToTextV1({
username: '',
password: ''
});
var params = {
content_type: 'audio/wav',
model: 'zh-CN_BroadbandModel',
continuous: true,
inactivity_timeout: -1
};
recognizeStream = speech_to_text.createRecognizeStream(params);
mic.stdout.pipe(recognizeStream);
//mic.stdout.pipe(require('fs').createWriteStream('test.wav'));
// Pipe in the audio.
fs.createReadStream('test.wav').pipe(recognizeStream);
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
recognizeStream.setEncoding('utf8');
console.log("start record");
recognizeStream.on('data', function(event) { onEvent('Data:', event); });
recognizeStream.on('error', function(event) { onEvent('Error:', event); });
recognizeStream.on('close', function(event) { onEvent('Close:', event); });
// Display events on the console.
function onEvent(name, event) {
console.log(name, JSON.stringify(event, null, 2));
}
}
Upvotes: 2
Views: 1121
Reputation: 23703
The Speech to Text service needs to know the format of the audio you are trying to send. 99% of the issues I've seen are because the service is expecting a different audio format than the one users are using.
'--format=S16_LE', '--rate=44100', '--channels=1'
That looks like a 44.1kHz PCM format.
In your code you are specifying:
content_type: 'audio/wav'
Take a look at the supported audio formats.
Maybe try with audio/l16; rate=44100;
. You can also record the audio in a different format.
Finally, take a look at the javascript-speech-sdk. We have examples of how to stream the microphone from the browser.
const mic = require('mic');
const SpeechToTextV1 = require('watson-developer-cloud/speech-to-text/v1');
const speechToText = new SpeechToTextV1({
username: 'YOUR USERNAME',
password: 'YOUR PASSWORD',
url: 'YOUR SERVICE URL',
version: 'v1'
});
// 1. Microphone settings
const micInstance = mic({
rate: 44100,
channels: 2,
debug: false,
exitOnSilence: 6
});
// 2. Service recognize settings
const recognizeStream = speechToText.createRecognizeStream({
content_type: 'audio/l16; rate=44100; channels=2',
model: 'zh-CN_BroadbandModel',
interim_results: true,
})
// 3. Start recording
const micInputStream = micInstance.getAudioStream();
micInstance.start();
console.log('Watson is listening, you may speak now.');
// 4. Pipe audio to service
const textStream = micInputStream.pipe(recognizeStream).setEncoding('utf8');
textStream.on('data', user_speech_text => console.log('Watson hears:', user_speech_text));
textStream.on('error', e => console.log(`error: ${e}`));
textStream.on('close', e => console.log(`close: ${e}`));
Upvotes: 1