Author Topic: Dynamically adjusting ambient sound volume  (Read 2753 times)  Share 

TheMagician

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Dynamically adjusting ambient sound volume
« on: 13 Jun 2004, 17:21 »
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
« Last Edit: 21 Dec 2004, 21:32 by strazer »

Re: Background sound ... how to code
« Reply #1 on: 13 Jun 2004, 18:56 »
Quote
I 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

Quote
How 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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #2 on: 14 Jun 2004, 09:04 »
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?


Re: Background sound ... how to code
« Reply #3 on: 14 Jun 2004, 09:12 »
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.
« Last Edit: 14 Jun 2004, 18:05 by strazer »

TheMagician

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #4 on: 14 Jun 2004, 16:38 »
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?


Re: Background sound ... how to code
« Reply #5 on: 14 Jun 2004, 18:05 »
Yes, music should be no problem, since it uses another channel.

Let me know how it turns out. :)

TheMagician

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #6 on: 15 Jun 2004, 10:13 »
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

Re: Background sound ... how to code
« Reply #7 on: 15 Jun 2004, 12:06 »
What algorithm are you using ("algorithm / character[EGO].y")?
I know what you're thinking ... Don't think that.

Barbarian

  • Mongol General: "Conan, what is best in life?"
    • I can help with AGS tutoring
    •  
    • I can help with backgrounds
    •  
    • I can help with characters
    •  
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
  • Barbarian worked on a game that was nominated for an AGS Award!
Re: Background sound ... how to code
« Reply #8 on: 15 Jun 2004, 12:24 »
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.  ;)
« Last Edit: 15 Jun 2004, 12:26 by Barbarian »
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

Re: Background sound ... how to code
« Reply #9 on: 15 Jun 2004, 14:31 »
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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #10 on: 15 Jun 2004, 15:16 »
@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

  • 26 orbits and beginning to get dizzy...
    • I can help with backgrounds
    •  
    • I can help with play testing
    •  
    • I can help with proof reading
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
    • I can help with web design
    •  
Re: Background sound ... how to code
« Reply #11 on: 15 Jun 2004, 15:24 »
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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #12 on: 15 Jun 2004, 18:34 »
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

Re: Background sound ... how to code
« Reply #13 on: 15 Jun 2004, 19:43 »
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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #14 on: 16 Jun 2004, 09:50 »
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

  • 100101101010b
Re: Background sound ... how to code
« Reply #15 on: 17 Jun 2004, 13:42 »
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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #16 on: 17 Jun 2004, 14:49 »
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

  • Creator of AGS
  • Administrator
  • Mittens TRAITOR
  • I sense danger.
    • Lifetime Achievement Award Winner
    •  
Re: Background sound ... how to code
« Reply #17 on: 18 Jun 2004, 20:47 »
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

    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: Background sound ... how to code
« Reply #18 on: 19 Jun 2004, 11:06 »
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

  • Creator of AGS
  • Administrator
  • Mittens TRAITOR
  • I sense danger.
    • Lifetime Achievement Award Winner
    •  
Re: Background sound ... how to code
« Reply #19 on: 19 Jun 2004, 21:59 »
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.