142 lines
3.2 KiB
JavaScript
142 lines
3.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) => {
|
|
audioInput.push(chunk);
|
|
});
|
|
|
|
const ao = new portAudio.AudioIO({
|
|
outOptions: {
|
|
channelCount: 1,
|
|
sampleFormat: portAudio.SampleFormat16Bit,
|
|
sampleRate: 16000,
|
|
deviceId: -1, // Use -1 or omit the deviceId to select the default device
|
|
closeOnError: true // Close the stream if an audio error is detected, if set false then just log the error
|
|
}
|
|
});
|
|
|
|
ao.start();
|
|
|
|
const callback = () => {
|
|
console.log('yayayay')
|
|
}
|
|
|
|
const play = (audio) => {
|
|
|
|
console.log('audio length', audio.length);
|
|
let i = 0;
|
|
write();
|
|
|
|
function write() {
|
|
let ok = true;
|
|
do {
|
|
const chunk = audio[i]
|
|
console.log(chunk, i)
|
|
|
|
i++;
|
|
if (i === audio.length - 1) {
|
|
// Last time!
|
|
ao.write(chunk, null, callback);
|
|
} else {
|
|
// See if we should continue, or wait.
|
|
// Don't pass the callback, because we're not done yet.
|
|
ok = ao.write(chunk, null);
|
|
}
|
|
} while (i < audio.length - 1 && ok);
|
|
if (i < audio.length - 1) {
|
|
// Had to stop early!
|
|
// Write some more once it drains.
|
|
ao.on('drain', write);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
async function processAudio() {
|
|
|
|
const buf = Buffer.concat(audioInput);
|
|
|
|
play(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)
|