Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Audio seems overly complicated :(

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
Audio seems overly complicated :(
nshade
Member #4,372
February 2004

In my game I have music that plays independent of sound effects. However, I need to the ability start and stop each on their own channel. I'm trying to set up mixers, but for some reason the sound effects don't work most of the time. It's just silent. Also if I monkey with things all of the sudden my music stops working as well. The example code is kind of big, so I'll post it to a pastebin.

It just seem way over kill to load up samples, set every one to an instance and then hook every one to a mixer. Can you tell me why my audio just stops working. Do I have to hook up all these mixers? (I want a separate volume control for music and sound)

also it looks like I have to make a stop routine for each sound effect? I think I made this very over complicated and there has to be an easier way...

The pastebin example is here
https://pastebin.com/yayDRCFB

The function order in main(is the order things are called in the code. I don't have control over function names or call order because I'm writing a allegro wrapper for these functions.

Edgar Reynaldo
Member #8,592
May 2007
avatar

101 - have you ever heard of enums and arrays?

I took a quick glance, and I only saw where you hooked up the master mixer. You didn't attach the other mixers to a voice.

It goes Sound->Voice->Mixer->SampleInstance

And you can also attach mixers to mixers. I assume there's some global gain setting function for a mixer that would allow you to have your separate sound and music volume controls.

In your case I would have :

                       SFX Mixer - sfx sample instances
                     /
Voice -> Master Mixer
                     \ 
                      Music Mixer - music sample on loop or whatever

nshade
Member #4,372
February 2004

music_mixer is created in SoundInit() line 192
sfx_mixer created in SoundInit() line 193
master_mixer is created in SoundInit() line 194
voice created in SoundInit() line 195
master_mixer is attached to voice in SoundInit() line 196

This music_mixer is attached to master_mixer at MusicInit() line 95
The sfx_mixer is attached master_mixer at the bottom of LoadSound() line 266

sample instances are attached when they are loaded. The order appears to be exactly how you described.

Edgar Reynaldo
Member #8,592
May 2007
avatar

NiteHackr
Member #2,229
April 2002
avatar

My biggest complaint about Allegro 5 was how the sound was set up. It was far simpler in Allegro 4.

Edgar Reynaldo
Member #8,592
May 2007
avatar

nshade
Member #4,372
February 2004

yup You can see the function order under main()

Edgar Reynaldo
Member #8,592
May 2007
avatar

nshade
Member #4,372
February 2004

The reason why they are stretched out between different functions is because the game I'm porting calls these functions. I'm replacing the soundblaster library with one that uses allegro calls in it's place.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I've got a working example with two mixers and two sample instances, one for music and one for sound. Their volume is independent. Next I'm going to try with a master mixer and a voice instead of the default voice and mixer, as you have it set up. Then I'll try multiple sample instances on the sound mixer and see if it still works.

I can't test it yet though, because al_create_voice returns NULL. I have to investigate a bit more.

NiteHackr
Member #2,229
April 2002
avatar

And could do far less. :/

I could gradually change the pitch in Allegro 4 with minimal fuss. It is FAR more complicated in Allegro 5.

In my Deluxe Pacman 1 game (Allegro 4), listen to the siren in the background. As you eat the dots, the pitch gradually goes up. Because it was simple to code.

Now listen to the same sound in Deluxe Pacman 2 (Allegro 5). You will hear the pitch suddenly change, because I could not gradually change it over time without a whole lot of headache. So instead I change it every so many pills which was easier to do.

I could do more with Allegro 4, at least what I wanted to do. Allegro 5 seemed to forget that the library was supposed to make life easier, and for the most part is does, but the sound system is horribad. The function to change the pitch in Allegro 4 is straight forward: adjust_sample() You can't do that in Allegro 5.

Edgar Reynaldo
Member #8,592
May 2007
avatar

NiteHackr
Member #2,229
April 2002
avatar

Really. I had this conversation years ago when working on DP2 and it was more complicated. There's more to it than just that one function, but hey, have at it. I'm not working on my Allegro games anymore anyhow. Maybe they have improved it since then, I certainly hope so because it used to be a pain in the ass, and still looks like it is, at least with the audio.

I was able to play music and sound samples and do periodic speed changes, but gradual changes while you played was more involved. I don't recall how now, and the problem wasn't resolved in these forums at the time. I really don't care to revisit it except to agree with the original poster.

Telling people they are wrong about it being overly complicated when you have several that tell you that it is, is burying your head in the sand and ignoring the problem.

Niunio
Member #1,975
March 2002
avatar

I agree with Neil: Allegro 5 sound subsystem is way too much complexest. And documentation doesn't help a lot. For example, I have no idea why I can't use more than 5 channels in my engine.

Anyway, this is a discussion for the development mailing lists. And I hope they discuss it. ::)

-----------------
Current projects: Allegro.pas | MinGRo

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy said:

Really. I had this conversation years ago when working on DP2 and it was more complicated

You're living in the past. A lot has happened in Allegro 5 in the last few years.

Neil Roy said:

I was able to play music and sound samples and do periodic speed changes, but gradual changes while you played was more involved. I don't recall how now, and the problem wasn't resolved in these forums at the time. I really don't care to revisit it except to agree with the original poster.

Well that was then and this is now. One function is all it takes to change the speed of the sample instance.

Allegro 4 has MIDI. That's about all that Allegro 5 doesn't have. Allegro 4 had one mixer and one voice. Allegro 5 lets you create your own mixer setup.

Allegro 5 is more flexible and gives you both a simple API and a complicated API for those who need to exercise more control over their sound.

Audio init aside, it's the same as it was in allegro 4. If all you want is normal sound then you call load sample and play sample. This is not complicated.

If you need control over your sound, you use an ALLEGRO_SAMPLE_INSTANCE*. That's the only difference.

Neil Roy said:

Telling people they are wrong about it being overly complicated when you have several that tell you that it is, is burying your head in the sand and ignoring the problem.

I'm just going to quote Elias's sig and Evert here.

Evert said:

Either help out or stop whining.

Niunio said:

I agree with Neil: Allegro 5 sound subsystem is way too much complexest. And documentation doesn't help a lot. For example, I have no idea why I can't use more than 5 channels in my engine.

Do you really need 7 channel sound? What, 5 isn't enough? I doubt you could tell the difference between the two. What is your target audience? If you really need 7 (or more?) channels, your best bed is FMOD, which is non-free.

Niunio said:

Anyway, this is a discussion for the development mailing lists. And I hope they discuss it. ::)

There are few active devs left these days, and I doubt an overhaul of the audio API is something they want to spend their time on. If you have valid concerns other than just "omg its so complicated" then by all means start a discussion on the mailing list. And if you think the docs are lacking important information then make a patch and submit it.

I've barely used the sound API in Allegro at all, but it didn't take me very long to get a working example. As for why I can't create a voice I'm going to debug that now. Had to rebuild Allegro so I could hack on it. Took me two hours to get all the dumb dependencies built. Did I mention how much I hate autotools? CMake is far superior and much easier to work with.

nshade
Member #4,372
February 2004

So did I hit on a bug and that's why it's not playing the audio correctly?

NiteHackr
Member #2,229
April 2002
avatar

I'm just going to quote Elias's sig and Evert here.

I'm not whining, I am agreeing with the original poster. He finds it overly complicated as well as others.

I could care less anymore as I can do what I want with SDL2. You may not like to hear that, and I hate to have to convert to another library when I have always loved Allegro, but after years of being told to put up or shut up by people like you, I have had it.

If I could program it myself, I wouldn't be using this or any library (actually,. I have done it myself with some straight Windows programming and no libraries), so expecting people to "help out or shut up" is very rude and is what drives people away.

It's more than that one simple function to change it as I recall it from the past, but you need more before you use it in place which is where the pain the buttocks comes into play. I don't have time to reprogram a library and debate this, if it doesn't work easy for me, like Allegro 4 could, than I will find a library that will do it. And I am not alone in this thinking.

But hey, keep telling everyone they are wrong about this. I liked the simplicity of Allegro 4, that's what attracted most people to it. And I like most of Allegro 5, and yes, I can play sounds with it and music via OGG files which is fine. Most of the time that is enough, but for a few things, it can be a pain where Allegro 4 was not. I feel that Allegro 5 provides more flexibility to do things that the vast majority don't want or need while sacrificing the ease of use where it is important.

Edgar Reynaldo
Member #8,592
May 2007
avatar

@Neil
Your argument is without merit. You haven't provided any evidence that you can't do audio with Allegro 5 simply. Saying it's complicated over and over again without providing any proof whatsoever is pointless and doesn't help anyone fix anything. And yes, I would immensely prefer that people help improve allegro over endlessly saying it's not good enough. All you do anymore is complain about Allegro, and I've had it with you.

It's not that complicated, regardless of how you are perceiving it. It's just different from Allegro 4, and because of that you complain and bitch and moan, without helping in any constructive way whatsoever. If you love SDL2 so much, what are you still doing here? I also find it funny that you say "Allegro 5 can't do what I want" when all it takes is a simple glance through the manual to prove you're wrong. RTFM noob. And if the manual isn't good enough, then help improve it. You don't like Allegro's audio? Then fork allegro and fix it. I will say it again, "Either help out, or quit whining". You don't like something about Allegro? Then fix it and submit a patch. No one's stopping you. Patches and pull requests have always been welcome. But you'd rather complain. I don't want to hear it anymore.

nshade said:

So did I hit on a bug and that's why it's not playing the audio correctly?

That remains to be seen.

I figured out why al_create_voice was failing. ALLEGRO_AUDIO_DEPTH_FLOAT32 is not supported for voices. Only ALLEGRO_AUDIO_DEPTH_UINT8 and ALLEGRO_AUDIO_DEPTH_INT16 are supported by DirectSound for voices.

So, my example now works to play music and a sound, using a created voice, a master mixer, and two mixers one each for music and sound. The volume can be manipulated independently for music and sound.

Next step is to try multiple sounds at once.

Can you try my example program and see if it works for you?

Mixer app (static win32 binary and source)

Press M and S to increase music or sound volume. Hold shift with M and S to decrease it. Press Control with M or S to start or stop playing the sound or music.

nshade
Member #4,372
February 2004

The example worked "YAAAAAAAY!" :P

I changed my settings to ALLEGRO_AUDIO_DEPTH_INT16, bit the sounds were still hit and miss though.

NiteHackr
Member #2,229
April 2002
avatar

I was just agreeing with nshade, I couldn't give a shit what you think about my opinion Edgar. :)

Chris Katko
Member #1,881
January 2002
avatar

Sigh. ::)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Edgar Reynaldo
Member #8,592
May 2007
avatar

@Neil - Cool Story Bro 8-)

Do you have anything constructive to add?

Meanwhile, our super heroes are off fighting evil...

EDIT1

Reading The Fine Manual said:

In order to just play some samples, here's how to quick start with Allegro's audio addon: Call al_reserve_samples with the number of samples you'd like to be able to play simultaneously (don't forget to call al_install_audio beforehand). If these succeed, you can now call al_play_sample, with data obtained by al_load_sample, for example (don't forget to initialize the acodec addon). You don't need to worry about voices, mixers or sample instances when using this approach. In order to stop samples, you can use the ALLEGRO_SAMPLE_ID that al_play_sample returns.

That's pretty simple. Just like Allegro 4.

Reed Teh F'in Manuel said:

al_lock_sample_id
ALLEGRO_SAMPLE_INSTANCE* al_lock_sample_id(ALLEGRO_SAMPLE_ID *spl_id)
Source Code

Locks a ALLEGRO_SAMPLE_ID, returning the underlying ALLEGRO_SAMPLE_INSTANCE. This allows you to adjust the various properties of the instance (such as volume, pan, etc) while the sound is playing.

This function will return NULL if the sound corresponding to the id is no longer playing.

While locked, ALLEGRO_SAMPLE_ID will be unavailable to additional calls to al_play_sample, even if the sound stops while locked. To put the ALLEGRO_SAMPLE_ID back into the pool for reuse, make sure to call al_unlock_sample_id when you're done with the instance.

See also: al_play_sample, al_unlock_sample_id

Now you don't need to create your own sample instances and you can modify them on the fly. Take that, bihatchian

@nshade
The mixers can handle FLOAT32 fine, just not the voice, your code was fine.

The only difference then, is the number of sample instances you load and then attach to a mixer.

You also call al_reserve_samples before creating your voice and master mixer and attaching the two. I think that may be part of it. Try moving al_reserve_samples until after they are both created and attached.

The real question is, how many instances are reserved for a mixer? Does it add them as you go, or do you have to set it somehow using al_reserve_samples? But that would only affect the current mixer? I don't really understand this bit

Audric
Member #907
January 2001

I also think that A4 had some very user-friendly shortcuts that I don't see in A5.
For example I would use release_voice()(*) as soon as a game element gets destroyed/de-allocated, and A4 would take care of not cutting any remaining sound it was emitting ("Aaaaarggg-")
I don't see any equivalent in A5, and I think re-implementing it outside allegro is more than a few lines of code :
- Keeping track of 'to be deleted resources' in a collection that allows removal in any order (doubly linked list)
- Periodically check for completed sounds to clean up their resources. It can be implemented by a timer and its own event queue, but host program still has to call the cleanup function a few times per second.

(*) https://www.allegro.cc/manual/4/api/digital-sample-routines/voice-control/release_voice

nshade
Member #4,372
February 2004

The code from my game was pasted almost exactly as it is in the pastebin (With a little sugar to make it compile on it's own)

I have 1 theme song and 20 samples that are played in the game. I just think it's strange as most games have more than just one or two sound effects.

I just realized it's only playing the first two sounds I attached to the mixer. I upped al_reserve_samples to 20 and it's still just playing the first two sounds attached. The code in the pastebin compiles so you can try it for yourself (changing the sample filenames of course)

I'm attaching every sample to an instance and then each instance to the mixer. I just think that was a little redundant if I just want to play 20 sounds. I can imagine a game have several hundred sounds so having to plug in so many things is strange. I was wondering if I was approaching it correctly, or a simpler way of doing it. For example:

play("sound.ogg")

loadsample("sound.ogg",sample); play(sample);

I can see the need for mixers and such, but why load each sample and then create an instance and then attach that instance to a mixer. Why I can't I just load the sample and then just play it, or attach the sample to the mixer directly? I'm only going to have one sound play at a time, so why instantiate it at all?

EDIT

OHMYGOD I FOUND THE BUG!

smpl_hits = al_load_sample("res\hits.ogg"); <--- did not work
smpl_hits = al_load_sample("res/hits.ogg"); <--- did work

I look in the allegro logs and there was no logging the it was failing to load the file... You would think it would at least that. Oh well, my fault for not checking "NULL" on sample load :/

I'm moving this all to ALLEGRO_PATH so I don't have to deal with those stupid backslashes anymore.

Audric
Member #907
January 2001

A good compiler would have told you "unknown escape sequence '\h'" :)
(My first compiler was not gcc-based, and I made the same mistake several times)

 1   2   3   4 


Go to: