123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System;
- using System.Collections;
- using System.IO;
- using UnityEngine;
- using UnityEngine.UIElements;
- using System.Runtime.InteropServices;
- public class VoiceController : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- private VisualElement waveForm;
- private Button voiceBtn;
- private Coroutine waveCoroutine;
- private AudioClip audioClip; // 保存的音频
- private bool isRecording = false;
- private string filePathWav, filePathZip;
- void Start()
- {
- var root = GetComponent<UIDocument>().rootVisualElement;
- var voiceArea = root.Q<VisualElement>("voiceArea");
- waveForm = voiceArea.Q<VisualElement>("waveForm");
- voiceBtn = voiceArea.Q<Button>("voice");
- // root加载完成后,所有element算出位置后再进行计算
- root.RegisterCallback<GeometryChangedEvent>(e => OnMainMenuClickGeometryChanged(e));
- voiceBtn.RegisterCallback<PointerDownEvent>(e => VoiceBtnPointerDown(e), TrickleDown.TrickleDown); // TrickleDown.TrickleDown参数,确保事件在捕获阶段优先处理。
- voiceBtn.RegisterCallback<PointerUpEvent>(e => VoiceBtnPointerUp(e));
- }
- // Update is called once per frame
- //void Update()
- //{
-
- //}
- IEnumerator WaveFormAnimation()
- {
- while (true)
- {
- waveForm.style.height = UnityEngine.Random.Range(15f, 30f);
- float alpha = UnityEngine.Random.Range(0.6f, 1f);
- waveForm.style.unityBackgroundImageTintColor = new Color(1f, 1f, 1f, alpha);
- yield return new WaitForSeconds(0.1f);
- }
- }
- void OnMainMenuClickGeometryChanged(GeometryChangedEvent evt)
- {
- if (waveCoroutine == null)
- {
- waveCoroutine = StartCoroutine(WaveFormAnimation());
- }
- }
- // 语言控制按键按下的触发事件
- void VoiceBtnPointerDown(PointerDownEvent evt)
- {
- Debug.Log("voice button pointer down.");
- StartRecording();
- waveForm.visible = true;
- }
- // 语音控制按键松开事件
- void VoiceBtnPointerUp(PointerUpEvent evt)
- {
- Debug.Log("voice button pointer up.");
- StopRecording();
- waveForm.visible = false;
- }
- // 开始录音
- void StartRecording()
- {
- if (isRecording) return; // 如果已经在录音,则不再重复开始
- // 设置录音文件名和路径
- filePathWav = Path.Combine(Application.persistentDataPath, "voiceCommand.wav");
- filePathZip = Path.Combine(Application.persistentDataPath, "voiceCommand.mp3");
- // 检测目录是否存在不存在就创建
- string directoryPath = Path.GetDirectoryName(filePathWav);
- if (!Directory.Exists(directoryPath))
- {
- Directory.CreateDirectory(directoryPath);
- }
- //删除旧文件
- if (File.Exists(filePathWav))
- {
- File.Delete(filePathWav);
- }
- if (File.Exists(filePathZip))
- {
- File.Delete(filePathZip);
- }
- // 开始录音,最长4秒
- audioClip = Microphone.Start(null, false, 4, 44100);
- isRecording = true;
- Debug.Log("开始录音...");
- }
- // 停止录音
- void StopRecording()
- {
- if (!isRecording) return; // 如果没有在录音,则直接返回
- // 停止录音
- Microphone.End(null);
- isRecording = false;
- Debug.Log("停止录音...");
- // 保存录音为WAV文件
- SaveWavFile(filePathWav, audioClip);
- Debug.Log("录音已保存到: " + filePathWav);
- ZipFileController.ZipFile(filePathWav, filePathZip);
- }
- // 保存音频文件wav
- void SaveWavFile(string path, AudioClip audio) {
- // 创建文件流
- using (FileStream fileStream = new(path, FileMode.Create))
- {
- using (BinaryWriter writer = new BinaryWriter(fileStream))
- {
- // 写入WAV文件头
- writer.Write(new char[4] { 'R', 'I', 'F', 'F' }); // RIFF标志
- writer.Write(36 + audio.samples * 2); // 文件大小
- writer.Write(new char[4] { 'W', 'A', 'V', 'E' }); // WAVE标志
- writer.Write(new char[4] { 'f', 'm', 't', ' ' }); // fmt标志
- writer.Write(16); // fmt块大小
- writer.Write((ushort)1); // 音频格式(PCM)
- writer.Write((ushort)audio.channels); // 声道数
- writer.Write(audio.frequency); // 采样率
- writer.Write(audio.frequency * audio.channels * 2); // 字节率
- writer.Write((ushort)(audio.channels * 2)); // 块对齐
- writer.Write((ushort)16); // 位深度
- writer.Write(new char[4] { 'd', 'a', 't', 'a' }); // data标志
- writer.Write(audio.samples * 2); // 数据大小
- // 写入音频数据
- float[] samples = new float[audio.samples * audio.channels];
- audio.GetData(samples, 0);
- for (int i = 0; i < samples.Length; i++)
- {
- writer.Write((short)(samples[i] * short.MaxValue)); // 将浮点数转换为16位整数
- }
- }
- }
- }
- }
|