Dynamically adjusting ambient sound volume

Started by TheMagician, Sun 13/06/2004 17:21:15

Previous topic - Next topic

TheMagician

Hi everyone.

I am looking for help with coding a background sound effect: blowing wind

Here is a small sketch of my scene and the different rooms relevant for the problem:



The player enters at the bottom of room 1. He can now climb the path to the top of the cliff.
When he reaches it,  I want my sound of blowing wind to SLOWLY FADE IN.

Once it is there I want it to LOOP, of course.

It must continue looping if the player walks from room 1 to 2 and 2 to 3 (one can cross the gap).

If the player decides to walk down the path in room 1 again ... the wind must slowly fade out.

The player can also leave/enter room 2 via room 4. If he does ... wind has to stop/start blowing as well.

My thoughts on the problem:
-I can't think of a way to fade a single sound in without blocking the script.
-So perhaps I have to use 2 sound files: one with the wind fading in ... one with the wind in normal volume.
-How can I get a non-interrupting (when changing rooms)  looping sound without using the Ambient-Sound function
-How about creating one continuing wind sound file of which the first half is the fade in wind and the second half is the  normal volume wind sound ... and then using the SeekMP3Position function ... to loop the normal volume part of the file.

Sound and Music programming is one major difficulty I find with using AGS.
Any ideas and thoughts are very welcome.

Thanks in advance
Stefan

strazer

QuoteI can't think of a way to fade a single sound in without blocking the script.

How about something like:

Player enters screen 1
  PlayAmbientSound(1, 1, 0, 0, 0); // play in channel 1 ambient sound 1 at volume 0

Repeatedly_execute(_always)
  SetChannelVolume(1, 5000 / player.y); // find some algorithm so that at the mountain top the volume is 100 and at the bottom it's 0

QuoteHow can I get a non-interrupting (when changing rooms)  looping sound without using the Ambient-Sound function

Set the game.ambient_sounds_persist variable to 1.

TheMagician

Hi strazer,

thanks for your comments. The idea sounds very good (I didnt know about the sound-persistent variable)

a fade in for a single sound is not possible without blocking the script, is it?


strazer

#3
If you mean a single, non-looping sound, I suppose you could use something similar as above by forcing the sound into a specific channel with PlaySoundEx and then adjusting its volume.
So it's certainly possible, yes, but probably not worth the trouble. Why not just pre-process the sound in a wave editor?

Edit:
Maybe we misunderstand each other.
The way my above suggestion would work is that when entering Room 1, the wind ambient sound is played.
The code in rep_ex would sets its volume to 0 at the bottom of the cliff and would increase the volume the higher the player is up the cliff.
This way, you would only need a single looping sound. Its volume is adjusted dynamically.
I assume it would sound much more natural than the wind suddenly fading in if you crossed some invisible line while walking up the cliff.

TheMagician

Ok strazer,

I think the way you described will really result in a very natural effect!

And as far as I know it is still possible to have a backgroud music (*.ogg) playing, am I right?


strazer

Yes, music should be no problem, since it uses another channel.

Let me know how it turns out. :)

TheMagician

Hi strazer.

Things are getting comlicated now.
Well I tried to set it up yesterday for about 2 hours.

First of all ... I find it very hard to think of an algorithm to set the sound at
player.y = 230 to 0 and
player.y = 135 to 255 (because the volume range of ambient sound is 0 - 255)

And then there is another strange problem which bugs me even more:
Changing the volume of the channel using the Rep_Ex or Rep_Ex_Always results in a wobbering (?) kind of sound. You know, it sound as if the sound gets a litte louder and and then more quiet again and that very fast ... or it seems as if multiple instances of the sound are player shortly after the other.

I checked the code and I am sure that the PlayAmbientSound function is only used once (in the "Room after fade in" part). The Rep_Ex only contains the code for the dynamik change of volume:

if ((character[EGO].y >135) && (character[EGO].y <230)) {
  SetChannelVolume (1,algorithm / player.y);
  }

Any ideas?

Ashen

What algorithm are you using ("algorithm / character[EGO].y")?
I know what you're thinking ... Don't think that.

Barbarian

#8
I'm no "scripting expert", so I try to think of more "simple" work-arounds to solve problems and such.
But, reading through what you are try to accomplish, the thought occured to me that you could simply set up a bunch of regions on your room. And when the character walks onto a certain region, adjust the appropriate sound volume by using the SetChannelVolume command (or whatever command you prefer to happen). And when the player walks onto the next region, adjust the volume to the next setting, etc..

I've used this same type of technique in my game, where my main character walks through a pool of water that is shallow at first, but deeper in towards the middle. I wanted the character view to adjust accordingly to where he was standing in the water, so it would look as if he was either standing just ankle deep in the water, or up to chest-deep in the water.Ã,  I divided my pool of water into different regions, and when he walks into the water at various levels (regions), I would run the command: ChangeCharacterView(EGO,11);Ã,  (or what ever view-number accordingly), and then simply run the ChangeCharacterView again for the next region he walked on.

So anyways, I was thinking you may be able to set up somthing similar to perhaps solve your sound-volume problem?Ã,  .
Maybe it's what you're looking for, maybe not. But at least it's a fresh idea to consider.Ã,  ;)
Conan: "To crush your enemies, see them driven before you, and to hear the lamentation of the women!"
Mongol General: "That is good."

Blade of Rage: www.BladeOfRage.com

Ashen

OK, so I've had a go at this myself. I think the 'choppyness' of the sound is caused by the repeated changing of the Channel volume. So, if I'm right, I think you might be stuck with it, unless you go with Barbarian's idea and use Regions. You'd have to use the 'Player walks onto region' interaction, though, as 'While Player stands on Region' also causes 'choppyness'.
I know what you're thinking ... Don't think that.

TheMagician

@ashen:

Do I get you right: you tried the set channel volume, too and your result was also 'choppy'?
Too bad.

@barbarian

your idea with the regions seems to be a good alternative then. Altough it isn't as 'fluent' as with the player.y algorithm. I will try that and report the result tonight ...
...after Germany has beaten the Netherlands :-)

YOke

It would also be possible to not set the volume in every cycle. Just make a counter that checks the position of the character and sets the volume every few cycles. If you have footstep sound on your character you could set the volume everytime the sound plays, and thereby maybe disguse any "chops" in the sound... Just a thought. Haven't tried it myself. ;)

Enlightenment is not something you earn, it's something you pay for the rest of your life.

TheMagician

Hi strazer ...

after just playing around with the hotspot idea of barbarian I realized that it is the SetChannelVolume function (just in the thread you linked to).

It only changes the ambient sound volume for a short moment and then jumps back to the original volume.
If I do the same with the same sound in Channels 3, 4 or 5 then everything works perfect. (the new volume is set and remains as I want it)

So its at least three users experiencing this problem: strazer, Ashen and me.

There is a loop music funcion ... but I can't find a loop sound function. If we had that we could just loop the sound in one of the working channels.




Ashen

Weird! Ah well, at least it's sorted now.

Magician:
Add this to your rep_ex:

if (IsChannelPlaying (3) == 0) PlaySoundEx (2, 3);

(obviously make sure it's the right channel and sound)
to loop it.
I know what you're thinking ... Don't think that.

TheMagician

Hello again.

First of all, thanks Ashen for the rep_ex looping function.

However: I experienced another problem:
to be more precice: it's the SetChannelVolume function

1.: For me, it changes the volume of a channel ONLY IF A SOUND IS PLAYING.
     If, for example, my code is

        PlaySoundEx(1,4);
        SetChannelVolume(4,0);

     then the sound is "muted".
     However coding like this:
       
        SetChannelVolume(4,0);
        PlaySoundEx(1,4);

     results in the sound playing at normal volume.


2.: (pretty much the same)  The change of channel volume is only temporarly, namely as long as the current sound file plays. If I use the looping function of Ashen and use the SetChannelVolume exactly one loop is played at the volume I want. As soon as the sound gets loaded again for the next loop ... it returns to the original volume.

What is this? Is it my sound driver?
Your ideas and suggestions are appreciated as usual.

Stefan


     

Scorpiorus

Hmm, I have just tried and yes - it restores the value for me too. A workaround, as you already mentioned, is to put the SetChannelVolume function just after PlaySoundEx:

if (IsChannelPlaying (3) == 0) {
   PlaySoundEx (2, 3);
   SetChannelVolume(3, global_variable_holding_current_volume);
}

TheMagician

Thanks for your test (and your workaround) Scorpiorus,

I'm glad it's not just me.

Should this be reported as a bug then?

Pumaman

This is sort of a bug, but on the other hand some people might be relying on this behaviour so I'm hesitant to just go and change it. The workaround suggested should do the trick though.

TheMagician

Thank you CJ for your comment.

I understand that some people might rely on this "feature".

But what about the behaviour of the volume of the ambient sound channel ... it does not work at all (at least for me) as we discussed earlier in this topic.

So perhaps you could think about implementing a new function that does both ambient and the other channels "correctly".

How about that?

Pumaman

Ah yes, the ambient sound channel has special volume processing to deal with directional ambient sounds, which is overriding any volume that you set manually. I'll get that fixed.

As for the other issue, perhaps a new ChangeChannelVolume command or something would be more appropriate, so that you can indicate that you definitely want the channel's volume set seperately from the main sound volume.

SMF spam blocked by CleanTalk