Bluetooth A2DP

今回は、Raspberry Pi Zeroを使用してA2DP Sinkデバイスを構築してみます。

必要なもの:

Raspberry Pi Zero

Micro SDカード (最低2GB)

Audio Interface

USB Serial Cable/USB Ethernet (アップデートを行う必要もあるので、USB Ethernetがおすすめ)

<準備>

最初にSD Cardをフォーマットします。

sudo gparted /dev/mmcblk0

パーティションをアンマウント後に全て削除します。

次に、Raspberry PI ORGより、イメージをダウンロードします。

今回はLiteバージョンを使用します。

https://www.raspberrypi.org/downloads/raspbian/

解凍後、次のコマンドでSD Cardにイメージをコピーします。

dd bs=4M if=2019-04-08-raspbian-stretch-lite.img of=/dev/mmcblk0 status=progress conv=fsync

いったん、SDカードを抜いて、再度挿入します。すると、/boot, /rootというパーティションがマウントされます。

Ethernetで接続する場合、上記/bootパーティションにsshというファイルを作成します(ゼロバイトで良い)。

USB Serialで接続する場合、/boot/config.txtにuart_enable=1というラインを追加します(これがないとデバグコンソールに接続できません)


<セットアップ>

今回は、USB Ethernetを接続して進めます。

nmapもしくはzenmapを使用して、ipアドレスを調べます。もしくはルータにアクセスして、DHCPのIPアドレス付与状況を確認します。

ssh pi@xxx.xxx.xxx.xxx (ログインプロンプトが出たらraspberrypiと入れログインします)

まず、サーバを日本のミラーサーバにセットします。

sudo vi /etc/apt/source.list

下記2行を追加します。

deb http://ftp.jaist.ac.jp/raspbian/ stretch main contrib non-free rpi

deb http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ stretch main contrib non-free rpi

sudo apt-get update

sudo apt-get upgrade

sudo rpi-update

リブートします。

sudo reboot

<BTインストールとコンフィグレーション>

下記のコマンドにより必要なファイルをインストールします。

sudo apt-get install alsa-utils bluez bluez-tools pulseaudio-module-bluetooth python-gobject python-gobject-2

sudo apt-get install bluealsa


確認

$ sudo bluetoothctl

[NEW] Controller XX:XX:XX:XX:XX:XX raspberrpi [default]

[bluetooth]# show

Controller XX:XX:XX:XX:XX:XX

Name: raspberrypi

Alias: raspberrypi

Class: 0x000000

Powered: yes

Discoverable: no

Pairable: yes

UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)

UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)

UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)

Modalias: usb:v1D6Bp0246d052B

Discovering: no


この段階で、Audio sinkが見えません。

次のファイルを編集します。

sudo vi /lib/systemd/system/bluealsa.service

[Unit]

Description=BluezALSA proxy

Requires=bluetooth.service

After=bluetooth.service

[Service]

Type=simple

User=root

ExecStart=/usr/bin/bluealsa -p a2dp-sink --a2dp-volume &

[Install]

WantedBy=multi-user.target


念のためBluetoothのクラスも変更します。

sudo vi /etc/bluetooth/main.conf

[General]

Class = 0x20041C


リブートします。

sudo reboot

再度確認します。

$ sudo bluetoothctl

[NEW] Controller XX:XX:XX:XX:XX:XX raspberrypi [default]

[bluetooth]# show

Controller XX:XX:XX:XX:XX:XX

Name: raspberrypi

Alias: raspberrypi

Class: 0x040000

Powered: yes

Discoverable: no

Pairable: yes

UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)

UUID: Audio Sink (0000110b-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)

UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)

UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)

UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)

Modalias: usb:v1D6Bp0246d052B

Discovering: no

<オーディオの出力>

Bluetooth Sourceデバイスと接続後、下記のコマンドでオーディオが出力されるか確認します。


sudo /usr/bin/bluealsa-aplay --profile-a2dp -vvv 00:00:00:00:00:00


Selected configuration:

HCI device: hci0

PCM device: default

PCM buffer time: 500000 us

PCM period time: 100000 us

Bluetooth device(s): ANY

Profile: A2DP

Used configuration for XX:XX:XX:XX:XX:XX:

PCM buffer time: 500000 us (88200 bytes)

PCM period time: 100000 us (17640 bytes)

Sampling rate: 44100 Hz

Channels: 2


System controlで自動起動するようにします。

sudo vi /lib/systemd/system/bluealsa-playback.service


[Unit]

Description=bluealsa a2dp playback

After=bluealsa.service syslog.service

Requires=bluealsa.service

[Service]

ExecStartPre=/bin/sleep 5

ExecStart=/usr/bin/bluealsa-aplay --profile-a2dp 00:00:00:00:00:00

StandardOutput=syslog

StandardError=syslog

SyslogIdentifier=Bluealsa-Playback

User=root

Restart=on-failure

[Install]

WantedBy=multi-user.target


サービスの有効化

sudo systemctl enable bluealsa-playback.service


リブートして、接続、音が出ることを確認します。

Bluetooth Source側のAbsolute Volumeを有効にしておけば、Source側でボリュームの制御が可能です。

<bluealsa-aplayの調整>

bluealsa-aplay --helpでパラメータの説明が出てきます。

$ sudo bluealsa-aplay --help

Usage:

bluealsa-aplay [OPTION]... <BT-ADDR>...


Options:

-h, --help print this help and exit

-V, --version print version and exit

-v, --verbose make output more verbose

-i, --hci=hciX HCI device to use

-d, --pcm=NAME PCM device to use

--pcm-buffer-time=INT PCM buffer time

--pcm-period-time=INT PCM period time

--profile-a2dp use A2DP profile

--profile-sco use SCO profile

--single-audio single audio mode


Note:

If one wants to receive audio from more than one Bluetooth device, it is

possible to specify more than one MAC address. By specifying any/empty MAC

address (00:00:00:00:00:00), one will allow connections from any Bluetooth

device.


下記のコマンドでオーディオ出力の遅延時間を制御できます。

$ sudo bluealsa-aplay --pcm-buffer-time=10000 --pcm-period=time=0 -vvv 00:00:00:00:00:00

Selected configuration:

HCI device: hci0

PCM device: default

PCM buffer time: 10000 us

PCM period time: 0 us

Bluetooth device(s): ANY

Profile: A2DP

Used configuration for XX:XX:XX:XX:XX:XX:

PCM buffer time: 10000 us (1764 bytes)

PCM period time: 3333 us (588 bytes)

Sampling rate: 44100 Hz

Channels: 2


<Audio DACのセッティング>

今回使用したAUDIO DACを動作させるための設定です。下記URLに詳細に記載されていますが、次の内容でオーディオは出力されました。

https://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install


sudo vi /boot/config.txt

1.下記行を追加する。

dtoverlay=hifiberry-dac

2. 下記行をコメント化する。

#dtparam=audio-on


sudo vi /etc/asound.conf

下記を追加・編集。

pcm.!default {

type hw card 0

}

ctl.!default {

type hw card 0

}


sudo reboot

<A2DP AAC decode>

bluealsaはインストールせずに下記により生成します。


A2DPでAACで接続可能とします。

sudo apt-get install autoconf automake flex bison make m4

sudo apt-get install libasound2-dev

sudo apt-get install libbluetooth-dev

sudo apt-get install glib2.0

sudo apt-get install libdbus-1-dev libudev-dev libical-dev libreadline-dev

sudo apt-get install libsbc1 libsbc-dev

sudo apt-get install git


git clone https://github.com/Arkq/bluez-alsa.git

cd bluez-alsa/

autoreconf --install

mkdir build && cd build

../configure --enable-aac --disable-hcitop --with-alsaplugindir=/usr/lib/arm-linux-gnueabihf/alsa-lib/

make && sudo make install