Raspberry PiでUSBマイクを取り付けて音声を録音させる

This text is also text!

Preparation


usb マイクを差し入れる前後に lsusbコマンドで出力を確認します。

1
2
3
4
5
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 08bb:2902 Texas Instruments PCM2902 Audio Codec
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

以下arecord -lコマンドを実行して確認します。

ALSAからの認識を確認

Linuxのオーディオドライバ「ALSA」から見たUSBマイクの認識

1
2
3
4
5
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

「card:1、device:0」としてと認識されていることがわかります。

ハードウェア・モジュールの認識確認

1
2
3
4
5
6
$ cat /proc/asound/modules
0 snd_bcm2835
1 snd_soc_rpi_simple_soundcard
2 snd_usb_audio
3 vc4
4 vc4

snd_usb_audioの優先順位を一番に上げます。

1
2
3
4
$sudo vi /etc/modprobe.d/alsa-base.conf
options snd slots=snd_usb_audio,snd_bcm2835
options snd_usb_audio index=0
options snd_bcm2835 index=1

その結果です

1
2
3
4
5
6
$ cat /proc/asound/modules
0 snd_usb_audio
1 snd_bcm2835
2 snd_soc_rpi_simple_soundcard
3 vc4
4 vc4

「aplay -L」コマンドを使用すると、ALSAで定義されているすべての PCM(サウンドデバイス) を一覧表示します

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 1: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 2: sndrpihifiberry [snd_rpi_hifiberry_dac], device 0: HifiBerry DAC HiFi pcm5102a-hifi-0 [HifiBerry DAC HiFi pcm5102a-hifi-0]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 3: vc4hdmi0 [vc4-hdmi-0], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 4: vc4hdmi1 [vc4-hdmi-1], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ nano ~/.asoundrc
pcm.!default { # サウンドデバイスのデフォルトを設定
type asym # Asym PCM
playback { # Playback slave definition
pcm {
type hw
card Headphones # [aplay -L]コマンドで確認したデバイスの名前
device 0 # [aplay -L]コマンドで確認したデバイスの番号
}
}
capture { # Capture slave definition
pcm {
type hw
card Device # [arecord -L]コマンドで確認したデバイスの名前
device 0 # [arecord -L]コマンドで確認したデバイスの番号
}
}
}

Recording


以下コマンドで録音します。

1
arecord test.wav

ノイズだらけです。。

【arecordのオプションパラメータ】
-c 1:チャネル数を指定します。(モノラル:1)
-d 5:録音時間を指定します。(5秒)
-f S16_LE:サンプリングのフォーマットを指定します。(S16_LE)
-r 44100:サンプリングレートを指定します。(44100Hz)
-t wav:ファイルフォーマットのタイプを指定します。(wav形式)

1
arecord -c 1 -d 10 -f S16_LE -r 44100 -t wav record.wav

割ときれいな音が録音できます。

Test

録音ファイルを再生して、音声を確認します。

1
aplay test.wav

音声ボリューム

[F4キー]を押して録音の設定画面に切り替えます。

1
alsamixer

Raspberry PiでBluetoothスピーカーを鳴らす

This text is also text!

Preparation


Amazon.co.jpで買ったEWA A106というスピーカーです

PulseAudioとPulseAudioのBluetoothスピーカーのモジュールをインストールします。

1
sudo apt-get install pulseaudio pulseaudio-module-bluetooth

Setting

PulseAudioの設定


PulseAudioを設定するため、/etc/pulse/default.paを編集します

1
sudo nano /etc/pulse/default.pa

ファイルの最後に次の行を追加します。

1
2
# automatically switch to newly-connected devices
load-module module-switch-on-connect

bluetooth権限追加

sudoなしでBluetoothが制御できるため、ユーザーをbluetoothグループに入れル必要があります。

1
2
sudo usermod -G bluetooth -a $USER
sudo reboot

## 動作
Bluetoothスピーカーをペアリングするためpulseaudioを起動します

1
pulseaudio --start

bluetoothctlコマンドを起動して以下操作します。

1
2
3
4
5
6
7
8
9
$ bluetoothctl
Agent registered
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
・・・
[NEW] Device 3D:8F:2B:2E:E4:07 EWA Audio A106
・・・

ペアリング

pairコマンドでペアリングします

1
2
[bluetooth]# pair 3D:8F:2B:2E:E4:07
Attempting to pair with 3D:8F:2B:2E:E4:07

Pairing successfulと出れば成功です。
ついでにtrustコマンドでTrustリストに登録します

1
2
3
[bluetooth]# trust 3D:8F:2B:2E:E4:07
[CHG] Device 3D:8F:2B:2E:E4:07 Trusted: yes
Changing 3D:8F:2B:2E:E4:07 trust succeeded

connectコマンドでスピーカーに接続します。

1
2
3
4
5
6
[bluetooth]# connect 3D:8F:2B:2E:E4:07
Attempting to connect to 3D:8F:2B:2E:E4:07
[CHG] Device 3D:8F:2B:2E:E4:07 Connected: yes
Connection successful
[CHG] Device 3D:8F:2B:2E:E4:07 ServicesResolved: yes
[EWA Audio A106]#

終了

以下のコマンドを実行してペアリングを終了します。

1
[EWA Audio A106]# quit

テスト

ファイルを再生して音を鳴らして確認しましょう。

1
$ aplay /usr/share/sounds/alsa/Front_Center.wav

音量調整

alsamixer コマンドで調整できます。
上下キーで音量を調整でき、F6 キーで出力先(ヘッドホンジャック、HDMI など)を変更できます。
また、M ボタンを押すとミュート/ミュート解除の切り替えができます