2023-11-21 08:57:37 +00:00
|
|
|
|
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;
|
2023-11-24 10:15:34 +00:00
|
|
|
|
public ChatBox chatBox;
|
2023-11-21 08:57:37 +00:00
|
|
|
|
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;
|
2023-11-24 10:15:34 +00:00
|
|
|
|
[NonSerialized]
|
|
|
|
|
public Expression expression = Expression.Neutral;
|
2023-11-21 08:57:37 +00:00
|
|
|
|
|
|
|
|
|
public event Action<string> onReceiveText;
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2023-11-24 10:15:34 +00:00
|
|
|
|
audioSource.dopplerLevel = 0;
|
2023-11-21 08:57:37 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-24 10:15:34 +00:00
|
|
|
|
PlayPresetAudioClip(TTS.PresetAudio.SayHi);
|
2023-11-21 08:57:37 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-11-24 10:15:34 +00:00
|
|
|
|
audioSource.PlayOneShot(clip);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 播放预设的音频
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="presetAudio"></param>
|
|
|
|
|
public void PlayPresetAudioClip(TTS.PresetAudio presetAudio)
|
|
|
|
|
{
|
|
|
|
|
chatSettings.m_TextToSpeech.Speak(presetAudio, (clip) => {
|
|
|
|
|
audioSource.PlayOneShot(clip);
|
|
|
|
|
});
|
2023-11-21 08:57:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-11-24 10:15:34 +00:00
|
|
|
|
|
|
|
|
|
public enum Expression
|
|
|
|
|
{
|
|
|
|
|
Neutral = 0,
|
|
|
|
|
Happy = 1,
|
|
|
|
|
Sad = 2,
|
|
|
|
|
Doubt = 3,
|
|
|
|
|
Suprised = 4,
|
|
|
|
|
Smile = 5
|
|
|
|
|
}
|
2023-11-21 08:57:37 +00:00
|
|
|
|
}
|