VoiceController.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. StopRecording();
  114. }
  115. #region 录音相关
  116. // 开始录音
  117. void StartRecording()
  118. {
  119. if (isRecording) return; // 如果已经在录音,则不再重复开始
  120. // 设置录音文件名和路径
  121. filePathWav = Path.Combine(Application.persistentDataPath, "voice.wav");
  122. filePathZip = Path.Combine(Application.persistentDataPath, "voice.zip");
  123. // 检测目录是否存在不存在就创建
  124. string directoryPath = Path.GetDirectoryName(filePathWav);
  125. if (!Directory.Exists(directoryPath))
  126. {
  127. Directory.CreateDirectory(directoryPath);
  128. }
  129. //删除旧文件
  130. if (File.Exists(filePathWav))
  131. {
  132. File.Delete(filePathWav);
  133. }
  134. if (File.Exists(filePathZip))
  135. {
  136. File.Delete(filePathZip);
  137. }
  138. // 开始录音,最长8秒
  139. startRecTime = DateTime.Now;
  140. audioClip = Microphone.Start(null, false, 8, 44100);
  141. isRecording = true;
  142. Debug.Log("开始录音...");
  143. }
  144. // 停止录音
  145. void StopRecording()
  146. {
  147. if (!isRecording) return; // 如果没有在录音,则直接返回
  148. // 停止录音
  149. Microphone.End(null);
  150. isRecording = false;
  151. Debug.Log("停止录音...");
  152. stopRecTime = DateTime.Now;
  153. TimeSpan duration = stopRecTime - startRecTime;
  154. if (duration.TotalSeconds < 0.5) return; // 如果录音时间小于0.5秒,则不保存文件
  155. // 保存录音为WAV文件和ZIP文件
  156. SaveWavFile(filePathWav, audioClip);
  157. Debug.Log("录音已保存到: " + filePathWav);
  158. ZipFileController.ZipFile(filePathWav, filePathZip);
  159. // 打印zip文件大小
  160. FileInfo fileInfo = new FileInfo(filePathZip);
  161. Debug.Log("zip文件大小: " + fileInfo.Length / 1024 + "KB");
  162. if (isCommandMode)
  163. {
  164. // command模式,调用Home Controller Command的方法上传音频文件
  165. HomeController.Instance.VoiceCommandRequest(filePathZip);
  166. }
  167. else
  168. {
  169. // 非command模式,调用Home Controller Call的方法上传音频文件
  170. HomeController.Instance.VoiceCallRequest(filePathZip);
  171. }
  172. }
  173. // 保存音频文件wav
  174. void SaveWavFile(string path, AudioClip audio)
  175. {
  176. // 创建文件流
  177. using (FileStream fileStream = new(path, FileMode.Create))
  178. {
  179. using (BinaryWriter writer = new BinaryWriter(fileStream))
  180. {
  181. // 写入WAV文件头
  182. writer.Write(new char[4] { 'R', 'I', 'F', 'F' }); // RIFF标志
  183. writer.Write(36 + audio.samples * 2); // 文件大小
  184. writer.Write(new char[4] { 'W', 'A', 'V', 'E' }); // WAVE标志
  185. writer.Write(new char[4] { 'f', 'm', 't', ' ' }); // fmt标志
  186. writer.Write(16); // fmt块大小
  187. writer.Write((ushort)1); // 音频格式(PCM)
  188. writer.Write((ushort)audio.channels); // 声道数
  189. writer.Write(audio.frequency); // 采样率
  190. writer.Write(audio.frequency * audio.channels * 2); // 字节率
  191. writer.Write((ushort)(audio.channels * 2)); // 块对齐
  192. writer.Write((ushort)16); // 位深度
  193. writer.Write(new char[4] { 'd', 'a', 't', 'a' }); // data标志
  194. writer.Write(audio.samples * 2); // 数据大小
  195. // 写入音频数据
  196. float[] samples = new float[audio.samples * audio.channels];
  197. audio.GetData(samples, 0);
  198. for (int i = 0; i < samples.Length; i++)
  199. {
  200. writer.Write((short)(samples[i] * short.MaxValue)); // 将浮点数转换为16位整数
  201. }
  202. }
  203. }
  204. }
  205. #endregion
  206. }