我想构建一个" hello world"用于OSX / Python3的音频,用正弦波填充缓冲区并通过扬声器播放。
我的基本选项列在此处:https://wiki.python.org/moin/Audio/
http://guzalexander.com/2012/08/17/playing-a-sound-with-python.html演示了其中的几个,但省略了安装的详细信息。
我先尝试PyAudio。
http://people.csail.mit.edu/hubert/pyaudio/有一个关于OSX的部分,说"对于Python 3支持,首先安装MacPython 3.3"
这让我很困惑。 OSX附带Python,我也使用自制软件来安装Python3。我从未遇到过MacPython这个词,所以我不确定我的MacPython是否合适。如果它不是,那么我想为我目前拥有的Python安装PyAudio,我不想下载一些特殊的Python来使用它。这将彻底击败目的。
我也试过" pip3安装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
✘
令人沮丧的是,即使进入梯子的第一梯队也是如此尴尬。
如何在我的(最新)OSX上正确安装PyAudio for Python3?我应该使用自制软件吗?我应该使用pip3吗?
PS http://blog.robindeits.com/2012/01/09/pyaudio-with-homebrew/ - 我希望我不必遵循此解决方案
PPS https://wiki.python.org/moin/PythonInMusic列出了令人生畏的可能解决方案路径 - 我根本不确定PyAudio是最佳路径。如果有人知道更好的,请发表评论!
答案 0 :(得分:3)
使用Homebrew安装必备的portaudio库,然后使用pip安装PyAudio:
brew install portaudio
pip install pyaudio
注意:
如果尚未安装,请下载Homebrew。 pip将下载PyAudio源代码并为您的Python版本构建它。 自制和构建PyAudio还需要安装Xcode命令行工具(更多信息)。
答案 1 :(得分:1)
不幸的是,王淳龙的解决方案对我不起作用(macOS Catalina 10.15.6,Python 3.8.5)。仍然存在以下错误:
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
因此,我不得不手动链接缺少的库:
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
这解决了依赖性,但没有解决以下编译器问题:
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))
^
强制使用系统范围的gcc
(Apple clang版本11.0.3)代替gcc-5
解决了此问题。我通过卸载gcc@5
并将gcc-5
链接到gcc
来做到这一点,但是必须有一种更优雅的方法。
答案 2 :(得分:0)
Python与MacPython:
AFAIK没有 MacPython 这样的东西 - 或者至少不是来自Python
的开发者。我只能假设在PyAudio
页面上创建链接的人试图通过名称获得创意,并且实际上意味着包含Python v3的" Mac OS X 64位/ 32位安装程序。 3 强>&#34 ;.
神秘名为 MacPython 的链接指向主Python
下载页面,其中包含最新的稳定版本(截至今日为v3)。 4.3):
https://www.python.org/downloads/release/python-343/
<强> PyAudio:强>
PyAudio
可以从源代码outlined here进行编译,这可能会产生更好的结果。
答案 3 :(得分:0)
今天我也遇到了这个问题,并且搜索了很多,最后我找到了解决方案: 1.brew install portaudio 2.pip install pyaudio
答案 4 :(得分:0)
(适用于Mac OSX)
首先pyaudio具有portaudio依赖项:
brew install portaudio
然后运行:
pip3 install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"
如果使用以下命令,有时会发生魔术:
python3 -m pip install pyaudio --global-option="build_ext" --global-option="-I/usr/local/include" --global-option="-L/usr/local/lib"