Real time audio processing in Python

Viewed 2050

I'm writing a program to check for glitches in an audio signal recorded by a computer. After audio has been detected, I would like to check for glitches in the first 5 seconds of data ( corresponding to 220500 samples at a sampling rate of 44.1kHz), move on to the next 5 seconds of data and check for glitches in that, then the next 5 seconds etc. I have a while loop which begins after audio has been detected, it starts reading audio samples from a stream into an array until it has 220500 samples in the array, after which it enters an if statement where it begins checking for glitches in the 220500 samples (and deletes all the elements in the array afterwards). My problem is that while this is happening , audio is still being recorded by the computer but it is not being read from the stream into the array and by the time I have exited the if statement and restarted the while loop I have missed a few seconds of audio data.

while 1:
    # little endian, signed short
    snd_data = array('h', stream.read(1500))
    if byteorder == 'big':
        snd_data.byteswap()
    r.extend(snd_data)
    if len(r) == 220500 or silent:
        r = trim(r)
        data = pack('<' + ('h'*len(r)), *r)
        data = np.fromstring(data,dtype=np.int16)

        glitch detection carried out here...

I am using PyAudio for recording audio

p=pyaudio.PyAudio() # start the PyAudio class
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,input_device_index = 1, output_device_index = 6,frames_per_buffer=1500)

I would like to know is there any way for me to continue reading from the audio stream into the array whilst carrying out the glitch detection in the if statement? Or if not, is there any other way I could go about this?

1 Answers
Related