[Python] 2, Python uses Pyaudio for recording, and its installation in the Python virtual environment virtualenv encountered problems

Source: Internet
Author: User
Tags unpack uuid virtual environment virtualenv

1, Pyaudio installation of large background

Recently in the smart speaker, you need to write a sound pickup code, I first Baidu two more common use of Python to record the operation of the article:

    • Python writes a recording applet:http://blog.csdn.net/yexiaohhjk/article/details/73132562
    • Using Baidu language recognition API to realize speech recognition Python:http://blog.csdn.net/yexiaohhjk/article/details/73134815

Discover the need to use Pyaudio and find out how to install it on its official website:http://people.csail.mit.edu/hubert/pyaudio/

However, in the Py virtual environment through PIP installation will error, and the use of sudo apt-get install python-pyaudio python3-pyaudio installation without error, but in the virtual environment.

Note: The global installation does not work in a Python virtual environment, and the Python virtual environment and the global environment do not affect each other.

2, solve the Python virtual environment installation Pyaudio through the pit

Pit One : utbutu16.10 installation Pyaudio module error fatal error:portaudio.h: No file or directory Error:command ' X86_64-LINUX-GN

try :http://blog.csdn.net/hellodrawing/article/details/60868939, found useless

Pit Two : installation Pyaudio Unable to find portaudio.h problem

try :http://blog.csdn.net/qq_23729557/article/details/78956602, found useless

1? ~sudoApt-getInstallPortaudio19-dev Python-all-dev python3-all-Dev2 Reading Package Lists ... Done3 Building Dependency Tree4 Reading State information ... Done5Python-all-dev is already the newest version (2.7. --2ubuntu1).6 The following additional packages'll be installed:7Jackd1 jackd1-firewire libasound2-dev libjack-dev libjack0 libportaudiocpp0 libpython3-all-dev libzita-alsa-pcmi0 Libzita-resampler1 Python3-all uuid-Dev8 Suggested Packages:9Jack-tools Meterbridge Libasound2-doc portaudio19-DocTen The following packages would be removed: OneJACKD2 Jackd2-firewire libjack-jackd2-0 A The following NEW packages'll 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 - 0Upgraded, -Newly installed,3To remove and5Not upgraded. theNeed to get0b/798KB of archives. -After this operation,2,595KB of additional disk space would be used. -Want to continue? [y/N] Y - preconfiguring Packages ... +Dpkg:warning:files Listfile  forPackage'Emacsen-common'missing; Assuming package have no files currently installed -(Reading database ...419524files and directories currently installed.) +Removing Jackd2-firewire (1.9.Ten+20150825git1ed50c92~dfsg-5ubuntu1) ... A dpkg:jackd2:dependency problems, but removing anyway as you requested: atJACKD depends on Jackd2 |jackd1; however: - Package jackd2 are to be removed. - Package jackd1 are not installed. -  -Removing JACKD2 (1.9.Ten+20150825git1ed50c92~dfsg-5ubuntu1) ... -Dpkg:warning:files Listfile  forPackage'Emacsen-common'missing; Assuming package have no files currently installed in(Reading database ...419435files and directories currently installed.) -Preparing to unpack .../jackd1_1%3a0.125.0-2_amd64.deb ... toUnpacking Jackd1 (1:0.125.0-2) ... +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',whichis alsoinchPackage libjack-jackd2-0: AMD641.9.Ten+20150825git1ed50c92~dfsg-5UBUNTU1 theErrors were encountered whileProcessing: */var/cache/apt/archives/jackd1_1%3a0.125.0-2_amd64.deb $E:sub-process/usr/bin/dpkg returned an error code (1)Panax Notoginseng? ~
Operation Log

Pit Three : Installing Pyaudio with PIP in a virtualenv

try :https://stackoverflow.com/questions/35708238/installing-pyaudio-with-pip-in-a-virtualenv, In operation, install a thing will error!!!

3. Final Solution
    • First in virtualenv installed in Pyaudio installation failed, search said to install first Portaudio19-dev :

  Https://stackoverflow.com/questions/35708238/installing-pyaudio-with-pip-in-a-virtualenv

    • installation Portaudio19-dev failed, search says missing dependency (dependent version is not correct), so to install sudo apt-get install jackd2

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

    • install later in the virtual environment (DU_TTS_STT)? ~ Pip Install Pyaudio , there is a cause SOCK Proxy cause problems, shut down all FQ services, restart the computer to install again OK

(It's so hard, haha ha)

after calling some of the official Demo : http://people.csail.mit.edu/hubert/pyaudio/ run with effect, but with warning (can be ignored)

4, the official simple demo

Official demo of a simple recording 3S:

1 """Pyaudio Example:record A few seconds of audio and save to a WAVE file."""2 3 ImportPyaudio4 ImportWave5 6CHUNK = 10247FORMAT =pyaudio.paint168CHANNELS = 19Rate = 8000TenRecord_seconds = 3 OneWave_output_filename ="Output.wav" A  -p =Pyaudio. Pyaudio () -  thestream = P.open (format=FORMAT, -channels=CHANNELS, -Rate=Rate , -input=True, +Frames_per_buffer=CHUNK) -  + Print("* Recording") A  atFrames = [] -  -  forIinchRange (0, int (rate/chunk *record_seconds)): -data =Stream.read (CHUNK) - frames.append (data) -  in Print("* Done Recording") -  to Stream.stop_stream () + stream.close () - p.terminate () the  *WF = Wave.open (Wave_output_filename,'WB') $ wf.setnchannels (CHANNELS)Panax Notoginseng wf.setsampwidth (P.get_sample_size (FORMAT)) - wf.setframerate (Rate) theWf.writeframes (b"'. Join (frames)) +Wf.close ()

An official demo that plays the above recorded audio:

1 """Pyaudio example:play a WAVE file."""2 3 ImportPyaudio4 ImportWave5 ImportSYS6 7CHUNK = 10248 9 ifLen (SYS.ARGV) < 2:Ten     Print("Plays a wave file.\n\nusage:%s filename.wav"%sys.argv[0]) OneSys.exit (-1) A  -WF = Wave.open (Sys.argv[1],'RB') -  thep =Pyaudio. Pyaudio () -  -stream = P.open (format=P.get_format_from_width (Wf.getsampwidth ()), -channels=wf.getnchannels (), +Rate=wf.getframerate (), -output=True) +  Adata =wf.readframes (CHUNK) at  -  whileData! ="': - stream.write (data) -data =wf.readframes (CHUNK) -  - Stream.stop_stream () in stream.close () -  toP.terminate ()

Reference links

    • 1. Python writes a recording applet
    • 2. Using the Baidu Language Recognition API for speech recognition python
    • 3. Pyaudio official website

:: If you feel good, please recommend to more people to help them solve the problem in the pit of real problems faster!

@beautifulzzzz Intelligent Hardware, Internet of things, love technology, focus on products blog: http://blog.beautifulzzzz.com Group of Friends: 414948975

[Python] 2, Python uses Pyaudio for recording, and its installation in the Python virtual environment virtualenv encountered problems

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.