简体   繁体   中英

Real time audio processing in Python

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?

您的答案是多线程 而非多处理 ,通过使用python 线程包及其出色的信号功能,您可以实现所需的功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM