Unity之使用火山引擎实现音频剪辑提问,流式语音回复

执行流程

  1. 输入音频文件,调用语音模型API,输出语音识别文字;
  2. 输入识别文字,调用语言模型API,输出回复文字(流式);
  3. 输入回复文字,调用语音模型API,输出音频数据(流式);
  4. 输入音频数据,通过流式AudioClip播放音频。

音频提问,流式语音回复

Unity之使用火山引擎实现语音识别

Unity之使用火山引擎实现文字提问流式回复

Unity之使用火山引擎实现流式语音合成

整合上述功能,实现音频提问,流式语音回复。

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
/// <summary>
/// 语音提问
/// </summary>
public class VoiceQuestion : MonoBehaviour
{
    [Header("语音转文字")]
    [SerializeField] SpeechRecognition speechRecognition;

    [Header("文字转语音")]
    [SerializeField] TextToSpeechOut textToSpeech;

    [Header("文字提问")]
    [SerializeField] TextQuestions textQuestions;
    List<Message> messages;
    StringBuilder thinkContentBuffer;
    Message assistantMessage;
    [Header("进行语音合成最少字符")]
    [SerializeField] int minTTSSendLength = 4;
    StringBuilder ttsSendBuffer;

    [Header("使用附加提示词")]
    [SerializeField] bool addtionPrompt;
    [Header("附加提示词")]
    [SerializeField] string prompt;

    public event Action OnTextAnswerCompleted;
    public event Action OnError;

    void Awake()
    {
        messages = new List<Message>();
        thinkContentBuffer = new StringBuilder();
        ttsSendBuffer = new StringBuilder();
        textQuestions.onReceive += OnReceiveAnswer;
        textQuestions.onCompleted += OnReceiveAnswerCompleted;
        textQuestions.OnError += OnReceiveAnswerError;
    }

    //1.输入提问音频
    public void InputQuestionAudio(AudioClip audioClip, bool newDialogue = true)
    {
        Debug.Log("语音转文字 开始");
        if (newDialogue)
        {
            messages.Clear();
            Debug.Log("清除历史对话");
        }
        speechRecognition.Request(audioClip, OnGetAudioToText);
    }

    //2.问题音频转文字后
    void OnGetAudioToText(bool sucess, string content)
    {
        Debug.Log("语音转文字 结束");
        if (sucess)
        {
            Debug.Log("文字提问 开始");
            if (addtionPrompt)
                content += prompt;
            thinkContentBuffer.Clear();

            messages.Add(new Message()
            {
                role = "user",
                content = content,
            });
            assistantMessage = new Message()
            {
                role = "assistant",
            };
            textQuestions.Request(messages);
        }
        else
        {
            Debug.LogError("语音转文字 失败");
            OnError?.Invoke();
        }
    }

    //3.接收回复文本
    void OnReceiveAnswer(string part, bool stop)
    {
        Debug.Log("接收回复文本");
        thinkContentBuffer.Append(part);
        if (part.Contains("*"))
            part = part.Replace("*", "");
        if (part.Contains("#"))
            part = part.Replace("#", "");

        ttsSendBuffer.Append(part);

        if (ttsSendBuffer.Length >= minTTSSendLength || stop)
        {
            var content = ttsSendBuffer.ToString();
            ttsSendBuffer.Clear();
            textToSpeech.ReadText(content);
        }
    }

    //4.接收回复文本结束
    void OnReceiveAnswerCompleted()
    {
        Debug.Log("文本提问 结束");
        assistantMessage.content = thinkContentBuffer.ToString();
        thinkContentBuffer.Clear();
        messages.Add(assistantMessage);
        textToSpeech.EndReadText();
        OnTextAnswerCompleted?.Invoke();
    }

    void OnReceiveAnswerError()
    {
        OnError?.Invoke();
    }

    void OnDestroy()
    {
        if (textQuestions)
        {
            textQuestions.OnError -= OnReceiveAnswerError;
            textQuestions.onReceive -= OnReceiveAnswer;
            textQuestions.onCompleted -= OnReceiveAnswerCompleted;
        }
        messages?.Clear();
        thinkContentBuffer?.Clear();
    }
}
相关推荐
the3clipse1 小时前
Windows 11 26H2 声音与音频问题排查指南:没声音、麦克风不收音与蓝牙音频优化
音视频·le audio·windows 11 26h2·win11没声音·麦克风没声音·蓝牙耳机音质·音频服务修复
开开心心就好5 小时前
PDF水印怎么去掉?勾选对象再点匹配
java·前端·mysql·支持向量机·pdf·音视频·模拟退火算法
ellis19706 小时前
Claude Code[二] 上下文相关的机制
unity·ai编程
RobinDevNotes1 天前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
伯奥克客服1 天前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频
开开心心就好1 天前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
zdr尽职尽责1 天前
Unity 3C 动画系统:从「蜘蛛网」到可维护的动画架构
unity·架构·游戏引擎
益企研究院1 天前
AMD X 火山引擎 第六代 EPYC 云实例为云上多元工作负载带来新能力
火山引擎·amd
FansAI_art1 天前
多角色 AIGC 短片的双模型分工:静态生图与图生视频怎么接
aigc·音视频
国家一级摸鱼选手1 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交