rochars / wavefile

Create, read and write wav files according to the specs. :star: :notes: :heart:
MIT License
224 stars 48 forks source link

Create wav file from socket buffer #42

Open jrichardsz opened 6 months ago

jrichardsz commented 6 months ago

Expected Behavior

I'm trying to receive the sound (buffer) recording from a web (javascript) with socket.io in the server and then create the wav file using your library.

Current Behavior

There is no error but when the created wav file is reproduced (VLC), it is like static or illegible sounds

Steps to Reproduce

  1. Send the audio as buffer with sockets from html/javascript client
scriptNode.onaudioprocess = function (audioEvent) {
    if (recording) {
        input = audioEvent.inputBuffer.getChannelData(0);

        // convert float audio data to 16-bit PCM
        var buffer = new ArrayBuffer(input.length * 2)
        var output = new DataView(buffer);
        for (var i = 0, offset = 0; i < input.length; i++, offset += 2) {
            var s = Math.max(-1, Math.min(1, input[i]));
            output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
        }
        socketio.emit('write-audio', buffer);
    }
}
  1. Receive the sound from javascript into the socket.io server with nodejs
//just initialize an uuid
@SocketIoEvent(eventName = "start-recording")
this.startRecording = async (message, currentSocket, globalSocket) => {
  console.log(message);
  currentSocket["current_wav_id"] = uuidv4();
}  

//join the chunks/buffer into one
@SocketIoEvent(eventName = "write-audio")
this.writeAudo = async (message, currentSocket, globalSocket) => {
  console.log(message);
  console.log(currentSocket["current_wav_id"])

  if(typeof buffers[currentSocket["current_wav_id"]]  === 'undefined'){
    buffers[currentSocket["current_wav_id"]] = message;
  }else{
    var newBuffer = Buffer.concat([buffers[currentSocket["current_wav_id"]], message]);
    buffers[currentSocket["current_wav_id"]] = newBuffer;
  }
}  

//save the buffer as wav file
@SocketIoEvent(eventName = "end-recording")
this.endRecording = async (message, currentSocket, globalSocket) => {
  console.log(message);
  console.log(currentSocket["current_wav_id"])
  console.log(buffers[currentSocket["current_wav_id"]]);
  var wav = new WaveFile();
  wav.fromScratch(1, 44100, '16', buffers[currentSocket["current_wav_id"]]);  
  fs.promises.writeFile(`/tmp/sandbox/${currentSocket["current_wav_id"]}.wav`, wav.toBuffer());   
} 

Context (Environment)

Additional

The html client is sending a valid waf file because it works with python: the write-audio event is received perfectly and the wav is created

Html client

The html client is this https://github.com/miguelgrinberg/socketio-examples/blob/8281e127cf0d2228e793594527d7d19e8138a62e/audio/static/audio/main.js#L145C42-L145C48

server

https://github.com/miguelgrinberg/socketio-examples/blob/8281e127cf0d2228e793594527d7d19e8138a62e/audio/audio.py#L23

Reproducible sample

I will try to create a Minimal, Reproducible Example.

Thanks in advance

fluffydonkey commented 1 month ago

I have the same problem.