[Gambas-devel] gb.sdl.sound channel volume bug

Benoît Minisini gambas at ...1...
Sat May 11 20:38:13 CEST 2013


Le 04/05/2013 09:11, Kevin Fishburne a écrit :
> When setting the Volume property (type float according to the
> documentation) of a channel, the assignable values are of a lower
> resolution than advertised resulting in audibly abrupt changes in
> amplitude. Here's an example:
>
> ' Audio effects.
> Public Struct Audio_Effect  ' Structure containing one effect's data.
>     Current As Single   ' Current amplitude.
>     Target As Single    ' Target amplitude.
>     Velocity As Single  ' Speed at which current amplitude is moving
> toward target amplitude.
>     Scale As Single     ' Audible amplitude coefficient.
>     Chan As Channel     ' Default channel to play on.
>     Name As String      ' Name of sample.
>     Sample As Sound     ' Sample data (file).
> End Struct
>
> Public Environment[16] As Struct Audio_Effect ' Environmental effects.
>
> ' Start wind effect.
> Environment[7].Sample = New Sound(Render.BasePath & "/sound/Wind.wav")
> Environment[7].Name = "Wind"
> Environment[7].Chan = Environment[7].Sample.Play(-1)
> Environment[7].Chan.Volume = 0
>
> Environment[7].Chan.Volume = 0.01
> Print Environment[7].Chan.Volume
> 0
>
> Environment[7].Chan.Volume = 0.02
> Print Environment[7].Chan.Volume
> 0.01333477219943
>
> This has been plaguing me for a while but I just now zeroed in on what
> was actually happening. I've noticed that when performing a calculation
> if you use the wrong datatype in the wrong place it can cause overflows
> or otherwise invalidate the equation. Maybe something like this is
> responsible?
>

The Gambas channel volume is actually different from the SDL channel volume.

The SDL channel volume is a linear integer between 0 and 128, whereas 
the Gambas channel volume is that value converted to a logarithmic scale 
between 0.0 and 1.0.

The logarithmic scale was chosen because it fits the way the human ear 
works: if you double the Gambas volume, the sound will be twice louder.

Consequently, the resolution is far lower than you can expect. And the 
effect of moving the SDL volume from 1 to 2 makes the sound 69% louder, 
whereas moving it from 127 to 128 makes it 1% louder only. So you get 
abrupt changes!

If you need changing the volume, note that you can use the fade optional 
arguments of the Play and Stop methods.

Regards,

-- 
Benoît Minisini




More information about the Devel mailing list