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 ?
Question about playing wav file
Started by iceboylsg, Nov 07 2009 06:24 AM
5 replies to this topic
#1
Posted 07 November 2009 - 06:24 AM
#2
Posted 07 November 2009 - 02:00 PM
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?
#3
Posted 07 November 2009 - 05:40 PM
Post your code, please. Also be sure you've read the docs.
reedbeta.com - developer blog, OpenGL demos, and other projects
#4
Posted 07 November 2009 - 06:43 PM
if using this code, it works fine. But if i take off the do while loop, it can't produce the sound.
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;
}
#5
Posted 07 November 2009 - 06:47 PM
if i put like this, it has no sound!..why=.=?
alSourcePlay(source);
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alutExit();
alSourcePlay(source);
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alutExit();
#6
Posted 07 November 2009 - 08:10 PM
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.
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.
reedbeta.com - developer blog, OpenGL demos, and other projects
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users












