[Libwebsockets] Receive and save audio stream as a file

Andy Green andy at warmcat.com
Thu May 12 01:18:39 CEST 2016

On May 12, 2016 7:01:06 AM GMT+08:00, Lucian Georgescu <lucgeo92 at gmail.com> wrote:
>I'm new to libwebsockets, I installed version 2.0 and I have a problem
>with my first application. 
>I want to create a simple server to be able to receive audio chunks and
>then to write them on a file on disk. Here is my code:

Thanks for sending the code.

>I got the message "client connected" and also the wav file is created
>(the file size is bigger than zero), but the content isn't ok, I cannot
>play it. 
>The client is based on Web Audio API. It captures live audio from
>microphone and it sends audio chunks already encoded as wav, 16Khz,
>mono. I used code from this project:
>http://kaljurand.github.io/dictate.js/. The client is working well, I
>used it in the past for exactly the same task with a Java server.
>I observed an interesting thing about the resulted wav file. Using the
>following command: "hexdump -C file.wav | head", I got the hex content.
>It contains only the information regarding the encoding type, the rest
>of the data being zero. Here are some rows in hex:
>00000000  52 49 46 46 f4 22 00 00  57 41 56 45 66 6d 74 20 
>|RIFF."..WAVEfmt |
>00000010  10 00 00 00 01 00 02 00  44 ac 00 00 10 b1 02 00 
>00000020  04 00 10 00 64 61 74 61  d4 22 00 00 00 00 00 00 
>00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00 
>I would really appreciate if someone could indicate me how to save
>these audio chunks in a valid file.

It's not the problem but you should also add a handler for CLOSE callback and do the fclose() there if the FILE was open.

Also not the problem but you should also memset protocolAudio to 0 before setting it, since there are more members in that struct than the ones you set, and those will contain garbage otherwise.

Since the right amount of data is coming and going in the file, and the WAV header came by that route and is correct, it seems likely you are in fact sending a load of 0 samples from the javascript.

You should be able to check that at the js before sending it, with an alert() or whatever?

Mixer levels, or alsa input selection could get you this situation.


> Thanks,
> Lucian
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org

More information about the Libwebsockets mailing list