// const speech = require('@google-cloud/speech'); const portAudio = require('naudiodon'); // const rs = fs.createReadStream('rawAudio.wav'); const audioContainer = { input: '', buffers: [] } let record = true; // Create an instance of AudioIO with inOptions (defaults are as below), which will return a ReadableStream const aio = new portAudio.AudioIO({ inOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 // Use -1 or omit the deviceId to select the default device }, outOptions: { channelCount: 2, sampleFormat: portAudio.SampleFormat16Bit, sampleRate: 44100, deviceId: -1 // Use -1 or omit the deviceId to select the default device } }); aio.start() aio.read() aio.on('data', buf => console.log(buf.timestamp)); const counter = 0; const tests = []; function testCallback() { play(tests[0]) } function splitArrayIntoChunksOfLen(buf, len) { const chunks = []; let i = 0; let L = len; console.log(buf.byteLength) while(i < buf.byteLength) { chunks.push(buf.slice(i, L)); i = L; L += len; } chunks.forEach(c => console.log(c)) return chunks; } function play(input) { let i = 0; const buf = Buffer.from(input, 'base64'); // format buffers into half their size to account for writable highwaterMark. const audio = splitArrayIntoChunksOfLen(buf, 8192); // call this fuction after last audio chunk has been written. const callback = () => { // TODO: clear portAudio writable buffer on write end. } write(); // iterate through audio array and write buffers to portAudio writable. function write() { let ok = true; do { if (i === audio.length - 1) { // write last chunk. ao.write(audio[i], null, callback); } else { // check for backpreassure. ok = ao.write(audio[i], null); } i++; } while (i < audio.length && ok); if (i < audio.length) { // Had to stop early! // Write some more once it drains. ao.once('drain', write); } } } // utility function used by play func function bufSplit(input){ const result = []; input.forEach((b) => { // split buffer into two. result.push(b.slice(0, b.length / 2), b.slice(b.length / 2, b.length)); }); return result; } async function test() { } setTimeout(async () => { record = false; test() }, 4000); setTimeout(async () => { return; }, 6000);