Convert AudioRawData to WAVE

I’m having trouble converting the written AudioRawData to a .wav file. After I convert from raw PCM to WAVE the audio plays random static. The static also is a different length than my original recording. Here’s my code:

In onMixedAudioRawDataReceived

ofstream f;"example.pcm", ios::out | ios::binary | ios::app);

char* buf = data_->GetBuffer();
f.write(buf, strlen(buf));

Then I have tried to convert the file using ffmpeg:

ffmpeg -f s16le -ar 32k -ac 1 -i example.pcm example.wav

and using sox:

sox -t raw -r 32000 -e signed --endian little -b 16 -c 1 example.pcm -r 32000 example.wav 

But example.wav just plays static. I can’t figure out where my code is going wrong and would appreciate any help.

Which Windows Meeting SDK version?

I figured I’d also add that I get the following two errors when I run the ffmpeg command:

Invalid PCM packet, data has size 1 but at least a size of 2 was expected
Error while decoding stream #0:0: Invalid data found when processing input

SOLVED! For anyone who stumbles across this, the error is with this line

Here, buf is a variable size, and is 0 when there is no audio. The static I was hearing in my wav file was my original audio with every bit of silence removed. That is also why I was getting an error from ffmpeg, because the size of the audio file was not nicely divisible. Instead the line of code should be:

f.write(buf, data_->GetBufferLen());

This writes a fixed buffer size of 640.

That’s great that you were able to resolve this, thanks for sharing the solution!