《我学院:Unity麦克风录制声音脚本.docx》由会员分享,可在线阅读,更多相关《我学院:Unity麦克风录制声音脚本.docx(12页珍藏版)》请在金锄头文库上搜索。
1、学游戏就上我学院Unity麦克风录制声音脚本/使用麦克风可以录制音频流,但是如何将录制的音频流通过代码的形式改成3d音效如果有知道的朋友情不吝赐教using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using UnityEngine;using System.Collections;using System.Net;RequireComponent (typeof(AudioSource)public class MicroPhoneInput : M
2、onoBehaviour private static MicroPhoneInput m_instance; public float sensitivity=100; public float loudness=0; private static string micArray=null; const int HEADER_SIZE = 44; const int RECORD_TIME = 10; / Use this for initialization void Start () public static MicroPhoneInput getInstance() if (m_in
3、stance = null) micArray = Microphone.devices; if (micArray.Length = 0) Debug.LogError (Microphone.devices is null); foreach (string deviceStr in Microphone.devices) Debug.Log(device name = + deviceStr); if(micArray.Length=0) Debug.LogError(no mic device); GameObject MicObj=new GameObject(MicObj); m_
4、instance= MicObj.AddComponent(); return m_instance; void OnGUI() /GUI.Label(new Rect(10,10,200,100),loudness = +loudness); /GUI.Label(new Rect(10,210,200,100),Microphone.GetPosition = +Microphone.GetPosition(null); public void StartRecord() audio.Stop(); if (micArray.Length = 0) Debug.Log(No Record
5、Device!); return; audio.loop = false; audio.mute = true; audio.clip = Microphone.Start(null, false, RECORD_TIME, 44100); /22050 while (!(Microphone.GetPosition(null)0) audio.Play (); Debug.Log(StartRecord); /倒计时 StartCoroutine(TimeDown(); publicvoid StopRecord() if (micArray.Length = 0) Debug.Log(No
6、 Record Device!); return; if (!Microphone.IsRecording(null) return; Microphone.End (null); audio.Stop(); Debug.Log(StopRecord); / PlayRecord(); /调试Int16 数据的转化与播放 /PlayClipData(GetClipData(); public Byte GetClipData() if (audio.clip = null) Debug.Log(GetClipData audio.clip is null); return null; floa
7、t samples = new floataudio.clip.samples; audio.clip.GetData(samples, 0); Byte outData = new bytesamples.Length * 2; /Int16 intData = new Int16samples.Length; /converting in 2 float steps to Int16, /then Int16 to Byte int rescaleFactor = 32767; /to convert float to Int16 for (int i = 0; i samples.Len
8、gth; i+) short temshort = (short)(samplesi * rescaleFactor); Byte temdata=System.BitConverter.GetBytes(temshort); outDatai*2=temdata0; outDatai*2+1=temdata1; if (outData = null | outData.Length = 0) Debug.Log(GetClipData intData is null); return null; /return intData; return outData; / / 调用GOOLE语音识别引擎 / / private string GoogleSTT(byte voice) string result = string.Empty;