473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Very strange behaviour WaveInProc.

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,s izeof(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)dwParam 1),sizeof(WAVEHDR));
result=waveInAddBuffer(hwi,((LPWAVEHDR)dwParam1),s izeof(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.
}
Nov 17 '05 #1
0 2229

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Syver Enstad | last post by:
Here's the interactive session Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ord('\xe5') 229 >>>...
4
by: Miroslaw Osys | last post by:
Hello everyone! I am using C for almost 10 years and recently was very surprised. Under Slackware Linux 8.1 and gcc 2.95.3 I tried to compile program which simpler version is ----- test.c...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
0
by: johnnykimble | last post by:
Hi, I'm currently having a problem with a Web Service application that uses the standard .net satellite resource mechanism for loading loaclised resources. I have a *.resources.dll in a language...
10
by: conor.robinson | last post by:
The Problem (very basic, but strange): I have a list holding a population of objects, each object has 5 vars and appropriate funtions to get or modify the vars. When objects in the list have...
9
by: fabio.bizzetti | last post by:
Hello all, I went across what seems possibly a bug of the compiler (VisualC 2005, just for the record) or a very strange and non-expected (by me at least) behaviour of the C++ ISO standard. Thus...
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
11
Dormilich
by: Dormilich | last post by:
recently I encounter a very strange behaviour of the session variable. if the cookies are disabled, the session id is totally misplaced (see code). instead being attached to the URL it is placed...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.