最近,自己尝试做一个智能车载系统时。需要用到语音唤醒功能,于是就用到了科大讯飞的语音唤醒。这是地址:控制台-讯飞开放平台 (xfyun.cn)
填好唤醒词后下载对应的SDK,我下载的是Windows的
这是下载下来的SDK文件目录:
我们直接将bin目录复制粘贴到你的项目中即可进行使用(我之前将这个SDK放没有放到我的项目中,结果一直保错:| INFO | __main__:ivw_wakeup:54 - QIVWSessionBegin failed, error code is: 10102)
语音唤醒代码示例:
import os
from ctypes import cdll, byref, string_at, c_void_p, CFUNCTYPE, c_char_p, c_uint64, c_int64
import pyaudio
from loguru import logger
# 唤醒成功后打印的日志
def py_ivw_callback(sessionID, msg, param1, param2, info, userDate):
# typedef int( *ivw_ntf_handler)( const char *sessionID, int msg, int param1, int param2, const void *info, void *userData );
# 在此处编辑唤醒后的动作
print("sessionID =>", sessionID)
print("msg =>", msg)
print("param1 =>", param1)
print("param2 =>", param2)
print("info =>", info)
print("userDate =>", userDate)
CALLBACKFUNC = CFUNCTYPE(None, c_char_p, c_uint64,
c_uint64, c_uint64, c_void_p, c_void_p)
pCallbackFunc = CALLBACKFUNC(py_ivw_callback)
def ivw_wakeup():
try:
msc_load_library = r'.\bin\msc_x64.dll'
app_id = '5277f5fd' # 填写自己的app_id
ivw_threshold = '0:1450'
jet_path = os.getcwd() + r'.\bin\msc\res\ivw\wakeupresource.jet'
work_dir = 'fo|' + jet_path
except Exception as e:
return e
# ret 成功码
MSP_SUCCESS = 0
dll = cdll.LoadLibrary(msc_load_library)
errorCode = c_int64()
sessionID = c_void_p()
# MSPLogin
Login_params = "appid={},engine_start=ivw".format(app_id)
Login_params = bytes(Login_params, encoding="utf8")
ret = dll.MSPLogin(None, None, Login_params)
if MSP_SUCCESS != ret:
logger.info("MSPLogin failed, error code is: %d", ret)
return
# QIVWSessionBegin
Begin_params = "sst=wakeup,ivw_threshold={},ivw_res_path={}".format(
ivw_threshold, work_dir)
Begin_params = bytes(Begin_params, encoding="utf8")
dll.QIVWSessionBegin.restype = c_char_p
sessionID = dll.QIVWSessionBegin(None, Begin_params, byref(errorCode))
if MSP_SUCCESS != errorCode.value:
logger.info("QIVWSessionBegin failed, error code is: {}".format(
errorCode.value))
return
# QIVWRegisterNotify
dll.QIVWRegisterNotify.argtypes = [c_char_p, c_void_p, c_void_p]
ret = dll.QIVWRegisterNotify(sessionID, pCallbackFunc, None)
if MSP_SUCCESS != ret:
logger.info("QIVWRegisterNotify failed, error code is: {}".format(ret))
return
# QIVWAudioWrite
# 创建PyAudio对象
pa = pyaudio.PyAudio()
# 设置音频参数
sample_rate = 16000
chunk_size = 1024
format = pyaudio.paInt16
channels = 1
# 打开音频流
stream = pa.open(format=format,
channels=channels,
rate=sample_rate,
input=True,
frames_per_buffer=chunk_size)
# 开始录制音频
logger.info("* start recording")
ret = MSP_SUCCESS
while ret == MSP_SUCCESS: # 这里会一直进行监听你的唤醒,只要监听到你的唤醒就调用上面的py_ivw_callback函数打印日志
audio_data = stream.read(chunk_size)
audio_len = len(audio_data)
ret = dll.QIVWAudioWrite(sessionID, audio_data, audio_len, 2)
logger.info('QIVWAudioWrite ret =>{}', ret)
logger.info("* done recording")
# 关闭音频流
stream.stop_stream()
stream.close()
# 终止PyAudio对象
pa.terminate()
if __name__ == '__main__':
ivw_wakeup()
我们需要将我们之前复制到项目中的bin目录下的msc_x64.dll和wakeupresource.jet文件路径放到指定位置,其他的位置基本不用改
运行起来后,当我们喊出唤醒词后就会打印信息
参考up:
http://t.csdn.cn/FS9zohttp://t.csdn.cn/FS9zo官方文档: