Windows系统实现唤醒+合成+命令词智能语音交互

1、之前写过离线能力调用,今天来个终极版,实现智能交互或者结合大模型的智能交互示例,下面进入正题。上B站效果离线唤醒+离线合成+离线命令词实现智能交互_哔哩哔哩_bilibili

2、到讯飞开放平台下载唤醒+合成+命令词的离线组合包,找到msc_64.dll复制三份出来,一定要注意路径位置,不然会出现错误。msc直接下载的原封不动的拷贝就行

3、常量类的定义,各位直接复制粘贴即可,注意换自己的APPID,不然报错的

package com.day.config;import com.sun.jna.ptr.IntByReference;import javax.sound.sampled.*;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;public class Constants {// 构造16K 16BIT 单声道音频public static final String APPID = "5e11538f";  // APPIDpublic static final String WORK_DIR = "src/main/resources";// 1、唤醒相关  ssb_param,一定注意IVW_SSB_PARAMS的fo|xxx资源的路径,xxx取值是指WORK_DIR目录下/msc/xxx   xxx是以后的路径开始拼接的!!!!!!!!!!!public static final AudioFormat IVW_ASR_AUDIO_FORMAT = new AudioFormat(16000F, 16, 1, true, false);public static final String IVW_DLL_PATH = "src/main/resources/ivw_msc_x64.dll"; // windows动态库路径public static final String IVW_LOGIN_PARAMS = "appid = " + APPID + ", work_dir = " + WORK_DIR;public static final String IVW_SSB_PARAMS = "ivw_threshold=0:1450,sst=wakeup,ivw_shot_word=1,ivw_res_path =fo|res/ivw/wakeupresource.jet";public static IntByReference IVW_ERROR_CODE = new IntByReference(-100);public static final Integer IVW_FRAME_SIZE = 6400;  // 一定要每200ms写10帧,否则会出现唤醒一段时间后无法唤醒的问题,一帧的大小为640B,其他大小可能导致无法唤醒。public static Integer IVW_AUDIO_STATUS = 1;public static DataLine.Info IVW_ASR_DATA_LINE_INFO = new DataLine.Info(TargetDataLine.class, IVW_ASR_AUDIO_FORMAT);public static TargetDataLine IVW_ASR_TARGET_DATA_LINE; // 录音static {try {IVW_ASR_TARGET_DATA_LINE = (TargetDataLine) AudioSystem.getLine(IVW_ASR_DATA_LINE_INFO);} catch (LineUnavailableException e) {e.printStackTrace();}}// 2、合成相关public static final AudioFormat TTS_AUDIO_FORMAT = new AudioFormat(16000F, 16, 1, true, false);public static final String TTS_DLL_PATH = "src/main/resources/tts_msc_x64.dll"; // windows动态库路径public static final String TTS_LOGIN_PARAMS = "appid = " + APPID + ", work_dir = " + WORK_DIR;public static final String TTS_SESSION_BEGIN_PARAMS = "engine_type = local, voice_name = xiaoyan, text_encoding = UTF8," +" tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";public static IntByReference TTS_ERROR_CODE = new IntByReference(-100);public static IntByReference TTS_AUDIO_LEN = new IntByReference(-100);public static IntByReference TTS_SYNTH_STATUS = new IntByReference(-100);public static String TTS_TEXT; // 合成文本public static Integer TTS_TOTAL_AUDIO_LENGTH; // 合成音频长度public static ByteArrayOutputStream TTS_BYTE_ARRAY_OUTPUT_STREAM; // 合成音频流public static DataLine.Info TTS_DATA_LINE_INFO = new DataLine.Info(SourceDataLine.class, TTS_AUDIO_FORMAT, AudioSystem.NOT_SPECIFIED);public static SourceDataLine TTS_SOURCE_DATA_LINE; // 播放static {try {TTS_SOURCE_DATA_LINE = (SourceDataLine) AudioSystem.getLine(Constants.TTS_DATA_LINE_INFO);} catch (LineUnavailableException e) {e.printStackTrace();}}// 3、离线命令词相关public static final String ASR_DLL_PATH = "src/main/resources/asr_msc_x64.dll"; // windows动态库路径public static final String ASR_LOGIN_PARAMS = "appid = " + APPID + ", work_dir = " + WORK_DIR;public static final String ASR_CALL_BNF_PATH = "src/main/resources/msc/res/asr/call.bnf";public static final String ASR_BUILD_PARAMS = "engine_type = local,asr_res_path = fo|res/asr/common.jet," +"sample_rate = 16000,grm_build_path = res/asr/GrmBuilld_x64";public static final String ASR_LEX_PARAMS = "engine_type=local,asr_res_path = fo|res/asr/common.jet, " +"sample_rate = 16000,grm_build_path =res/asr/GrmBuilld_x64, grammar_list =call";public static IntByReference ASR_ERROR_CODE = new IntByReference(-100);public static final String ASR_SESSION_PARAMS = "vad_bos =3000 ,vad_eos = 10000,engine_type = local,asr_res_path = fo|res/asr/common.jet, " +"sample_rate = 16000,grm_build_path = res/asr/GrmBuilld_x64, local_grammar = call,result_type = json, result_encoding = UTF8";public static IntByReference ASR_EP_STATUS = new IntByReference(-100);public static IntByReference ASR_RECOG_STATUS = new IntByReference(-100);public static Integer ASR_AUDIO_STATUS = 1;public static Integer ASR_FRAME_SIZE = 640;   // 16k采样率的16位音频,一帧的大小为640Byte(来自Windows SDK的说明)public static FileInputStream ASR_FILE_INPUT_STREAM;public static String ASR_GRAMMAR_CONTENT;public static IntByReference ASR_RESULT_STATUS = new IntByReference(-100);
}

4、唤醒方法重写(唤醒成功执行回调函数,往下看)

package com.day.service;import com.day.config.Constants;
import com.day.service.imp.IvwCallback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;public interface IvwService extends Library {/*** 重点:* 1.char *   对应  String* 2.int *    对应  IntByReference* 3.void *   对应  Pointer或byte[]* 4.int      对应  int* 5.无参     对应  无参* 6.回调函数  对应  根据文档自定义回调函数,实现接口Callback*///加载dll动态库并实例化,从而使用其内部的方法IvwService INSTANCE = Native.loadLibrary(Constants.IVW_DLL_PATH, IvwService.class);//定义登录方法    MSPLogin(const char *usr, const char *pwd, const char *params)public Integer MSPLogin(String usr, String pwd, String params);//定义开始方法    QIVWSessionbegin(const char *grammarList, const char *params, int *errorCode)public String QIVWSessionBegin(String grammarList, String params, IntByReference errorCode);//定义写音频方法  QIVWAudioWrite(const char *sessionID, const void *audioData, unsigned int audioLen, int audioStatus)public Integer QIVWAudioWrite(String sessionID, byte[] audioData, int audioLen, int audioStatus);//定义结束方法    QIVWSessionEnd(const char *sessionID, const char *hints)public Integer QIVWSessionEnd(String sessionID, String hints);//定义获取结果方法 QIVWRegisterNotify(const char *sessionID, ivw_ntf_handler msgProcCb, void *userData)public Integer QIVWRegisterNotify(String sessionID, IvwCallback ivwCallback, byte[] userData);//定义退出方法 唤醒一般不用退出public Integer MSPLogout();
}

 5、合成方法重写

package com.day.service;import com.day.config.Constants;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;public interface TtsService extends Library {/*** 重点:* 1.char *   对应  String* 2.int *    对应  IntByReference* 3.void *   对应  byte[]/Pointer,回调函数里此类型需用String来对应。* 4.int      对应  int* 5.无参     对应  void* 6.回调函数  对应  根据文档自定义回调函数,实现接口Callback,离线语音合成无回调*///加载dll动态库并实例化,从而使用其内部的方法TtsService INSTANCE = Native.loadLibrary(Constants.TTS_DLL_PATH, TtsService.class);//定义登录方法public Integer MSPLogin(String usr, String pwd, String params);//开始一次普通离线语音合成public String QTTSSessionBegin(String params, IntByReference errorCode);//写入需要合成的文本public Integer QTTSTextPut(String sessionID, String textString, int textLen, String params);//获取离线合成的音频public Pointer QTTSAudioGet(String sessionID, IntByReference audioLen, IntByReference synthStatus, IntByReference errorCode);//结束本次普通离线语音合成public Integer QTTSSessionEnd(String sessionID, String hints);//定义退出方法public Integer MSPLogout();
}

6、离线命令词方法重写

package com.day.service;import com.day.config.Constants;
import com.day.service.imp.AsrGrammarCallback;
import com.day.service.imp.AsrLexiconCallback;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;public interface AsrService extends Library {/*** 重点:* 1.char *   对应  String* 2.int *    对应  IntByReference* 3.void *   对应  byte[],回调函数里此类型需用String来对应。* 4.int      对应  int* 5.无参     对应  void* 6.回调函数  对应  根据文档自定义回调函数,实现接口Callback*///加载dll动态库并实例化,从而使用其内部的方法AsrService INSTANCE = Native.loadLibrary(Constants.ASR_DLL_PATH, AsrService.class);//定义登录方法public Integer MSPLogin(String usr, String pwd, String params);//开始一次语音识别。public String QISRSessionBegin(String grammarList, String params, IntByReference errorCode);//写入本次识别的音频public Integer QISRAudioWrite(String sessionID, byte[] byteArrayAudioData, int waveLen, int audioStatus, IntByReference epStatus, IntByReference recogStatus);//获取识别结果。public String QISRGetResult(String sessionID, IntByReference rsltStatus, int waitTime, IntByReference errorCode);//结束本次语音识别。public Integer QISRSessionEnd(String sessionID, String hints);//获取当次语音识别信息,如上行流量、下行流量等public Integer QISRGetParam(String sessionID, String paramName, String paramValue, IntByReference valueLen);//构建语法,生成语法ID。有回调public Integer QISRBuildGrammar(String grammarType, String grammarContent, int grammarLength, String params, AsrGrammarCallback asrGrammarCallback, byte[] userData);//更新本地语法词典。有回调public Integer QISRUpdateLexicon(String lexiconName, String lexiconContent, int lexiconLength, String params, AsrLexiconCallback asrLexiconCallback, byte[] userData);//定义退出方法public Integer MSPLogout();
}

7、回调函数的定义(1个唤醒的,2个离线命令词的)

package com.day.service.imp;import com.day.AIMain;
import com.sun.jna.Callback;public class IvwCallback implements Callback {public int cb_ivw_msg_proc(String sessionID, int msg, int param1, int param2,String info, String userData) {System.out.println("回调函数返回的唤醒结果...:" + info);AIMain.startTts("在的,请说指令");AIMain.startAsr(); // 答复完毕调用命令词return 0;}
}
package com.day.service.imp;import com.sun.jna.Callback;public class AsrGrammarCallback implements Callback {public int build_grm_cb(int errorCode, String info, String userData) {System.out.println("构建语法返回的ID信息...:" + info + ",错误码...:" + errorCode);return 0;}
}
package com.day.service.imp;import com.sun.jna.Callback;public class AsrLexiconCallback implements Callback {public int LexiconCallBack(int errorCode, String info, String userData) {System.out.println("更新词典返回的信息...:" + info + ",错误码...:" + errorCode);return 0;}
}

8、为了方便各位看官,上POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><!--父工程坐标############################################################################################--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><!--自己被别人引用的坐标#########################################################################################--><groupId>com.example</groupId><artifactId>day</artifactId><version>0.0.1-SNAPSHOT</version><name>day</name><description>day</description><!--指定JDK版本################################################################################################--><properties><java.version>1.8</java.version></properties><!--总体依赖JAR################################################################################################--><dependencies><!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version></dependency><!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna --><dependency><groupId>net.java.dev.jna</groupId><artifactId>jna</artifactId><version>5.5.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.6.RELEASE</version></plugin></plugins></build><!--配置阿里云仓库下载--><repositories><repository><id>nexus-aliyun</id><name>nexus-aliyun</name><url>https://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>nexus-aliyun</name><url>https://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories></project>

9、命令词也给一份示例Call.bnf(老生常谈,注意放置位置)

#BNF+IAT 1.0;
!grammar call;
!slot <enter>;
!slot <scanSolicitation>;
!slot <scanDelivery>;
!slot <exit>;
!start <callStart>;
<callStart>:[<enter>][<scanSolicitation>][<scanDelivery>][<exit>];
<enter>:立刻|马上|一分钟后|十分钟后|半小时后;
<scanSolicitation>:打开|关闭|调量|调暗|调高|调低;
<scanDelivery>:主卧|次卧|书房|客厅;
<exit>:空调|点灯|窗户|窗帘|衣柜;

10、实现完美的智能语音交互,感兴趣的可以结合下大模型做智能问答场景。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/7569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【华为OD机试】按单词下标区间翻转文章内容【2023 B卷|100分】

【华为OD机试】-真题 !!点这里!! 【华为OD机试】真题考点分类 !!点这里 !! 题目描述: 输入一个英文文章片段,翻转指定区间的单词顺序,标点符号和普通字母一样处理。 例如输入字符串 “I am a developer.”,区间[0,3]则输出 “developer. a am I”。 输入描述: 使用换…

【趟坑记录】d3.zoom()的正确使用姿势 @d3.v7

【趟坑记录】d3.zoom()的正确使用姿势 d3.v7 文章目录 【趟坑记录】d3.zoom()的正确使用姿势 d3.v7问题重现原因分析解决方案放缩平移写法特殊修改transform函数的写法 总结 在开发一个D3应用的时候遇到了一个 zoom相关的问题&#xff0c;记录解决思路与方案 问题重现 最近在…

动态监控U盘重启容器

需求背景 Ubuntu机器需要动态根据插入的U盘进行导入数据, 路径是约定为U盘内的固定路径. 但是服务是docker服务, 插入U盘并不会直接挂在到容器内部, 需要重启容器才能生效, 每次手动重启很麻烦, 自动检测U盘路径变化来操作容器. 配置动态监控脚本和服务 编写脚本 vim moni…

Windows 10, version 22H2 (updated Jul 2023) 中文版、英文版下载

Windows 10, version 22H2 (updated Jul 2023) 中文版、英文版下载 Windows 10 22H2 企业版 arm64 x64 请访问原文链接&#xff1a;https://sysin.org/blog/windows-10/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.org Window…

FPGA——verilog实现格雷码与二进制的转换

文章目录 一、格雷码简介二、二进制转格雷码三、格雷码转二进制四、仿真 一、格雷码简介 格雷码是一种循环二进制码或者叫作反射二进制码。跨时钟域会产生亚稳态问题&#xff08;CDC问题&#xff09;&#xff1a;从时钟域A过来的信号难以满足时钟域B中触发器的建立时间和保持时…

python实现远程服务器的操作

前言 测试过程中经常会遇到需要将本地的文件上传到远程服务器上&#xff0c;或者需要将服务器上的文件拉到本地就行操作&#xff0c;以前安静经常会用到xftp工具。今天介绍一种python库Paramiko&#xff0c;可以帮助我们通过代码的方式进行完成对远程服务器的上传和下载操作。…

MongoDB University课程M310 MongoDB Security 学习笔记

环境准备 此课程需要两台虚机。因此需要提前安装Vagrant和VirtualBox&#xff0c;这些我已经有了。因此只需要下载课程提供的Vagrant文件m310-vagrant-env.zip就可以了。 解压文件&#xff0c;进入目录&#xff0c;运行以下命令即可&#xff1a; $ cd m310-vagrant-env $ va…

elementui el-table 封装表格

ps: 1.3版本 案例&#xff1a; 完整代码&#xff1a; 可直接复制粘贴&#xff0c;但一定要全看完&#xff01; v-slot"scopeRows" 是vue3的写法&#xff1b; vue2是 slot-scope"scope" <template><!-- 简单表格、多层表头、页码、没有合并列行…

flutter开发实战-Stagger Animation实现水波纹动画

flutter开发实战-实现水波纹动画&#xff0c;使用到了交织动画&#xff0c;实现三个圆逐渐放大与渐变的过程。 一、效果图 二、实现水波纹效果 实现水波纹动画&#xff0c;使用到了交织动画&#xff0c;实现三个圆逐渐放大与渐变的过程。 交织动画 有些时候我们可能会需要一些…

线程系列 7 - JUC高并发容器类

线程系列 7 - JUC高并发容器类 1、JUC高并发容器1.1、为什么需要JUC高并发容器1.2、什么是 JUC 高并发容器1.3、CopyOnWriteArrayList1.4、BlockingQueue1.4.1、阻塞队列的常用方法1.4.2、ArrayBlockingQueue1.4.3、LinkedBlockingQueue1.4.4、DelayQueue1.4.5、PriorityBlocki…

TypeScript快速入门

文章目录 一、初识TypeScript1、安装TypeScript2、Hello TypeScript 二、结合项目---配置1、tsconfig.jsontsconfig.json 重要字段compilerOptions 每个选项的详细说明 2、ts-loader 三、语法1、基本类型2、类型注解3、函数4、接口5、类6、泛型 四、结合项目---vue3结合使用 一…

可观测之调用链Skywalking

简介 分布式系统的应用程序性能监视工具&#xff0c;专为微服务、云原生架构和基于容器&#xff08;Docker、K8s、Mesos&#xff09;架构而设计。提供分布式追踪、服务网格遥测分析、度量聚合和可视化一体化解决方案。 多种监控手段。可以通过语言探针和 service mesh 获得监控…

HTTPS连接过程中的中间人攻击

HTTPS连接过程中的中间人攻击 HTTPS连接过程中间人劫持攻击 HTTPS连接过程 https协议就是httpssl/tls协议&#xff0c;如下图所示为其连接过程&#xff1a; HTTPS连接的整个工程如下&#xff1a; https请求&#xff1a;客户端向服务端发送https请求&#xff1b;生成公钥和私…

矩阵置零(力扣)思维 JAVA

给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 输入&#xff1a;matrix [[1,1,1],[1,0,1],[1,1,1]] 输出&#xff1a;[[1,0,1],[0,0,0],[1,0,1]] 输入&#xff1a;matrix [[0,1,2,0],[3,4,5,2],[…

[ 华为云 ] 云计算中Region、VPC、AZ 是什么,他们又是什么关系,应该如何抉择

前几天看到一个问答帖&#xff0c;我回答完了才发现这个帖子居然是去年的也没人回复&#xff0c;其中他问了一些华为云的问题&#xff0c;对于其中的一些概念&#xff0c;这里来总结讲解一下&#xff0c;希望对学习华为云的小伙伴有所帮助。 文章目录 区域&#xff08;Region&a…

计算机基础专升本笔记四 计算机系统

计算机基础专升本笔记四 计算机系统 计算机系统 计算机系统由计算机硬件系统和计算机软件系统 组成。且是按照存储程序的方式工作的。计算机硬件就是由各种电子器件按照一定逻辑连接而成&#xff0c;看的见摸得着&#xff0c;是计算机系统的物质基础&#xff0c;计算机软件系统…

# jellyfin安装设置使用散记

jellyfin安装设置使用散记 文章目录 jellyfin安装设置使用散记0 软件简介1 安装2 视频转码问题2.1 局域网转码情况测试&#xff08;不同网段&#xff09;2.2 局域网jellyfin app默认转码问题解决2.3 外网转码情况测试 3 一些坑4 插件5 最后 0 软件简介 Jellyfin 是一个自由的软…

UDS之11服务

11服务&#xff1a; 功能&#xff1a;控制MCU进行重启&#xff0c;重启分为硬重启和软重启&#xff0c;11服务一般代表软重启&#xff0c;虽然它里面有个子服务是硬件重启&#xff0c;这里需要注意下&#xff1b;硬重启在日常工作中一般代表B重启。命令格式&#xff08;请求&am…

LiveGBS流媒体平台GB/T28181功能-视频直播流媒体平台分屏展示设备树分组树记录上次分屏播放记录

LiveGBS视频直播流媒体平台分屏展示设备树分组树记录上次分屏播放记录 1、分屏展示1.1、单屏1.2、四分屏1.3、九分屏1.4、十六分屏 2、分屏记录3、搭建GB28181视频直播平台 1、分屏展示 LiveGBS分屏页面支持&#xff0c;多画面播放&#xff0c;支持单屏、四分屏、九分屏、十六…

python中的os._exit()、sys.exit()和exit()/quit()函数

python中的os._exit()、sys.exit()和exit()/quit()函数 os._exit() 官方文档https://docs.python.org/zh-cn/3/library/os.html#os._exit 语法格式&#xff1a; os._exit(n) 以状态码 n 退出进程&#xff08;process&#xff09;&#xff0c;不会调用清理处理程序&#xf…