Created
April 7, 2020 20:18
-
-
Save ebraminio/546543d9c40dfccdbe52ffec5c839adc to your computer and use it in GitHub Desktop.
naudiodon test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const portAudio = require('naudiodon'); | |
var http = require('http'); | |
var clients = []; | |
http.createServer((req, res) => { | |
if (req.url === '/data') clients.push(res); | |
else res.end(`<script> | |
var context = new AudioContext(); | |
fetch('/data').then(response => { | |
const reader = response.body.getReader(); | |
const stream = new ReadableStream({ | |
start(controller) { | |
function push() { | |
reader.read().then(({ done, value }) => { | |
if (done) return; | |
var floats = Float32Array.from(new Uint16Array(value.buffer)); | |
for (var i = 0; i < floats.length; ++i) floats[i] = (floats[i] / 0x10000 * 2) - 1; | |
var audioBuffer = context.createBuffer(1, floats.length, 44100); | |
audioBuffer.getChannelData(0).set(floats); | |
var source = context.createBufferSource(); | |
source.buffer = audioBuffer; | |
source.connect(context.destination); | |
source.start(0); | |
push(); | |
}); | |
}; | |
push(); | |
} | |
}); | |
return new Response(stream, { headers: { "Content-Type": 'binary/octet-stream' } }); | |
}); | |
</script> | |
Hello!`); | |
}).listen(5050) | |
var fs = require('fs'); | |
var ai = new portAudio.AudioIO({ | |
inOptions: { | |
channelCount: 1, | |
sampleFormat: portAudio.SampleFormat16Bit, | |
sampleRate: 44100, | |
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 | |
} | |
}); | |
ai.on('data', data => clients.forEach(c => c.write(data))) | |
ai.start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment