raspberry pi 3 上安裝 python的pyalsaaudio 來控制音量

目前我安裝pyalsaaudio的目的,是要用程式的方式控制rpi3上音量的大小

但在安裝過程中遇到一些瓶頸,因為我用的是python3,在這裡紀錄遇到的情形,及是如何解決的。

安裝

一開始輸入
sudo pip3 install pyalsaaudio

出現以下錯誤
pi@raspberrypi:~ $ sudo pip3 install pyalsaaudio
Downloading/unpacking pyalsaaudio
  Downloading pyalsaaudio-0.8.4.tar.gz (315kB): 315kB downloaded
  Running setup.py (path:/tmp/pip-build-150927bh/pyalsaaudio/setup.py) egg_info for package pyalsaaudio
   
    warning: no files found matching '*.json' under directory 'doc'
Installing collected packages: pyalsaaudio
  Running setup.py install for pyalsaaudio
    building 'alsaaudio' extension
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c alsaaudio.c -o build/temp.linux-armv7l-3.4/alsaaudio.o
    alsaaudio.c:28:28: fatal error: alsa/asoundlib.h: 沒有此一檔案或目錄
     #include 
                                ^
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    Complete output from command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-150927bh/pyalsaaudio/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-a1duhzf0-record/install-record.txt --single-version-externally-managed --compile:
    running install

running build

running build_ext

building 'alsaaudio' extension

creating build

creating build/temp.linux-armv7l-3.4

arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.4m -c alsaaudio.c -o build/temp.linux-armv7l-3.4/alsaaudio.o

alsaaudio.c:28:28: fatal error: alsa/asoundlib.h: 沒有此一檔案或目錄

 #include <alsa/asoundlib.h>

                            ^

compilation terminated.

error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-150927bh/pyalsaaudio/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-a1duhzf0-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip-build-150927bh/pyalsaaudio
Storing debug log for failure in /root/.pip/pip.log

說找不到 #include
上網查說這個包含在 libasound2-dev

輸入
sudo apt-get install libasound2-dev

sudo pip3 install pyalsaaudio


我是這樣輸入過的
#不小心打成這樣
sudo apt-get install libasound-dev
#再輸入檢查
sudo apt-get install libasound2-dev
#說已是最新版本
#再安裝pyalsaaudio
sudo pip3 install pyalsaaudio
#完成

範例程式

找到的簡易範例程式,是將聲音調高
import alsaaudio
m = alsaaudio.Mixer()
vol = m.getvolume()
vol = int(vol[0])

newVol = vol + 10         #將音量調高10 (範圍0~100)
m.setvolume(newVol)

其他作法  (不限用程式控制)

pi內建的console模式下的圖形介面,可以按方向鍵↑↓來調整音量

在終端機輸入

amixer set PCM -- 100%
指定音量100%

amixer get PCM
讀取現在的音量值





留言

這個網誌中的熱門文章

C# 模擬鍵盤滑鼠控制電腦

android 定時通知(永久長期的) 本篇只講AlarmManager使用

python nn 聲音辨識 -1 傅立葉轉換

python pyautogui 簡介

python opencv 基本讀取、轉換、顯示、儲存等