注意:有个重要的问题,树莓派板载中没有MIC输入接口,需要外加USB声卡实现录音功能。而且要有耐心,声卡顺序等配置不当事,使用PyAudio稍有不当就会引起内核崩溃。耐心调整。
安装树莓派到tf卡
下载最新版本的raspbian,它有两个版本,如果你不需要桌面环境,可以下载lite版本,下载地址为:https://www.raspberrypi.org/downloads/ 下载完后是个zip包,请解压缩。
Mac电脑里请使用以下步骤:
- 插入tf读卡器并插入要写入镜像的tf卡,然后umount它,为写入准备
$ diskutil list $ df $ diskutil umount /dev/disk3s1
- 把解压后的img后缀的镜像写入tf卡里,完成后弹出tf卡,并取下插入树莓派
sudo dd bs=1m if=2017-11-29-raspbian-stretch-lite.img of=/dev/disk3 conv=sync sudo diskutil eject /dev/disk3
树莓派默认安装后没有开启ssh,请连接鼠标、键盘、显示器登录本机开启ssh服务,默认的用户名为:pi 密码为: raspberry,登录后使用raspi-config开启ssh服务;
sudo raspi-config
更新系统并安装一些必要的工具
sudo apt-get update sudo apt-get -y upgrade sudo apt-get install -y git sudo apt-get install -y python-pip
安装PyAudio
二进制安装
sudo apt-get install -y python-pyaudio
从源码安装
sudo apt-get install -y libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev sudo apt-get install -y python-dev git clone http://people.csail.mit.edu/hubert/git/pyaudio.git cd pyaudio sudo python setup.py install
测试PyAudio
查看系统中的声卡顺序,在录音时要使用这里使用list.py获取设备信息,执行时可能会返回一些错误,不影响使用。
#!/usr/bin/env python # -*- coding:utf-8 -*- import pyaudio po = pyaudio.PyAudio() for index in range(po.get_device_count()): desc = po.get_device_info_by_index(index) #print desc print "index:%-5d maxInputChannels: %-5d device: %-40s rate: %-10d" % (index, desc["maxInputChannels"], desc["name"], int(desc["defaultSampleRate"]))
python list.py
开始录音测试,使用record.py录音5秒,程序中使用的input_device_index=0要跟录音设备一致,否则可能引起系统内核崩溃,这个顺序是声卡的顺序,不受默认声卡配置影响,但是默认声卡会影响错误信息的输出。
# -*- coding:utf-8 -*- import os import pyaudio import wave import sys CHUNK = 512 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, input_device_index=2, frames_per_buffer=CHUNK) print("* recording") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("* done recording") stream.stop_stream() stream.close() p.terminate() wf_path = os.path.join(sys.path[0], WAVE_OUTPUT_FILENAME) wf = wave.open(wf_path, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() print(wf_path)
python record.py
完成录音后,进行播放时需要播放软件,可以使用sox工具
sudo apt-get install -y sox
安装后play命令可用
play output.wav
估计你play时会遇到问题,USB默认顺序有可能不在0,而是1或者其它顺序,需要查看/proc/asound/cards确认回放设备的顺序
pi@raspberrypi:~ $ cat /proc/asound/cards 0 [ALSA ]: bcm2835 - bcm2835 ALSA bcm2835 ALSA 1 [Audio ]: USB-Audio - USB Audio Generic USB Audio at usb-3f980000.usb-1.4, high speed
然后在播放是使用参数指定
AUDIODEV=hw:1,0 play output.wav
调试声卡
还有好多调整声卡顺序、设置默认设备顺序的方式可以尝试一下;不过有可能出现各种各样的问题,甚至内核崩溃,这些之前都遇到过。
查看声卡信息
查看声卡的顺序
pi@raspberrypi:~ $ cat /proc/asound/cards 0 [ALSA ]: bcm2835 - bcm2835 ALSA bcm2835 ALSA 1 [Audio ]: USB-Audio - USB Audio Generic USB Audio at usb-3f980000.usb-1.4, high speed
查看回放设备
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: Audio [USB Audio], device 0: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0
查看录音设备
pi@raspberrypi:~ $ arecord -l **** List of CAPTURE Hardware Devices **** card 1: Audio [USB Audio], 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=Audio USB Audio, USB Audio Default Audio Device sysdefault:CARD=Audio USB Audio, USB Audio Default Audio Device front:CARD=Audio,DEV=0 USB Audio, USB Audio Front speakers surround21:CARD=Audio,DEV=0 USB Audio, USB Audio 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=Audio,DEV=0 USB Audio, USB Audio 4.0 Surround output to Front and Rear speakers surround41:CARD=Audio,DEV=0 USB Audio, USB Audio 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=Audio,DEV=0 USB Audio, USB Audio 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=Audio,DEV=0 USB Audio, USB Audio 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=Audio,DEV=0 USB Audio, USB Audio 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers iec958:CARD=Audio,DEV=0 USB Audio, USB Audio IEC958 (S/PDIF) Digital Audio Output dmix:CARD=Audio,DEV=0 USB Audio, USB Audio Direct sample mixing device dsnoop:CARD=Audio,DEV=0 USB Audio, USB Audio Direct sample snooping device hw:CARD=Audio,DEV=0 USB Audio, USB Audio Direct hardware device without any conversions plughw:CARD=Audio,DEV=0 USB Audio, USB Audio Hardware device with all software conversions
采集并回放
pi@raspberrypi:~ $ alsaloop -Cplughw:CARD=Audio -Pplughw:CARD=Audio
录音
arecord -D "plughw:1,0" -f S16_LE -r 8000 -d 5 arecord.wav
设置默认声卡
$ sudo nano /etc/asound.conf
内容如下:
defaults.ctl.card 1 defaults.pcm.card 1 defaults.timer.card 1
重启生效,可对sox的play、arecord、aplay等程序生效,对于alsaloop不起作用。
可使用alsamixer查看默认声卡。
注意:上面的方式有时会引起内核崩溃,采用如下方式目前测试还未发现内核崩溃问题
nano ~/.asoundrc
内容如下,修改后重新运行程序,立即生效
pcm.!default{ type asym playback.pcm{ type plug slave.pcm "hw:1,0" } capture.pcm{ type plug slave.pcm "hw:1,0" } }
调整USB声卡顺序
调整USB声卡顺序到0,新建立一个文件/lib/modprobe.d/sagelit.conf ,内容如下
# Load USB audio before the internal soundcard options snd_usb_audio index=0 options snd_bcm2835 index=1 # Make sure the sound cards are ordered the correct way in ALSA options snd slots=snd_usb_audio,snd_bcm2835