# play wav file while moving.

18 replies to this topic

### #1stranomavero

Member

• Members
• 35 posts

Posted 01 October 2007 - 09:39 AM

Hello everyone!
finally i managed to make openAL work and it's great.

I'm creating a 3D environment to display urban developments.
I want to play a wav file (steps) while the user is moving inside the environment and don't play it while is standing still.

that's the bit of code i wrote to achieve this outcome [obviously it doesn't work!!! :( ]:

float x[2],y[2];
float xmov,ymov;

for (int j=0;j>0;j++){
xmov=x[j+1]-x[j];
ymov=y[j+1]-y[j];
if(xmov=0,ymov=0) alSourcef(g_sources[2],AL_GAIN,0);
else alSourcef(g_sources[2],AL_GAIN,1);
}


do i need to add a sort of time counter to make j change every second (or half a second)?

### #2Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 01 October 2007 - 01:53 PM

The code in your for-block is never executed. :)

You initialize j to be zero, but the code is only executed if j is greater than zero. Even if it was executed, it would loop 2^31 times and access memory it doesn't have access to :blink:
"Stupid bug! You go squish now!!" - Homer Simpson

### #3stranomavero

Member

• Members
• 35 posts

Posted 01 October 2007 - 01:58 PM

That's a good idea! ;)
i'm learning c++ at the moment...and as u can see,i'm still a newbie!
i get an error if i get a rid of the j>0 part.
is
for (int j=1;j>0;j++){

ok?

do u know a better way that my lame attempt?
I need to play a wav file (footsteps) ONLY when the user is moving...that's why a put the condition on the coordinates.

### #4Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 01 October 2007 - 02:24 PM

A simpler way might be to just stop or start the sound when the player hits any of the movement keys:

if(any of the movement keys are pressed && source is not playing)

alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);

alSourcePlay(g_sources[2]);

else

alSourcei (g_sources[2], AL_LOOPING, AL_FALSE);



"Stupid bug! You go squish now!!" - Homer Simpson

### #5stranomavero

Member

• Members
• 35 posts

Posted 01 October 2007 - 02:53 PM

thank u very much!
that sounds a much more clever way to solve my problem! :)

i'm trying something like this:

void main()
{
char KeyStroke;

do
{
KeyStroke =	getch();

if (KeyStroke == 0)
{
KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
switch (KeyStroke)
{
case UP_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case DOWN_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case LEFT_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case RIGHT_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;

}
}
else
alSourcei (g_sources[2], AL_LOOPING, AL_FALSE);
}
}

...still have to fix few errors though! :)

### #6Reedbeta

DevMaster Staff

• 5308 posts
• LocationSanta Clara, CA

Posted 01 October 2007 - 03:28 PM

please use the [code][/code]tags when you post code.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #7stranomavero

Member

• Members
• 35 posts

Posted 01 October 2007 - 03:44 PM

sorry...will do from now on

### #8stranomavero

Member

• Members
• 35 posts

Posted 03 October 2007 - 11:25 AM

any other tips?
i'm still groping in the dark... :(

### #9Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 03 October 2007 - 02:35 PM

You'll to be a little more specific. What are you having problems with?
"Stupid bug! You go squish now!!" - Homer Simpson

### #10stranomavero

Member

• Members
• 35 posts

Posted 03 October 2007 - 03:04 PM

Hi Kenneth!
sorry for bothering u again,but i tried to change the code with the lines i quoted above,but it doesn't work.

it says :
C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp In function int main(int, char**)':
197 C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp expected primary-expression before "void"
197 C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp expected ;' before "void"

### #11Reedbeta

DevMaster Staff

• 5308 posts
• LocationSanta Clara, CA

Posted 03 October 2007 - 07:22 PM

Please post the code that is causing the errors (and indicate which line is line 197). It doesn't do us any good to see the error messages without the code.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #12stranomavero

Member

• Members
• 35 posts

Posted 04 October 2007 - 11:43 AM

ok.

I had to change the code in the way that it plays the file if the position change,i.e. if x and y change. I understand it would be better do it checking if any key is pressed,but i have to do it with coordinates for other issues.

the code now is not giving errors...it just crashes. i mean,the executable file crashes.

the offending part of the code is:

float x[2],y[2];

float xmov,ymov;

for (int j=1;j<1000000;j++){

xmov=x[j+1]-x[j];

ymov=y[j+1]-y[j];

if(xmov=0,ymov=0) alSourcef(g_sources[2],AL_GAIN,0);

else alSourcef(g_sources[2],AL_GAIN,1);

}



thanks!

### #13Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 04 October 2007 - 01:54 PM

You have declared x and y to only hold 2 floats, but you are trying to read 999998 floats past the end. I would be surprised if it didn't chrash :)

Why this big loop anyway? The way I see it, you are to figure out if the player has moved since the last movement update, right? The code I suggested earlier was meant to replace that part...
"Stupid bug! You go squish now!!" - Homer Simpson

### #14stranomavero

Member

• Members
• 35 posts

Posted 04 October 2007 - 02:01 PM

oooops....u can notice that my c++ skills are dreadful! :)

my problem is that my supervisor wants the code to check coordinates changes and not key presses.

maybe i wrote the code in the wrong way...what i wanted it to do is check the position at the time j:if it's different from the one at the time j+1,then play the audio file.

i understand the solution u suggested is better,but is it possible to achieve the same result using coordinates?

### #15stranomavero

Member

• Members
• 35 posts

Posted 17 October 2007 - 04:51 PM

i wrote that bit of nasty (for me!!) code this way:

int j=0;
float x[j],y[j];
float xmov,ymov;

for(;;j++){
xmov=x[j+1]-x[j];
ymov=y[j+1]-y[j];
if(xmov!=0 || ymov!=0)
alSourcei(g_sources[2],AL_LOOPING,AL_TRUE),alSourcePlay(g_sources[2]);
else alSourcei(g_sources[2],AL_LOOPING,AL_FALSE);
Sleep(250);
}


it doesn't give me any error...but it doesn't work too.
I didn't set any limit to j as i want it to loop endlessly. is this the problem?

### #16gardon

Valued Member

• Members
• 282 posts

Posted 25 October 2007 - 12:55 AM

I think you need to brush up on your c++ skills before attempting this type of stuff. If you don't know how to access array data then you're in for a world of hurt :(

### #17stranomavero

Member

• Members
• 35 posts

Posted 25 October 2007 - 11:15 AM

thank you for the hint!
i'm sure i really need to improve my skills in C++...that's what i'm doing at the moment with lots of manuals! :)

by the way,do you think i need a timer to make this stuff work?

### #18gardon

Valued Member

• Members
• 282 posts

Posted 25 October 2007 - 04:59 PM

I'm not really sure how OpenGL/AL works, but from experience you will definitely need a game timer for your game. Timers are good because you can move the player the same amount on all systems, regardless of framerate.

Generally you will set a variable like so:

float timeSinceLastFrame = Timer.GetTime();

Or something, then you can do the following:

Player.Move(timeSinceLastFrame * playerMovement);

If the user has a higher framerate, then the timeSinceLastFrame Variable will be lower, since the frame will have passed quicker. The opposite is true for slower framerates. The playerMovement variable is used to make a general speed to your player (since the timeSinceLastFrame Variable will tend to be a little low). This will map it to your world speed.

But hey, you're learning, that's the key. Keep going, and keep your motivation up. You'll have it going in no time.

The reason I posted that comment was because I've been programming in c++ for years, and when I look at this type of stuff it still baffles me (3D). It's like writing an english paper while trying to learn english at the same time. Hard isn't it? Now imagine knowing english but trying to write a novel.... it's still hard no matter how you slice it :P

Just keep at it dude.

### #19stranomavero

Member

• Members
• 35 posts

Posted 29 November 2007 - 10:47 PM

i like your example...and i like it even more because i'm also learning english and i'm supposed to write a "novel" about my project in a proper formal english.

anyway,coming back to business,i realized i needed something like a timer also for other issues related to the creation of the audio for my 3D environment so i'm working on it.
actually i kind of fixed the problem in another way:the guys who are creating the environment are providing me with a text file updated every fraction of seconf from which i can get the position of listener and moving objects.
then i just use OpenAL and i have moving sounds and doppler effect.

when i have a bit of spare time i'm still trying to add that walking noise,but i haven't still realized how to use the timer properly.

is the timer funciot included in windows.h?

cheers again

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users