Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Question about multiple instances of a loop sound sample

Credits go to Bruce Perry, gnolam, and Richard Phipps for helping out!
This thread is locked; no one can reply to it. rss feed Print
Question about multiple instances of a loop sound sample
Epsi
Member #5,731
April 2005
avatar

Hi,

I've got a question about sounds.

For my game I have a weapon (chainsaw) wich should play a sound constantly when the player is pressing the fire button.

I don't have any problem with inputs, but I wonder how to do the sound part correctly.
Are SAMPLEs made for this task ?

Let me explain the code:

I have a global sound sample loaded with the WAV file
SAMPLE *chainsaw = load_wav("Sfx/wpn_chainsaw.wav");

Currently if a characters use its weapon, I just "play_sample()" the SAMPLE corresponding to the weapon type (a global SAMPLE).
But to play the sound with a loop I need control of the SAMPLE individually for each character, because I need to stop the sound when the character stops firing.

Each weapons used by characters (the player and enemies) use the "Weapon" class.
So my idea would be to have a "fire_sfx" SAMPLE member that would play the sound with a loop if required, and I would be able to control each sound individually for each Weapon instance since it's a local member variable.

However, I don't know how to correctly load the SAMPLE for individual loop usage .

If I just use a pointer to the global variable loaded with the WAV file, when I will want to stop the sound loop, it will stop all the instances of the sound sample. (as written in the help file for stop_sample() :

Quote:

If there are several copies of the sample playing, it will stop them all.

E.g: player 1 and 2 are using the chainsaw. Player 1 stops, but the sound SAMPLE is the same for both player (since SAMPLE it's just a pointer) so the sound will be stopped for Player 2 aswell.

How can I have two different SAMPLEs of the same sound file without having to load_sample() for a .wav file on the hard disk for each Weapon instance ? (because I think it will be very slow, I would prefer to copy it from the global variable)

I hope that there is a simple solution, or maybe another method than SAMPLEs.

Thanks :)

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

Richard Phipps
Member #1,632
November 2001
avatar

You are going to have to look into the voice control of the manual to do this. You'll need to play the sample for say player 1 and store the resulting voice channel allocated. Then you can stop that voice channel when needed and still have other weapon sounds (or the same sample) playing for different players.

Quote:

How can I have two different SAMPLEs of the same sound file without having to load_sample() for a .wav file on the hard disk for each Weapon instance ?

I don't understand what the problem is.. You can load a sample and play it multiple times and play as a looped sample. It's in the manual and I think the allegro examples.

Let me know if you need more help..

Epsi
Member #5,731
April 2005
avatar

Quote:

I don't understand what the problem is.. You can load a sample and play it multiple times and play as a looped sample. It's in the manual and I think the allegro examples.

Yes you can play it multiple times, and it works correctly. However if you stop one you stop all the played sounds using that SAMPLE. (I haven't tried it yet but that's what the manual says)

What I would like is to copy the SAMPLE value in a new variable instead of using a pointer to that SAMPLE. But I would like to know how to do that without loading it from the wav file everytime...

I want to stay away from the voices if possible :(

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

gnolam
Member #2,030
March 2002
avatar

Quote:

What I would like is to copy the SAMPLE value in a new variable instead of using a pointer to that SAMPLE. But I would like to know how to do that without loading it from the wav file everytime...

No, you really do want to use voices:

int voice = play_sample(foo, 255, 128, 1000, 1);

voice_stop(voice);

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Richard Phipps
Member #1,632
November 2001
avatar

There is no other way (AFAIK) to do that without voices. As gnolam showed, it doesn't have to involve a total rewrite.

Rick
Member #3,572
June 2003
avatar

Quote:

I want to stay away from the voices if possible

Any reason why?

I think your option is to use voice or load the sound twice, or load it once and make a copy of it.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

gnolam
Member #2,030
March 2002
avatar

Quote:

I think your option is to use voice or load the sound twice, or load it once and make a copy of it.

When the proper way doesn't involve as much as an extra line of code in the playing logic (there's one more variable to store, and that's it), there's no reason to kludge around with copied SAMPLEs. :P

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Rick
Member #3,572
June 2003
avatar

Just giving options g. He said he didn't want to use voices. If he truly hates using voices (not sure why he would, but hey to each their own), then he would need other options.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Epsi
Member #5,731
April 2005
avatar

Well by taking a first look at voices it looked too complicated for what I need.
But gnolam's method seems indeed way more simple than copying SAMPLEs.

Thanks :)

I'll try that right now

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

Bruce Perry
Member #270
April 2000

You might not need to play the sample twice. You might find you're better off with one global voice, which is allocated some of the time and not the rest of the time.

Every frame, calculate the volume of each player using the chain saw, square each volume, and add all those squared volumes together and square-root the result. If your result is 0 (Don't Compare Against Floats), make sure the voice is not allocated; deallocate it if necessary. If your result is nonzero, start the voice up if necessary, and set its volume to the result, clamped at 256 or 255 or whatever Allegro's maximum is.

That way you don't use up as many of the voices in the mixer as you would otherwise. Whether the approach works or is worth it or not depends on the sample, whether it's noticeable that there are two copies playing, whether playing two copies actually sounds worse sometimes (because it lines up with itself and your ear spots the similarity), and how many you're likely to be playing at once if you don't combine them.

You should do the squaring and square-rooting because doubling the volume = amplitude of a sample actually quadruples the energy present. Normally, when two sounds mix together, some of the component sine waves will double in amplitude but some of them will cancel out (destructive interference), and the average increase in energy is close to sqrt(2) or sqrt(1+1). In real-world physics, the energy will concentrate in certain places, where the constructive interference is occurring, so presumably it either works out exactly or does something weird and quantumy in order to work.

By the way, copying a SAMPLE would have been a very bad idea: you would have used more memory for no good reason. Always think about things like that :)

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Epsi
Member #5,731
April 2005
avatar

Thanks Bruce Perry, but while I'm sure it would work it's way too complicated for the scope of my project (regarding sounds).

I'm not that concerned about the memory used by a few wav files versus more code+testing+frustration resulting in a loss of motivation ;D

___________________________________

piccolo: "soon all new 2d alegro games will be better. after i finsh my MMRPG. my game will serve as a code reference. so you can understand and grab code from."
piccolo: "just wait until my invetion comes out its going to take the wii to the next leave of game play. it will run sony and microsoft out of busness if i dont let them use it aswell."

Go to: