简体   繁体   English

在 OSX 上为 Python3 安装 PyAudio

[英]Installing PyAudio for Python3 on OSX

I would like to construct a "hello world" of audio for OSX/Python3 that populates a buffer with a sinewave and plays it through the speakers.我想为 OSX/Python3 构建一个音频的“hello world”,用正弦波填充缓冲区并通过扬声器播放。

My basic options are listed here: https://wiki.python.org/moin/Audio/我的基本选项在这里列出: https : //wiki.python.org/moin/Audio/

http://guzalexander.com/2012/08/17/playing-a-sound-with-python.html demonstrates several of these, but omits details of installation. http://guzalexander.com/2012/08/17/playing-a-sound-with-python.html演示了其中的几个,但省略了安装细节。

I'm trying PyAudio first.我首先尝试 PyAudio。

http://people.csail.mit.edu/hubert/pyaudio/ has a section on OSX, saying "For Python 3 support, first install MacPython 3.3" http://people.csail.mit.edu/hubert/pyaudio/有一个关于 OSX 的部分,说“对于 Python 3 支持,首先安装 MacPython 3.3”

This confuses me greatly.这让我非常困惑。 OSX ships with Python, also I have used homebrew to install Python3. OSX 附带 Python,我也使用自制软件来安装 Python3。 I've never come across the term MacPython, so I'm not sure if what I have is MacPython or not.我从来没有遇到过 MacPython 这个词,所以我不确定我所拥有的是否是 MacPython。 And if it isn't, then I want to install PyAudio for the Python I currently have, I don't want to have to download some special Python to use it.如果不是,那么我想为我目前拥有的 Python 安装 PyAudio,我不想下载一些特殊的 Python 来使用它。 That would be completely defeating the purpose.那将完全违背目的。

I also tried "pip3 install pyaudio", with the following (negative) results:我也试过“pip3 install pyaudio”,结果如下(否定的):

 ⤐  pip3 install pyaudio
Collecting pyaudio
  Could not find a version that satisfies the requirement pyaudio (from versions: )
  Some externally hosted files were ignored as access to them may be unreliable (use --allow-external pyaudio to allow).
No matching distribution found for pyaudio
 ✘

pi@piBookAir.local ~ /Users/pi:
 ⤐  pip install --allow-external pyaudio
You must give at least one requirement to install (see "pip help install")
 ✔

pi@piBookAir.local ~ /Users/pi:
 ⤐  pip install pyaudio --allow-external pyaudio
Collecting pyaudio
  Could not find a version that satisfies the requirement pyaudio (from versions: )
  Some insecure and unverifiable files were ignored (use --allow-unverified pyaudio to allow).
No matching distribution found for pyaudio
 ✘

It's frustrating that it is so awkward to even get onto the first rung of the ladder.令人沮丧的是,连爬上梯子的第一级都如此尴尬。

How to correctly install PyAudio for Python3 on my (up-to-date) OSX?如何在我的(最新)OSX 上正确安装 Python3 的 PyAudio? Should I use homebrew?我应该使用自制软件吗? Should I use pip3?我应该使用 pip3 吗?

PS http://blog.robindeits.com/2012/01/09/pyaudio-with-homebrew/ -- I hope I don't have to follow this solution PS http://blog.robindeits.com/2012/01/09/pyaudio-with-homebrew/——我希望我不必遵循这个解决方案

PPS https://wiki.python.org/moin/PythonInMusic lists a daunting number of possible solution paths -- I'm not at all certain PyAudio is the best path. PPS https://wiki.python.org/moin/PythonInMusic列出了大量可能的解决方案路径——我完全不确定 PyAudio 是最佳路径。 If someone knows of a better one, please do comment!如果有人知道更好的,请发表评论!

Use Homebrew to install the prerequisite portaudio library, then install PyAudio using pip:使用Homebrew安装必备的 portaudio 库,然后使用 pip 安装 PyAudio:

brew install portaudio 

pip install pyaudio

Notes:笔记:

If not already installed, download Homebrew.如果尚未安装,请下载 Homebrew。 pip will download the PyAudio source and build it for your version of Python. pip 将下载 PyAudio 源并为您的 Python 版本构建它。 Homebrew and building PyAudio also require installing the Command Line Tools for Xcode (more information). Homebrew 和构建 PyAudio 还需要为 Xcode 安装命令行工具(更多信息)。

Unfortunately,王淳龙's solution did not work for me (macOS Catalina 10.15.6, Python 3.8.5).不幸的是,王淳龙的解决方案对我不起作用(macOS Catalina 10.15.6,Python 3.8.5)。 The following errors persisted:以下错误仍然存​​在:

  gcc-5 -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/tom/anaconda3/include -arch x86_64 -I/Users/tom/anaconda3/include -arch x86_64 -DMACOSX=1 -I/Users/tom/anaconda3/include/python3.6m -c src/_portaudiomodule.c -o build/temp.macosx-10.9-x86_64-3.6/src/_portaudiomodule.o
  In file included from src/_portaudiomodule.c:33:0:
  /usr/local/include/pa_mac_core.h:48:33: fatal error: AudioUnit/AudioUnit.h: No such file or directory
  compilation terminated.
  error: command 'gcc-5' failed with exit status 1

Therefore, I had to link the missing libraries manually:因此,我不得不手动链接缺少的库:

cd /usr/local/include/
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AudioUnit.framework/Versions/A/Headers AudioUnit
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AudioToolbox.framework/Versions/A/Headers AudioToolbox
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreAudioTypes.framework/Versions/A/Headers CoreAudioTypes
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/A/Headers CoreFoundation
ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/CoreAudio.framework/Versions/A/Headers CoreAudio

This solved the dependencies, but not the following compiler problem:这解决了依赖关系,但没有解决以下编译器问题:

    In file included from /usr/local/include/AudioToolbox/AUComponent.h:65:0,
                     from /usr/local/include/AudioUnit/AUComponent.h:1,
                     from /usr/local/include/AudioUnit/AudioUnit.h:11,
                     from /usr/local/include/pa_mac_core.h:48,
                     from src/_portaudiomodule.c:33:
    /usr/local/include/AudioToolbox/AudioComponent.h:509:39: error: expected ')' before '^' token
                                     void (^inCompletionHandler)(AudioComponentInstance __nullable, OSStatus))
                                           ^

Forcing the use of system-wide gcc (Apple clang version 11.0.3) instead of gcc-5 solved the problem.强制使用系统范围的gcc (Apple clang 版本 11.0.3)而不是gcc-5解决了这个问题。 I did that by uninstalling gcc@5 and linking gcc-5 to gcc , however there must be a more elegant way.我通过卸载gcc@5并将gcc-5链接到gcc来做到这一点,但是必须有一种更优雅的方式。

今天我也遇到了这个问题,搜索了很多,终于找到了解决办法: 1.brew install portaudio 2.pip install pyaudio

(works on mac OSX) (适用于 Mac OSX)

at first pyaudio has portaudio dependency:首先 pyaudio 有 portaudio 依赖:

brew install portaudio 

and then run:然后运行:

pip3 install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"

or sometimes magic happens, if you use:或者有时会发生魔法,如果你使用:

python3 -m pip install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"

Python vs MacPython: Python 与 MacPython:

AFAIK there is no such thing as MacPython — or at least not from the developers of Python . AFAIK 没有MacPython这样的东西——或者至少不是来自Python的开发人员。 I can only assume whoever created the link on the PyAudio page tried to get creative with the name and were actually meaning the " Mac OS X 64-bit/32-bit installer containing Python v3.3 ".我只能假设在PyAudio页面上创建链接的PyAudio试图通过名称获得创意,并且实际上是指“包含 Python v3.3 的 Mac OS X 64 位/32 位安装程序”。

The link to the mysteriously named MacPython leads to the main Python download page that features the most recent stable versions (which as of today is v3.4.3):神秘命名的MacPython的链接指向主要的Python下载页面,其中包含最新的稳定版本(截至今天是 v3.4.3):

https://www.python.org/downloads/release/python-343/ https://www.python.org/downloads/release/python-343/

PyAudio:音频:

PyAudio can be compiled from source, outlined here , which might yield better results. PyAudio可以从源代码编译,这里概述,这可能会产生更好的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM