Sound troubles
DavyKager

Hi!

Last month I downloaded and installed Allegro, and it must been said that it's really cool! However, I'm stuck on how to let my sounds play. Maybe someone here can help me?

I'm trying to build an audiogame, which is my first project, so I'm not used to work with Allegro and C++. I get the keyboard input in a while-loop. That works great, but in this case the sounds are in the while-loop too. And that cause them to constantly restart on every loop...

Can anyone please give me some hints or tips how to deal with this?
Thanks in advance!

Johan Halmén

Show some code.

Arthur Kalliokoski
DavyKager

Well, let me give some example code. Assume that I've loaded a sample and initialized Allegro before.
while (!key[KEY_ESC]) {
if (x >= 50) {
play_sample(the_sample, 255, 128, 1000, FALSE);
}
}

This will start a new sample every loop, which is of course not the thing I wanted to do.

I know that there are some sounds in the Allegro demo game, but I can't find out how it all works.

ngiacomelli
Quote:

which is of course not the thing I wanted to do.

A quick look at the manual here at Allegro.cc shows that play_sample is used in exsample.c (which can be found in your allegro examples folder). Here's a snippet that sould make everything clear:

1 play_sample(the_sample, 255, pan, pitch, TRUE);
2 
3 do {
4 poll_keyboard();
5 
6 /* alter the pan position? */
7 if ((key[KEY_LEFT]) && (pan > 0))
8 pan--;
9 else if ((key[KEY_RIGHT]) && (pan < 255))
10 pan++;
11 
12 /* alter the pitch? */
13 if ((key[KEY_UP]) && (pitch < 16384))
14 pitch = ((pitch * 513) / 512) + 1;
15 else if ((key[KEY_DOWN]) && (pitch > 64))
16 pitch = ((pitch * 511) / 512) - 1;
17 
18 /* adjust the sample */
19 adjust_sample(the_sample, 255, pan, pitch, TRUE);
20 
21 /* delay a bit */
22 rest(2);
23 
24 } while ((!key[KEY_ESC]) && (!key[KEY_SPACE]));
25 
26 /* destroy the sample */
27 destroy_sample(the_sample);

DavyKager

That's cool, but then I have to start all my samples at volume 0 and adjust them later when they're needed. And since it's an audiogame, that would be loads of sounds.

Thomas Fjellstrom

or you have a list/array of your playing sounds, and use adjust_sample on the ones you want to change at any given time.

Ariesnl

use FMOD.. it's free if you're not gonna make money :)

Thomas Fjellstrom

How does FMOD help? He's asking a rather basic question about how to use sounds...

Johan Halmén

Your problem is not related to sound functions. It is related to program logic. You have a thing you want to do once, not every loop. No matter if it is a sound or drawing a rectangle or whatnot. Set a flag when you play the sound. Check if flag is set before you try to play the sound again. If you don't get it, try to deal with simpler program logic first.

DavyKager

I was already thinking about using a flag, but I didn't see that in the demos/examples, so I thought there was another way.

Audric

See Voice control in the manual, this is the API to control more finely the sounds while they are being played.
For example: You can reserve one voice for a looping sound, start or stop the sample in this voice anytime, and adjust the volume/pitch/panning depending on game elements.

ImLeftFooted
Sounds::loop("engine sound");

Sounds::PlayInfo pi;

pi.pan = 0.5;
pi.vol = 1.0;
pi.frequency = 1.0;

Sounds::adjust("engine sound", pi);

Thread #589935. Printed from Allegro.cc