Get audio samples from an audio element

I want to receive live audio from a microphone using a web browser and send it to the Node.js server via websockets. I am using the BinaryJS library to send binary data to the server. I am having trouble getting sound samples from a microphone. Here is what I have:

window.AudioContext = window.AudioContext || window.webkitAudioContext; var context = new AudioContext(); var audio = document.querySelector('audio'); navigator.webkitGetUserMedia({audio: true}, function(micstream){ audio.src = window.URL.createObjectURL(micstream); }, errorCallback); }); var errorCallback = function(e){ console.log("Rejected!", e); }; 

I want some way to get an audio sample every 10 ms so that I can write it to the websocket stream. I am looking for something like this:

 function getSample(){ //read the current data in byte buffer. setTimeout(getSample, 10); } 

Can someone tell me how to do this? Or is there another way to do this? Thanks!

+6
source share
1 answer

These points should lead you to the right path:

  • You want to send raw data, so create a JavaScriptNode or ScriptProcessor from your context and use onaudioprocess for your function. You can easily set your time intervals to make sure that it is only 10 ms. This is very similar to what the current RTCRecorders do.
  • By default, float PCM packages are created. You CAN convert them to 16 bit PCM in JavaScript, but media libraries worthy of their weight (I know for sure that Gstreamer) can translate the stream.

Here is the code I wrote . This code is just modified versions of existing recorders, but I send data via websocket and I do not change it in javascript, but the function of changing from float to 16-bit PCM sound is included in the source code if you need it. You can do a time check in onaudioprocess and migrate the websocket from worker (only Chrome supports WebSockets in Worker threads at the moment) and you should be good to go.

0
source

Source: https://habr.com/ru/post/957476/


All Articles