2021年3月24日星期三

Audio Amplitude in Qt/PySide2

I am trying to convert some C++ code into Python using PySide2. The sample C++ code should display amplitude of a microphone in a progress bar. My Python code should just print to console the amplitude, without a GUI. When I run my Python3 script it outputs nothing and the script finishes immediately.

C++ Code: How to simply get audio amplitude in Qt

from PySide2.QtMultimedia import QAudioInput, QAudioFormat, QAudioDeviceInfo  from PySide2.QtCore import QCoreApplication, QObject, QIODevice  import sys    class Device(QIODevice):      def __init__(self, parent: QObject = None):          super().__init__(parent)        def readData(data, maxSize):          Q_UNUSED(data)          Q_UNUSED(maxSize)          return -1        def writeData(data, maxSize):          resolution = 4          i = 0          for i in range(maxSize/resolution):              data += resolution              print(data)          return maxSize      if __name__ == "__main__":      app = QCoreApplication()        format = QAudioFormat()      format.setSampleRate(8000)      format.setChannelCount(1)      format.setSampleSize(8)      format.setCodec("audio/pcm")      format.setByteOrder(QAudioFormat.LittleEndian)      format.setSampleType(QAudioFormat.UnSignedInt)        info = QAudioDeviceInfo.defaultInputDevice()      if not info.isFormatSupported(format):          print("Raw audio fmt not supported by backend, cannot play audio.")          sys.exit(-1)        audio = QAudioInput(info, format)        device = Device()      device.open(QIODevice.WriteOnly)        audio.start(device)        sys.exit(app.exec_())  
https://stackoverflow.com/questions/66792475/audio-amplitude-in-qt-pyside2 March 25, 2021 at 11:05AM

没有评论:

发表评论