文章改编自http://blog.sina.com.cn/s/blog_55c3a5ae0102wqku.html
在没有连接USB声卡时,/proc/asound/cards显示的只有树莓派缺省音频设备bcm2835 ALSA,连接USB声卡后,可以看到USB音频设备USB Audio Device。ALSA是Advanced Linux Sound Architecture的缩写,现在的版本的Linux通常都提供对ALSA的支持。
pi@raspberrypi:~ $ cat /proc/asound/cards 0 [ALSA ]: bcm2835 - bcm2835 ALSA bcm2835 ALSA 1 [Device ]: USB-Audio - USB Audio Device C-Media Electronics Inc. USB Audio Device at usb-3f980000.usb-1.5, full speed
ALSA相关的主要命令有aplay、arecord、amixer、alsamixer、alsaloop、alsactl、speaker-test等等,可以用man查看详细的说明,简单使用介绍如下。
aplay -l或arecord -l可以看到树莓派包括USB声卡在内有多个播放设备,但只有USB声卡有采集录制设备。
pi@raspberrypi:~ $ aplay -l **** List of PLAYBACK Hardware Devices **** card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA] 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 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI] Subdevices: 1/1 Subdevice #0: subdevice #0 card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0
pi@raspberrypi:~ $ arecord -l **** List of CAPTURE Hardware Devices **** card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0
aplay -L或arecord -L命令可以列出设备名称,用户可以选择相应的设备播放或采集录制音频。
pi@raspberrypi:~ $ aplay -L null Discard all samples (playback) or generate zero samples (capture) default:CARD=ALSA bcm2835 ALSA, bcm2835 ALSA Default Audio Device sysdefault:CARD=ALSA bcm2835 ALSA, bcm2835 ALSA Default Audio Device dmix:CARD=ALSA,DEV=0 bcm2835 ALSA, bcm2835 ALSA Direct sample mixing device dmix:CARD=ALSA,DEV=1 bcm2835 ALSA, bcm2835 IEC958/HDMI Direct sample mixing device dsnoop:CARD=ALSA,DEV=0 bcm2835 ALSA, bcm2835 ALSA Direct sample snooping device dsnoop:CARD=ALSA,DEV=1 bcm2835 ALSA, bcm2835 IEC958/HDMI Direct sample snooping device hw:CARD=ALSA,DEV=0 bcm2835 ALSA, bcm2835 ALSA Direct hardware device without any conversions hw:CARD=ALSA,DEV=1 bcm2835 ALSA, bcm2835 IEC958/HDMI Direct hardware device without any conversions plughw:CARD=ALSA,DEV=0 bcm2835 ALSA, bcm2835 ALSA Hardware device with all software conversions plughw:CARD=ALSA,DEV=1 bcm2835 ALSA, bcm2835 IEC958/HDMI Hardware device with all software conversions default:CARD=Device USB Audio Device, USB Audio Default Audio Device sysdefault:CARD=Device USB Audio Device, USB Audio Default Audio Device front:CARD=Device,DEV=0 USB Audio Device, USB Audio Front speakers surround21:CARD=Device,DEV=0 USB Audio Device, USB Audio 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=Device,DEV=0 USB Audio Device, USB Audio 4.0 Surround output to Front and Rear speakers surround41:CARD=Device,DEV=0 USB Audio Device, USB Audio 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=Device,DEV=0 USB Audio Device, USB Audio 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=Device,DEV=0 USB Audio Device, USB Audio 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=Device,DEV=0 USB Audio Device, USB Audio 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers iec958:CARD=Device,DEV=0 USB Audio Device, USB Audio IEC958 (S/PDIF) Digital Audio Output dmix:CARD=Device,DEV=0 USB Audio Device, USB Audio Direct sample mixing device dsnoop:CARD=Device,DEV=0 USB Audio Device, USB Audio Direct sample snooping device hw:CARD=Device,DEV=0 USB Audio Device, USB Audio Direct hardware device without any conversions plughw:CARD=Device,DEV=0 USB Audio Device, USB Audio Hardware device with all software conversions
可以使用speaker-test命令测试USB声卡,例如speaker-test -Dplughw:CARD=Device -c2 -twav,其中-D选项按设备名称plughw:CARD=Device选定USB声卡,-c2是左右双声道,-twav选定播放.wav声音文件,如果没有指定.wav声音文件名,则播放缺省的声音,在本例中会有左右声道交替出现的英语声音"Front Left"和"Front Right"。
pi@raspberrypi:~ $ speaker-test -Dplughw:CARD=Device -c2 -twav speaker-test 1.0.28 Playback device is plughw:CARD=Device Stream parameters are 48000Hz, S16_LE, 2 channels WAV file(s) 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 Time per period = 5.539493 0 - Front Left 1 - Front Right
可以使用arecord命令录制音频,然后使用aplay命令回放,-D选项按名称选定设备,-f选项设定音频格式,-c选项是声道数,-t选项是音频文件格式,详细选项设置内容可使用man查看。
pi@raspberrypi:~ $ arecord -Dplughw:CARD=Device -fcd -c2 -twav test.wav Recording WAVE 'test.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
pi@raspberrypi:~ $ aplay -Dplughw:CARD=Device test.wav Playing WAVE 'test.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
可以使用alsaloop命令将所采集音频回放,-C选项是采集设备,-P选项是回放设备,这一命令支持跨声卡的音频采集回放。
#使用USB声卡采集并回放 pi@raspberrypi:~ $ alsaloop -Cplughw:CARD=Device -Pplughw:CARD=Device #使用USB声卡采集,树莓派缺省的集成音频设备回放 pi@raspberrypi:~ $ alsaloop -Cplughw:CARD=Device -Pplughw:CARD=ALSA,DEV=0
amixer和alsamixer是关于音量控制相关的命令,将另文介绍。更为详尽的内容可以参见www.linux-sound.org和alsa.opensrc.org
设置USB声卡为默认声卡。
#创建配置文件 sudo nano /etc/asound.conf #设置声卡1为默认,则写入以下内容 defaults.ctl.card 1 defaults.pcm.card 1 defaults.timer.card 1