Game Development Community

Dynamically changing the volume of a sound effect

by Ian Omroth Hardingham · in Torque Game Engine · 07/13/2005 (7:28 am) · 7 replies

Hi everyone.

Pretty simple question: I have an AUDIOHANDLE for a looping sound effect in c and I'd like to change it's volume (and if at all possible pitch) while it's playing. Anyone know if this is possible?

Thanks,
Ian

#1
07/13/2005 (11:53 am)
alxSourcef(AUDIOHANDLE soundHandle, AL_GAIN_LINEAR, F32 volume);
alxSourcef(AUDIOHANDLE soundHandle, AL_PITCH, F32 pitch);

As in: alxSourcef(mMySound, AL_GAIN_LINEAR, 2.0f); Which should play the sound at twice the base volume, I do believe.
#2
07/14/2005 (5:40 am)
Thankyou hugely Scott, that's exactly what I needed.

On a side question, do you know if it's possible to fade out an effect over a given time?

Ian
#3
07/14/2005 (10:47 am)
I don't know of any command to set an effect to fade out automatically. AFIAK you'd have to set up a loop and gradually change the values yourself.

A value of 1 should be baseline for both pitch and gain. In the case of volume, 0 is muted.

Also, volume is clamped to a range of 0 to 1. So I was mistaken before, as setting the gain to 2 would not double the volume of the sound. This strikes me as an inappropriate restriction, but I'm not sure if openAL will accept a gain greater than 1. I'd have to test it to be sure. If openAL will allow it, then I think Torque should be changed to allow it as well.

Pitch on the other hand, doesn't appear to clamp the incoming value. AFAIK the only restriction on pitch is that it must be a value greater than 0.
#4
07/14/2005 (11:33 am)
Thanks again for the info Scott. Unfortunately, the set gain function appears to not work properly, setting the volume obscurely low below 0.2 and not playing part of the treble until set at 1. Which is annoying.

Ian
#5
07/14/2005 (1:24 pm)
You're right. It doesn't work properly. I've been digging through Torque's audio code for the past several days, and this is one of several problems I've found.

What's happening there is that Torque is taking the value you pass it, and converting it to some logarithmic scale. The result is a non-linear progression from 0 to 1, where 0.5 becomes roughly 0.1. Why it's written this way, I could not say. Maybe someone had a good reason, but IMO this behavior is just not right. Anyhow..

For a quick fix, open engine/audio/audio.cc and find the functions "linearToDB" and "DBToLinear" near the bottom of the file. Make the first line of each function "return value;". This should stop Torque from messing with the linear scale.
#6
07/14/2005 (2:12 pm)
I thought that OpenAL worked on the decibel scale, making volume adjustment logarithmic and not linear.

I notice that the alxLoopSourcef code swtiches on the gain flag, calling DBToLinear for AL_GAIN, and just passing the value straight through for AL_GAIN_LINEAR. From my understanding of OpenAL, volume gain is described as a linear progression from 0.0dB to -100.0dB, which would make this switch statement the wrong way round. (Assuming that the expected behaviour for a linear scale is for a volume gain of 1.0 to be twice as loud as a gain of 0.5).

This might also explain the issues I'm having with determining the volume of sound sources.

Can anyone with a better understanding of OpenAL's... umm... intricacies shed some light?

- Seb.
#7
07/15/2005 (11:48 am)
If Torque intended me to set sound volume in decibels, it should not be clamping the values from 0 to 1. This just doesn't make sense. A whisper is something like 20dB, I think. 1dB is practically silent, and that's the max value for a sound in Torque? That can't be what the designers intended. A range of 0 to 1 only makes sense if we're using a linear scale. In this situation, I expect a value of 0.5 to produce a sound at half volume. Which it will do only if you disable the DBToLinear and linearToDB functions. The very concept of a "convert decibels to linear factor" function that clamps the incoming (presumably dB) value to a range from 0 to 1.. just doesn't make sense.

I'm guessing these functions may have been left over from a time when Torque used a different sound system. Perhaps before it became Torque? I really don't know. But I do know that the version of OpenAL I'm currently using seems to work with a linear scale from 0 to 1, where 0 is silent, 0.5 is half the original volume, and 1 is the original sound volume.