98 lines
2.2 KiB
JavaScript
98 lines
2.2 KiB
JavaScript
|
|
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();
|
|
|
|
const encoding = 'LINEAR16';
|
|
const sampleRateHertz = 16000;
|
|
const languageCode = 'en-US';
|
|
|
|
const config = {
|
|
encoding: encoding,
|
|
sampleRateHertz: sampleRateHertz,
|
|
languageCode: languageCode,
|
|
};
|
|
|
|
/**
|
|
* Note that transcription is limited to 60 seconds audio.
|
|
* Use a GCS file for audio longer than 1 minute.
|
|
*/
|
|
let audio;
|
|
|
|
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}`);
|
|
}
|
|
|
|
let audioInput = [];
|
|
// Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream
|
|
const 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);
|
|
});
|
|
|
|
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);
|
|
|
|
setTimeout(() => {
|
|
ia.resume();
|
|
}, 4000)
|
|
|
|
setTimeout(async () => {
|
|
ia.pause();
|
|
await processAudio();
|
|
// ia.resume();
|
|
}, 8000);
|
|
|
|
setTimeout(() => {
|
|
ia.pause();
|
|
}, 9000)
|