Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create wav file from socket buffer #42

Open
jrichardsz opened this issue Jan 12, 2024 · 0 comments
Open

Create wav file from socket buffer #42

jrichardsz opened this issue Jan 12, 2024 · 0 comments

Comments

@jrichardsz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant