python – Get a frequency of currently recording audio with PyAudioWPatch

i am trying to record a system audio with PyAudioWPatch and than from the data read the frequency live in the moment.

I have managed to succesfully record the system audio and get the data in this format:

b'\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\xff\xff'
b'\x00\x00\x00\x00\x00\x00\xff\xff'

I have tried something like this:

while True:
  data = stream.read(CHUNK)
  data_np = np.frombuffer(data, dtype=np.float32)

  # Apply Fast Fourier Transform (FFT)
  frequencies = np.fft.fft(data_np)

  # Get the magnitudes of the frequencies
  magnitudes = np.abs(frequencies)

  # Find the index corresponding to the peak magnitude
  index = np.argmax(magnitudes)

  # Calculate the frequency corresponding to the peak index
  frequency = index * RATE / CHUNK
  print("Frequency:", frequency)

But when i recorded a tone it didnt match the frequency of the tone.

I cant find the right formula to get the frequency information from the data in hexadecimal format.

Thank you for you answers and your help.

Read more here: Source link