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

 

1、pyaudio安装大背景 

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

 

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

 

但是,在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,发现无用

 1 ➜  ~  sudo apt-get install portaudio19-dev python-all-dev python3-all-dev
 2 Reading package lists... Done
 3 Building dependency tree       
 4 Reading state information... Done
 5 python-all-dev is already the newest version (2.7.14-2ubuntu1).
 6 The following additional packages will be installed:
 7   jackd1 jackd1-firewire libasound2-dev libjack-dev libjack0 libportaudiocpp0 libpython3-all-dev libzita-alsa-pcmi0 libzita-resampler1 python3-all uuid-dev
 8 Suggested packages:
 9   jack-tools meterbridge libasound2-doc portaudio19-doc
10 The following packages will be REMOVED:
11   jackd2 jackd2-firewire libjack-jackd2-0
12 The following NEW packages will be installed:
13   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
14 0 upgraded, 13 newly installed, 3 to remove and 5 not upgraded.
15 Need to get 0 B/798 kB of archives.
16 After this operation, 2,595 kB of additional disk space will be used.
17 Do you want to continue? [Y/n] Y
18 Preconfiguring packages ...
19 dpkg: warning: files list file for package 'emacsen-common' missing; assuming package has no files currently installed
20 (Reading database ... 419524 files and directories currently installed.)
21 Removing jackd2-firewire (1.9.10+20150825git1ed50c92~dfsg-5ubuntu1) ...
22 dpkg: jackd2: dependency problems, but removing anyway as you requested:
23  jackd depends on jackd2 | jackd1; however:
24   Package jackd2 is to be removed.
25   Package jackd1 is not installed.
26 
27 Removing jackd2 (1.9.10+20150825git1ed50c92~dfsg-5ubuntu1) ...
28 dpkg: warning: files list file for package 'emacsen-common' missing; assuming package has no files currently installed
29 (Reading database ... 419435 files and directories currently installed.)
30 Preparing to unpack .../jackd1_1%3a0.125.0-2_amd64.deb ...
31 Unpacking jackd1 (1:0.125.0-2) ...
32 dpkg: error processing archive /var/cache/apt/archives/jackd1_1%3a0.125.0-2_amd64.deb (--unpack):
33  trying to overwrite '/usr/lib/x86_64-linux-gnu/libjackserver.so.0', which is also in package libjack-jackd2-0:amd64 1.9.10+20150825git1ed50c92~dfsg-5ubuntu1
34 Errors were encountered while processing:
35  /var/cache/apt/archives/jackd1_1%3a0.125.0-2_amd64.deb
36 E: Sub-process /usr/bin/dpkg returned an error code (1)
37 ➜  ~  
操作log

 

坑三:Installing pyaudio with pip in a virtualenv

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

 

 3、最终解决方案

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

  https://stackoverflow.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导致有问题,关闭所有FQ的服务,重启电脑再次安装OK

         

  (真是费劲呀,哈哈哈)

 

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

 

4、官方简单DEMO

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

 1 """PyAudio example: Record a few seconds of audio and save to a WAVE file."""
 2 
 3 import pyaudio
 4 import wave
 5 
 6 CHUNK = 1024
 7 FORMAT = pyaudio.paInt16
 8 CHANNELS = 1
 9 RATE = 8000
10 RECORD_SECONDS = 3
11 WAVE_OUTPUT_FILENAME = "output.wav"
12 
13 p = pyaudio.PyAudio()
14 
15 stream = p.open(format=FORMAT,
16                 channels=CHANNELS,
17                 rate=RATE,
18                 input=True,
19                 frames_per_buffer=CHUNK)
20 
21 print("* recording")
22 
23 frames = []
24 
25 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
26     data = stream.read(CHUNK)
27     frames.append(data)
28 
29 print("* done recording")
30 
31 stream.stop_stream()
32 stream.close()
33 p.terminate()
34 
35 wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
36 wf.setnchannels(CHANNELS)
37 wf.setsampwidth(p.get_sample_size(FORMAT))
38 wf.setframerate(RATE)
39 wf.writeframes(b''.join(frames))
40 wf.close()

 

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

 1 """PyAudio Example: Play a WAVE file."""
 2 
 3 import pyaudio
 4 import wave
 5 import sys
 6 
 7 CHUNK = 1024
 8 
 9 if len(sys.argv) < 2:
10     print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
11     sys.exit(-1)
12 
13 wf = wave.open(sys.argv[1], 'rb')
14 
15 p = pyaudio.PyAudio()
16 
17 stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
18                 channels=wf.getnchannels(),
19                 rate=wf.getframerate(),
20                 output=True)
21 
22 data = wf.readframes(CHUNK)
23 
24 while data != '':
25     stream.write(data)
26     data = wf.readframes(CHUNK)
27 
28 stream.stop_stream()
29 stream.close()
30 
31 p.terminate()

 

 

参考链接

 

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


@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975
posted @ 2018-02-04 00:56  beautifulzzzz  阅读(11384)  评论(0编辑  收藏  举报