HTML5 Audio API inputBuffer.getChannelData for audio Array buffer

I am making an application where I take microphone data from an inputBuffer and I want to transfer to another client and play it. However, I cannot get it wokring.

My post / post is working fine, so I'll move on to the relevant parts of the code.

function recorderProcess(e) {
   var left = e.inputBuffer.getChannelData(0);
    var convert =  convertFloat32ToInt16(left); 
    window.stream.write(convert);

  var src = window.URL.createObjectURL(lcm);
  playsound(convert);
ss(socket).emit('file',convert, {size: src.size},currentgame);
ss.createBlobReadStream(convert).pipe(window.stream);
//ss.createReadStream(f).pipe(widnow.stream);

}



 function playsound(raw) {
   console.log("now playing a sound, that starts with", new Uint8Array(raw.slice(0, 10)));
    context.decodeAudioData(raw, function (buffer) {
    if (!buffer) {
        console.error("failed to decode:", "buffer null");
        return;
    }
    var source = context.createBufferSource();
    source.buffer = buffer;
    source.connect(context.destination);
    source.start(0);
    console.log("started...");
   }, function (error) {
      console.error("failed to decode:", error);
   });
   }

      

I can successfully create an array buffer using the float32toint16 function, however, when I use the sound initialization function, I get a "null" error, which means the arraybuffer won't decode into an audio stream? Has anyone else had this problem? I have browsed the internet with no answer on how to do this. I am trying to reproduce it this way because I will end up streaming from client to client, so I will be sending arrays over sockets.

early.

+3


source to share


2 answers


If I understand this correctly (there are some missing parts in your example code) ...

decodeAudioData

can only decode files like MP3 or WAV. It looks like you are passing raw Int16Array

or Uint16Array

. Since base is ArrayBuffer

not a format it understands decodeAudioData

, it refuses.

I think you want to do something like this:



function playsound( raw ) {
  // i'll assume you know how to convert in this direction
  // since you have convertFloat32ToInt16
  var buffer = convertInt16ToFloat32( raw ),
    src = context.createBufferSource(),
    audioBuffer = context.createBuffer( 1, buffer.length, context.sampleRate );
  audioBuffer.getChannelData( 0 ).set( buffer );
  src.buffer = audioBuffer;
  src.connect( context.destination );
  src.start( 0 );
}

      

Basically, you already have a way to create raw Float32Array

, which the Web Audio API likes, so there is no need to decode (and you cannot decode anyway, since your data is not a valid file format), so you just convert back to Float32Array

, create your own AudioBuffer

, write data from buffer

and go from there.

+2


source


To convert from float32 to unsigned int 16, you can multiply each float32 value by 0xffff (this is the maximum 16-bit value). and for int16 to float32 do it the other way around, which means division by 0xffff. audio should be good.



I am new to stackoverflow. I have to write this as a comment, but due to the lack of a reputation point, I cannot. This is why I have to write this as an answer. sorry for the inconvenience.

+1


source







All Articles