使用多处理的python pyaudio [英] python pyaudio using multiprocessing

查看:152
本文介绍了使用多处理的python pyaudio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从音频流中获取样本,并将其放入共享队列中.我还有另一个从此队列中拉出的进程.

I'm trying to grab samples from a stream of audio and put them in a shared Queue. I have another process that pulls from this queue.

我跑步时出现此错误:

* recording
Traceback (most recent call last):
  File "record.py", line 43, in <module>
    data = stream.read(CHUNK)
  File "/Library/Python/2.7/site-packages/pyaudio.py", line 605, in read
    return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981

显然问题已经存在了一段时间,没有发布解决方案(我尝试了他们的建议):

Apparently problem has been around for a while with no solution posted (I tried their suggestions):

获取IOError:[设置PyAudio Stream输入和输出为True时Errno输入溢出-9981

(简化)代码在这里:

import pyaudio
import wave
import array
import time
from multiprocessing import Queue, Process

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 2

p = pyaudio.PyAudio()
left = Queue()
right = Queue()

def other(q1, q2):
    while True: 
        try:
                a = q1.get(False)
        except Exception:
            pass

        try:
                b = q2.get(False)
        except Exception:
            pass

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")
Process(target=other, args=(left, right)).start()

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    byte_string = ''.join(data)
    nums = array.array('h', byte_string)
    for elt in nums[1::2]:
        left.put(elt)
    for elt in nums[0::2]:
        right.put(elt)

print("* done recording")

stream.stop_stream()
stream.close()
print "terminated"

我做错了什么?我在Mac OSX和Python 2.7上,我通过homebrew安装了portaudio,并尝试了pip和dmg安装,但都没有运气.

What am I doing wrong? I'm on Mac OSX and Python 2.7, I installed portaudio through homebrew and tried both the pip and dmg installation of `pyaudio with no luck with either.

推荐答案

缓冲区溢出错误是因为您的frames_per_buffer和读取的块大小可能太小. 尝试为其设置更大的值,例如512,2048、4096、8192等

The buffer overflow error is because your frames_per_buffer and read chunksize might be too small. Try larger values for them i.e. 512,2048, 4096, 8192 etc

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 2

for CHUNK1 in [512,2048,4096,8192,16384]:
    for CHUNK2 in [512,2048,4096,8192,16384]:
        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK1)


        try:
            print CHUNK1,CHUNK2
            for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
                data = stream.read(CHUNK2)
        except:
            print "Boohoo"

        stream.stop_stream()
        stream.close()


更新

好的,我想我明白了.如果您在下一次读取之前等待了太长时间,则pyaudio库将引发溢出错误.

ok i think I got it. the pyaudio library will raise an overflow error if you wait too long before your next read.

byte_string = ''.join(data)
nums = array.array('h', byte_string)
for elt in nums[1::2]:
    left.put(elt)
for elt in nums[0::2]:
    right.put(elt)

这在这里做了很多非常慢的处理.尤其是python中的两个for循环.让处理过程可以处理整个单流数据的大块,而不是一次处理一个int.

this does a whole lot of very slow processing here. especially the two for loops which are in python. let the processing process get a whole chunk of mono-stream data which it can deal with instead of one int at a time.

import numpy as np

...

n=np.fromstring(data,np.uint16)
left.put(n[1::2])
right.put(n[0::2])

我什至不想想象for循环对延迟的影响,但是即使相对在使用arraynp.array之间的较小性能改进也值得注意:

I don't even want to imagine what the for loops were doing to latency, but even the relatively minor performance improvement between using array and np.array are worth noting:

a=array.array('h',s)
n=np.array(a)

In [26]: %timeit n[1::2]
1000000 loops, best of 3: 669 ns per loop

In [27]: %timeit n[1::2].copy()
1000 loops, best of 3: 725 us per loop

In [28]: %timeit a[1::2]
1000 loops, best of 3: 1.91 ms per loop

这篇关于使用多处理的python pyaudio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆