# Logarithmic channel volume control. How to do it?

**URL:** <https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231>\
**Category:** FMOD Engine\
**Created:** [February 12, 2024, 4:40pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231 "2024-02-12T16:40:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![soose](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@soose](https://qa.fmod.com/u/soose)\
**Post date:** [February 12, 2024, 4:40pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/1 "2024-02-12T16:40:56Z")

</div>

Hello everyone,  
So I want to control my channel volume logarithmicly rather than linear. I need logarithmic volume controls because the linear curve is too steep. Is there a way to achieve my goal? I’ve searched around for formulas, but none were exactly suitable. Also there is nothing about logarithmic volume control in the fmod engine api.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [February 14, 2024, 11:33pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/2 "2024-02-14T23:33:00Z")

</div>

Generally this would be a little out of scope for the FMOD forums, but I will try to get you pointed in the right direction.  
I am not sure what your range of desired values is, but let’s say for now you want a minimum value of 0 and a maximum value of 1. In code such a transformation would look like:

```cpp
float linear(float x)
{
    if (x < 0)
        return 0;
    else if (x < 1)
        return x;
    else
        return 1;
}

```

So anything less than 0 we want to map to 0, anything greater than 1 we want to map to 1, and everything between that we want to map directly to _x_. Here is what that would look like graphing all possible _x_ input values to their corresponding _y_ output values:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/e/eb2887823bbaf605faa792612f9532364cf72a91.png)

So you are saying you want a logarithmic function specifically. To do that, we can swap out the `x < 1` case for something that uses a `log` function:

```cpp
float logarithmic(float x)
{
    if (x < 0)
        return 0;
    else if (x < 1)
        return log2f(x + 1); // +1 for desired max value
    else
        return 1;
}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/fmod/original/2X/d/d84a600f64c5e19d3542b236fd45d8693daac6e4.png)

With this function, any values closer to 0 will change rapidly, and values closer to 1 will change slowly. Then you just need to pass that into `setVolume` instead of your raw 0 → 1 value.

```cpp
channel->setVolume(logarithmic(sliderValue));

```

Hopefully that makes sense. You can experiment with different calculations in the `x < 1` case. For a steeper curve you can raise _x_ to the power of a fractional value until you find a curve that suits your needs.

---

<div class="post-metadata">

**Author:** ![soose](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@soose](https://qa.fmod.com/u/soose)\
**Post date:** [February 18, 2024, 11:46am UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/3 "2024-02-18T11:46:11Z")

</div>

Thankyou for the solution. However, I have another problem now. It’s now with the Pan dsp. So I need to pan the channel and I do the following: create the sound, play it with Start paused set to true, then I add a pan dsp to index 0. However, when I unpause the said channel, I hear a click in the center and after a fraction of a second it actually panns right. Fmod updates on my main thread every frame, I’ve tried a lot of thought up solutions and googled, but I found nothing that could solve my problem. Everything is ok but the small click in the center at the start of a sound is there. Any hints on where to go from here please? I’ve tried disabling MixFromUpdate and StreamFromUpdate flags, but it didn’t help.

---

<div class="post-metadata">

**Author:** ![soose](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@soose](https://qa.fmod.com/u/soose)\
**Post date:** [February 18, 2024, 12:04pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/4 "2024-02-18T12:04:56Z")

</div>

I have also noticed that the problem is only when using the 2d stereo panning mode. Other panning methods seem to work without clicking in the start.

---

<div class="post-metadata">

**Author:** ![soose](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@soose](https://qa.fmod.com/u/soose)\
**Post date:** [February 18, 2024, 12:49pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/5 "2024-02-18T12:49:31Z")

</div>

I found a very unexpected solution:  
Setting the software format to 192khz, speaker mode to raw and number of channels 2 solved the click problem and now the panner does fine. I think it’s a bug or something, I’m not sure.

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [February 20, 2024, 3:19am UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/6 "2024-02-20T03:19:00Z")

</div>

I have not been able to reproduce this, what version of FMOD are you using?  
Could you please share a code snippet of how you are creating the sound, adding the Pan DSP, and any additional calls you are making to [`Channel::set3DAttributes`](https://fmod.com/docs/2.02/api/core-api-channelcontrol.html#channelcontrol_set3dattributes) or [`DSP::setParameterFloat`](https://fmod.com/docs/2.02/api/core-api-dsp.html#dsp_setparameterfloat)?

---

<div class="post-metadata">

**Author:** ![soose](https://avatars.discourse-cdn.com/v4/letter/s/839c29/32.png) [@soose](https://qa.fmod.com/u/soose)\
**Post date:** [February 20, 2024, 8:49am UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/7 "2024-02-20T08:49:17Z")

</div>

fmod v 2.2.20.  
code:  
FMOD.Factory.System\_Create (out FMOD.System FmodSystem);  
FmodSystem.init (4095, INITFLAGS.MIX\_FROM\_UPDATE|INITFLAGS.STREAM\_FROM\_UPDATE, 0);  
byte LoadedBytes = File.ReadAllBytes(“test.ogg”);  
FmodSystem.createSound(“test.ogg”, MODE.DEFAULT, out Sound SoundHandle);  
FmodSystem.playSound (SoundHandle, default(ChannelGroup), true, out Channel ChannelHandle);  
FmodSystem.createDSPByType (DSP\_TYPE.PAN, out DSP Panner);  
Panner.setParameterInt ((int)DSP\_PAN.MODE, (int)DSP\_PAN\_MODE\_TYPE.STEREO);|  
ChannelHandle.addDSP (0, Panner);  
Panner.setParameterFloat((int)DSP\_PAN.\_2D\_STEREO\_POSITION, 100);  
ChannelHandle.setPaused (false);

---

<div class="post-metadata">

**Author:** ![jeff\_fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/jeff_fmod/32/1766_2.png) [@jeff\_fmod](https://qa.fmod.com/u/jeff_fmod)\
**Post date:** [February 26, 2024, 11:26pm UTC](https://qa.fmod.com/t/logarithmic-channel-volume-control-how-to-do-it/21231/8 "2024-02-26T23:26:14Z")

</div>

Thank you for sharing the code snippet. I still haven’t been able to reproduce this issue unfortunately.  
Please let me know if this issue comes up again and I will investigate further.
