树莓派(USB麦克风和麦克风阵列) 录音和播放

因pyaudio录音有各种问题,所以尽量不用它。用arecord。树莓派只有音频输出口,没有录入口,需要免驱动的USB麦克风或者麦克风阵列模块(raspeaker),我用的是后者。
arecord –l
通过如上命令可以得到可用于录音的设备,比如card x device x
arecord -Dhw:1,0 -d 10 -f cd -r 44100 -c 2 -t wav test.wav
参数解析
-D 指定了录音设备,0,1 是card 0 device 1的意思,也就是TDM_Capture
-d 指定录音的时长,单位时秒
-f 指定录音格式,通过上面的信息知道只支持 cd cdr dat
-r 指定了采样率,单位时Hz
-c 指定channel 个数
-t 指定生成的文件格式
————————————————
原文链接:https://blog.csdn.net/xiongtiancheng/article/details/80577478

由于训练语料均采用16khz、16bit的单通道WAV格式的语音文件所以以下参数经常用,
使用arecord录制16khz、16bit语音,测试是否在pi目录下有语料生成
arecord -D “plughw:1,0” -f S16_LE -d 2 -r 16000 /home/pi/test.wav
测试播放语音功能也可以使用omxplayer:
omxplayer -o local /home/pi/test.wav

以下是个人实践成果(麦克风阵列):

arecord -d 3 -c 2 -r 44100 -f S16_LE xiaoben.wmv
录音3s,自动退出进程并在当前目录下生成xiaoben.wmv文件,然后可以放了:
aplay xiaoben.wmv

如果是USB麦克风的话,需要改一下硬件配置,才能用上句。

To configure the microphone and speaker

Create and open the ~/.asoundrc file by typing the following command in the terminal: sudo nano ~/.asoundrc.
Add these lines to the file and save it (CTRL + O).
pcm.!default {
type asym
playback.pcm {
type plug
slave.pcm “hw:0,0”
}
capture.pcm {
type plug
slave.pcm “hw:1,0”
}
}
To ensure the microphone is capturing audio data, run the following command.

sudo apt-get install sox -y && rec test.wav
This command might install some extra audio dependencies. After it finishes, you should see a message indicating that audio is recording. It’s working if you see a running minute counter that is incrementing by the second. It looks similar to the following command.

In: 0.00% 00:00:01 [00:00:00:00] out:0.00M [ | ] clip:0

To exit the sound test, type CTRL + C
树莓派(USB麦克风和麦克风阵列) 录音和播放_第1张图片

你可能感兴趣的:(树莓派)