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

这篇具有很好参考价值的文章主要介绍了Windows系统实现唤醒+合成+命令词智能语音交互。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

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

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";  // APPID
    public 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、实现完美的智能语音交互,感兴趣的可以结合下大模型做智能问答场景。文章来源地址https://www.toymoban.com/news/detail-600025.html

到了这里,关于Windows系统实现唤醒+合成+命令词智能语音交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • python--基于百度aip的语音交互及语音唤醒

    背景:当前随着人工智能的快速发展,人机交互的热度变得越来越大,作为人机交互的很重要的一部分-----语音交互,逐渐成为当前的热门论点。 语音交互的几大部分组成 1.获得音频文件-----2.识别音频文件-----3.将音频文件转换为字符串------4.进行其他相关操作(打开网址,语

    2024年02月12日
    浏览(24)
  • 【实用技巧】掌握人工智能语音转换的核心技术,轻松实现多语言语音转换和语音合成

    作者:禅与计算机程序设计艺术 【实用技巧】掌握人工智能语音转换的核心技术,轻松实现多语言语音转换和语音合成 1.1. 背景介绍 随着全球化的加速,跨文化交流需求日益增长,多语言语音转换和语音合成技术应运而生。人工智能技术的发展为语音合成和转换提供了便利

    2024年02月08日
    浏览(45)
  • 离线语音交互技术路线之语音合成(TTS)篇

      在ChatGPT大行其道之际,我心血来潮想要研究研究如何实现离线语音交互,把它和ChatGPT相结合,自己尝试实现个语音助手玩玩。本篇文章主要先从整体上分析了离线语音交互的技术实现路线,以及每个环节可能用到的参考技术,然后详细阐述了其中一个环节:语音合成(

    2024年02月09日
    浏览(34)
  • 基于语音交互的智能翻译系统

    作者:禅与计算机程序设计艺术 引言 1.1. 背景介绍 随着全球化的发展,跨文化交流日益频繁,语言障碍和文化差异成为了影响人们交流的一个重要因素。为了更好地解决这一问题,人工智能翻译技术逐渐得到了普及和发展。 1.2. 文章目的 本文旨在介绍一种基于语音交互的智

    2024年02月14日
    浏览(38)
  • 语音识别与语音合成:实现完整的自然语言处理系统

    自然语言处理(NLP)是一门研究如何让计算机理解、生成和处理人类语言的学科。在NLP中,语音识别和语音合成是两个重要的子领域。语音识别是将声音转换为文本的过程,而语音合成则是将文本转换为声音。本文将深入探讨这两个领域的核心概念、算法原理、实践和应用场景

    2024年02月22日
    浏览(33)
  • snowboy 自定义唤醒词 实现语音唤醒【语音助手】

    本系列主要目标初步完成一款智能音箱的基础功能,包括语音唤醒、语音识别(语音转文字)、处理用户请求(比如查天气等,主要通过rasa自己定义意图实现)、语音合成(文字转语音)功能。 语音识别、语音合成采用离线方式实现。 操作系统:Ubuntu 22.04.3 LTS CPU:Intel® Core™

    2024年02月14日
    浏览(40)
  • ESP32-S3语音唤醒技术,智能机器人应用,物联网技术发展

    随着技术的发展,智能服务正在融入群众的生活。智能机器人的应用,大大提高人们的工作效率,并减少投入的成本。 传感器检测是否有来宾,指纹识别模块可以用来签到,语音识别处理模块可以进行简单的日常提问进行识别并用相应的回答,触摸屏界面实现人机互动,可以

    2024年02月13日
    浏览(27)
  • 实现手机app和微信小程序和树莓派智能音箱远程控制arduino获取甲醛温湿度和控制灯(esp8266 ZE08-CH2O DHT11 MQTT 语音识别 语言合成 http请求转串口通信系统 )

    首先你有这样的esp8266 这种esp8266自身带2个按键和烧录芯片方便调试,综合性价比较高。 需要有一个arduino uno 连接甲醛探测器和温湿度探测器 或者其他芯片都行。 还有就是你要有树莓派和usb麦克风,用来实现智能音箱,有了这3个开发板我们开始吧! https://www.bilibili.com/video

    2024年02月14日
    浏览(37)
  • 【智能交互】OPPO接入小布语音技能通关教程:个人开发者实现接口调用

    适用人群:本教程适合大赛接入小布语音技能的同学以及初次使用小布助手的开发者 本篇文章是博主弄了多次测试才勉强弄明白的,OPPO的开发文档和没有没啥区别 很多人会问:“为啥有更成熟的其他语音技能平台放着不用?” 答:“因为我在参加的比赛是由oppo主办方举办

    2024年02月16日
    浏览(144)
  • TTS合成技术中的语音合成和人工智能和自然语言生成

    TTS合成技术中的语音合成和人工智能和自然语言生成是当前人工智能技术应用中的重要领域。本文旨在介绍TTS合成技术中的语音合成、人工智能和自然语言生成的概念和技术原理,并给出实现步骤和优化建议,旨在帮助读者更好地理解这个领域的技术细节和发展趋势。 TTS合成

    2024年02月07日
    浏览(43)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包