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))]
2、 public class MicroPhoneInput : MonoBehaviour { private static MicroPhoneInput m_instance; public float sensitivity=100; public float loudness=0; private static string[] micArray=null; const int HEADER_SIZE = 44;
3、 const int RECORD_TIME = 10; // Use this for initialization void Start () { } public static MicroPhoneInput getInstance() { if (m_instance == null) { micArray
4、 = Microphone.devices; if (micArray.Length == 0) { Debug.LogError ("Microphone.devices is null"); } foreach (string deviceStr in Microphone.devices)
5、 { Debug.Log("device name = " + deviceStr); } if(micArray.Length==0) { Debug.LogError("no mic device"); }
6、
GameObject MicObj=new GameObject("MicObj");
m_instance= MicObj.AddComponent
7、),"loudness = "+loudness); //GUI.Label(new Rect(10,210,200,100),"Microphone.GetPosition = "+Microphone.GetPosition(null)); } public void StartRecord() { audio.Stop(); if (micArray.Length == 0) {
8、 Debug.Log("No Record Device!"); return; } audio.loop = false; audio.mute = true; audio.clip = Microphone.Start(null, false, RECORD_TIME, 44100); //22050 while (!(Microphone
9、GetPosition(null)>0)) { } audio.Play (); Debug.Log("StartRecord"); //倒计时 StartCoroutine(TimeDown()); } public void StopRecord() { if (micArray.Length
10、 == 0) { Debug.Log("No Record Device!"); return; } if (!Microphone.IsRecording(null)) { return; } Microphone.End (null);
11、 audio.Stop(); Debug.Log("StopRecord"); // PlayRecord(); //调试Int16[] 数据的转化与播放 //PlayClipData(GetClipData()); } public Byte[] GetClipData()
12、 { if (audio.clip == null) { Debug.Log("GetClipData audio.clip is null"); return null; } float[] samples = new float[audio.clip.samples];
13、 audio.clip.GetData(samples, 0); Byte[] outData = new byte[samples.Length * 2]; //Int16[] intData = new Int16[samples.Length]; //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]
14、 int rescaleFactor = 32767; //to convert float to Int16 for (int i = 0; i < samples.Length; i++) { short temshort = (short)(samples[i] * rescaleFactor);
15、 Byte[] temdata=System.BitConverter.GetBytes(temshort); outData[i*2]=temdata[0]; outData[i*2+1]=temdata[1]; } if (outData == null
16、 || outData.Length <= 0)
{
Debug.Log("GetClipData intData is null");
return null;
}
//return intData;
return outData;
}
///
17、
///
///
18、ream fs = new FileStream(inFile, FileMode.Open); byte[] voice = new byte[fs.Length]; fs.Read(voice, 0, voice.Length); fs.Close(); */ HttpWebRequest request = null; strin
19、g url = " Uri uri = new Uri(url); request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "POST"; request.ContentType = "audio/x-flac; rate=16000"; request.Con
20、tentLength = voice.Length; using (Stream writeStream = request.GetRequestStream()) { writeStream.Write(voice, 0, voice.Length); } using
21、HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader readStream
22、 new StreamReader(responseStream, Encoding.UTF8)) { result = readStream.ReadToEnd(); Debug.Log("result="+result); }
23、 } } } catch (Exception ex) { //Console.WriteLine(ex.StackTrace); Debug.Log(ex.StackTrace); } return
24、 result; } public void PlayClipData(Int16[] intArr) { string aaastr = intArr.ToString(); long aaalength=aaastr.Length; Debug.LogError("aaalength=" + aaalength); stri
25、ng aaastr1 = Convert.ToString (intArr); aaalength = aaastr1.Length; Debug.LogError("aaalength=" + aaalength); if (intArr.Length == 0) { Debug.Log("get intarr clipdata is null");
26、 return; } //从Int16[]到float[] float[] samples = new float[intArr.Length]; int rescaleFactor = 32767; for (int i = 0; i < intArr.Length; i++) { samples[i] = (flo
27、at)intArr[i] / rescaleFactor;
}
//从float[]到Clip
AudioSource audioSource = this.GetComponent
28、ate("playRecordClip", intArr.Length, 1, 44100, false, false); } audioSource.clip.SetData(samples, 0); audioSource.mute = false; audioSource.Play(); } public void PlayRecord() { if (audio.c
29、lip == null) { Debug.Log("audio.clip=null"); return; } audio.mute = false; audio.loop = false; audio.Play (); Debug.Log("PlayRecord");
30、 } public void LoadAndPlayRecord() { string recordPath ="your path"; //SavWav.LoadAndPlay (recordPath); } public float GetAveragedVolume() { float[] data=new float[25
31、6]; float a=0; audio.GetOutputData(data,0); foreach(float s in data) { a+=Mathf.Abs(s); } return a/256; } // Update is called once per frame voi
32、d Update () { loudness = GetAveragedVolume () * sensitivity; if (loudness > 1) { Debug.Log("loudness = "+loudness); } } private IEnumerator TimeDown() {
33、 Debug.Log(" IEnumerator TimeDown()"); int time = 0; while (time < RECORD_TIME) { if (!Microphone.IsRecording (null)) { //如果没有录制 Debug.Log ("IsRec
34、ording false"); yield break; } Debug.Log("yield return new WaitForSeconds "+time); yield return new WaitForSeconds(1); time++; } if (time >= 10) { Debug.Log("RECORD_TIME is out! stop record!"); StopRecord(); } yield return 0; } } 更多参考资料见我学院在线平台: Unity3D|Cocos|php|HTML5|Java|ios|Android|C#|AS3|UI设计|原画设计|动漫美术|游戏策划






