[python] 2、python使用pyaudio进行录音,及其在python虚拟环境virtualenv中安装遇到的问题

时间:2023-12-15 10:50:38

1、pyaudio安装大背景

最近在做智能音箱,需要编写声音拾取代码,我先是百度两篇比较常见的用python进行录音的操作的文章:

发现需要用到pyaudio,然后在其官网找到如何安装:http://people.csail.mit.edu/hubert/pyaudio/

[python] 2、python使用pyaudio进行录音,及其在python虚拟环境virtualenv中安装遇到的问题

但是,在py虚拟环境中通过pip安装会报错,而采用sudo apt-get install python-pyaudio python3-pyaudio安装没有报错,但是在虚拟环境中不行。

注:全局安装不适用于python虚拟环境中,python虚拟环境和全局环境互不影响。

2、解决python虚拟环境中安装pyaudio走过的坑

坑一:utbutu16.10 安装pyaudio模块过程出现错误 fatal error: portaudio.h: 没有那个文件或目录 error: command 'x86_64-linux-gn

尝试http://blog.csdn.net/hellodrawing/article/details/60868939,发现没用

坑二:安装pyaudio找不到portaudio.h的问题

尝试http://blog.csdn.net/qq_23729557/article/details/78956602,发现无用

 ➜  ~  sudo apt-get install portaudio19-dev python-all-dev python3-all-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-all-dev is already the newest version (2.7.-2ubuntu1).
The following additional packages will be installed:
jackd1 jackd1-firewire libasound2-dev libjack-dev libjack0 libportaudiocpp0 libpython3-all-dev libzita-alsa-pcmi0 libzita-resampler1 python3-all uuid-dev
Suggested packages:
jack-tools meterbridge libasound2-doc portaudio19-doc
The following packages will be REMOVED:
jackd2 jackd2-firewire libjack-jackd2-
The following NEW packages will be installed:
jackd1 jackd1-firewire libasound2-dev libjack-dev libjack0 libportaudiocpp0 libpython3-all-dev libzita-alsa-pcmi0 libzita-resampler1 portaudio19-dev python3-all python3-all-dev uuid-dev
upgraded, newly installed, to remove and not upgraded.
Need to get B/ kB of archives.
After this operation, , kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Preconfiguring packages ...
dpkg: warning: files list file for package 'emacsen-common' missing; assuming package has no files currently installed
(Reading database ... files and directories currently installed.)
Removing jackd2-firewire (1.9.+20150825git1ed50c92~dfsg-5ubuntu1) ...
dpkg: jackd2: dependency problems, but removing anyway as you requested:
jackd depends on jackd2 | jackd1; however:
Package jackd2 is to be removed.
Package jackd1 is not installed. Removing jackd2 (1.9.+20150825git1ed50c92~dfsg-5ubuntu1) ...
dpkg: warning: files list file for package 'emacsen-common' missing; assuming package has no files currently installed
(Reading database ... files and directories currently installed.)
Preparing to unpack .../jackd1_1%3a0.125.0-2_amd64.deb ...
Unpacking jackd1 (:0.125.-) ...
dpkg: error processing archive /var/cache/apt/archives/jackd1_1%3a0.125.0-2_amd64.deb (--unpack):
trying to overwrite '/usr/lib/x86_64-linux-gnu/libjackserver.so.0', which is also in package libjack-jackd2-:amd64 1.9.+20150825git1ed50c92~dfsg-5ubuntu1
Errors were encountered while processing:
/var/cache/apt/archives/jackd1_1%3a0.125.0-2_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code ()
➜ ~

操作log

坑三:Installing pyaudio with pip in a virtualenv

尝试https://*.com/questions/35708238/installing-pyaudio-with-pip-in-a-virtualenv,在操作时,安装一个东西会报错!!!

[python] 2、python使用pyaudio进行录音,及其在python虚拟环境virtualenv中安装遇到的问题

3、最终解决方案

  • 首先在virtualenv中安装pyaudio安装失败,搜索下说要先安装portaudio19-dev:

  https://*.com/questions/35708238/installing-pyaudio-with-pip-in-a-virtualenv

  • 安装portaudio19-dev失败,搜索说缺少依赖(依赖版本不对),因此要安装sudo apt-get install jackd2

  https://ubuntuforums.org/showthread.php?t=1680154

  • 之后在虚拟环境中安装(du_tts_stt) ➜ ~ pip install pyaudio,出现因SOCK proxy导致有问题,关闭所有*的服务,重启电脑再次安装OK

[python] 2、python使用pyaudio进行录音,及其在python虚拟环境virtualenv中安装遇到的问题

  (真是费劲呀,哈哈哈)

之后调用官方的一些demo :http://people.csail.mit.edu/hubert/pyaudio/运行有效果,但是有警告(可以忽略)

4、官方简单DEMO

官方一个简单的录音3S的DEMO:

 """PyAudio example: Record a few seconds of audio and save to a WAVE file."""

 import pyaudio
import wave CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 8000
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK) print("* recording") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data) print("* done recording") stream.stop_stream()
stream.close()
p.terminate() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

官方一个播放上述录制音频的DEMO:

 """PyAudio Example: Play a WAVE file."""

 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()

参考链接

:: 如果您觉得不错,请推荐给更多人,帮助他们更快地解决实际问题中的坑~


@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975