const fs = require('fs'); // const portAudio = require('naudiodon'); // Imports the Google Cloud client library const speech = require('@google-cloud/speech'); const portAudio = require('naudiodon'); // Create a stream to pipe into the AudioOutput // Note that this does not strip the WAV header so a click will be heard at the beginning const rs = fs.createReadStream('rawAudio.wav'); const WebSocket = require("ws") // Start piping data and start streaming const {Writable} = require('stream'); const ws = new WebSocket("ws://localhost:8080"); //const duplex = WebSocket.createWebSocketStream(ws, { encoding: 'binary' }); let audioChunks = []; const audioInputStreamTransform = new Writable({ write(chunk, encoding, next) { // console.log(chunk) // ws send chunk audioChunks.push(chunk); next(); }, final() { console.log(audioChunks) ws.send(audioChunks) // stop ws transer }, }); // Creates a client const client = new speech.SpeechClient(); /** * TODO(developer): Uncomment the following lines before running the sample. */ const filename = './rawAudio.wav'; const encoding = 'LINEAR16'; const sampleRateHertz = 16000; const languageCode = 'en-US'; const config = { encoding: encoding, sampleRateHertz: sampleRateHertz, languageCode: languageCode, }; const audio = { content: fs.readFileSync(filename).toString('base64'), }; // const request = { // config: config, // audio: audio, // }; // Detects speech in the audio file async function test() { const [response] = await client.recognize(request); const transcription = response.results .map(result => result.alternatives[0].transcript) .join('\n'); console.log('Transcription: ', transcription); } // test(); const request = { config, interimResults: true, }; const speechCallback = (data) => { console.log( `Transcription: ${data.results[0].alternatives[0].transcript}` ) } const recognizeStream = client .streamingRecognize(request) .on('error', err => { if (err.code === 11) { // restartStream(); } else { console.error('API request error ' + err); } }) .on('data', speechCallback); // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream const audioOptions = { channelCount: 2, 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 } let audioData = []; const ai = new portAudio.AudioIO({ inOptions: audioOptions }); // ai.on('data', (d) => { // audioData.push(d.toString('binary')) // }) ai.pipe(audioInputStreamTransform) ai.start(); setTimeout(() => { ai.quit(); }, 2000)