1. 用到的命令行工具
- PulseAudio 服务工具集(pacat, parec, paplay 等)
- Alsa 驱动工具集(arecord, aplay)
- sox 音频处理工具
2. 任务:同时录制回放和麦克风
脚本如下:
#!/bin/sh
filename=$1 # 脚本参数:保存的音频文件名
monitor=$(pacmd list | grep -e 'name:.*.monitor' | sed -e 's/\s*name: <//' | sed -e 's/>//')
# 麦克风
arecord -f S16_LE -c 2 -r 44100 $filename.1.wav &
ARECORD_PID=$!
# 回放
parec -d $monitor | sox -t raw -r 44100 -e signed-integer -L -b 16 -c 2 - $filename.2.wav &
PAREC_PID=$!
echo "recording... (press CTRL+C to end recording)"
BEGIN_TIME=$(date +%s)
END_RECORD=0
# 混音
trap "echo 'mixing audio'; kill $ARECORD_PID; kill $PAREC_PID; sleep 1; sox -m $filename.1.wav $filename.2.wav -c 1 $filename.wav; exit" 2
while [ $END_RECORD -le 1 ]
do
COST_TIME=`expr $(date +%s) - $BEGIN_TIME`
printf "$COST_TIME\r"
sleep 1
done