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();
    }
}
相关推荐
hz567896 小时前
音频视频sdk开发实践:从实时通话到视频互动的完整方案
音视频·实时音视频·信息与通信
纽格立科技6 小时前
把警报链从纸面接到机房——读德国DAB+自动安全警报实施指南
车载系统·音视频·信息与通信·传媒
hz567897 小时前
视频会议私有化部署指南:架构、成本与实施流程详解
安全·音视频·实时音视频·信息与通信
平行云7 小时前
3D应用推流太贵太慢?ImmerShare Lite:利用本地算力实现极简一键分享
unity·ue5·实时云渲染·云桌面·像素流·云游戏·串流
纽格立科技7 小时前
9月10日,德国的收音机会自己醒来——DAB+自动安全警报进入常态运行
车载系统·音视频·信息与通信·传媒
淡海水8 小时前
11-04-Unity-Span-foreach-LINQ的分配与热路径边界
unity·c#·linq·foreach·span
AI码农小姐姐1 天前
AI漫剧推文短视频动态镜头实现:FFmpeg zoompan 与 Ken Burns 效果实践
人工智能·音视频·ai工具·ai漫剧
技灵AI1 天前
ChatGPT Images 2.5 深度解读:延迟减半、Sketch 草图与 Flare/Sunburst 双模型 API
人工智能·gpt·aigc·音视频·images2.5
灸木1 天前
Unity 多线程与并发:什么时候该用多线程?
unity
林澈在路上1 天前
游戏场景背景音乐定制AI软件哪款好 2026对比推荐
大数据·人工智能·aigc·音视频