add speech to text transcription

This commit is contained in:
riqo 2021-02-05 16:41:55 -06:00
commit 0ba02f2f09
10 changed files with 378 additions and 438 deletions

View file

@ -2,86 +2,101 @@
const fs = require('fs');
const speech = require('@google-cloud/speech');
const portAudio = require('naudiodon');
const rs = fs.createReadStream('rawAudio.wav');
const WebSocket = require("ws")
const {Writable} = require('stream');
// Creates a client
const client = new speech.SpeechClient();
// connect to test server
const ws = new WebSocket("ws://localhost:8080");
// const ws = new WebSocket("ws://localhost:8080");
// Creates a client const client = new speech.SpeechClient();
// google cloud speech to text settings
const encoding = 'LINEAR16';
const sampleRateHertz = 44100;
const sampleRateHertz = 16000;
const languageCode = 'en-US';
const audioChunks = [];
const audioInputStreamTransform = new Writable({
write(chunk, encoding, next) {
console.log(chunk);
audioChunks.push(chunk);
next();
},
final() {
console.log(audioChunks);
},
});
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
const request = {
config,
interimResults: true,
};
/**
* Note that transcription is limited to 60 seconds audio.
* Use a GCS file for audio longer than 1 minute.
*/
let audio;
const speechCallback = (data) => {
console.log(
`Transcription: ${data.results[0].alternatives[0].transcript}`
);
async function transcribeSpeech (audio) {
const request = {
config: config,
audio: audio,
};
// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
const recognizeStream = client
.streamingRecognize(request)
.on('error', err => {
if (err.code === 11) {
// restartStream();
} else {
console.error('API request error ' + err);
}
})
.on('data', speechCallback);
const audioChunks = [];
let audioInput = [];
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
const recorderOptions = {
channelCount: 1,
sampleFormat: portAudio.sampleFormat16Bit,
sampleRate: 16000,
deviceId: -1, // Use -1 or omit the deviceId to select the default device
closeOnError: false // Close the stream if an audio error is detected, if set false then just log the error
}
const ai = new portAudio.AudioIO({
inOptions: recorderOptions
let ia = new portAudio.AudioIO({
inOptions: {
channelCount: 1,
sampleFormat: 16,
sampleRate: 16000,
deviceId: -1,
closeOnError: false,
}
});
ia.setEncoding('base64');
ia.start();
ia.on('error', (e) => {
console.log('error recording audio', + e);
});
ia.on('data', (chunk) => {
audioInput.push(chunk);
console.log('Got %d characters of string data:', chunk.length);
});
// manipulate individual chunks as they're available
// const audioData = [];
// ai.on('data', (d) => {
// audioData.push(d.toString('binary'))
// })
async function processAudio() {
audio = '';
audioInput.forEach(s => {
audio += s;
})
const buffer = Buffer.from(audio, 'base64');
audioInput = [];
await transcribeSpeech({
content: buffer
});
}
setTimeout(async () => {
ia.pause();
await processAudio();
// ia.resume();
}, 3000);
ai.pipe(audioInputStreamTransform)
ai.start();
setTimeout(() => {
ai.quit();
}, 2000)
ia.resume();
}, 4000)
setTimeout(async () => {
ia.pause();
await processAudio();
// ia.resume();
}, 8000);
setTimeout(() => {
ia.pause();
}, 9000)