【UE5/UE4】超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)

这篇具有很好参考价值的文章主要介绍了【UE5/UE4】超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

【UE5/UE4】超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)

先导

Windows环境下
**UE版本为UE4.27及以下
你需要具备一定的C++基础,或全程一对一对照我的代码编写你的代码
使用Offline Voice Recognition插件作为录音插件(仅做录音插件使用)
基于https://github.com/zhangmei126/XunFei二次开发
语音识别部分参考CSDNUE4如何接入科大讯飞的语音识别
在此基础上增加了语音唤醒功能,实际上语音唤醒与上述文章中是两个模块
由于讯飞插件的使用需要调用MSPLogin,也就是需要先注册
科大讯飞invalid time or time required,ue5,ue4
其插件中的SpeechInit()方法已经为我们注册好了,如果可以自己写注册的话,后述本文语音唤醒部分是不分引擎版本的

语音唤醒环境配置

参考UE4如何接入科大讯飞的语音识别接入科大讯飞sdk以及使用Offline Voice Recognition插件了后,在plugins中确保他们都是开启的状态
科大讯飞invalid time or time required,ue5,ue4
科大讯飞invalid time or time required,ue5,ue4
你要确保你的SDK下载的是Windows版本以及一下SDK文件包
科大讯飞invalid time or time required,ue5,ue4
你要确保你的SDK下载后正确导入了且appid已经拥有了正确的配置
科大讯飞invalid time or time required,ue5,ue4
你要确保你SDK下载后将Windows_awaken_exp1227_iat1226_af12f15d\bin\msc\res\ivw\wakeupresource.jet放置到了你的c盘根目录下
科大讯飞invalid time or time required,ue5,ue4
to科大讯飞invalid time or time required,ue5,ue4
如果你是为了10102错误码来此文章:
此处是讯飞的坑,讯飞的wakeupresource.jet的路径必须是绝对路径
c++中也就必须要使用转义符"\\"即
const char* params = "ivw_threshold=0:5, ivw_res_path =fo|c:\\wakeupresource.jet";
至此10102解决了,就这么简单,但是很坑
打开VisualStudio

  1. 打开讯飞插件文件夹
    科大讯飞invalid time or time required,ue5,ue4
  2. 在SpeechActor.cpp中引入qivw.h
    科大讯飞invalid time or time required,ue5,ue4
    qivw.h语音唤醒具备以下方法
    科大讯飞invalid time or time required,ue5,ue4
  3. 在SpeechActor.h中
#pragma once

#include "SpeechTask.h"

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SpeechActor.generated.h"

UCLASS()
class XUNFEI_API ASpeechActor : public AActor
{
	GENERATED_BODY()
private:
	FString Result;
	DECLARE_DYNAMIC_MULTICAST_DELEGATE(FWakeUpBufferDelegate);
public:
	// Sets default values for this actor's properties
	ASpeechActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "SpeechInit", Keywords = "Speech Recognition Initialization"))
	void SpeechInit();

	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "SpeechOpen", Keywords = "Speech Recognition Open"))
	void SpeechOpen();

	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "SpeechStop", Keywords = "Speech Recognition Stop"))
	void SpeechStop();

	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "SpeechQuit", Keywords = "Speech Recognition Quit"))
	void SpeechQuit();
	
	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "SpeechResult", Keywords = "Speech Recognition GetResult"))
	FString SpeechResult();
	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "WakeUpStart", Keywords = "Speech Recognition GetResult"))
	FString WakeUpStart();
	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "WakeUpEnd", Keywords = "Speech Recognition GetResult"))
	bool WakeUpEnd(FString SessionID);
	UFUNCTION(BlueprintCallable, Category = "XunFei", meta = (DisplayName = "WakeUpBuffer", Keywords = "Speech Recognition GetResult"))
	bool WakeUpBuffer(TArray<uint8> MyArray, FString SessionID);

	//这是一个回调函数
	UPROPERTY(BlueprintAssignable)
	 FWakeUpBufferDelegate OnWakeUpBuffer;
	
};
  1. 在SpeechActor.cpp中
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once

#include "SpeechActor.h"
#include "XunFei.h"
#include "Dom/JsonObject.h"
#include "Serialization/JsonReader.h"
#include "Serialization/JsonSerializer.h"
#include <qivw.h>

// Sets default values
ASpeechActor::ASpeechActor() :
	Result{}
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
}



// Called when the game starts or when spawned
void ASpeechActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void ASpeechActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ASpeechActor::SpeechInit()
{
	FAutoDeleteAsyncTask<FSpeechTask>* SpeechTask = new FAutoDeleteAsyncTask<FSpeechTask>();

	if (SpeechTask)
	{
		SpeechTask->StartBackgroundTask();
	}
	else
	{
		UE_LOG(XunFeiLog, Error, TEXT("XunFei task object could not be create !"));
		return;
	}

	UE_LOG(XunFeiLog, Log, TEXT("XunFei Task Stopped !"));
	return;
}

void ASpeechActor::SpeechOpen()
{
	xunfeispeech->SetStart();
	return;
}

void ASpeechActor::SpeechStop()
{
	xunfeispeech->SetStop();
	return;
}

void ASpeechActor::SpeechQuit()
{
	xunfeispeech->SetQuit();
	Sleep(300);
	return;
}

FString ASpeechActor::SpeechResult()
{	
	Result = FString(UTF8_TO_TCHAR(xunfeispeech->get_result()));

	FString LajiString("{\"sn\":2,\"ls\":true,\"bg\":0,\"ed\":0,\"ws\":[{\"bg\":0,\"cw\":[{\"sc\":0.00,\"w\":\"\"}]}]}");

	int32 LajiIndex = Result.Find(*LajiString);

	if (LajiIndex != -1)
	{
		Result.RemoveFromEnd(LajiString);
	}

	TSharedPtr<FJsonObject> JsonObject;

	TSharedRef< TJsonReader<TCHAR> > Reader = TJsonReaderFactory<TCHAR>::Create(Result);

	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
		Result.Reset();
		TArray< TSharedPtr<FJsonValue> > TempArray = JsonObject->GetArrayField("ws");
		for (auto rs : TempArray)
		{
			Result.Append((rs->AsObject()->GetArrayField("cw"))[0]->AsObject()->GetStringField("w"));
		}
	}

	UE_LOG(XunFeiLog, Log, TEXT("%s"), *Result);
	
	return Result;
}

// 在 cb_ivw_msg_proc 静态函数中进行事件触发
int cb_ivw_msg_proc(const char* sessionID1, int msg, int param1, int param2, const void* info, void* userData)
{
	if (MSP_IVW_MSG_ERROR == msg) //唤醒出错消息
	{
		UE_LOG(LogTemp, Warning, TEXT("不在"));
		return 0;
	}
	else if (MSP_IVW_MSG_WAKEUP == msg) //唤醒成功消息
	{
		UE_LOG(LogTemp, Warning, TEXT("imhere"));
		if (userData) {
			ASpeechActor* MYThis = reinterpret_cast<ASpeechActor*>(userData);
			if (MYThis) {
				UE_LOG(LogTemp, Warning, TEXT("diaoyongle"));
				MYThis->OnWakeUpBuffer.Broadcast();
				return 1;
			}
		}
	}
	return 0;
}
FString ASpeechActor::WakeUpStart()
{
	int err_code = MSP_SUCCESS;
	const char* params = "ivw_threshold=0:5, ivw_res_path =fo|c:\\wakeupresource.jet";
	int ret = 0;
	const char* sessionID = QIVWSessionBegin(NULL, params, &ret);
	err_code = QIVWRegisterNotify(sessionID,cb_ivw_msg_proc, this);
	if (err_code != MSP_SUCCESS)
	{
		UE_LOG(LogTemp, Warning, TEXT("QIVWRegisterNotify failed, error code is: %d"), ret);
	}
	else {
		UE_LOG(LogTemp, Warning, TEXT("QIVWRegisterNotify success, error code is: %d"), ret);
	}
	
	if (MSP_SUCCESS != ret)
	{
		
		
		UE_LOG(LogTemp, Warning, TEXT("QIVWSessionBegin failed, error code is: %d"),ret);
	}
	return FString(sessionID);
	UE_LOG(LogTemp, Warning, TEXT("QIVWSessionBegin is working"));
	
	
}

bool ASpeechActor::WakeUpEnd(FString SessionID)
{
	
		int ret = QIVWSessionEnd(TCHAR_TO_ANSI(*SessionID) , "normal end");
	if (MSP_SUCCESS != ret)
	{
		UE_LOG(LogTemp, Warning, TEXT("QIVWSessionEnd failed, error code is: %d"), ret);
		return false;
	}
	UE_LOG(LogTemp, Warning, TEXT("QIVWSessioniSEnd"));
	return true;
}


bool ASpeechActor::WakeUpBuffer(TArray<uint8> BitArray, FString SessionID)
{
	int ret = 0;

	if (BitArray.Num() == 0)
	{
		
		return false;
	}
	else
	{
		int audio_len = BitArray.Num();
		int audio_status =2;  // 设置音频状态,这里假设为MSP_AUDIO_SAMPLE_LAST

		ret = QIVWAudioWrite(TCHAR_TO_ANSI(*SessionID), BitArray.GetData(), audio_len, audio_status);
		if (MSP_SUCCESS != ret)
		{
				printf("QIVWAudioWrite failed, error code is: %d", ret);
				return false;
		}
		return true;
	}
}

至此准备工作完成
打开我们的蓝图

  1. 在确保你的Offline Voice Recognition插件打开的前提下添加vosk插件
    科大讯飞invalid time or time required,ue5,ue4

  2. 考虑本文的应用环境需要他初始化时就持续监听
    故而让其开始运行时就开启录音(语音唤醒需要录音文件才能监听)

科大讯飞invalid time or time required,ue5,ue4

  1. 接下里我们需要注册讯飞,并且在运行结束释放讯飞注册
    科大讯飞invalid time or time required,ue5,ue4
    4.讯飞注册后等待两秒注册我们的wakeup语音唤醒
    科大讯飞invalid time or time required,ue5,ue4
  2. 绑定一个唤醒事件

科大讯飞invalid time or time required,ue5,ue4
3. 在设置一个1.1秒的定时器
科大讯飞invalid time or time required,ue5,ue4
4. 定时器内部(正在语音识别默认值为false)
科大讯飞invalid time or time required,ue5,ue4
科大讯飞invalid time or time required,ue5,ue4
科大讯飞invalid time or time required,ue5,ue4
5. 语音唤醒的自定义事件(唤醒五秒钟后恢复继续监听)
科大讯飞invalid time or time required,ue5,ue4
蓝图文件以绑定
author:Dacimal
定制化开发联系:19815779273(不闲聊)文章来源地址https://www.toymoban.com/news/detail-845666.html

到了这里,关于【UE5/UE4】超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【UE5】离线AI聊天-接入LLAMA语言模型 教程

    前言:LLAMA是一种神经网络模型,全称为Language Model with an Average Attention Mechanism(具有平均注意机制的语言模型)。它是一种用于自然语言处理任务的模型,特别适用于生成文本和回答问题。LLAMA模型结合了注意力机制和平均池化,以提高模型对输入文本的理解和生成能力。它

    2024年03月21日
    浏览(37)
  • vue使用科大讯飞的语音识别(语音听写)

    使用的是封装好的插件:voice-input-button2 真的很好使很好使 1、先使用npm下载插件 npm i voice-input-button2 -save -dev 2、在main.js中引入 import voiceInputButton from \\\'voice-input-button2\\\' Vue.use(voiceInputButton, { appId: \\\'xxx\\\', // 您申请的语音听写服务应用的ID apiKey: \\\'xxxxxxxxxxxxxxxxxxxxxxxxx\\\', // 您开通的语音

    2024年01月19日
    浏览(32)
  • 科大讯飞语音离线命令识别

    准备工作 注册讯飞账号,做相关的认证,只有认证通过才能下载部分免费的资源。官网地址:https://console.xfyun.cn/ 创建我的应用后再在离线命令识别 操作前先查看一下这个官方文档Android 语音识别(Recognizer) | 讯飞开放平台文档中心 (xfyun.cn) 1、必要文件包复制到自己的项目目录

    2023年04月08日
    浏览(41)
  • 科大讯飞语音SDK下载及测试

    一、SDK 下载 进入讯飞开发平台官网http://www.xfyun.cn/,右上角进行注册登录,登录后点击进入SDK下载。            2.创建新应用               3.填入相关信息         4.创建完后提交后回到SDK下载页面,刷新页面,应用选择前面创建的应用,平台选择Linux,SDK选择

    2024年02月08日
    浏览(50)
  • 科大讯飞语音合成Java springboot集成

    科大讯飞语音合成 文本转语音 一、引入依赖: 二、下载响应的sdk,我这里是下载的java win版本的sdk SDK下载 - 科大讯飞api接口 - 讯飞开放平台 三、具体代码: 从下载的依赖里面找到对应文件,给代码里面替换成你的绝对路径,运行即可 备注:这个地方需要你自己的账号下载

    2024年02月15日
    浏览(39)
  • vue 利用科大讯飞实现实时语音转写

    1:新建js文件,该文件在科大讯飞api的demo种可以找到 2: 引入第一个文件在vue页面中 3:如果在引入的过程中有些关于worker的报错,可以参考以下方法  在vue.config.js中加入  

    2024年02月12日
    浏览(30)
  • 技术解读 | 科大讯飞语音技术最新进展之二:语音识别与语音合成

    这一篇内容将围绕语音识别与合成方向,继续为大家带来相关技术解析。 “风物长宜放眼量”。面向人机交互更加自然流畅的未来,智能语音技术进展如何?该走向何方? 以下内容根据讯飞研究院杰出科学家潘嘉在NCMMSC 2022上的主题演讲《科大讯飞语音技术前沿进展》整理。

    2024年02月07日
    浏览(39)
  • GEC6818科大讯飞离线语音识别

    在下载SDK时需要注意选择Linux的版本!! 在官网下载离线语音的包,解压后可以得到下面的一些文件夹: 解压后你需要知道自己命令放在下面的文件夹中 关于Make file文件: 关于asr_offline_sample.c文件: asr_offline_sample.c 文件是我们更改为自己的逻辑的文件,但是也不需要都了解

    2024年01月17日
    浏览(41)
  • 微信小程序调用科大讯飞 在线合成语音接口(文字转语音)

    科大讯飞在线文档 https://www.xfyun.cn/doc/tts/online_tts/API.html 科大讯飞调用接口 地址 https://blog.csdn.net/jinxi1112/article/details/122835386 微信小程序base64转ArrayBuffer替代方案 https://www.homedt.net/43939.html 注意点 调用函数 参考大佬的实例 这里说一下注意的点 微信小程序 不支持在线的 base64

    2024年02月10日
    浏览(31)
  • C#调用科大讯飞离线语音合成实现文本转语音

    文本转语音(Text To Speech),简称TTS,在很多业务场景会用到,比如广播大厅,人机互动等。C#要实现TTS有不少选择,比如调用System.Speech,此处就不细说了,下面主要介绍一下C#调用科大讯飞的离线语音合成SDK来实现文本转语音。 地址:[https://www.xfyun.cn/service/offline_tts] 一、创建

    2024年02月12日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包