181 lines
4.7 KiB
C#
181 lines
4.7 KiB
C#
|
using System;
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using TMPro;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public class CarAssistant : MonoBehaviour
|
|||
|
{
|
|||
|
public StatusManager statusManager;
|
|||
|
public RobotAnimControl animController;
|
|||
|
/// <summary>
|
|||
|
/// 播放声音
|
|||
|
/// </summary>
|
|||
|
public AudioSource audioSource;
|
|||
|
/// <summary>
|
|||
|
/// 聊天配置
|
|||
|
/// </summary>
|
|||
|
public ChatSetting chatSettings;
|
|||
|
/// <summary>
|
|||
|
/// 语音输入处理类
|
|||
|
/// </summary>
|
|||
|
public AudioRecorder voiceInputs;
|
|||
|
[System.NonSerialized]
|
|||
|
public AudioClip clip;
|
|||
|
public VoiceWakeUp voiceWakeUp;
|
|||
|
//保存聊天记录
|
|||
|
public List<string> chatHistory;
|
|||
|
public TextMeshPro textMeshPro;
|
|||
|
public string currentTalking = "";
|
|||
|
/// <summary>
|
|||
|
/// 启用语音唤醒
|
|||
|
/// </summary>
|
|||
|
public bool EnableVoiceWakeup
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _EnableVoiceWakeup;
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
_EnableVoiceWakeup = value;
|
|||
|
if(value && voiceWakeUp.recgnizeStatus == VoiceWakeUp.RecgnizeStatus.Idle)
|
|||
|
{
|
|||
|
voiceWakeUp.StartRecgnition(OnRecgnizedKeyWord);
|
|||
|
}
|
|||
|
else if(!value &&voiceWakeUp.recgnizeStatus == VoiceWakeUp.RecgnizeStatus.Recgnizing)
|
|||
|
{
|
|||
|
voiceWakeUp.StopRecognition();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
bool _EnableVoiceWakeup = false;
|
|||
|
|
|||
|
public event Action<string> onReceiveText;
|
|||
|
private void Awake()
|
|||
|
{
|
|||
|
statusManager = new StatusManager(this);
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator Start()
|
|||
|
{
|
|||
|
while (!voiceWakeUp.IsEnable)
|
|||
|
{
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
statusManager.MakeTransition(statusManager.idle);
|
|||
|
voiceInputs.onRecordOver += AcceptClip;
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
statusManager.Update();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 当识别到语音
|
|||
|
/// </summary>
|
|||
|
/// <param name="successed"></param>
|
|||
|
void OnRecgnizedKeyWord(bool successed)
|
|||
|
{
|
|||
|
if (successed)
|
|||
|
{
|
|||
|
statusManager.MakeTransition(statusManager.listening);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 处理录制的音频数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="_data"></param>
|
|||
|
public void AcceptClip(AudioClip _audioClip,AudioRecorder.StopReason stopReason)
|
|||
|
{
|
|||
|
if (chatSettings.m_SpeechToText == null)
|
|||
|
return;
|
|||
|
if(stopReason == AudioRecorder.StopReason.WaitTimeOut)
|
|||
|
{
|
|||
|
TalkingPreset(TTS.PresetAudio.SorrySaid);
|
|||
|
return;
|
|||
|
}
|
|||
|
if (_audioClip == null)
|
|||
|
return;
|
|||
|
//切换思考动作
|
|||
|
statusManager.MakeTransition(statusManager.thinking);
|
|||
|
|
|||
|
chatSettings.m_SpeechToText.SpeechToText(_audioClip, DealingTextCallback);
|
|||
|
}
|
|||
|
|
|||
|
public void PlayAudioClip(AudioClip clip)
|
|||
|
{
|
|||
|
audioSource.clip = clip;
|
|||
|
audioSource.Play();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 处理识别到的文本
|
|||
|
/// </summary>
|
|||
|
/// <param name="_msg"></param>
|
|||
|
private void DealingTextCallback(string _postWord)
|
|||
|
{
|
|||
|
if (_postWord.Equals(""))
|
|||
|
{
|
|||
|
TalkingPreset(TTS.PresetAudio.SorrySaid);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
//添加记录聊天
|
|||
|
chatHistory.Add(_postWord);
|
|||
|
if (onReceiveText != null)
|
|||
|
onReceiveText(_postWord);
|
|||
|
//提示词
|
|||
|
string _msg = _postWord;
|
|||
|
Debug.Log("玩家说:" + _postWord);
|
|||
|
//发送数据
|
|||
|
chatSettings.m_ChatModel.PostMsg(_msg, Talking);
|
|||
|
}
|
|||
|
|
|||
|
void TalkingPreset(TTS.PresetAudio presetAudio)
|
|||
|
{
|
|||
|
Debug.Log("播放预置的声音:" + presetAudio);
|
|||
|
chatSettings.m_TextToSpeech.Speak(presetAudio, (clip) => {
|
|||
|
this.clip = clip;
|
|||
|
statusManager.MakeTransition(statusManager.talking);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 说话,但不加进历史记录
|
|||
|
/// </summary>
|
|||
|
/// <param name="_response"></param>
|
|||
|
void TalkingDontAddToHistory(string _response)
|
|||
|
{
|
|||
|
_response = _response.Trim();
|
|||
|
currentTalking = _response;
|
|||
|
Debug.Log("收到AI回复:" + _response);
|
|||
|
chatSettings.m_TextToSpeech.Speak(_response, (clip) => {
|
|||
|
this.clip = clip;
|
|||
|
statusManager.MakeTransition(statusManager.talking);
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// AI回复的信息的回调
|
|||
|
/// </summary>
|
|||
|
/// <param name="_response"></param>
|
|||
|
void Talking(string _response)
|
|||
|
{
|
|||
|
_response = _response.Trim();
|
|||
|
currentTalking = _response;
|
|||
|
|
|||
|
Debug.Log("收到AI回复:" + _response);
|
|||
|
//记录聊天
|
|||
|
chatHistory.Add(_response);
|
|||
|
|
|||
|
chatSettings.m_TextToSpeech.Speak(_response, (clip) => {
|
|||
|
this.clip = clip;
|
|||
|
statusManager.MakeTransition(statusManager.talking);
|
|||
|
});
|
|||
|
}
|
|||
|
}
|