VoiceController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.UIElements;
  6. using System.Runtime.InteropServices;
  7. using Unity.VisualScripting;
  8. public class VoiceController : MonoBehaviour
  9. {
  10. // Start is called once before the first execution of Update after the MonoBehaviour is created
  11. private VisualElement waveForm;
  12. private Button voiceBtn;
  13. private Coroutine waveCoroutine;
  14. private AudioClip audioClip; // 保存的音频
  15. private bool isRecording = false;
  16. private string filePathWav, filePathZip;
  17. public bool isCommandMode = false; // 是否在命令对话模式下,是的话只显示语音按钮
  18. private DateTime startRecTime, stopRecTime;
  19. void OnEnable()
  20. {
  21. //Debug.Log("voice controller start");
  22. var root = GetComponent<UIDocument>().rootVisualElement;
  23. var voiceArea = root.Q<VisualElement>("voiceArea");
  24. waveForm = voiceArea.Q<VisualElement>("waveForm");
  25. voiceBtn = voiceArea.Q<Button>("voice");
  26. // root加载完成后,所有element算出位置后再进行计算
  27. root.RegisterCallback<GeometryChangedEvent>(e => OnMainMenuClickGeometryChanged(e));
  28. voiceBtn.RegisterCallback<PointerDownEvent>(e => VoiceBtnPointerDown(e), TrickleDown.TrickleDown); // TrickleDown.TrickleDown参数,确保事件在捕获阶段优先处理。
  29. voiceBtn.RegisterCallback<PointerUpEvent>(e => VoiceBtnPointerUp(e));
  30. }
  31. // Update is called once per frame
  32. void Update()
  33. {
  34. VoiceOnlyCheck();
  35. }
  36. // 只显示语音菜单
  37. private void VoiceOnlyCheck()
  38. {
  39. var root = GetComponent<UIDocument>().rootVisualElement;
  40. var menuArea = root.Q<VisualElement>("menuArea");
  41. var dogMenu = root.Q<VisualElement>("dogMenu");
  42. var quit = root.Q<VisualElement>("quit");
  43. if (isCommandMode)
  44. {
  45. menuArea.style.display = DisplayStyle.None;
  46. dogMenu.style.display = DisplayStyle.None;
  47. quit.style.display = DisplayStyle.None;
  48. }
  49. else
  50. {
  51. menuArea.style.display = DisplayStyle.Flex;
  52. dogMenu.style.display = DisplayStyle.Flex;
  53. quit.style.display = DisplayStyle.Flex;
  54. }
  55. }
  56. IEnumerator WaveFormAnimation()
  57. {
  58. while (true)
  59. {
  60. waveForm.style.height = UnityEngine.Random.Range(15f, 30f);
  61. float alpha = UnityEngine.Random.Range(0.6f, 1f);
  62. waveForm.style.unityBackgroundImageTintColor = new Color(1f, 1f, 1f, alpha);
  63. yield return new WaitForSeconds(0.1f);
  64. }
  65. }
  66. void OnMainMenuClickGeometryChanged(GeometryChangedEvent evt)
  67. {
  68. if (waveCoroutine == null)
  69. {
  70. waveCoroutine = StartCoroutine(WaveFormAnimation());
  71. }
  72. }
  73. // 语言控制按键按下的触发事件
  74. void VoiceBtnPointerDown(PointerDownEvent evt)
  75. {
  76. Debug.Log("voice button pointer down.");
  77. HomeController.listenBreak = true;
  78. // 隐藏Dog list
  79. var root = GetComponent<UIDocument>().rootVisualElement;
  80. var dogList = root.Q<VisualElement>("dogMenu").Q<VisualElement>("dogList");
  81. dogList.visible = false;
  82. // 狗动作变化注释镜头
  83. foreach (var dog in HomeController.dogsInScene)
  84. {
  85. if (dog.dogState == DogState.IDLE || dog.dogState == DogState.SLEEP)
  86. {
  87. dog.RemoveZzzParticle();
  88. dog.animator.SetTrigger("listen");
  89. dog.animator.SetBool("isListening", true);
  90. }
  91. }
  92. StartRecording();
  93. waveForm.visible = true;
  94. // 如果时间是在深夜,用户点击后恢复正常的场景的光照
  95. int hour = System.DateTime.Now.Hour;
  96. if (hour >= 22 || hour < 5) // 深夜时间
  97. {
  98. HomeSunLight.Instance.DogWakeUpLightSetting();
  99. }
  100. }
  101. // 语音控制按键松开事件
  102. void VoiceBtnPointerUp(PointerUpEvent evt)
  103. {
  104. Debug.Log("voice button pointer up.");
  105. //HomeController.listenBreak = false;
  106. waveForm.visible = false;
  107. HomeController.listenBreak = false;
  108. // TODO 以后根据音频识别返回值修改狗的行动
  109. foreach (var dog in HomeController.dogsInScene)
  110. {
  111. dog.animator.SetBool("isListening", false);
  112. // 如果在交互模式,就刷新最后交互时间
  113. if (dog.dogState == DogState.INTERACT)
  114. {
  115. dog.interactLastUpdate = DateTime.Now;
  116. }
  117. }
  118. StopRecording();
  119. }
  120. #region 录音相关
  121. // 开始录音
  122. void StartRecording()
  123. {
  124. if (isRecording) return; // 如果已经在录音,则不再重复开始
  125. // 设置录音文件名和路径
  126. filePathWav = Path.Combine(Application.persistentDataPath, "voice.wav");
  127. filePathZip = Path.Combine(Application.persistentDataPath, "voice.zip");
  128. // 检测目录是否存在不存在就创建
  129. string directoryPath = Path.GetDirectoryName(filePathWav);
  130. if (!Directory.Exists(directoryPath))
  131. {
  132. Directory.CreateDirectory(directoryPath);
  133. }
  134. //删除旧文件
  135. if (File.Exists(filePathWav))
  136. {
  137. File.Delete(filePathWav);
  138. }
  139. if (File.Exists(filePathZip))
  140. {
  141. File.Delete(filePathZip);
  142. }
  143. // 开始录音,最长8秒
  144. startRecTime = DateTime.Now;
  145. audioClip = Microphone.Start(null, false, 8, 44100);
  146. isRecording = true;
  147. Debug.Log("开始录音...");
  148. }
  149. // 停止录音
  150. void StopRecording()
  151. {
  152. if (!isRecording) return; // 如果没有在录音,则直接返回
  153. // 停止录音
  154. Microphone.End(null);
  155. isRecording = false;
  156. Debug.Log("停止录音...");
  157. stopRecTime = DateTime.Now;
  158. TimeSpan duration = stopRecTime - startRecTime;
  159. if (duration.TotalSeconds < 0.5) return; // 如果录音时间小于0.5秒,则不保存文件
  160. // 保存录音为WAV文件和ZIP文件
  161. SaveWavFile(filePathWav, audioClip);
  162. Debug.Log("录音已保存到: " + filePathWav);
  163. ZipFileController.ZipFile(filePathWav, filePathZip);
  164. // 打印zip文件大小
  165. FileInfo fileInfo = new FileInfo(filePathZip);
  166. Debug.Log("zip文件大小: " + fileInfo.Length / 1024 + "KB");
  167. if (isCommandMode)
  168. {
  169. // command模式,调用Home Controller Command的方法上传音频文件
  170. HomeController.Instance.VoiceCommandRequest(filePathZip);
  171. }
  172. else
  173. {
  174. // 非command模式,调用Home Controller Call的方法上传音频文件
  175. HomeController.Instance.VoiceCallRequest(filePathZip);
  176. }
  177. }
  178. // 保存音频文件wav
  179. void SaveWavFile(string path, AudioClip audio)
  180. {
  181. // 创建文件流
  182. using (FileStream fileStream = new(path, FileMode.Create))
  183. {
  184. using (BinaryWriter writer = new BinaryWriter(fileStream))
  185. {
  186. // 写入WAV文件头
  187. writer.Write(new char[4] { 'R', 'I', 'F', 'F' }); // RIFF标志
  188. writer.Write(36 + audio.samples * 2); // 文件大小
  189. writer.Write(new char[4] { 'W', 'A', 'V', 'E' }); // WAVE标志
  190. writer.Write(new char[4] { 'f', 'm', 't', ' ' }); // fmt标志
  191. writer.Write(16); // fmt块大小
  192. writer.Write((ushort)1); // 音频格式(PCM)
  193. writer.Write((ushort)audio.channels); // 声道数
  194. writer.Write(audio.frequency); // 采样率
  195. writer.Write(audio.frequency * audio.channels * 2); // 字节率
  196. writer.Write((ushort)(audio.channels * 2)); // 块对齐
  197. writer.Write((ushort)16); // 位深度
  198. writer.Write(new char[4] { 'd', 'a', 't', 'a' }); // data标志
  199. writer.Write(audio.samples * 2); // 数据大小
  200. // 写入音频数据
  201. float[] samples = new float[audio.samples * audio.channels];
  202. audio.GetData(samples, 0);
  203. for (int i = 0; i < samples.Length; i++)
  204. {
  205. writer.Write((short)(samples[i] * short.MaxValue)); // 将浮点数转换为16位整数
  206. }
  207. }
  208. }
  209. }
  210. #endregion
  211. }