Skip to content

Instantly share code, notes, and snippets.

@ZeroByter
Last active March 7, 2022 18:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeroByter/f5690fa9a7c20e2b24cccaa5a8cf3b86 to your computer and use it in GitHub Desktop.
Save ZeroByter/f5690fa9a7c20e2b24cccaa5a8cf3b86 to your computer and use it in GitHub Desktop.
try{
window.AudioContext = window.AudioContext || window.webkitAudioContext
}catch(e){
alert("Web audio API is not supported in this browser")
}
try{
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia
var hasMicrophoneInput = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia)
}catch(e){
alert("getUserMedia() is not supported in your browser")
}
var bufferSize = 1024
var audioContext = new AudioContext()
socket.on("mic", function(msg){
//console.log(msg)
var source = audioContext.createBufferSource();
source.connect(audioContext.destination);
array = new Int8Array(msg.data);
abs = new Float32Array(msg.data);
arrays = new Float32Array(abs.length);
ab = audioContext.createBuffer(1, array.length+1, 44100);
ab.getChannelData(0).set(arrays);
source.buffer = ab;
source.start(0)
})
function myPCMFilterFunction(inputSample) {
var noiseSample = Math.random() * 2 - 1
return inputSample + noiseSample * 0
}
var myPCMProcessingNode = audioContext.createScriptProcessor(bufferSize, 1, 1)
myPCMProcessingNode.onaudioprocess = function(e){
var input = e.inputBuffer.getChannelData(0)
var output = e.outputBuffer.getChannelData(0)
for(var i = 0; i < bufferSize; i++){
output[i] = myPCMFilterFunction(input[i])
}
socket.emit("mic", output)
}
var errorCallback = function(e){
alert("Error in getUserMedia: " + e)
}
navigator.getUserMedia({audio: true}, function(stream){
var microphone = audioContext.createMediaStreamSource(stream)
microphone.connect(myPCMProcessingNode)
myPCMProcessingNode.connect(audioContext.destination)
//microphone.start(0)
var gainNode = audioContext.createGain()
myPCMProcessingNode.connect(gainNode)
gainNode.connect(audioContext.destination)
gainNode.gain.value = -1 //Mute the audio here so I can hear if I am playing the audio that im receiving
}, errorCallback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment