-
September 26th, 2009, 10:01 PM
#1
problem recording from mic and writting a wav file
Hi,
It's been 10 years i didnt write any code so i m pretty rusty. I've been trying all
day to make this code work, but there seems to be a buffer problem.
The program only seems to be writting the header, without any audio data. (file size 44 bytes)
What i want : record one second from the mic and write it in .wav format.
In the sample code i used it was written:
LPSTR lpData[SOUNDBUFF];
but this doesnt compile, so i used malloc instead, maybe the problem comes from here...
Any help would be greatly appreciated ! thank you in advance
the code :
WAVEFORMATEX wf;
wf.wFormatTag = WAVE_FORMAT_PCM;
wf.nChannels = 1;
wf.nSamplesPerSec = 8000;
wf.nAvgBytesPerSec = wf.nSamplesPerSec;
wf.nBlockAlign = 1;
wf.wBitsPerSample = 8;
wf.cbSize = 0;
///
#define SOUNDBUFF 8000 // Sound buffer size
WAVEHDR whdr;
HWAVEIN hWaveIn;
//LPSTR lpData[SOUNDBUFF]; // Sound buffer?????????? written like that in sample, but doesnt compile
LPSTR lpData = (LPSTR)malloc(SOUNDBUFF);
whdr.lpData = lpData;
whdr.dwBufferLength = SOUNDBUFF;
whdr.dwBytesRecorded = 0;
whdr.dwFlags = 0L;
whdr.dwLoops = 0L;
/////
waveInOpen(&hWaveIn, WAVE_MAPPER, &wf, 0, 0, CALLBACK_THREAD);
waveInPrepareHeader(hWaveIn, &whdr, sizeof(whdr));
waveInAddBuffer(hWaveIn, &whdr, sizeof(whdr));
waveInStart(hWaveIn);
do
{
this->toolStripStatusLabel2->Text = L"Recording started";
Sleep(100);
}
while(!(whdr.dwFlags & WHDR_DONE));
waveInUnprepareHeader(hWaveIn, &whdr, sizeof(whdr));
waveInClose(hWaveIn);
this->toolStripStatusLabel2->Text = L"Recording completed";
//////// SAVE
int blocksize=SOUNDBUFF;
DWORD bWrite;
WAVHEADER wh;
memcpy(wh.RIFF,"RIFF",4);
wh.bytes = blocksize+36;
memcpy(wh.WAVE,"WAVE",4);
memcpy(wh.fmt,"fmt ",4);
wh.siz_wf=8; //?????? couldnt find what it is
memcpy(wh.data,"data",4);
wh.pcmbytes = blocksize;
wh.wFormatTag =1;
wh.nChannels = 1;
wh.nSamplesPerSec = 8000;
wh.nAvgBytesPerSec = 8000;
wh.nBlockAlign = 1;
wh.wBitsPerSample = 8;
HANDLE hFile = CreateFile(L"test.wav", GENERIC_WRITE, FILE_SHARE_WRITE,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(hFile,(char *)&wh,sizeof(wh),&bWrite,NULL);
WriteFile(hFile,(char *)&whdr.lpData, sizeof(whdr.lpData),&bWrite,NULL);
CloseHandle(hFile);
-
September 27th, 2009, 06:52 AM
#2
Re: problem recording from mic and writting a wav file
What is WAVHEADER? I've never heard of it.
-
September 27th, 2009, 07:24 AM
#3
Re: problem recording from mic and writting a wav file
Originally Posted by njalsonj
so i used malloc instead, maybe the problem comes from here...
Yes that is the problem. Your code assumes whdr.lpData is an array but it is just a pointer
Code:
WriteFile(hFile,(char *)&whdr.lpData, sizeof(whdr.lpData),&bWrite,NULL);
use
Code:
WriteFile(hFile,(char *)&whdr.lpData, SOUNDBUFF,&bWrite,NULL);
Kurt
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|