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 -
- 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?
- 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.