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.

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.