通过 aplay 指定格式来播放 单声道WAV文件的结果比较:
aplay -Dhw:0,0 -c 2 -r 48000 -f S16_LE /data/48k_mono.wav
Playing WAVE '/usr/share/sounds/alsa/48k_mono.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1305: Channels count non available
aplay -Dplughw:0,0 -c 2 -r 48000 -f S16_LE /data/48k_mono.wav 可以正常播放.
其中的区别就是 hw 和 plughw , 参考 Difference between “hwplug” and “hw” 得出结论
hw: accesses the hardware device directly. // 直接访问硬件
plughw: inserts sample rate and format conversion plugins, if needed. //经过采样率和格式转换插件
可以通过mediainfo查看文件信息, 发现为1通道格式,而Codec driver 是需要播放的2 channel的,所以需要plug转换为2 channel才可以播放.
特意找了一个2 channel / 48kHz , 验证了猜想结果. 不加plug可以正常播放
➜ alsa mediainfo Front_Left.wav
General
Complete name : 48k_mono.wav
Format : Wave
File size : 139 KiB
Duration : 1 s 480 ms
Overall bit rate mode : Constant
Overall bit rate : 768 kb/s
Audio
Format : PCM
Format settings : Little / Signed
Codec ID : 1
Duration : 1 s 480 ms
Bit rate mode : Constant
Bit rate : 768 kb/s
Channel(s) : 1 channel
Sampling rate : 48.0 kHz
Bit depth : 16 bits
Stream size : 139 KiB (100%)
上面例子是播放指定格式的wav音频文件,其实对于指定采集播放设备也是一样理解的:
使用plughw
接口,程序员不必过多关心硬件,而且如果设置的配置参数和实际硬件支持的参数不一致,ALSA 会自动转换数据。
如果使用hw
接口,我们就必须检测硬件是否支持设置的参数了。
hw
和plughw
后面的两个数字分别表示card号和device号。
[1] https://blog.51cto.com/yiluohuanghun/868048
[2] https://developer.aliyun.com/article/438059