1
votes

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?

2
There are no functions for getting or setting the volume for an audio input/output device in Qt4 (they do exist in Qt5, though).ekhumoro
You can try using Phonon instead qt-project.org/doc/qt-4.8/phonon-volumeslider.htmlkartikg3
Mmh... okay, thanx, will try one of themkwaxs
Btw. you should not use sleep in Qt. Use Timers instead. qt-project.org/doc/qt-4.8/qtimer.htmlTrilarion

2 Answers

0
votes

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.

0
votes

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)