I want to record some audio from microphone under Windows, so I use the wave API.
This is what I do
QByteArray tmp;
QByteArray data;
char dst[SAMPLES_TO_SEND];
qint64 tot=0;
const int NUMPTS = SAMPLES_TO_SEND*3;
int sampleRate = 48000;
short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
// for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
HWAVEIN hWaveIn;
WAVEHDR WaveInHdr;
MMRESULT result;
// Specify recording parameters
WAVEFORMATEX pFormat;
pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
pFormat.nChannels=1; // 1=mono, 2=stereo
pFormat.nSamplesPerSec=sampleRate; // 48000
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;
result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
if (result)
{
WCHAR fault[256];
waveInGetErrorText(result, fault, 256);
return -1;
}
// Set up and prepare header for input
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS*2;
WaveInHdr.dwBytesRecorded=0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
// Insert a wave input buffer
result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if (result)
{
return -1;
}
// Commence sampling input
result = waveInStart(hWaveIn);
if (result)
{
return -1;
}
At this point, what I must do to access and managing recorded data? My final goal is to continuously recording small data (I want every time exactly 512 sample) subsampling what I recorded to have a 16 kHz samples (so I would divide data by 3) and apply a ulaw algorithm and send everything via udp. This is why my buffer is so small (NUMPTS = 1536 shorts -> 3072 bytes). After a sleep of 100 milli seconds, I have checked what there is in waveIn, but is all 0... (well, of course my microphone is working correctly)
The waveInOpen function provides four callback options that are for notifying you when a buffer has been filled. Use one of those choices.
You should also allocate a second buffer and output it immediately after the first. It will be queued in the driver and switched to seamlessly when the first buffer has been filled.