About wavein API
-
Hello I have a program using waveIn API. This is part of my code. Originally, my program has a deadlock while running waveInReset. I google it and it is said putting the wavein function in the callback function is forbidden. So I delete the code which I put waveinstop in the callback function. It can run waveInReset and waveInunprepareheader successfully. But it halt at waveInClose. I can't find information about waveInClose halt. Can anybody solve this problem?Thx, Jane.
while(1){ if(isopen==1){ if(waveInOpen(&hWavin,WAVE_MAPPER, &wfx,(DWORD_PTR)WaveInProc ,0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) { isopen=1; for(int i=0;i<4;i++) { mmrr = waveInPrepareHeader(hWavin, headersend + i, sizeof(WAVEHDR)); mmrr = waveInAddBuffer(hWavin, headersend + i, sizeof(WAVEHDR)); } mmrr = waveInStart(hWavin); } } if(isclose==1) { if(hWavin) { mmrr=waveInReset(hWavin); for(int i=0;i<4;i++) { mmrr=waveInUnprepareHeader(hWavin,headersend+i, sizeof(WAVEHDR)); } for(int i=0;i<4;i++) { mmrr=waveInReset(hWavin); } mmrr=waveInClose(hWavin); sclose=0; } } } static void CALLBACK WaveInProc(HWAVEIN hw, UINT uMsg,DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD dwParam2) { WAVEHDR* curhdrin = (WAVEHDR*)dwParam1; if(uMsg != WIM_DATA) return; if(isclose==1){ Sleep(1000); return; } ///other code//// waveInAddBuffer(hw, curhdrin, sizeof(WAVEHDR)); }
-
Hello I have a program using waveIn API. This is part of my code. Originally, my program has a deadlock while running waveInReset. I google it and it is said putting the wavein function in the callback function is forbidden. So I delete the code which I put waveinstop in the callback function. It can run waveInReset and waveInunprepareheader successfully. But it halt at waveInClose. I can't find information about waveInClose halt. Can anybody solve this problem?Thx, Jane.
while(1){ if(isopen==1){ if(waveInOpen(&hWavin,WAVE_MAPPER, &wfx,(DWORD_PTR)WaveInProc ,0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR) { isopen=1; for(int i=0;i<4;i++) { mmrr = waveInPrepareHeader(hWavin, headersend + i, sizeof(WAVEHDR)); mmrr = waveInAddBuffer(hWavin, headersend + i, sizeof(WAVEHDR)); } mmrr = waveInStart(hWavin); } } if(isclose==1) { if(hWavin) { mmrr=waveInReset(hWavin); for(int i=0;i<4;i++) { mmrr=waveInUnprepareHeader(hWavin,headersend+i, sizeof(WAVEHDR)); } for(int i=0;i<4;i++) { mmrr=waveInReset(hWavin); } mmrr=waveInClose(hWavin); sclose=0; } } } static void CALLBACK WaveInProc(HWAVEIN hw, UINT uMsg,DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD dwParam2) { WAVEHDR* curhdrin = (WAVEHDR*)dwParam1; if(uMsg != WIM_DATA) return; if(isclose==1){ Sleep(1000); return; } ///other code//// waveInAddBuffer(hw, curhdrin, sizeof(WAVEHDR)); }
How does isclose ever get set to 1? Why is there no waveInStop() call anywhere? Why do you use the Sleep() call in the waveInProc? That's really going to mess things up if isclose==1. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
How does isclose ever get set to 1? Why is there no waveInStop() call anywhere? Why do you use the Sleep() call in the waveInProc? That's really going to mess things up if isclose==1. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Thx for ur reply. I have a button that would let user click to stop recording. That would make isclose = 1; Originally, I use waveInStop but I waveInStop only stop recording. waveInReset would stop recording and set the position to 0. I see some information in the Internet that says use waveInUnPrepareHeader before using waveInReset. Originally, I don't use Sleep() function,but it has same problem. I write it in a new small program as follow. It halts at waveInReset. If I delete waveInReset, waveInUnPrepareHeader and waveInClose falses and returns 33.
#include "stdafx.h" #include #include #include #include #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Winmm.lib") using namespace System; using namespace std; static void CALLBACK WaveInProc(HWAVEIN, UINT, DWORD , DWORD , DWORD ); static const int SNDNBUF = 2; static const int SNDSIZEBUF = 320; HWAVEOUT hWavout; MMRESULT mmrr; WAVEHDR headersend[SNDNBUF]; char hdrDatasend[SNDSIZEBUF*SNDNBUF]; HWAVEIN hWavin; int _tmain(int argc, _TCHAR* argv[]) { WAVEFORMATEX wfx; wfx.nSamplesPerSec = 8000; wfx.nChannels = 1; wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wBitsPerSample = 16; wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8); wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; memset(headersend, 0,sizeof(WAVEHDR)*SNDNBUF); for(int i=0;i<sndnbuf;i++){ headersend[i].lpData = hdrDatasend + i*SNDSIZEBUF; headersend[i].dwBufferLength = SNDSIZEBUF;} memset(hdrDatasend,0,SNDSIZEBUF*SNDNBUF); if(waveInOpen(&hWavin,WAVE_MAPPER, &wfx,(DWORD_PTR)WaveInProc ,0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR){ for(int i=0;i{ mmrr = waveInPrepareHeader(hWavin, headersend + i, sizeof(WAVEHDR)); mmrr = waveInAddBuffer(hWavin, headersend + i, sizeof(WAVEHDR));} mmrr = waveInStart(hWavin); } int i; cin >> i; mmrr=waveInStop(hWavin); mmrr=waveInReset(hWavin); Sleep(500); for(int i=0;i<sndnbuf;i++){ if ((headersend+i)->dwFlags & WHDR_PREPARED){ mmrr=waveInUnprepareHeader(hWavin,headersend+i, sizeof(WAVEHDR));} Sleep(500); mmrr=waveInClose(hWavin); cin >> i; return 0; } static void CALLBACK WaveInProc(HWAVEIN hw, UINT uMsg,DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD dwParam2) { WAVEHDR* curhdrin = (WAVEHDR*)dwParam1; if(uMsg != WIM_DATA)return; waveInAddBuffer(hw, curhdrin, sizeof
-
Thx for ur reply. I have a button that would let user click to stop recording. That would make isclose = 1; Originally, I use waveInStop but I waveInStop only stop recording. waveInReset would stop recording and set the position to 0. I see some information in the Internet that says use waveInUnPrepareHeader before using waveInReset. Originally, I don't use Sleep() function,but it has same problem. I write it in a new small program as follow. It halts at waveInReset. If I delete waveInReset, waveInUnPrepareHeader and waveInClose falses and returns 33.
#include "stdafx.h" #include #include #include #include #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Winmm.lib") using namespace System; using namespace std; static void CALLBACK WaveInProc(HWAVEIN, UINT, DWORD , DWORD , DWORD ); static const int SNDNBUF = 2; static const int SNDSIZEBUF = 320; HWAVEOUT hWavout; MMRESULT mmrr; WAVEHDR headersend[SNDNBUF]; char hdrDatasend[SNDSIZEBUF*SNDNBUF]; HWAVEIN hWavin; int _tmain(int argc, _TCHAR* argv[]) { WAVEFORMATEX wfx; wfx.nSamplesPerSec = 8000; wfx.nChannels = 1; wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wBitsPerSample = 16; wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8); wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; memset(headersend, 0,sizeof(WAVEHDR)*SNDNBUF); for(int i=0;i<sndnbuf;i++){ headersend[i].lpData = hdrDatasend + i*SNDSIZEBUF; headersend[i].dwBufferLength = SNDSIZEBUF;} memset(hdrDatasend,0,SNDSIZEBUF*SNDNBUF); if(waveInOpen(&hWavin,WAVE_MAPPER, &wfx,(DWORD_PTR)WaveInProc ,0, CALLBACK_FUNCTION) == MMSYSERR_NOERROR){ for(int i=0;i{ mmrr = waveInPrepareHeader(hWavin, headersend + i, sizeof(WAVEHDR)); mmrr = waveInAddBuffer(hWavin, headersend + i, sizeof(WAVEHDR));} mmrr = waveInStart(hWavin); } int i; cin >> i; mmrr=waveInStop(hWavin); mmrr=waveInReset(hWavin); Sleep(500); for(int i=0;i<sndnbuf;i++){ if ((headersend+i)->dwFlags & WHDR_PREPARED){ mmrr=waveInUnprepareHeader(hWavin,headersend+i, sizeof(WAVEHDR));} Sleep(500); mmrr=waveInClose(hWavin); cin >> i; return 0; } static void CALLBACK WaveInProc(HWAVEIN hw, UINT uMsg,DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD dwParam2) { WAVEHDR* curhdrin = (WAVEHDR*)dwParam1; if(uMsg != WIM_DATA)return; waveInAddBuffer(hw, curhdrin, sizeof
The order should be: waveInStop waveInReset waveInUnprepareHeader (for all buffers) And get rid of the Sleep() calls. There is absolutely NO reason for them. When you prepare the headers, you must initialize dwFlags (Use 0 for input buffers) as well as lpData and dwBufferLength. Also, IMO, "headersend + i" is confusing to read. I'd use "&headersend[i]". Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: