1

I'm completely new to QMultimedia. At the moment, I try to get the audio stream from the microphone in my webcam for further processing. Right now I just try to continuously show the volume level of the sound "heard" by the mic with a slider. So I googled some code together (found nearly 10 tons of examples how I can play an audio, but only a few blocks of C++ code about audio input) and got stuck.

This is my actual code:

import sys, time
from PyQt4 import Qt, QtGui, QtCore, QtMultimedia

class VolumeSlider(QtGui.QSlider):
    def __init__(self, parent=None):
        super(VolumeSlider, self).__init__(parent)

        self.audio = None

        self.volumeSlider = QtGui.QSlider(QtCore.Qt.Horizontal)
        self.volumeSlider.setTickInterval(1)
        self.volumeSlider.setMaximum(100)
        self.volumeSlider.setValue(49)

        self.volumeSlider.show()

        self.openMicStream()

        # THIS IS WHAT I WANT - DOESN'T WORK
        while True:
            self.volumeSlider.setValue(self.audio.volume())
            time.sleep(0.02)



    def openMicStream( self ):
        #audioInputDevices = QtMultimedia.QAudioDeviceInfo.availableDevices(QtMultimedia.QAudio.AudioInput)
        #for d in audioInputDevices: d.deviceName()

        info = QtMultimedia.QAudioDeviceInfo(QtMultimedia.QAudioDeviceInfo.defaultInputDevice())
        print "Default audio input device:", info.deviceName()

        audioFormat = QtMultimedia.QAudioFormat()            
        audioFormat.setFrequency(8000);
        audioFormat.setChannels(1);
        audioFormat.setSampleSize(8);
        audioFormat.setCodec("audio/pcm");
        audioFormat.setByteOrder(QtMultimedia.QAudioFormat.LittleEndian);
        audioFormat.setSampleType(QtMultimedia.QAudioFormat.UnSignedInt);

        audioDeviceInfo = QtMultimedia.QAudioDeviceInfo.defaultInputDevice();
        if not audioDeviceInfo.isFormatSupported(audioFormat):
            sys.stderr("default audioFormat not supported try to use nearest")
            audioFormat = audioDeviceInfo.nearestFormat(audioFormat);

        self.audioInput = QtMultimedia.QAudioInput(audioFormat);

        fmtSupported = info.isFormatSupported(audioFormat)
        print "Is the selected format supported?", fmtSupported

        if not fmtSupported:
            audioFormat = info.nearestFormat(audioFormat)
            print "Is the nearest format supported?", info.isFormatSupported(audioFormat)

        self.audio = QtMultimedia.QAudioInput(audioFormat, None)
        self.audio.start()


if __name__ == "__main__":    
    app = Qt.QApplication(sys.argv)
    x = VolumeSlider()
    sys.exit(app.exec_()) 

Could anybody poke me in the head what I have to do at the "#THIS IS WHAT I WANT" place to calculate and show the current level of volume?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
kwaxs
  • 11
  • 3

2 Answers2

0

There is no inbuilt function for computing the current volume level of the input sound signal when recorded with QAudioInput neither in Qt 4 (QAudioInput documentation) nor in Qt 5.

But you could calculate it for yourself. The root-mean-square in a moving window of the signal is often used as a measure for current loudness. See How can I determine how loud a WAV file will sound? for more suggestions.

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • In Qt5, the `QAudioInput` class can [get/set the volume](http://doc.qt.io/qt-5/qaudioinput.html#setVolume) (so long as the device supports it). – ekhumoro Jan 06 '15 at 20:48
  • @ekhumoro I saw it but I think they might mean a different volume than what is the current level of audio loudness as wished by the OP if I understand him correctly. Its more like an additional gain factor. But I'm not completely sure what is actually meant here. I guess we have to wait for the OP to come back and give more info. – NoDataDumpNoContribution Jan 06 '15 at 20:57
0

Solved it after a while of working on another parts. Now I can at least hear the sound out of the boxes, after I changed the openMicStream(self) to this:

def openMicStream( self ):
    info = QAudioDeviceInfo(QAudioDeviceInfo.defaultInputDevice())
    print "Default audioInput input device: ", info.deviceName()

    audioFormat = QAudioFormat()

    audioFormat.setFrequency(44100);
    audioFormat.setChannels(1);
    audioFormat.setSampleSize(16);
    audioFormat.setCodec("audioInput/pcm");
    audioFormat.setByteOrder(QAudioFormat.LittleEndian);
    audioFormat.setSampleType(QAudioFormat.UnSignedInt);

    audioDeviceInfo = QAudioDeviceInfo.defaultInputDevice();
    if not audioDeviceInfo.isFormatSupported(audioFormat):
        messages.error(__name__, "default audioFormat not supported try to use nearest")            
        audioFormat = audioDeviceInfo.nearestFormat(audioFormat);
        print audioFormat.frequency()
        print audioFormat.channels()
        print audioFormat.sampleSize()
        print audioFormat.codec()
        print audioFormat.byteOrder()
        print audioFormat.sampleType()

    self.audioInput = QAudioInput(audioFormat);

    audioFmtSupported = info.isFormatSupported(audioFormat)        
    messages.info(__name__, "Is the selected format supported?"+str(audioFmtSupported))

    if not audioFmtSupported:
        audioFormat = info.nearestFormat(audioFormat)
        messages.info(__name__, "Is the nearest format supported?"+str(info.isFormatSupported(audioFormat)))

    self.audioInput = QAudioInput(audioFormat, None)
    self.audioOutput = QAudioOutput(audioFormat, None)

    device = self.audioOutput.start()   
    self.audioInput.start(device)
kwaxs
  • 11
  • 3