WAVEFORMATEX _wfex;
_wfex.wFormatTag = WAVE_FORMAT_PCM;
_wfex.nChannels = 1;
_wfex.wBitsPerSample = 16;
_wfex.nSamplesPerSec = 44100;
_wfex.nBlockAlign = _wfex.nChannels * _wfex.wBitsPerSample / 8;
_wfex.nAvgBytesPerSec = _wfex.nSamplesPerSec * _wfex.nBlockAlign;
_wfex.cbSize = 0;
UINT myDevice = WAVE_MAPPER;
waveInOpen(&_hWaveIn, myDevice, &_wfex, 0L, 0L, WAVE_FORMAT_DIRECT);
int N = _wfex.nSamplesPerSec * 1 * t_seconds;
short int * buffer = new short int[N];
WAVEHDR WaveHdr;
WaveHdr.lpData = (LPSTR)buffer;
WaveHdr.dwBufferLength = N * 2;
WaveHdr.dwBytesRecorded = 0;
WaveHdr.dwUser = 0L;
WaveHdr.dwFlags = 0L;
WaveHdr.dwLoops = 0L;
waveInPrepareHeader(_hWaveIn, &WaveHdr, sizeof(WAVEHDR));
while (WaveHdr.dwFlags != WHDR_PREPARED);
waveInAddBuffer(_hWaveIn, &WaveHdr, sizeof(WAVEHDR));
waveInStart(_hWaveIn);
while (waveInUnprepareHeader(_hWaveIn, &WaveHdr, sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
waveInClose(_hWaveIn);
WAVHEADER wh;
memcpy(wh.RIFF, "RIFF", 4);
wh.bytes = N * 2 + 36;
memcpy(wh.WAVE, "WAVE", 4);
memcpy(wh.fmt, "fmt ", 4);
wh.siz_wf = 16;
wh.wFormatTag = WAVE_FORMAT_PCM;
wh.nChannels = _wfex.nChannels;
wh.nSamplesPerSec = _wfex.nSamplesPerSec;
wh.nAvgBytesPerSec = _wfex.nAvgBytesPerSec;
wh.nBlockAlign = _wfex.nBlockAlign;
wh.wBitsPerSample = _wfex.wBitsPerSample;
memcpy(wh.data, "data", 4);
wh.pcmbytes = N * 2 * 1;
HANDLE hFile = CreateFile((LPCWSTR)L"audio.wav", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile)
{
DWORD dwWrite;
WriteFile(hFile, &wh, sizeof(WAVHEADER), &dwWrite, NULL);
WriteFile(hFile, WaveHdr.lpData, WaveHdr.dwBufferLength, &dwWrite, NULL);
CloseHandle(hFile);
std::cout << "File was created\n";
}
else {
std::cout << "File not created\n";
}