I am trying to record sound in a .wave file using waveInOpen and co ( see here )
Here is my code to write to the buffer and it seems to work:
#include <Windows.h>
#include <MMSystem.h>
#include <iostream>
using namespace std;
int main(){
HWAVEIN microHandle;
WAVEHDR waveHeader;
const int NUMPTS = 22050 * 10;
int sampleRate = 22050;
short int waveIn[NUMPTS];
MMRESULT result = 0;
WAVEFORMATEX format;
format.wFormatTag=WAVE_FORMAT_PCM;
format.wBitsPerSample=8;
format.nChannels=1;
format.nSamplesPerSec=sampleRate;
format.nAvgBytesPerSec=format.nSamplesPerSec*format.nChannels*format.wBitsPerSample/8;
format.nBlockAlign=format.nChannels*format.wBitsPerSample/8;
format.cbSize=0;
result = waveInOpen(µHandle, WAVE_MAPPER, &format, 0L, 0L, WAVE_FORMAT_DIRECT);
if (result)
{
cout << "Fail step 1" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
waveHeader.lpData = (LPSTR)waveIn;
waveHeader.dwBufferLength = NUMPTS*2;
waveHeader.dwBytesRecorded=0;
waveHeader.dwUser = 0L;
waveHeader.dwFlags = 0L;
waveHeader.dwLoops = 0L;
waveInPrepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR));
result = waveInAddBuffer(microHandle, &waveHeader, sizeof(WAVEHDR));
if (result)
{
cout << "Fail step 2" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
result = waveInStart(microHandle);
if (result)
{
cout << "Fail step 3" << endl;
cout << result << endl;
Sleep(10000);
return 0;
}
do {} while (waveInUnprepareHeader(microHandle, &waveHeader, sizeof(WAVEHDR))==WAVERR_STILLPLAYING);
waveInClose(microHandle);
return 0;
}
But I did not find any function or explanation on how to save it to disk
Should I keep the WAVEFORMATEX structure? or are there other things to add to the header of the wave file?
The only thing that I should use only Windows libraries, I can not install any other
Thank:)
source
share