0

Basically, I want to analyse the spectrum of the input audio stream to my Raspberry Pi. I have configured my Audio USB Adapter and ran sample tests on Play and Record.

I am using PyAudio to take audio samples of a fixed chunk size (in my case, 2048) and do some frequency domain based processing. I face the following issues -

  1. When I convert the data from the pyaudio stream to float (using numpy.frombuffer), I get a lot of NANs in the stream. But when I record and play the same audio source, it plays well. What is the best way to obtain audio data and process it? I read a lot of rants about pyaudio being unstable.Is there a better audio library?
  2. Within a few seconds of running the code, RPi throws memory full error. I could even save my program and when I restarted it, my SD card got corrupted. How do I manage these memory issues?

The gist of my code -

import pyaudio
import numpy as np
import time
import freq

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
            channels=1,
            rate=44100,
            input=True,
            imput_device_index=1,
            frames_per_buffer=2048)

for i in range(ITERS):
    aud_data = stream.read(CHUNK)
    data = np.frombuffer(aud_data, dtype='Float32')

    # Audio Processing
    g_time = time.time()
    for ind in range(NUM_FREQ):
        mag[ind] = freq.freq_calc(data, FREQ[ind], RATE)
    print("Process Time:",time.time()-g_time)

P.S I know I haven't used threads as my application does not require it to be strictly Real-Time.

goldilocks
  • 58,859
  • 17
  • 112
  • 227
Injitea
  • 1
  • 2
  • you can see the following link.It may help you https://raspberrypi.stackexchange.com/questions/38756/real-time-audio-input-output-in-python-with-pyaudio thank you – Vivek Singh Sep 02 '17 at 12:14

1 Answers1

0

I was able to get this to work after some effort. I used scipy.signal welch, but I expect you can use numpy fft as well.

data = np.frombuffer(samples, dtype = np.int16)
data = data.astype('float_')

ff, psd  = welch(data, RATE, nperseg = CHUNK,
                         detrend = 'linear',
                         scaling = 'spectrum',
                         return_onesided=True)
Ru887321
  • 101
  • 1