基于USB麦克风的娱乐功能

基于4麦克风阵列(ReSpeaker 4-Mic Array)的娱乐功能包括 点歌,讲故事,相声,评书,天气预报和股市行情等 是淘宝上买的 套件,人家已经完全布好 并给了镜像文件,直接烧录到SD卡即可树莓派3B+上使用。上面附有说明书,运行程序前需要先进入虚拟环境,否则无法正常运行。
使用前,先授权,有效期只有一个月,运行 dueros-auth 获取百度的授权。 授权的文件保存在/home/pi/.avs.json。
但是麦克风阵列(ReSpeaker 4-Mic Array)影响我使用树莓派的其它引脚,所以打算改成基于USB麦克风的,亚马逊官网查了一下,人家本来就是针对USB麦克风的,我这行为类似于历史倒退,但适合我的需求,我喜欢,那就折腾吧! 首先 ,汉语搜不到任何资料,自己将文件夹内的.py文件 翻了个遍,也没有改成,老是提示 arecord录音错误,找不到录音设备(虽然安了pyaudio,看来没有使用,倍感清醒),感觉不是我理解的问题,应该解决硬件问题,单句执行
arecord -d 3 -c 2 -r 44100 -f S16_LE xiaoben.wmv
发现果真不能用(见我的“树莓派(USB麦克风和麦克风阵列) 录音和播放”https://blog.csdn.net/weixin_44345862/article/details/101355529)。问题出现在官网所说的,代码所用的录音设备是 ‘default’ ,这里不指明设备,也默认用的default设备,可usb 麦克风(这次仅仅只有此麦克风,没有麦克风阵列的)虽然也是hw:1,0但不default,如果用的是麦克风阵列(包括仅有此设备的时候),由于它装了驱动,是default设备,所以“树莓派(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
经过测试没有问题了,然后再运行我改写的代码,成功。可是遗憾的是 没有了类似麦克风阵列的灯的指示功能,有没有识别到我的唤醒声音,心里没底,所以又加了此功能,经测试 一切OK!
当我说唤醒词的时候 ,11和12引脚的指示灯会亮2s然后熄灭,效果跟麦克风阵列的一样啦!哈哈。代码如下:

#coding=UTF-8
“”"
Hands-free Voice Assistant with Snowboy and Alexa Voice Service. The wake-up keyword is “alexa”
Requirement:
pip install webrtc-audio-processing
pip install avs
“”"
import time
import logging
from voice_engine.source import Source
from voice_engine.kws import KWS
from voice_engine.ns import NS
from avs.alexa import Alexa
import RPi.GPIO as GPIO
#1 BOARD编号方式,基于插座引脚编号
GPIO.setmode(GPIO.BOARD)
#1 输出模式
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.output(11, GPIO.LOW)
GPIO.output(12, GPIO.LOW)

def main():
logging.basicConfig(level=logging.DEBUG)

src = Source(rate=16000)
ns = NS(rate=16000, channels=1)
kws = KWS(model='Hey_yahboom.pmdl')
alexa = Alexa()

src.link(ns)
ns.link(kws)
kws.link(alexa)

def on_detected(keyword):
    print('found {}'.format(keyword))
    GPIO.output(11, GPIO.HIGH)
    GPIO.output(12, GPIO.HIGH)
    time.sleep(2)
    GPIO.output(11, GPIO.LOW)
    GPIO.output(12, GPIO.LOW)
    alexa.listen()

kws.set_callback(on_detected)

src.recursive_start()

while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        kws.stop()
        src.stop()
        GPIO.cleanup()
        break

src.recursive_stop()

if name == ‘main’:
main()
在这里插入图片描述
文件位置如下:
在这里插入图片描述
运行程序前需要先进入虚拟环境,否则无法正常运行。
进入虚拟环境: source ~/env/bin/activate
退出虚拟环境:deactivate

在终端运行 alexa-auth ,然后登陆获取alexa的授权, 或者运行 dueros-auth 获取百度的授权(运行这个即可)。 授权的文件保存在/home/pi/.avs.json。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值