Pergunta

I'm using WinAPI - Wave functions to create a recording program that records the microphone for X seconds. I've searched a bit over the net, and found out PCM data is too large, and it'll be a problem to send it through sockets...

How can I compress it to something smaller? Any simple / "cheap" way ?

I've also noticed, when I'm declaring the format using the Wave API functions, I'm using this code :

WAVEFORMATEX pFormat;
pFormat.wFormatTag= WAVE_FORMAT_PCM;     // simple, uncompressed format
pFormat.nChannels=1;                    //  1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate;      // 44100
pFormat.nAvgBytesPerSec=sampleRate*2;   // = nSamplesPerSec * n.Channels * wBitsPerSample/8
pFormat.nBlockAlign=2;                  // = n.Channels * wBitsPerSample/8
pFormat.wBitsPerSample=16;              //  16 for high quality, 8 for telephone-grade
pFormat.cbSize=0;

As you can see, pFormat.wFormatTag= WAVE_FORMAT_PCM; maybe I can insert instead of WAVE_FORMAT_PCM something else, so it'll be compressed right away? I've checked MSDN for other values, though none of them works for me in my Visual Studio...

So what can I do?

Thanks!

Foi útil?

Solução

The simplest way is to simply reduce your sample rate from 44100 to something more manageable like 22050, 16000, 11025, or even 8000. Most voice codecs don't go higher than 16000 hz anyway. And the older ones are optimized for 8khz.

The next step is to find a codec. There's a handful of codecs to use with the Windows Audio Compression Manager, but almost all of them date back to Windows 95 and sound terrible by modern standards after being decompressed.

You can always convert to WMA in real time using the Format SDK or with Media Foundation APIs. Or just go get an open source MP3 library like LAME.

Outras dicas

For telephone quality speech you can change to 8 bits per sample and a sample rate of 8000. This will greatly reduce the amount of data.

GSM has good compression. You can convert a block of PCM data to GSM (or any other codec you have installed) using acmStreamConvert(). Refer to MSDN for more details:

Converting Data from One Format to Another

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top