Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Store audio from QAudioInput and pass it to SciPy FFT

Store audio from QAudioInput and pass it to SciPy FFT

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 774 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Jaime02J Offline
    Jaime02J Offline
    Jaime02
    Qt Champion 2021
    wrote on last edited by
    #1

    How can I get audio input in real time from QAudioInput, store it in a NumPy array and pass it to SciPy FFT? What I have tried:

    from PyQt5.QtMultimedia import QAudioDeviceInfo, QAudioFormat, QAudioInput
    import sys
    
    class Window(QMainWindow):
        def __init__(self):
            info = QAudioDeviceInfo()
            input_device = info.defaultInputDevice()
            if input_device.isNull():
                # If no avaiable device is found, we display a error
                print("There is no audio input device available.")
                exit(-1)
    
            audio_format = QAudioFormat()
            audio_format.setSampleRate(44100)
            audio_format.setSampleSize(8)
            audio_format.setChannelCount(1)
            audio_format.setCodec("audio/pcm")
            audio_format.setSampleType(QAudioFormat.UnSignedInt)
    
            if sys.byteorder == "little":
                audio_format.setByteOrder(QAudioFormat.LittleEndian)
            else:
                audio_format.setByteOrder(QAudioFormat.BigEndian)
            self.audioInput = QAudioInput(input_device, audio_format, self)
            self.ioDevice = self.audioInput.start()
            self.ioDevice.readyRead.connect(self.read_audio)
    
        def read_audio(self):
            data: QByteArray = self.ioDevice.readAll()
            print(data.toUInt()) # Prints (0, False) which means error converting data
    
    JonBJ 1 Reply Last reply
    0
    • Jaime02J Jaime02

      How can I get audio input in real time from QAudioInput, store it in a NumPy array and pass it to SciPy FFT? What I have tried:

      from PyQt5.QtMultimedia import QAudioDeviceInfo, QAudioFormat, QAudioInput
      import sys
      
      class Window(QMainWindow):
          def __init__(self):
              info = QAudioDeviceInfo()
              input_device = info.defaultInputDevice()
              if input_device.isNull():
                  # If no avaiable device is found, we display a error
                  print("There is no audio input device available.")
                  exit(-1)
      
              audio_format = QAudioFormat()
              audio_format.setSampleRate(44100)
              audio_format.setSampleSize(8)
              audio_format.setChannelCount(1)
              audio_format.setCodec("audio/pcm")
              audio_format.setSampleType(QAudioFormat.UnSignedInt)
      
              if sys.byteorder == "little":
                  audio_format.setByteOrder(QAudioFormat.LittleEndian)
              else:
                  audio_format.setByteOrder(QAudioFormat.BigEndian)
              self.audioInput = QAudioInput(input_device, audio_format, self)
              self.ioDevice = self.audioInput.start()
              self.ioDevice.readyRead.connect(self.read_audio)
      
          def read_audio(self):
              data: QByteArray = self.ioDevice.readAll()
              print(data.toUInt()) # Prints (0, False) which means error converting data
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Jaime02 said in Store audio from QAudioInput and pass it to SciPy FFT:

      print(data.toUInt()) # Prints (0, False) which means error converting data

      I know nothing about audio input or format. But you do realize that

      • QByteArray::toUInt() only attempts (if it succeeds) to return the first unsigned int in the array?
      • You are not supposed to assume that one call to read_audio() and one readAll() read all the data available. It might have read as little as 1 byte. Check data's length?
      1 Reply Last reply
      2
      • Jaime02J Offline
        Jaime02J Offline
        Jaime02
        Qt Champion 2021
        wrote on last edited by
        #3

        @JonB data length is 1764

        1 Reply Last reply
        0
        • Jaime02J Offline
          Jaime02J Offline
          Jaime02
          Qt Champion 2021
          wrote on last edited by
          #4

          https://stackoverflow.com/questions/67408378/store-audio-from-qaudioinput-and-pass-it-to-scipy-fft/67409243#67409243

          JonBJ 1 Reply Last reply
          0
          • Jaime02J Jaime02

            https://stackoverflow.com/questions/67408378/store-audio-from-qaudioinput-and-pass-it-to-scipy-fft/67409243#67409243

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Jaime02
            So it seems to me that the stackoverflow answer say the same as I did:

            data.toUInt() converts whole byte array to one uint value - not what you want.

            Since you seem to have the answers you seek there, you might like to mark this as solved.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved