0
101 Nov 07, 2009 at 06:24

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 ?

#### 5 Replies

Please log in or register to post a reply.

0
101 Nov 07, 2009 at 14:00

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?

0
167 Nov 07, 2009 at 17:40

Post your code, please. Also be sure you’ve read the docs.

0
101 Nov 07, 2009 at 18:43

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;
}

0
101 Nov 07, 2009 at 18:47

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

alSourcePlay(source);
alDeleteBuffers(1, &buffer);
alDeleteSources(1, &source);
alutExit();

0
167 Nov 07, 2009 at 20:10

First of all, please use the …[/code[b][/b]] 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. [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.