204 lines
5.3 KiB
C#
204 lines
5.3 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 ChatBox chatBox;
|
||
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;
|
||
[NonSerialized]
|
||
public Expression expression = Expression.Neutral;
|
||
|
||
public event Action<string> onReceiveText;
|
||
private void Awake()
|
||
{
|
||
audioSource.dopplerLevel = 0;
|
||
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)
|
||
{
|
||
PlayPresetAudioClip(TTS.PresetAudio.SayHi);
|
||
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.PlayOneShot(clip);
|
||
}
|
||
/// <summary>
|
||
/// 播放预设的音频
|
||
/// </summary>
|
||
/// <param name="presetAudio"></param>
|
||
public void PlayPresetAudioClip(TTS.PresetAudio presetAudio)
|
||
{
|
||
chatSettings.m_TextToSpeech.Speak(presetAudio, (clip) => {
|
||
audioSource.PlayOneShot(clip);
|
||
});
|
||
}
|
||
|
||
/// <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);
|
||
});
|
||
}
|
||
|
||
public enum Expression
|
||
{
|
||
Neutral = 0,
|
||
Happy = 1,
|
||
Sad = 2,
|
||
Doubt = 3,
|
||
Suprised = 4,
|
||
Smile = 5
|
||
}
|
||
}
|