Skip to content

Create wav file from socket buffer #42

@jrichardsz

Description

@jrichardsz

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)

  • Ubuntu 22
  • Node 16.20.2
  • wavfile 11.0.0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions