python的无gil portaudio流

pastream的Python项目详细描述


https://badge.fury.io/py/pastream.svghttps://travis-ci.org/tgarc/pastream.svg?branch=masterhttps://ci.appveyor.com/api/projects/status/wk52r5jy9ri7dsi9/branch/master?svg=true

python的无gil portaudio流

pastream构建在portaudio和 优秀的sounddevice python绑定提供了一些更高级的功能 盒子。注意,除了pastreamlibrary之外,pastream还包括 command line application用于播放 以及录制音频文件。

文档:
http://pastream.readthedocs.io/
源代码存储库和问题跟踪程序:
http://github.com/tgarc/pastream/

功能

无gil音频回调
用c实现portaudio回调意味着音频中断可以 在不需要获取python的情况下快速可靠地提供服务 全局解释器锁(gil)。在使用库时,这一点至关重要 就像Pillow可能贪婪地抓住 按住gil,随后会导致音频溢出/不足。
输入流迭代器

通过iterable有效地检索实时音频捕获数据。简单到:

importpastreamaspsforchunkinps.chunks():process(chunk)

请参见pastream.chunkspastream.InputStream.chunks方法。

内置支持使用声音文件和numpy ndarrays
无缝支持numpy ndarrays的回放/录制,通用缓冲区 类型和声音文件。
读/写线程
Pastream简化了流读写器的实现过程 离开时在后台操作和/或生成数据的线程 用于更高级别管理任务的主线程。

外部依赖性

Pastream需要几个编译的库,这些库可能需要 根据您的操作系统单独安装。Windows用户是 幸运的是,他们可以完全跳过这一部分。

libffi(linux/unix/macosx):

在linux/unix/macosx平台下,您需要安装ffi 图书馆。(对于windows用户,ffi已经包含在python cffi中 libffi可通过大多数包管理器获得:

$ yum install libffi-devel # Red-hat/CentOS Linux
$ apt-get install libffi-dev # Ubuntu/debian derivatives
$ brew install libffi # Homebrew on OSX

cffi中提供了有关安装libffi的更多信息 文档here

PortAudiolibsndfile(Linux/Unix):
Linux和Unix用户还需要安装 PortAudiolibsndfile库。(对于Windows和OSX, sounddevice和soundfile python包包括 您可以从包管理器安装最新的 (例如,apt-get install libportaudio2 libsndfile用于debian/raspbian)或 从软件包网站安装最新的稳定版本(推荐)。

安装

一旦解决了上述依赖关系,就可以使用 管道:

$ pip install pastream

从源构建

使用--recursive标志克隆pastream:

$ git clone --recursive http://github.com/tgarc/pastream

或者,如果您已经结帐:

$ cd <path/to/checkout>
$ git submodule update --init

最后,从本地工作副本执行pip安装:

$ pip install <path/to/checkout>

建筑文档

Pastream的文档可以很容易地以多种格式生成 使用狮身人面像。只要按照下面的步骤。

签出存储库并将CD放入其中:

$ git clone http://github.com/tgarc/pastream
$ cd pastream

使用需求文件安装文档依赖项:

$ pip install -r docs/requirements.txt

然后使用包含的makefile/make.bat生成文档。(在这里我们 输出为HTML格式:

$ cd docs
$ make html

示例

将一秒钟的音频录制到内存中,然后播放:

importpastreamasps# Use *with* statements to auto-close the streamwithps.DuplexStream()asstream:out=stream.record(int(stream.samplerate),blocking=True)stream.play(out,blocking=True)

播放文件10秒,如果文件较短,则添加零填充,并且 将结果记录到内存:

importpastreamasps,soundfileassfwithsf.SoundFile('my-file.wav')asinfile,ps.DuplexStream.from_file(infile)asstream:out=stream.playrec(infile,frames=10*int(stream.samplerate),pad=-1,blocking=True)

抓取(真实)频率转换的现场音频流,50%重叠:

importpastreamasps,numpyasnpchunksize=1024window=np.hanning(chunksize)forx_linps.chunks(chunksize,overlap=chunksize//2,channels=1):X_l=np.fft.rfft(x_l*window)

动态生成纯音

importtimeimportpastreamaspsimportnumpyasnp# A simple tone generatordeftone_generator(stream,buffer,f,loop=False):fs=stream.samplerate# Create a time indext=2*np.pi*f*np.arange(len(buffer),dtype=stream.dtype)/fs# Loop until the stream stopswhilenotstream.finished:frames=buffer.write_availableifnotframes:time.sleep(0.010)continue# Get the write buffers directly to avoid making any extra copiesframes,part1,part2=buffer.get_write_buffers(frames)out=np.frombuffer(part1,dtype=stream.dtype)np.sin(t[:len(out)],out=out)iflen(part2):# part2 will be nonempty whenever we wrap around the end of the ring bufferout=np.frombuffer(part2,dtype=stream.dtype)np.sin(t[:len(out)],out=out)# flag that we've added data to the bufferbuffer.advance_write_index(frames)# advance the time indext+=2*np.pi*f*frames/fswithps.OutputStream(channels=1)asstream:# Set our tone generator as the source and pass along the frequencyfreq=1000stream.set_source(tone_generator,args=(freq,))# Busy-wait to allow for keyboard interruptstream.start()whilestream.active:time.sleep(0.1)

另请参见/examples下包含的示例。

命令行应用程序

一旦安装,pastream应用程序应该可以从命令中调用 行。如果你熟悉SoX你会 注意,有些命令行语法非常相似。这里有一些 帮助你开始的例子。

显示帮助文件:

$ pastream -h

列出可用的音频设备:

$ pastream -l

从默认音频设备同时播放和录制:

$ pastream input.wav output.wav

使用AU格式从SOX输入管道并记录回放:

$ sox -n -t au - synth sine 440 | pastream - output.wav

播放原始文件:

$ pastream -c1 -r48k -e=pcm_16 output.raw

以48kHz的频率录制10分钟的音频:

$ pastream null output.wav -r48k -d10:00

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java什么数据库最类似于Map,每个用户/id存储无限多个“键”和“值”?   java仅使用super pom进行测试   内存不足如何解析java。OutOfMemoryError:Java堆空间在增加堆大小的情况下将意味着延迟OutOfMemoryError   来自另一个类的mysql和java jdbc调用[运行时应用程序]   java通过下拉菜单更改搜索框搜索的内容   JAVAlang.ClassNotFoundException:sun。jdbc。odbc。JdbcOdbcDriver   java Selenium点击链接   JavaSpringHibernate:从唯一值列表中获取对象列表   java Bing广告与桌面身份验证问题   java如何在没有任何外部SDK的情况下从安卓打印到收据打印机?   未调用java菜单片段类   java在IDEA和PyCharm中同时为同一个项目工作   java我们如何为同一个异常提供不同的海关信息   jakarta ee中是否预定义了“请求”和“响应”变量或值?   java更好地解决“之前和之后”难题?   尝试将数据从Excel添加到Java   发送电子邮件的Java代码只适用于一个电子邮件id?   java如何从资产解析XML?