Frosthaven / voicemeeter-windows-volume

Tray app that allows you to sync windows volume and mute state to Voicemeeter volume controls
265 stars 9 forks source link

Conversion of windows volume to voicemeeter gain dB #16

Closed metahexane closed 2 years ago

metahexane commented 2 years ago

An issue with the library (which i love btw), is the converison between volume (0-100) and dB, whose relationship is logarithmic. This problem essentially lies in the convertVolumeToVoicemeeterGain function. What is done here is simply a linear interpolation, but we need it to be logarithmic. The formula displayed here is the one we should be using. In our case, we already have the ratio, since this is just ((new_win_vol - old_win_vol) / old_win_vol), so we need to solve for the difference in dB (a-b). This results into our equation diff_in_db = 20 * log_10 (ratio) and just simply change voicemeeter's gain to: gain = gain + diff_in_db.

The library would be truly complete with the correct conversion.

Frosthaven commented 2 years ago

Hey thanks for this recommendation! I'll take a look at this when I get a breather. Hopefully by this weekend.

You are a rockstar for chiming in :)

metahexane commented 2 years ago

Hey man, I actually just built the project from source and changed the code a little bit to function correctly. Had to change the function I proposed a bit as well to suit the options for gain_min and gain_max. The function now looks like this:

/**
 * converts a A windows volume level (0-100) to Voicemeeter decibel level
 */
 const convertVolumeToVoicemeeterGain = (windowsVolume, gain_min, gain_max) => {
    if (isToggleChecked('limit_db_gain_to_0')) {
        gain_max = 0;
    }

    // Avoid taking log_10 (0)
    let amp = -1000;
    if (windowsVolume > 0) {
        amp = Math.log(windowsVolume / 100) / Math.log(10);
    }

    // Use function for dB = 20 * log_10 (amp), with an additional offset for gain_max
    const gain = Math.max(20 * amp + gain_max, gain_min);
    const roundedGain = Math.round(gain * 10) / 10;

    return roundedGain;
};

Wasn't able to branch off and submit a pull request tho.

Frosthaven commented 2 years ago

https://github.com/Frosthaven/voicemeeter-windows-volume/releases/tag/v1.7.3.0

I've also added you to a contributors list for providing this upgrade in functionality. Many thanks!

Jerrk commented 2 years ago

Is it possible to make this an optional setting?

i personally liked the older method better, though it's not a big deal :slightly_smiling_face:

metahexane commented 2 years ago

Hi Jerrk, I'm sure that's possible, however if anything the old method should be optional, since users of this repo will expect the volume to behave like windows's volume.

To give an example: if your windows volume goes up from 2 to 4, it essentially doubles. Translating this into dB would mean roughly +6 dB. The logarithmic method takes care of this rigorously, while the old flat method doesn't. No single volume slider is implemented without this transformation, because it is not intuitive otherwise.

Of course, it still boils down to preference, so maybe Frosthaven has some time to implement it.

Frosthaven commented 2 years ago

Hey friends! I'm not against having this as an option in the settings. I do think logarithmic is the most correct route to take for the base offering, but I can understand the desire to customize the experience, too. I'll try and take a look this weekend, but I'll be happy to review pull requests if someone beats me to it :)

blacktornadog7 commented 2 years ago

@Jerrk Thanks for mentioning this, I really wasn't aware of it.

To preface this and to be completly honest, I don't know how Windows volume works and I also know zero about programming. But I really don't feel like logarithmic is better or colser to how Windows volume behaves, though from what I've seen online most if not all people who are programmers and know about this stuff way more than I do say logarithmic is the way to go, but I still feel linear is closer to how Windows volume behaves, though also linear is not a perfect 1:1 with Windows. I don't know maybe Windows volume is shit too and I've become used to it but to me it feels like the gold standard idk...

I think it would be a nice option if we can set whatever ratio we want.

Frosthaven commented 2 years ago

I appreciate the added chime in on having a linear volume scaling option return. I was hoping to have that feature re-implemented a long time ago but my main job sort of took over.

I'll do what I can to push out an update this week - I do read these and appreciate the time spent in trying to make this software the best it can be :)

blacktornadog7 commented 2 years ago

No worries, take your time And right back at ya, we appreciate the respond to the feedback and the work making this software better.

Frosthaven commented 2 years ago

This has now been re-implemented in the latest version https://github.com/Frosthaven/voicemeeter-windows-volume/releases/tag/v1.7.4.0