PDA

View Full Version : How to access audio recording volume in PyQt 5.9?



e1i45
12th July 2017, 11:03
I'm on Windows 7 and I want to be able to access (read/write) the recording volume of the default audio input device.

In PyQt 5.7 the code below worked and audio.volume() returned the actual recording level (e.g. 0.8). In PyQt 5.8.2 and 5.9 the approach below does not work anymore and it always returns 0.0 (and I'm also not able to change the level). One difference I noticed is that 5.8 and 5.9 have the audio input device name "Default Input Device", while in 5.7 it was the actual (truncated) device name (e.g. "Microphone (Logitech USB Headse"). It makes sense to me that it returns "Default Input Device" as I asked for the default device.


import PyQt5.QtCore
from PyQt5 import QtMultimedia

PyQt5.QtCore.PYQT_VERSION_STR # '5.9'

audioFormat = QtMultimedia.QAudioFormat()
audioFormat.setChannelCount(1)
audioFormat.setSampleRate(16000)
audioFormat.setSampleSize(16)
audioFormat.setCodec("audio/pcm")
audioFormat.setByteOrder(QtMultimedia.QAudioFormat .LittleEndian)
audioFormat.setSampleType(QtMultimedia.QAudioForma t.SignedInt)

info = QtMultimedia.QAudioDeviceInfo.defaultInputDevice()

info.deviceName() # 'Default Input Device'
audio = QtMultimedia.QAudioInput(info, audioFormat)
audio.volume() # 0.0

info.isFormatSupported(audioFormat) # True

e1i45
12th July 2017, 14:13
It looks like this change in behavior was introduced with this commit (http://code.qt.io/cgit/qt/qtmultimedia.git/commit/?h=5.8&id=3c5bbb0dac7bed3199ddddc88c0175d5a2ac1036).

To restore the old behavior in new Qt versions (5.8+): get the list of all input devices and take the first one. This is the exact same as what the previous implementation of the default device function did. On Windows 7 the first device in the list is always the default device.