From: riqo Date: Fri, 5 Feb 2021 22:47:03 +0000 (-0600) Subject: Merge remote-tracking branch 'origin/main' X-Git-Tag: v0.9~100 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=9d64ffcb41436ac2409e51220bd4d69cca8c3582;p=mime-chat Merge remote-tracking branch 'origin/main' --- 9d64ffcb41436ac2409e51220bd4d69cca8c3582 diff --cc testAudio.js index de50527,815964a..a83ef63 --- a/testAudio.js +++ b/testAudio.js @@@ -7,96 -9,79 +9,90 @@@ const {Writable} = require('stream') // Creates a client const client = new speech.SpeechClient(); --// connect to test server --// 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, -}; - -const speechCallback = (data) => { - console.log( - `Transcription: ${data.results[0].alternatives[0].transcript}` - ); +/** + * 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}`); } - const audioChunks = []; -// const recognizeStream = client -// .streamingRecognize(request) -// .on('error', err => { -// if (err.code === 11) { -// // restartStream(); -// } else { -// console.error('API request error ' + err); -// } -// }) -// .on('data', speechCallback); -- +let audioInput = []; // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream - let ia = new portAudio.AudioIO({ -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 ++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); }); -// 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)