U are right , however I still don't know what is the relevant part of code. Maybe these parts:
Code:
int main(int argc, char *argv[])
{
const int NUMPTS = 44100 * 10; // 10 seconds
int sampleRate = 44100;
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;
memset(waveIn,0,NUMPTS);
// Specify recording parameters
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;
result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
0L, 0L, WAVE_FORMAT_DIRECT);
Code:
void SoundOutWrite(soundOut* so, char* data, int size)
{
WAVEHDR* current;
int written = 0;
int i;
while(size > 0)
{
while(so->freeBuff <= 0) //go on only if we have some free buffers.
Sleep(100);
//note for the disbelievers : usually we got much more than one free buffer,
//so I put this busy waiting here just in case.
//I couldn't use semaphores or Critical section because of some reasons related
//to this case.
current = &(so->buffers[so->firstBuff].header); //current header.
//first make sure the header we're going to use is unprepared
if(current->dwFlags & WHDR_PREPARED)
{
waveOutUnprepareHeader(so->hWaveOut, current, sizeof(WAVEHDR));
//I don't actually know yet why can we unprepare stuff at the callback function.
//there is probably some reason for that.
}
if(size > SOUND_BUFFER_SIZE)
{
memcpy(current->lpData,(char*)(data+written),SOUND_BUFFER_SIZE);
size-=SOUND_BUFFER_SIZE;
written+=SOUND_BUFFER_SIZE;
current->dwBufferLength = SOUND_BUFFER_SIZE;
}
else
{
memset(current->lpData,0,SOUND_BUFFER_SIZE); //TODO: check
memcpy(current->lpData,(char*)(data+written),size);
written+=size;
size = 0;
current->dwBufferLength = size;
}
EnterCriticalSection(&(so->freeSection));
so->freeBuff--;
LeaveCriticalSection(&(so->freeSection));
so->firstBuff = (so->firstBuff+1)%SOUND_BUFFERS;
waveOutPrepareHeader(so->hWaveOut, current, sizeof(WAVEHDR));
waveOutWrite(so->hWaveOut, current, sizeof(WAVEHDR));
}
Code:
//the callback function.
void CALLBACK SoundOutProc(
HWAVEOUT hWaveOut,
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2
)
{
//printf("I wuz hear\n");
if(uMsg != WOM_DONE) //ignore calls that occur due to opening and closing the device.
return;
//pointer to the soundOut struct.
soundOut* so = (soundOut*)dwInstance;
so->lastBuff = (so->lastBuff+1)%SOUND_BUFFERS;
EnterCriticalSection(&(so->freeSection));
so->freeBuff++; //we got another free buffer.
LeaveCriticalSection(&(so->freeSection));
}