虚谷号

 找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 461|回复: 2

当Yeelight遇上虚谷号#14 声控灯(pyaudio库)

[复制链接]

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
发表于 2019-3-20 10:25:52 | 显示全部楼层 |阅读模式
本帖最后由 linmiaoyan 于 2019-4-4 10:48 编辑

由于pyaudio库在虚谷号ubuntu系统中的应用情况较差,需要等到在固件中添加该库后运行
运行时发生的一个典型问题是
>>>导入pyaudio
>>> p = pyaudio.PyAudio()
ALSA lib pcm.c:2267:(snd_pcm_open_noupdate)未知的PCM卡.pcm.rear
ALSA lib pcm.c:2267:(snd_pcm_open_noupdate)未知的PCM卡.pcm.center_lfe
ALSA lib pcm.c:2267:(snd_pcm_open_noupdate)未知的PCM卡。 pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap)找不到匹配的通道映射
ALSA lib pcm_route.c:867:(find_matching_chmap)找不到匹配的通道映射
无法连接到服务器套接字错误=没有这样的文件或目录
无法连接到服务器请求通道
插孔服务器未运行或无法启动


可以在PC上正常运行,以下安装方式都是基于windows系统

py控制项目:
安装方式①:pyaudio库(直接下载轮下过来在本地安装)
pip install wheel
http://www.lfd.uci.edu/~gohlke/pythonlibs/#在链接中找到适合当前系统的
pip install d:\ pycurl-7.43.0-cp36-cp36m-win32.whl

安装方式②:pyaudio库(如果无法通过pip正常安装)https://blog.csdn.net/qq_31163325/article/details/83144444
1.sudo apt-get安装portaudio19-dev
2.sudo apt-get install python-pyaudio python3-pyaudio
3.pip install pyaudio

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

16

主题

40

帖子

161

积分

注册会员

Rank: 2

积分
161
 楼主| 发表于 2019-3-20 10:42:38 | 显示全部楼层
本帖最后由 linmiaoyan 于 2019-3-20 10:45 编辑

from pyaudio import PyAudio,paInt16
from yeelight import Bulb
import wave
import json
import base64
import os
import requests
import time

#参数设置
bulb = Bulb("192.168.31.39")

RATE = "16000"
FORMAT = "wav"
CUID="wate_play"
DEV_PID="1536"

framerate=16000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=2

#注册获取百度API的账号密码https://ai.baidu.com/docs#/TTS-API/top
def get_token():
    server = "https://openapi.baidu.com/oauth/2.0/token?"
    grant_type = "client_credentials"
    #API Key
    client_id = "c06hUdY8BnwNqSreGnkRwmNN"
    #Secret Key
    client_secret = "5f0jpnsaE9c1z0H3yKAURcl1Xy0FHS5V"


#拼url
    url ="%sgrant_type=%s&client_id=%s&client_secret=%s"%(server,grant_type,client_id,client_secret)
    #获取token
    res = requests.post(url)
    token = json.loads(res.text)["access_token"]
    return token

#读取本地音频信息,上传至百度服务器,得到返回的文字
def get_word(token):
    with open(r'vo.wav', "rb") as f:
        speech = base64.b64encode(f.read()).decode('utf8')
    size = os.path.getsize(r'vo.wav')
    headers = { 'Content-Type' : 'application/json'}
    url = "https://vop.baidu.com/server_api"
    data={
            "format":FORMAT,
            "rate":RATE,
            "dev_pid":DEV_PID,
            "speech":speech,
            "cuid":CUID,
            "len":size,
            "channel":1,
            "token":token,
        }
    req = requests.post(url,json.dumps(data),headers)
    result = json.loads(req.text)
    ret=result["result"][0]
    print(result)
    return ret

#文件格式
def save_wave_file(filename,data):
    '''save the date to the wavfile'''
    wf=wave.open(filename,'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(b"".join(data))
    wf.close()

#录音并存储
def my_record():
    pa=PyAudio()
    stream=pa.open(format = paInt16,channels=1,
                   rate=framerate,input=True,
                   frames_per_buffer=NUM_SAMPLES)
    my_buf=[]
    count=0
    print('.')
    # 控制录音时间
    while count<TIME*10:
        string_audio_data = stream.read(NUM_SAMPLES)
        my_buf.append(string_audio_data)
        count+=1

    save_wave_file('vo.wav',my_buf)
    stream.close()

#声音的播放
chunk=2014
def play():
    wf=wave.open(r"vo.wav",'rb')
    p=PyAudio()
    stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
    wf.getnchannels(),rate=wf.getframerate(),output=True)
    while True:
        data=wf.readframes(chunk)
        if data=="":break
        stream.write(data)
    stream.close()
    p.terminate()

mesglist=['开','关','加亮','高亮','少亮','低亮']
#判断语句
def judge(ret):
    if mesglist[0] in ret:
        bulb.turn_on()
    if mesglist[1] in ret:
        bulb.turn_off()
    if mesglist[2] in ret:
        bulb.set_brightness(80)
    if mesglist[3] in ret:
        bulb.set_brightness(80)
    if mesglist[4] in ret:
        bulb.set_brightness(20)
    if mesglist[5] in ret:
        bulb.set_brightness(20)

if __name__ == '__main__':
    while True:
        time.sleep(0.5)
        my_record()
        token=get_token()
        try:
            ret = get_word(token)
            print(ret)
            judge(ret)
        except:
            print('失败')

回复

使用道具 举报

3

主题

4

帖子

462

积分

中级会员

Rank: 3Rank: 3

积分
462
发表于 2019-3-26 11:08:49 | 显示全部楼层
声卡切换问题,我们这边研究下,感谢反馈
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

虚谷号

GMT+8, 2024-3-28 18:03 , Processed in 0.056309 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表