Code:
/* in sound.h */
WAVEFORMATEX pcmWaveFormat;
HWAVEIN m_hRecord;
LPWAVEHDR pHdr;
/* part of sound.c */
char buf[512];
void CALLBACK m_ThreadID(HWAVEIN hwi, UINT uMessage, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
switch(uMessage) {
case MM_WIM_OPEN:
sprintf(buf, "wim open");
break;
case MM_WIM_CLOSE:
sprintf(buf, "wim close");
break;
case MM_WOM_DONE:
sprintf(buf, "wom done");
break;
case MM_WIM_DATA:
sprintf(buf, "wim data");
break;
default:
break;
}
MessageBox(NULL, buf, "Debug", MB_OK);
}
void startRecording(void)
{
char err[256] = "";
int i = 0;
pHdr = (LPWAVEHDR) GlobalLock(GlobalAlloc(GHND, sizeof(WAVEHDR)));
// if(isPlaying) {
// MessageBox(hwndMain, "You are currently playing a file", "Error", MB_ICONERROR|MB_OK);
// return;
// }
// if(isRecording) {
// MessageBox(hwndMain, "You are already recording a file", "Error", MB_ICONERROR|MB_OK);
// return;
// }
isRecording = 1;
m_hRecord = NULL;
pcmWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
pcmWaveFormat.cbSize = 0;
pcmWaveFormat.wBitsPerSample = 16;
pcmWaveFormat.nSamplesPerSec = 22050;
pcmWaveFormat.nChannels = 2;
pcmWaveFormat.nAvgBytesPerSec = pcmWaveFormat.nSamplesPerSec*(pcmWaveFormat.wBitsPerSample/8);
pcmWaveFormat.nBlockAlign = pcmWaveFormat.nChannels *(pcmWaveFormat.wBitsPerSample/8);
if(i = waveInOpen(&m_hRecord, WAVE_MAPPER, &pcmWaveFormat, (DWORD)m_ThreadID, 0, CALLBACK_FUNCTION))
{
waveInGetErrorText(i, err, 255);
sprintf(buf, "WAVEIN Open (%d): %s", i, err);
MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
return;
}
if(i = waveInPrepareHeader(m_hRecord, pHdr, sizeof(WAVEHDR)))
{
waveInGetErrorText(i, err, 255);
sprintf(buf, "WAVEIN PrepareHeader (%d): %s", i, err);
MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
return;
}
if(i = waveInAddBuffer(m_hRecord, pHdr, sizeof(WAVEHDR)))
{
waveInGetErrorText(i, err, 255);
sprintf(buf, "WAVEIN AddBudder (%d): %s", i, err);
MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
return;
}
if(i = waveInStart(m_hRecord))
{
waveInGetErrorText(i, err, 255);
sprintf(buf, "WAVEIN InStart (%d): %s", i, err);
MessageBox(NULL, buf, "Error", MB_ICONERROR|MB_OK);
return;
}
/* save file */
/* clean up */
/* let them know it's done */
return;
}
I hope I'm going through the right process. There should be a library for this :P