I am overlaying a bunch of audio segments, and want to be able to pass a tuple of values in the form of (1, 1, 1, 0.5, 0...) to my function, each number being a ratio that the volume of a segment should be scaled to. 0 should be absolutely silent, while 1 should be the original volume unmodified, and 0.5 exactly half. This is, as far as I understand, the behavior of the GainNode "gain" property.
I tried these so far:
def adjust_volume(audio_segment, ratio):
decibel = pydub.utils.ratio_to_db(audio_segment.rms)
return audio_segment - decibel * (1 - ratio)
and
SILENCE_THRESHOLD = -120.00
def adjust_volume(audio_segment, ratio):
difference = SILENCE_THRESHOLD - audio_segment.dBFS
return audio_segment + (difference - (difference * ratio))
Unfortunately both work imperfectly, meaning they don't exactly replicate the browser's (Mozilla Firefox) behavior. Using the first one it's possible to hear sounds with my audio player (foobar2000) even if I pass in a tuple only containing 0s, and while the second one manages to silence entire segments with the correct silence threshold, using 0.3 for example creates an audio level that is way lower than the one I can observe in my browser using the same value.
It should be noted that my technical audio knowledge is very limited. Are these simply technical inaccuracies created by different audio equipment, audio implementation details etc.? If that's the case, could someone suggest me the most "correct" way to do this scaling?