Search code examples
c++wavfile-formatwasapiaudioformat

WASAPI Loopback: Save wave file


I want to record the system's audio output using WASAPI and then saving it to a .wav file.

So far I've followed these guides on WASAPI:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd316551%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/dd370800%28v=vs.85%29.aspx

I get the buffer data using

audioCaptureClient->GetBuffer(&data, &numFramesAvailable, &flags, NULL, NULL);

then, I'm processing this data, by just writing it at the end of the .wav file:

size_t dataSize = format.nChannels * (format.wBitsPerSample / 8) * numFramesAvailable;
fwrite(data, dataSize, 1, fp);

format is a WAVEFORMATEX received from audioClient->GetMixFormat(&format):

cbSize:          22
nAvgBytesPerSec: 352800
nBlockAlign:     8
nChannels:       2
nSamplesPerSec:  44100
wBitsPerSample:  32
wFormatTag:      65534 (WAVE_FORMAT_EXTENSIBLE)

Apparently the subtype of WAVE_FORMAT_EXTENSIBLE is Float:

WAVEFORMATEXTENSIBLE *waveformatextensible = (WAVEFORMATEXTENSIBLE *)format;
if (IsEqualGUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, waveformatextensible->SubFormat)) { // true

Before writing all the captured data to the file I fill in the headers (following http://www.topherlee.com/software/pcm-tut-wavformat.html):

UINT32 sizePlaceholder = 0;
UINT32 fmtLength = 16;

// RIFF Header
fputs("RIFF", fp);                       // offset 0 (0x00)
fwrite(&sizePlaceholder, 4, 1, fp);      // offset 4 (0x04)
fputs("WAVE", fp);                       // offset 8 (0x08)
// fmt-Section
fputs("fmt ", fp);                         // offset 12 (0x0C)
fwrite(&fmtLength, 4, 1, fp);              // offset 16 (0x10)
fwrite(&format.wFormatTag, 2, 1, fp);      // offset 20 (0x14)
fwrite(&format.nChannels, 2, 1, fp);       // offset 22 (0x16)
fwrite(&format.nSamplesPerSec, 4, 1, fp);  // offset 24 (0x18)
fwrite(&format.nAvgBytesPerSec, 4, 1, fp); // offset 28 (0x1C)
fwrite(&format.nBlockAlign, 2, 1, fp);     // offset 32 (0x20)
fwrite(&format.wBitsPerSample, 2, 1, fp);  // offset 34 (0x22)
// Data-Section
fputs("data", fp);                         // offset 36 (0x24)
fwrite(&sizePlaceholder, 4, 1, fp);        // offset 40 (0x28)

After finishing writing 3 seconds of data I fill in the placeholders for the file size and the data section size using fwrite.


The file is not being readable. I suspect it has to do with WAVE_FORMAT_EXTENSIBLE, but I couldn't figure it out.

I tried overwriting several elements of format like:

cbSize = 0;
wFormatTag = WAVE_FORMAT_IEEE_FLOAT;

Producing a readable .wav file, but playing as silence with some clicks in it (I tried recording a song).

wFormatTag = WAVE_FORMAT_PCM;

is producing all random noise.


Solution

  • So, finally after long hours of experimenting I found the solution.

    There were multiple problems with the code.

    1. WAVE_FORMAT_EXTENSIBLE uses a file layout, being a bit different. See this great link for more details.
    2. I did not set binary mode in fopen, so the audio data got corrupted, because fwrite detects newlines (\n) in the data and adds a carriage return (\r). I had to use fopen("foo.wav", "wb") instead of fopen("foo.wav", "w").

    The second problem, was the decisive reason, because I already tried replacing the WAVE_FORMAT_EXTENSIBLE-tag with the WAVE_FORMAT_IEEE_FLOAT-tag, which should have worked, because the additional information are not needed for a .wav file to work.