DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Programming & Development > Sound and Music Programming
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 11-06-2009, 11:24 PM   #1
iceboylsg
New Member
 
Join Date: Oct 2009
Location: Malaysia
Posts: 21
Default Question about playing wav file

hmm..i still new to this openAL. I write my own code to run the wav file. BUt i dunno how to play the wav for only once? which function should i use? if i put the alsourcestop after the alsourceplay, it produces no sound..which function should i use ?
iceboylsg is offline   Reply With Quote
Old 11-07-2009, 07:00 AM   #2
iceboylsg
New Member
 
Join Date: Oct 2009
Location: Malaysia
Posts: 21
Default alSourcePlay function

i have question. In my code, when i using the alSourceplay function without looping, it comes out no sound. But, if i put this function inside a loop such as do and while loop or for loop, it produces the sound? OMG, why?
iceboylsg is offline   Reply With Quote
Old 11-07-2009, 10:40 AM   #3
Reedbeta
DevMaster Staff
 
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
Default Re: Question about playing wav file

Post your code, please. Also be sure you've read the docs.
___________________________________________
Currently working at Sucker Punch
reedbeta.com - OpenGL demos and other projects
Luabridge - a lightweight, dependency-free C++/Lua binding library.
CD Lite - an unobtrusive, minimal CD player application for Windows.
Reedbeta is online now   Reply With Quote
Old 11-07-2009, 11:43 AM   #4
iceboylsg
New Member
 
Join Date: Oct 2009
Location: Malaysia
Posts: 21
Default Re: Question about playing wav file

if using this code, it works fine. But if i take off the do while loop, it can't produce the sound.
Code:
int main(int argc, char *argv[]) { alutInit(&argc, argv); ALuint buffer, source; ALenum error; ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; ALenum format; ALsizei size; ALsizei freq; ALboolean loop; ALvoid* data; alGenBuffers(1, &buffer); alutLoadWAVFile("bug.wav", &format, &data, &size, &freq, &loop); if ((error = alGetError()) != AL_NO_ERROR) { printf("alutLoadWAVFile exciting_sound.wav : %d", error); return 0; } else { printf("success loading......\n"); } alBufferData(buffer, format, data, size, ALsizei(freq)); alutUnloadWAV(format, data, size, ALsizei(freq)); alGenSources(1, &source); alSourcei (source, AL_BUFFER, buffer ); alSourcef (source, AL_PITCH, 1.0f ); alSourcef (source, AL_GAIN, 1.0f ); alSourcefv(source, AL_POSITION, SourcePos); alSourcefv(source, AL_VELOCITY, SourceVel); alSourcei (source, AL_LOOPING, AL_TRUE); alListenerfv(AL_POSITION, ListenerPos); alListenerfv(AL_VELOCITY, ListenerVel); alListenerfv(AL_ORIENTATION, ListenerOri); printf("Controls:\n"); printf("1 = play\n"); printf("2 = pause\n"); printf("3 = stop\n"); printf("0 = exit\n"); char key; do { key = _getch(); if (key == '1') alSourcePlay(source); if (key == '2') alSourcePause(source); if (key == '3') alSourceStop(source); } while (key != '0'); alDeleteBuffers(1, &buffer); alDeleteSources(1, &source); alutExit(); return 0; }

Last edited by Reedbeta : 11-07-2009 at 01:06 PM.
iceboylsg is offline   Reply With Quote
Old 11-07-2009, 11:47 AM   #5
iceboylsg
New Member
 
Join Date: Oct 2009
Location: Malaysia
Posts: 21
Default Re: Question about playing wav file

if i put like this, it has no sound!..why=.=?


alSourcePlay(source);
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alutExit();
iceboylsg is offline   Reply With Quote
Old 11-07-2009, 01:10 PM   #6
Reedbeta
DevMaster Staff
 
Join Date: Oct 2004
Location: Seattle, WA
Posts: 3,707
Default Re: Question about playing wav file

First of all, please use the [code]...[/code] tags in the forum to post code.

Second, it looks like your problem is you don't realize alSourcePlay() is asynchronous. alSourcePlay() just *starts* playing a sound - then returns control to you immediately. It does not wait for the sound to be finished. If you think about it this is exactly what you want for making a game or other interactive application. You don't want the game to pause just because a sound played.

So, in your second code sample, you are telling the sound to start playing and then immediately deleting it and exiting. So you do not hear anything because you stopped the sound before it had a chance to play at all. In the first code sample, it works because your program waits for you to press a key in getch(), and while it's waiting the sound has time to play.

If you want to play the sound once and then exit, you have to wait for the sound to finish yourself. I'm sure there are AL functions to let you do this, although I don't know which ones since I'm not very familiar with AL myself. If you look around in the API I'm sure you can find it.
___________________________________________
Currently working at Sucker Punch
reedbeta.com - OpenGL demos and other projects
Luabridge - a lightweight, dependency-free C++/Lua binding library.
CD Lite - an unobtrusive, minimal CD player application for Windows.
Reedbeta is online now   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 05:05 PM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.