22
22

More than 5 years have passed since last update.

Linuxで音声の出力先をUSBスピーカーにする

Last updated at Posted at 2018-02-17

Nano Pi Neo2にスピーカーを繋いだのだが、ノイズが入ってたまらない。
そこでUSBスピーカーから音を出すようにしてみる。
USBスピーカーはこれ。USBケーブル一本でいいので重宝している。

まずalsamixerで音量を確かめておく。画像は割愛。

次。
aplayコマンドを使って、音声出力のハードウェアデバイスのcardとdevice番号を調べる。
---helpオプションで使い方を。

$ aplay --help
Usage: aplay [OPTION]... [FILE]...

-h, --help              help
    --version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
(略)

-lオプションで一覧を得る。

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Codec [H3 Audio Codec], device 0: CDC PCM Codec-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: allwinnerhdmi [allwinner,hdmi], device 0: 1c22800.i2s-i2s-hifi i2s-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: MicroII [Audio Advantage MicroII], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

USBスピーカーだから最後のものが該当。
MicroIIということがわかる。

speaker-testコマンドでテストする。
--helpで確認するのが定石

$ speaker-test --help

speaker-test 1.1.0

Usage: speaker-test [OPTION]... 
-h,--help       help
-D,--device     playback device
-r,--rate       stream rate in Hz
-c,--channels   count of channels in stream
-f,--frequency  sine wave frequency in Hz
-F,--format     sample format
-b,--buffer     ring buffer size in us
-p,--period     period size in us
-P,--nperiods   number of periods
-t,--test       pink=use pink noise, sine=use sine wave, wav=WAV file
-l,--nloops     specify number of loops to test, 0 = infinite
-s,--speaker    single speaker test. Values 1=Left, 2=right, etc
-w,--wavfile    Use the given WAV file as a test sound
-W,--wavdir     Specify the directory containing WAV files
-m,--chmap      Specify the channel map to override
-X,--force-frequency    force frequencies outside the 30-8000hz range
-S,--scale      Scale of generated test tones in percent (default=80)

Recognized sample formats are: S8 S16_LE S16_BE FLOAT_LE S32_LE S32_BE

-Dオプションで出力先を選ぶ。
出力先の指定はデバイスネームだからさっきのaplayコマンドで再チェック

$ aplay -L|grep MicroII
sysdefault:CARD=MicroII
    Audio Advantage MicroII, USB Audio
front:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround21:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround40:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround41:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround50:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround51:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
surround71:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
iec958:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
dmix:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
dsnoop:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
hw:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio
plughw:CARD=MicroII,DEV=0
    Audio Advantage MicroII, USB Audio

ということで、hw:MicroIIで試す。

$ speaker-test -Dhw:MicroII

speaker-test 1.1.0

Playback device is hw:MicroII
Stream parameters are 48000Hz, S16_LE, 1 channels
Using 16 octaves of pink noise
Channels count (1) not available for playbacks: Invalid argument
Setting of hwparams failed: Invalid argument

鳴らない。

この辺を読み、hwではなくplughwにしてみる。
http://www.volkerschatz.com/noise/alsa.html

$ speaker-test -Dplughw:MicroII

speaker-test 1.1.0

Playback device is plughw:MicroII
Stream parameters are 48000Hz, S16_LE, 1 channels
Using 16 octaves of pink noise
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 96 to 262144
Period size range from 48 to 131072
Using max buffer size 262144
Periods = 4
was set period_size = 65536
was set buffer_size = 262144
 0 - Front Left
Time per period = 2.769702
 0 - Front Left
^CTime per period = 2.426893

鳴った。
チャンネル2つにしてみると

$ speaker-test -c2 -Dplughw:MicroII

speaker-test 1.1.0

Playback device is plughw:MicroII
Stream parameters are 48000Hz, S16_LE, 2 channels
Using 16 octaves of pink noise
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 96 to 262144
Period size range from 48 to 131072
Using max buffer size 262144
Periods = 4
was set period_size = 65536
was set buffer_size = 262144
 0 - Front Left
 1 - Front Right
^CTime per period = 3.587277

左右で鳴る。
さらにaplayで試す。

$ aplay /usr/share/sounds/alsa/Front_Center.wav -D plughw:MicroII
Playing WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono

よさそう。
これでmpg123などを使えば、mp3ファイルなども鳴らすことができる。
mpg123でデバイスを指定するには-aオプションで。

mpg123 -q -a plughw:MicroII XXXX.mp3

ためしにデバイスにdefaultを指定すると文字通り標準設定のデバイスから出力を試みる。
USBスピーカーはならないはず。

$ aplay /usr/share/sounds/alsa/Front_Center.wav -D default

デフォルトの出力先を変えるには、以下の手順に沿えばよい。
https://www.alsa-project.org/main/index.php/Setting_the_default_device

22
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22