前置
(1) centos7环境下,运行yum -y install portaudio portaudio-devel进行环境准备
否则会产生如下错误信息:src/_portaudiomodule.c:29:23: fatal error: portaudio.h: No such file or directory
(2) 安装pyaudio:pip3 install pyaudio
错误信息
gcc -pthread -shared -Wl,-z,relro -g build/temp.linux-x86_64-3.6/src/_portaudiomodule.o -L/usr/lib64 -lportaudio -lpython3.6m -o build/lib.linux-x86_64-3.6/_portaudio.cpython-36m-x86_64-linux-gnu.so
/bin/ld: cannot find -lpython3.6m
解决:ln -s /usr/lib64/libpython3.6m.so.1.0 /usr/lib64/libpython3.6m.so
错误信息
src/_portaudiomodule.c:28:20: fatal error: Python.h: No such file or directory
解决:首先通过find /usr -name Python.h找到Python.h所在的目录,由于安装包中指定的python3.6版本,本机未安装,故通过软连接实现:ln -s /usr/local/include/python3.7m/ /usr/include/python3.6m
示例DEMO
import pyaudio
import wave
import sys
CHUNK = 1024
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
wf = wave.open(sys.argv[1], 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(CHUNK)
while data != '':
stream.write(data)
data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()
参考链接
网友评论