ラズパイ3に pyaudio インストール
1 | sudo pip install pyaudio |
を実行すると
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | Looking in indexes: https: //pypi .org /simple , https: //www .piwheels.org /simple Collecting pyaudio Downloading https: //files .pythonhosted.org /packages/ab/42/b4f04721c5c5bfc196ce156b3c768998ef8c0ae3654ed29ea5020c749a6b/PyAudio-0 .2.11. tar .gz Building wheels for collected packages: pyaudio Running setup.py bdist_wheel for pyaudio ... error Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-q8X_DO/pyaudio/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-DpglbX --python-tag cp27: running bdist_wheel running build running build_py creating build creating build /lib .linux-armv7l-2.7 copying src /pyaudio .py -> build /lib .linux-armv7l-2.7 running build_ext building '_portaudio' extension creating build /temp .linux-armv7l-2.7 creating build /temp .linux-armv7l-2.7 /src arm-linux-gnueabihf- gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate- time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map= /build/python2 .7-InigCj /python2 .7-2.7.16=. -fstack-protector-strong -Wformat -Werror= format -security -fPIC -I /usr/include/python2 .7 -c src /_portaudiomodule .c -o build /temp .linux-armv7l-2.7 /src/_portaudiomodule .o src /_portaudiomodule .c:29:10: fatal error: portaudio.h: そのようなファイルやディレクトリはありません #include "portaudio.h" ^~~~~~~~~~~~~ compilation terminated. error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1 ---------------------------------------- Failed building wheel for pyaudio Running setup.py clean for pyaudio Failed to build pyaudio Installing collected packages: pyaudio Running setup.py install for pyaudio ... error Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-q8X_DO/pyaudio/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-POjPm0/install-record .txt --single-version-externally-managed --compile: running install running build running build_py creating build creating build /lib .linux-armv7l-2.7 copying src /pyaudio .py -> build /lib .linux-armv7l-2.7 running build_ext building '_portaudio' extension creating build /temp .linux-armv7l-2.7 creating build /temp .linux-armv7l-2.7 /src arm-linux-gnueabihf- gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate- time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map= /build/python2 .7-InigCj /python2 .7-2.7.16=. -fstack-protector-strong -Wformat -Werror= format -security -fPIC -I /usr/include/python2 .7 -c src /_portaudiomodule .c -o build /temp .linux-armv7l-2.7 /src/_portaudiomodule .o src /_portaudiomodule .c:29:10: fatal error: portaudio.h: そのようなファイルやディレクトリはありません #include "portaudio.h" ^~~~~~~~~~~~~ compilation terminated. error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python -u -c " import setuptools, tokenize;__file__= '/tmp/pip-install-q8X_DO/pyaudio/setup.py' ;f=getattr(tokenize, 'open' , open )(__file__);code=f. read ().replace( '\r\n' , '\n' );f.close(); exec (compile(code, __file__, 'exec' )) " install --record /tmp/pip-record-POjPm0/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-q8X_DO/pyaudio/ |
というエラーがでる
CentOSにPyenvをインストールしてPython3の開発環境を構築する
を参考に
1 | pip list |
を実行すると
1 2 3 4 5 6 | Package Version ---------- ------- pip 18.1 setuptools 40.6.2 You are using pip version 18.1, however version 20.2b1 is available. You should consider upgrading via the 'pip install --upgrade pip' command . |
となり
バージョンが古いらしい
1 | pip install --upgrade pip |
でアップデート
再度実行してもエラーになるので
1 | sudo python --version |
としたら
1 | Python 2.7.16 |
となっていたので
1 | sudo pyenv global 3.6.10 |
としたところ
1 | sudo : pyenv: コマンドが見つかりません |
となってしまうので
PyenvによるPython3.x環境構築(CentOS, Ubuntu)
を参考に
1 | sudo visudo |
でファイルを編集する
なお nano エディタで操作することになるので
使い方に関しては
GNU nanoを使いこなす
を参考に
1 | Defaults secure_path= "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
の部分を
1 2 3 | #Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" Defaults env_keep += "PATH" Defaults env_keep += "PYENV_ROOT" |
として
ctrl + s で上書き保存し
ctrl + x で終了
これで
1 | sudo pyenv global 3.6.10 |
とすれば
1 | sudo python --version |
を実行したときに
1 | Python 3.6.10 |
と変更されるようになった
次に
1 | sudo pip install pyaudio |
を実行してみたが
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Looking in indexes: https: //pypi .org /simple , https: //www .piwheels.org /simple Collecting pyaudio Downloading PyAudio-0.2.11. tar .gz (37 kB) Using legacy setup.py install for pyaudio, since package 'wheel' is not installed. Installing collected packages: pyaudio Running setup.py install for pyaudio ... error ERROR: Command errored out with exit status 1: command : /home/pi/ .pyenv /versions/3 .6.10 /bin/python3 .6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = ' "'" ' /tmp/pip-install-_p6von58/pyaudio/setup .py '"' " '; __file__=' "'" ' /tmp/pip-install-_p6von58/pyaudio/setup .py '"' " ';f=getattr(tokenize, ' "'" ' open '"' " ', open)(__file__);code=f.read().replace(' "'" '\r\n '"' " ', ' "'" '\n '"' " ');f.close();exec(compile(code, __file__, ' "'" ' exec '"' " '))' install --record /tmp/pip-record-eikj46h2/install-record .txt --single-version-externally-managed --compile -- install -headers /home/pi/ .pyenv /versions/3 .6.10 /include/python3 .6m /pyaudio cwd: /tmp/pip-install-_p6von58/pyaudio/ Complete output (16 lines): running install running build running build_py creating build creating build /lib .linux-armv7l-3.6 copying src /pyaudio .py -> build /lib .linux-armv7l-3.6 running build_ext building '_portaudio' extension creating build /temp .linux-armv7l-3.6 creating build /temp .linux-armv7l-3.6 /src gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I /home/pi/ .pyenv /versions/3 .6.10 /include/python3 .6m -c src /_portaudiomodule .c -o build /temp .linux-armv7l-3.6 /src/_portaudiomodule .o src /_portaudiomodule .c:29:10: fatal error: portaudio.h: そのようなファイルやディレクトリはありません #include "portaudio.h" ^~~~~~~~~~~~~ compilation terminated. error: command 'gcc' failed with exit status 1 ---------------------------------------- ERROR: Command errored out with exit status 1: /home/pi/ .pyenv /versions/3 .6.10 /bin/python3 .6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = ' "'" ' /tmp/pip-install-_p6von58/pyaudio/setup .py '"' " '; __file__=' "'" ' /tmp/pip-install-_p6von58/pyaudio/setup .py '"' " ';f=getattr(tokenize, ' "'" ' open '"' " ', open)(__file__);code=f.read().replace(' "'" '\r\n '"' " ', ' "'" '\n '"' " ');f.close();exec(compile(code, __file__, ' "'" ' exec '"' " '))' install --record /tmp/pip-record-eikj46h2/install-record .txt --single-version-externally-managed --compile -- install -headers /home/pi/ .pyenv /versions/3 .6.10 /include/python3 .6m /pyaudio Check the logs for full command output. |
となってしまう
このため
を参考に
1 | sudo apt-get install python-dev portaudio19-dev |
でパッケージをインストールし
1 | sudo pip install pyaudio |
でインストール
1 2 | sudo bluetoothctl connect 6C:56:97:3A:52:98 |
を実行し Amazon Echo へ接続
ctrl + d で抜けて
1 | vim test .py |
でファイルを作成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import wave import pyaudio def play(wav_file): try: wf = wave. open (wav_file, "rb" ) except FileNotFoundError: print( "No such file " + wav_file) return p = pyaudio.PyAudio() ## Bluetoothスピーカーの情報を取得 for device_index in range(p.get_device_count()): device = p.get_device_info_by_index(device_index) if device[ "name" ] == "bluealsa" : bluetooth_device = device_index ## Bluetoothスピーカーを指定してストリームオープン stream = p. open ( format =p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True, output_device_index=bluetooth_device ) # 再生 chunk_size = 1024 data = wf.readframes(chunk_size) while len(data) > 0: stream.write(data) data = wf.readframes(chunk_size) stream.stop_stream() stream.close() p.terminate() if __name__ == "__main__" : play( "/usr/share/sounds/alsa/Front_Center.wav" ) |
として保存
音声再生サンプルコードは
Amazon EchoをRaspberry PiのBluetoothスピーカーとして利用する
のものを使わせていただきました
あとは
1 | python test .py |
を実行し
Amazon Echo から
フロント センター
と聞こえれば
python による音声出力ができることが確認できる