我是QMultimedia的新手。目前,我尝试从网络摄像头中的麦克风获取音频流,以便进一步处理。现在我只是尝试用滑块连续显示麦克风“听到”声音的音量。所以我用谷歌搜索了一些代码(发现了近10吨的例子,我可以播放音频,但只有几块关于音频输入的C ++代码)并且卡住了。
这是我的实际代码:
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_())
任何人都可以在头脑中戳我的“我想要的东西”来计算并显示当前的音量水平吗?
答案 0 :(得分:0)
在Qt 4(QAudioInput documentation)和Qt 5中均未使用QAudioInput
录制时,没有用于计算输入声音信号当前音量的内置功能。</ p>
但你可以自己计算一下。信号移动窗口中的均方根通常用作当前响度的度量。有关更多建议,请参阅How can I determine how loud a WAV file will sound?。
答案 1 :(得分:0)
经过一段时间处理其他部件后解决了这个问题。在我将openMicStream(self)改为此后,我至少能听到盒子里的声音:
TTT_PATH = 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\\'