2015-06-15 01:45:30

Ok, I'm sorry if this is a really really stupid question and it most likely is.
I'm confused with sound pool, mostly, referring to objects in it. See, the way I have it set in breached skies (yell at me now), is I manage sounds individually.
I've been looking at converting it to sound pool but I'm confused, how do I refer to individual sounds should I want to just pause/destroy or whatever, to just one sound?
I have a couple ideas, one involving using an int variable to manage one category of sounds, meaning, should I be done with ambiance for, say, the spooky village, I can then reuse the same one for the haunted town.
Another I've had is using an array (yikes), and I could I believe should I pull this off correctly (yikes again), where by I could store the sound slot in an index of the array.
Thanks in advance everyone!

2015-06-15 08:55:25

When you play a sound through the sound pool, it gives you a way to manage the sound through a slot ID. You need to keep this ID around if you want to do other things to that particular sound in the future. Just remember that by default, after that sound finishes playing, the original sound in that slot is deleted. If you try accessing the sound after that happens, you may get unexpected results.

2015-06-15 09:32:11

Victorious is correct.

The sound pool helps with fire-and-forget sounds, it helps position sounds and move the listener with minimal effort, and it cleans up after itself.
There are two ways to access individual sounds in the sound pool: use the slot variable returned by the play methods, and pass it to other methods for updating sounds, or hunt down the sound objects that the pool uses directly. You almost never need to do the latter--the only case I can think of off the top of my head is if you need to do something like fade or pitch bend all of the sounds in the pool at once, since there aren't already methods for that.

As for the pool cleaning up sounds that have finished playing, the play methods do have an optional "persistent" parameter, with which you can prevent sounds from being automatically destroyed. You generally wouldn't need this, unless it's to let another object keep track of a sound that can change--for example, if you wanted an enemy to be able to say a variety of things, but only one at a time.


So, you might have an enemy or vehicle class with a slot property, to keep track of its sound.

sound_pool pool; // Global pool.
int player_x=0, player_y=0; // These are global for this example. In practice, a class for the player is usually a good idea, so I usually have a global vector for the camera instead.

class moving_object {
int x=0, y=0;
int slot=-1; // This will be used to move sounds in the sound pool.
string[] sounds; // play one of these at random

moving_object() {}
moving_object(int xx, int yy) {
this.x=xx;
this.y=yy;
}

void move(int dx, int dy) {
this.x+=dx;
this.y+=dy;
if(this.slot<0 or pool.sound_is_playing(this.slot)==false)
{
if(this.slot>=0) pool.destroy_sound(this.slot);
this.slot=pool.play_2d(this.sounds[random(0, this.sounds.length()-1)], player_x, player_y, this.x, this.y, false, true);
}
pool.update_sound_2d(this.slot, this.x, this.y);
}
}

timer time;
void main() {
// Set up sound pool properties, maybe play some looping ambience.


// Set up the moving objects:
moving_object@[] objects(4);
string[] sounds={"sounds/growl1.wav", "sounds/growl2.wav", "sounds/growl3.wav", "sounds/growl4.wav"};
for(uint i=0; i<objects.length(); i++) {
moving_object mo(random(-30, 30), random(-30, 30));
mo.sounds=sounds;

@(objects[i])=mo;

}

// Create the window and start the main loop:
show_game_window("Sound pool example");
while(true) {
if(key_pressed(KEY_ESCAPE)) exit();
else if(key_pressed(KEY_LEFT)) player_move(-1, 0);
else if(key_pressed(KEY_RIGHT)) player_move(1, 0);
else if(key_pressed(KEY_UP)) player_move(0, 1);
else if(key_pressed(KEY_DOWN)) player_move(0, -1);

// Update the moving objects:
if(time.elapsed>500) {
for(uint i=0; i<objects.length(); i++) {
int dx=0;
if(objects[i].x<player_x) dx=1;
else if(objects[i].x>player_x) dx=-1;
if(objects[i].y<player_y) dy=1;
else if(objects[i].y>player_y) dy=-1;
objects[i].move(dx, dy);
}
time.restart();
time.resume(); // I can never remember if this is necessary after restart.
}

wait(5);
}
}

// Move the player, updating the listener position in the process.
void player_move(int dx, int dy) {
if(player_x+dx<-30 or player_x+dx>30) pool.play_stationary("sounds/bump.wav", false);
else player_x+=dx;
if(player_y+dy<-30 or player_y+dy>30) play_stationary("sounds/bump.wav", false);
else player_y+=dy;
pool.play_stationary("sounds/step" + random(1, 3) + ".wav", false);
pool.update_listener_2d(player_x, player_y);
}

If this only made things more confusing, let me know so I can try to fix it.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-06-16 06:34:24

Yes, great move CAE. It is a good idea to track it either way, but occasionally I use the folowing method. I never got this at first, but there is an array in the sound_pool of course you know, and you may access items like this, say, change the volume, you would do this.
int slot=pool.play_stationary("sounds/thing.wav",false,true); //persistant from what somebody else said earlier here.
for(double x=0; x>-40; x--)
{
pool.items[slot].handle.volume=x;
wait(20);
}
and that would make a sound fader, no point in using update_sound_start_values if you can just modify the handle directly.
You see, Omar, the sound pool is simply an array of sound_pool_item objects, which in turn hold sound handles, and use sound_positioning for the movement.
So if you access the pool's items array directly, you can have say int amb, and do something like this for a quick change.
int amb; //ambience for later use.
amb=pool.play_stationary("ambience.ogg",true);
wait(5000); //it won't really do this but it needs to play.
//now we access the pool items array, and change the sound
pool.items[amb].handle.stop();
pool.items[amb].handle.stream("ambience2.ogg");
pool.items[amb].handle.play_looped();
And there.

----------
An anomaly in the matrix. An error in existence. A being who cannot get inside the goddamn box! A.K.A. Me.