Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm a starter in VC++, and I have a little (for me is a big) problem with real time audio playback. I Use the following code:

#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
#define SOUNDBUFF 65536;
WAVEFORMATEX wf;
WAVEHDR whdr;
HWAVEIN hWaveIn;
HWAVEOUT hWaveOut;
LPSTR lpData;
int main(void)
{
char clpData[65536];
lpData=clpData;
wf.wFormatTag=WAVE_FORMAT_PCM;
wf.nChannels=1;
wf.nSamplesPerSec=8000;
wf.nAvgBytesPerSec=wf.nSamplesPerSec;
wf.nBlockAlign=1;
wf.wBitsPerSample=8;
wf.cbSize=0;
waveInOpen((LPHWAVEIN)&hWaveIn,WAVE_MAPPER,&wf,0,0,CALLBACK_NULL);

whdr.lpData = lpData;
whdr.dwBufferLength = SOUNDBUFF;
whdr.dwBytesRecorded = 0;
whdr.dwFlags = 0L;
whdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn,&whdr,sizeof(whdr));
waveInAddBuffer(hWaveIn,&whdr,sizeof(whdr));
waveInStart(hWaveIn);
int n=0;
do {
} while (!(whdr.dwFlags & WHDR_DONE));
waveInUnprepareHeader((HWAVEIN)hWaveOut,&whdr,sizeof(whdr));
waveInClose(hWaveIn);
waveOutOpen(&hWaveOut,WAVE_MAPPER,&wf,0,0,CALLBACK_NULL);
waveOutPrepareHeader(hWaveOut,&whdr,sizeof(whdr));
waveOutWrite(hWaveOut,&whdr,sizeof(whdr));
n=0;
do {
} while (!(whdr.dwFlags & WHDR_DONE));
waveOutUnprepareHeader(hWaveOut,&whdr,sizeof(whdr));
waveOutClose(hWaveOut);
return 0;
}


It's work fine, but I would hear real time, what is input buffer, and continuously. I haven't idea how can i make it, i don't understand, how can i read a buffer when is under writing. Please help, cause I probe it a week ago and no results.

Thanks for everyone!
Posted
Updated 27-Aug-10 0:27am
v2

1 solution

If you're a beginner in C++, you should learn C++ and not lose yourself in the vagaries of different windows APIs.

I can't really understand what you're trying to ask, tho. You want it to work in real time and it doesn't ?
 
Share this answer
 
Comments
Sandeep Mewara 27-Aug-10 13:52pm    
Comment from OP: Ah, sorry form my English. I'm a beginner in audio not in c++ , i write it wrong. And Yes, I would hearing realtime what is comming in my input.
Aswin Waiba 20-Dec-12 9:16am    
Don't really know how it is done but would suggest using streams(input and output streams). And also have a look at producer consumer problem. That should clear up your doubts. And to solve that you could use mutex or more appropriate in your case semaphores for access control to shared memory resource.

Also one way of going about it. Correct me if I am wrong. You could have three buffers. One Input Buffer, One main buffer, one for output. The shared buffer being the main buffer. You could add contents to the Inputbuffer as it comes. And read from output buffer as you need.
You would add contents from Input buffer to Main Buffer, and From Main Buffer to output buffer in a round robin pattern.
I am not sure whether this will be helpful. You decide.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900