Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Sound troubles

This thread is locked; no one can reply to it. rss feed Print
Sound troubles
DavyKager
Member #8,299
February 2007

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
Member #1,550
September 2001

Show some code.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Arthur Kalliokoski
Second in Command
February 2005
avatar

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

DavyKager
Member #8,299
February 2007

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
Member #5,114
October 2004

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
Member #8,299
February 2007

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Ariesnl
Member #2,902
November 2002
avatar

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

- Wisdom is the art of using knowledge
- String theory: There's music in everything

Thomas Fjellstrom
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Johan Halmén
Member #1,550
September 2001

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

DavyKager
Member #8,299
February 2007

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
Member #907
January 2001

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.

Dustin Dettmer
Member #3,935
October 2003
avatar

Sounds::loop("engine sound");

Sounds::PlayInfo pi;

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

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

Go to: