mime-chat/tests/audio.js
2021-02-28 14:27:51 -06:00

95 lines
2 KiB
JavaScript

const speech = require('@google-cloud/speech');
const portAudio = require('naudiodon');
// const rs = fs.createReadStream('rawAudio.wav');
// 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) => {
const buf = Buffer.from(chunk, 'base64');
audioInput.push(buf);
});
async function processAudio() {
const buf = Buffer.concat(audioInput);
audioInput = [];
transcribeSpeech({
content: buf
});
}
setTimeout(async () => {
ia.pause();
processAudio();
// ia.resume();
}, 3000);
setTimeout(() => {
ia.resume();
}, 5000)
setTimeout(async () => {
ia.pause();
processAudio();
// ia.resume();
}, 8000);
setTimeout(() => {
ia.pause();
}, 9000)