Archived

This topic is now archived and is closed to further replies.

How to play sound data?

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to load a wav file but i dont know how to play it when i have loaded it. Is there any function that i can use to play the sound data in the wav file which i have loaded?

Share this post


Link to post
Share on other sites
Here is how you can play a wav sound in Win32, this is a copy and paste straight from my WaveLoader utility class. This is an example of synchronous playback, i.e. it doesn''t return until the sound is finished. It''s easy to change it to asynchronous.


void WaveLoader::syncPlay(WAVEFORMATEX *format, char* data, DWORD data_size, DWORD numLoops)
{
HWAVEOUT hWaveOut;
MMRESULT mres=waveOutOpen(&hWaveOut,WAVE_MAPPER,format,NULL,0,WAVE_FORMAT_DIRECT);
if(mres!=0)
return;
WAVEHDR hdr;
memset(&hdr,0,sizeof(hdr));
hdr.lpData=data;
hdr.dwBufferLength=data_size;
hdr.dwLoops=numLoops;
hdr.dwFlags=WHDR_BEGINLOOP | WHDR_ENDLOOP;
mres=waveOutPrepareHeader(hWaveOut,&hdr,sizeof(hdr));
if(mres!=0)
{
waveOutClose(hWaveOut);
return;
}
DWORD delay=data_size*1000/format->nAvgBytesPerSec*hdr.dwLoops;
mres=waveOutWrite(hWaveOut,&hdr,sizeof(hdr));
Sleep(delay);
waveOutReset(hWaveOut);
waveOutUnprepareHeader(hWaveOut,&hdr,sizeof(hdr));
waveOutClose(hWaveOut);
}

Share this post


Link to post
Share on other sites
Might as well show you my asynchronous implementation too:


HANDLE WaveLoader::hEvent=NULL;
HWAVEOUT WaveLoader::m_hWaveOut=NULL;

void WaveLoader::asyncPlay(WAVEFORMATEX *format, char* data, DWORD data_size, DWORD numLoops, HWND hwnd)
{
if(hEvent!=NULL)
return;

thread_blob* tb=new thread_blob;
if(!tb)
return;
tb->format=format;
tb->data=data;
tb->data_size=data_size;
tb->numLoops=numLoops;
tb->hwnd=hwnd;
CreateThread(NULL,0,Play,tb,0,NULL);
}

void CALLBACK WaveLoader::WaveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
if(uMsg!=WOM_OPEN)
{
if(hEvent)
SetEvent(hEvent);
}
}

DWORD WINAPI WaveLoader::Play(void *blob)
{
if(m_hWaveOut)
return 0;

thread_blob *tb=(thread_blob*)blob;

hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);

MMRESULT mres=waveOutOpen(&m_hWaveOut,WAVE_MAPPER,tb->format,(DWORD_PTR)WaveOutProc,0,CALLBACK_FUNCTION | WAVE_FORMAT_DIRECT);
if(mres!=0)
{
CloseHandle(hEvent);
hEvent=NULL;
delete tb;
return 0;
}
WAVEHDR hdr;
memset(&hdr,0,sizeof(hdr));
hdr.lpData=tb->data;
hdr.dwBufferLength=tb->data_size;
hdr.dwLoops=tb->numLoops;
hdr.dwFlags=WHDR_BEGINLOOP | WHDR_ENDLOOP;
mres=waveOutPrepareHeader(m_hWaveOut,&hdr,sizeof(hdr));
if(mres!=0)
{
CloseHandle(hEvent);
hEvent=NULL;
waveOutClose(m_hWaveOut);
delete tb;
return 0;
}
mres=waveOutWrite(m_hWaveOut,&hdr,sizeof(hdr));

WaitForSingleObject(hEvent,INFINITE);

waveOutReset(m_hWaveOut);
waveOutUnprepareHeader(m_hWaveOut,&hdr,sizeof(hdr));

CloseHandle(hEvent);
hEvent=NULL;

waveOutClose(m_hWaveOut);

m_hWaveOut=NULL;

if(tb->hwnd)
PostMessage(tb->hwnd,WM_APP_ASYNC_PLAY_DONE,0,0);

delete tb;
return 0;
}

void WaveLoader::asyncStop()
{
if(m_hWaveOut)
waveOutReset(m_hWaveOut);
}



[edited by - Mastaba on November 7, 2003 12:34:16 PM]

Share this post


Link to post
Share on other sites