QMediaPlayer and volume control
-
Hello,
I'm rewriting my music player from Qt4 to Qt5. I switched from Phonon to QtMultimedia. I now have a serious problem with volume control. To support ReplayGain, I read the tags from media files and then change the volume with every track to keep it equal.
In phonon I use setVolumeDecibel because the volume differences stored in tags are in dB. But in QtMultiMedia I can not use it. The QMediaPlayer class only has a setVolume function. Is it planned to add the setVolumeDecibel function to this class? Is there any other possibility how I could control the volume in dB? What is the calculation for setVolume internally? Maybe I could reverse it to set the volume in dB.
-
Nobody has an idea? At least I would like to know if it is possible at all or I should rather switch to another backend (libVLC, GStreamer).
-
Hi,
There is currently no setVolumeDecible() function, but setVolume() scales your amplitudes linearly from 0 to 100: http://qt-project.org/doc/qt-5/qmediaplayer.html#volume-prop
-
Hi JKSH, I know this but I need to change volume in dB.
Do yo know what "replay gain":http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification is? You have in your files stored tags which tell you if the file is louder or quieter then an average. The value is stored in dB. Thsi is why I need to control the volume in dB and a linear volume control is totally unusable for me.
If at least I would know what is the calculation behind setVolume. Does it mean that if I call setVolume(50) it will set volume to -6 dB? I found some references but I really don't know how Qt is calculating it internally. I'll probably have to do some experimental tests...
Here are some interesting references:
"MSDN":http://social.msdn.microsoft.com/Forums/windowsdesktop
/en-US/fd848086-0c73-4502-9fa9-a246f44e7dd7/converting-decibels-to-linear-0100-scale?forum=mediafoundationdevelopment
"Audacity Forums":http://forum.audacityteam.org/viewtopic.php?f=21&t=26266 -
I know some very basic audio processing stuff. I wasn't familiar with replay gain before, but thank you for the link; I learnt something new today :)
[quote author="jech" date="1393317490"]Does it mean that if I call setVolume(50) it will set volume to -6 dB?[/quote]Yes.
- setVolume(100) gives you unattenuated playback (0 dB).
- setVolume(0) mutes the audio.
In other words,
@
finalAmplitude = sourceAmplitude * volume/100.0;
@[quote]I found some references but I really don’t know how Qt is calculating it internally.[/quote]Qt simply passes the volume value to your system's audio backend. Here's an implementation where QMediaPlayer uses a GStreamer backend: http://code.woboq.org/qt5/qtmultimedia/src/plugins/gstreamer/mediaplayer/qgstreamerplayersession.cpp.html#_ZN23QGstreamerPlayerSession9setVolumeEi
(It divides the volume value by 100 because GStreamer uses a linear scale of 0 to 1, while Qt uses a linear scale of 0 to 100)See:
-
Thanks a lot for the links it's now much more clear to me what is going on in the background.
This means that it can behave differently based on the backend QtMultimedia uses. I studied some Internet sources and the basic conclusion is that multiplying or dividing the perceived volume by 2 is equal to a change of 6dB.
I used these equations to solve the problem:
dbVolume = 20*log10(volume/100);
correctedVolume = pow(10, (dbVolume+replay_gain/2)/20)*100;For some reason I have to divide the replay_gain by 2. If I don't do it, the change would be too big. This seems to be working fine on Windows with DirectShow backend. I'll test it when I get home on a Linux machine.