Very strange behaviour WaveInProc.

G

Guest

Hello. I need help.
I'm writing an application that requires audio recording and I have decided
to use waveform audio interface.
I'm able to process WIM_DATA message but when i call waveInStop the buffer
is not marked as done until it's full. The documentation says that calling
the sequence
WaveinStop(HWAVEIN) we stop recording marking a buffer as done, end the
member dwbyterecorded contains the amount of data.
why calling WaveInstop then WaveInReset and then WaveInClose the application
deadlocks.
Moreover why Microsoft doesn't give a more exaustive reference on wave
interface. We are getting crazy on guessing the behaviour of the
interface.Very little explanation I found on the owner web site.
We know that we couldn't call any system define functions inside WaveInProc
but a lot of indetermination is causing me blindness.
Follow the code: MANY THANK TO YOU ALL IN ADVANCE.

// WaveIn.cpp : definisce le routine di inizializzazione per la DLL.
#include "stdafx.h"
#include "WaveIn.h"
#include "Mmreg.h"//per operazioni wave
#include "Mmsystem.h"//idem
#include "Windows.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static WAVEFORMATEX *formatoUsed ;
static WAVEHDR headwav[2] ;
static LPWAVEHDR pwh;//Long Pointer to WAVEHEADER, used to convert dwParam1
in the callback function
static int bufSize=44100;
static int *times;
static CFile *AudioFile;
static HWAVEIN devId;
static int byterec;
// P R E P A R A T I O N ************
BEGIN_MESSAGE_MAP(CWaveInApp, CWinApp) END_MESSAGE_MAP()
CWaveInApp::CWaveInApp() {}
CWaveInApp theApp; BOOL CWaveInApp::InitInstance()
{CWinApp::InitInstance();return TRUE;}
// E N D P R E P A R A T I O N *********************

//*************************** F I R S T ** T O ** D E F I N E : C A L L B A
C K *************************
static void CALLBACK waveInProc(HWAVEIN hwi, UINT uMsg, DWORD dwInstance,
DWORD dwParam1, DWORD dwParam2)
{int buf_len=0;
switch(uMsg)
{ case WIM_DATA:
(*times)++ ;
if(waveInUnprepareHeader(hwi,(LPWAVEHDR)dwParam1,sizeof(WAVEHDR))==MMSYSERR_NOERROR )
{ //memcpy(buffer,((LPWAVEHDR)dwParam1)->lpData,bufSize);
buf_len=((LPWAVEHDR)dwParam1)->dwBytesRecorded;
((LPWAVEHDR)dwParam1)->dwUser=0;
((LPWAVEHDR)dwParam1)->dwBufferLength=bufSize;
((LPWAVEHDR)dwParam1)->dwFlags=0;
int result=waveInPrepareHeader(hwi,((LPWAVEHDR)dwParam1),sizeof(WAVEHDR));
result=waveInAddBuffer(hwi,((LPWAVEHDR)dwParam1),sizeof(WAVEHDR));
}
if(*times==1){
AudioFile = new CFile("C:\\data.wav", CFile::modeCreate | CFile::modeWrite);
LPSTR alfa = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
AudioFile->Write(alfa,44);
AudioFile->Write(((LPWAVEHDR)dwParam1)->lpData,bufSize);
byterec=buf_len;
AudioFile->Close();}
else
{
AudioFile = new CFile("C:\\data.wav", CFile::modeWrite);
AudioFile->SeekToEnd();
AudioFile->Write(((LPWAVEHDR)dwParam1)->lpData,buf_len);
byterec+=buf_len;
AudioFile->Close();
}// end of WIM_DATA
}
}
//****************** S E C O N D T O D E F I N E **
STARTWAVEIN**********************************
__declspec(dllexport) int __stdcall StartWaveIn(){formatoUsed= new
WAVEFORMATEX;times = new int;*times=0;
WAVEINCAPS *caps ;caps = new WAVEINCAPS;
waveInGetDevCaps(WAVE_MAPPER ,caps,sizeof(WAVEINCAPS));
if((caps->dwFormats & WAVE_FORMAT_4M16) == 0)return -1;
formatoUsed->nSamplesPerSec=44100;
formatoUsed->wFormatTag=WAVE_FORMAT_PCM;
formatoUsed->wBitsPerSample=16;
formatoUsed->nChannels=2;
formatoUsed->nBlockAlign=(formatoUsed->nChannels *
formatoUsed->wBitsPerSample)/8;
formatoUsed->nAvgBytesPerSec=(formatoUsed->nSamplesPerSec *
formatoUsed->nChannels * (formatoUsed->wBitsPerSample/8));
int result=waveInOpen(&devId, WAVE_MAPPER, formatoUsed,(DWORD)waveInProc, 0,
CALLBACK_FUNCTION);
if(bufSize % formatoUsed->nBlockAlign != 0 ){ bufSize +=
formatoUsed->nBlockAlign - (bufSize % formatoUsed->nBlockAlign);}
headwav[1].lpData=new char[bufSize];//(LPSTR)bufData;
headwav[1].dwBufferLength=bufSize;
headwav[1].dwFlags=0;
headwav[2].lpData=new char[bufSize];//(LPSTR)bufData;
headwav[2].dwBufferLength=bufSize;
headwav[2].dwFlags=0;
waveInPrepareHeader(devId,&headwav[1],sizeof(WAVEHDR));
waveInPrepareHeader(devId,&headwav[2],sizeof(WAVEHDR));
waveInAddBuffer(devId,&headwav[1],sizeof(WAVEHDR));
waveInAddBuffer(devId,&headwav[2],sizeof(WAVEHDR));
delete caps;
result= waveInStart(devId);
return result;
}
//******************* T H I R D T O D E F I N E **********************
STOPWAVEIN AND SAVE TO .WAV
__declspec(dllexport) int __stdcall StopWaveIn()
{waveInStop(devId);
Sleep(500);
// now it's time to create the header overriding previous blank information.
AudioFile = new CFile("C:\\data.wav", CFile::modeWrite);
int chunksize=byterec+36;
AudioFile->SeekToBegin();
LPSTR alfa = "RIFF";
AudioFile->Write(alfa,4);
AudioFile->Write(&chunksize,sizeof(int));
alfa = "WAVEfmt ";
AudioFile->Write(alfa,8);
int q=16;
AudioFile->Write(&q,sizeof(int));
formatoUsed->nSamplesPerSec=44100;
formatoUsed->wFormatTag=WAVE_FORMAT_PCM;
formatoUsed->wBitsPerSample=16;
formatoUsed->nChannels=2;
formatoUsed->nBlockAlign=(formatoUsed->nChannels *
formatoUsed->wBitsPerSample)/8;
formatoUsed->nAvgBytesPerSec=(formatoUsed->nSamplesPerSec *
formatoUsed->nChannels * (formatoUsed->wBitsPerSample/8));
AudioFile->Write(formatoUsed,16); // la struttura WAVEFORMATEX ha size pari
a 16 no cbSize;
alfa = "data";
AudioFile->Write(alfa,4);
AudioFile->Write(&byterec,sizeof(int));
AudioFile->Close();
waveInUnprepareHeader(devId,&headwav[1],sizeof(WAVEHDR));
waveInUnprepareHeader(devId,&headwav[2],sizeof(WAVEHDR));
delete[] headwav[0].lpData;
delete[] headwav[1].lpData;
delete formatoUsed;
delete times;
return byterec;//This function returns byte recorded.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top