チートシート (ラズパイ、ALSA、Bluetooth) 


# はじめに

ラズパイ上でインターネットラジオを作成した経験をもとに、使用したコマンド (ALSA、Bluetooth、Unix) をまとめました。

# ALSA（Advanced Linux Sound Architecture）　
### 出力
aplay /usr/share/sounds/alsa/Front_Center.wav
aplay -v  /usr/share/sounds/alsa/Front_Center.wav
aplay -D plughw:1,0 /usr/share/sounds/alsa/Front_Left.wav
aplay -D hw:1,0 /usr/share/sounds/alsa/Front_Center.wav

### Bluetooth出力
aplay -D bluealsa:HCI=hci0,DEV=6C:56:97:90:15:1F,PROFILE=a2dp /usr/share/sounds/alsa/Front_Center.wav
aplay -D bluealsa /usr/share/sounds/alsa/Front_Center.wav

### リスト
aplay -l  (List all soundcards and digital audio devices)
aplay -L  (List all PCMs defined)

### サウンドloopBack
sudo modprobe -r --first-time snd-aloop || true
aplay -l 
sudo modprobe snd-aloop pcm_substreams=2
aplay -l 

sudo -u pi alsaloop -C dsnooped -P plughw:1,0 -c 1 -t 500000 &
sudo -u pi alsaloop -C dsnooped -P bluealsa -c 1 -t 500000 &
sudo -u pi alsaloop -C dsnooped -P bluealsa:HCI=hci0,DEV=C9:72:03:77:06:71,PROFILE=a2dp -c 1 -t 500000 &

sudo -u pi  alsaloop -C dsnooped -P bluealsa -c 1 -t 50000　（MicroSecond、underrun for playback bluealsa）
alsaloop -C hubcap -P hw:0 -v -v -U 　(デバッグ)

# Bluetooth
### ペアリング、接続、状況確認
$ sudo /usr/bin/bluetoothctl
info
show
scan on/off
pair 
trust 
connect 
remove
### デバイス確認
```
$ amixer -D bluealsa
Simple mixer control 'Echo Dot-BF4 - A2DP',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 127
  Mono:
  Front Left: Playback 127 [100%] [on]
  Front Right: Playback 127 [100%] [on]
```
### 再起動　　sudo service bluetooth restart
# vlc　　/usr/bin/cvlc --aout=alsa --alsa-audio-device=bluealsa 


# PulseAudio の自動起動を中止する
/etc/pulse/client.conf　を１行修正する
autospawn = no


# インストール
sudo apt-get install bluez pulseaudio-module-bluetooth python-gobject python-gobject-2
sudo apt install bluealsa

# Unix

### プロセス監視、kill
top
ps -ef | grep alsaloop | grep -v grep | awk '{ print $2}'
ps -ef | grep alsaloop | grep -v grep | awk '{ print $2}' | sudo xargs --no-run-if-empty kill -9
ps -ef | grep -E "pulse|vlc|node|loop|alsa|startx|X|light|crom|python"
### CPU、Memory、Net、Paging、Dsk、Interrupts監視　 
dstat -cmngdi

### Log監視
tail -f /var/log/syslog
dmesg -T --follow
### サーバ起動
sudo -u pi /usr/bin/tightvncserver &
export HOME=/root ; startx
sudo service lightdm stop/start
sudo -u pi /usr/bin/node-red &

### ロガー
logger msg
| logger  

## その他
sleep 1
sudo -u pi
particular_script || true
set -o pipefail

# 参考URL
https://www.alsa-project.org/wiki/Asoundrc
https://wiki.archlinux.org/index.php/Advanced_Linux_Sound_Architecture

Raspberry PiでBluetoothスピーカーを鳴らす
https://qiita.com/homelan/items/e82b919638d3c458d2ec

Bluez5/bluez-alsa
https://wiki.archlinux.jp/index.php/Bluetooth_%E3%83%98%E3%83%83%E3%83%89%E3%82%BB%E3%83%83%E3%83%88#Bluez5.2Fbluez-alsa

Raspberry Pi で Bluetooth レシーバー (オーディオ:A2DP) を作成するための設定方法解説
https://qiita.com/dwarfJP/items/a4a90d5c0aa2833dac5b

Bluetooth audio streaming
https://www.raspberrypi.org/forums/viewtopic.php?t=68779

Raspberry Pi をA2DPのsinkにして携帯やタブレットから音楽を再生する
http://d.hatena.ne.jp/penkoba/20130909/1378744109

# 付録

### 設定ファイル　　　
```~/.asoundrc　(/etc/.asoundrc)
defaults.bluealsa {
    interface "hci0"
    device "6C:56:97:90:15:1F"
    profile "a2dp"
    delay 500000
    channels 2
}

# dsnooped input
# dsnooped allows multiple applications to use an input device (microphone)
# without dsnoop, devices are exclusive, and the mic can only be used by one app at a time.
pcm.dsnooped {
    type dsnoop
    ipc_key 5978293     # must be unique for all dmix plugins!!!!
    ipc_key_add_uid yes
    slave {
            pcm {
                format S16_LE
                rate 48000
                nonblock true

                type hw
                card Loopback
                device 1
                subdevice 0
            }
        channels 2
        period_size 1024
        buffer_size 8192
        periods 0
        period_time 0
    }
}

pcm.dmixed {
        type dmix
        ipc_key_add_uid true
        ipc_key 5678293
        ipc_perm 0660
        ipc_gid audio
        slave {
            channels 2
            pcm {
                format S16_LE
                rate 48000
                nonblock true

                type hw
                card Loopback
                device 0
                subdevice 0
            }
            period_size 1024
            buffer_size 8192
        }
}

# this 'combines' both input and optput devices into a single pseudo-device
pcm.asymd {
    type asym
    playback.pcm "dmixed"
    capture.pcm "dsnooped"
}

# this sets the above combined device as the default input/output
pcm.!default {
    type plug
    slave.pcm "asymd"
}
```