Unity3d中MicroPhone的使用

Unity3d中MicroPhone的使用

目标实现录音功能、保存为wav、播放、保存为int16[]发送网络数据流、接收Int16[]播放数据流


MicroPhoneInput类


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using UnityEngine;  
  7. using System.Collections;  
  8. [RequireComponent (typeof(AudioSource))]  
  9.   
  10. public class MicroPhoneInput : MonoBehaviour {  
  11.   
  12.     private static MicroPhoneInput m_instance;  
  13.   
  14.     public float sensitivity=100;  
  15.     public float loudness=0;  
  16.   
  17.     private static string[] micArray=null;  
  18.   
  19.     const int HEADER_SIZE = 44;  
  20.   
  21.     const int RECORD_TIME = 10;  
  22.   
  23.     // Use this for initialization   
  24.     void Start () {  
  25.     }  
  26.   
  27.     public static MicroPhoneInput getInstance()  
  28.     {  
  29.         if (m_instance == null)   
  30.         {  
  31.             micArray = Microphone.devices;  
  32.             if (micArray.Length == 0)  
  33.             {  
  34.                 Debug.LogError ("Microphone.devices is null");  
  35.             }  
  36.             foreach (string deviceStr in Microphone.devices)  
  37.             {  
  38.                 Debug.Log("device name = " + deviceStr);  
  39.             }  
  40.             if(micArray.Length==0)  
  41.             {  
  42.                 Debug.LogError("no mic device");  
  43.             }  
  44.   
  45.             GameObject MicObj=new GameObject("MicObj");  
  46.             m_instance= MicObj.AddComponent<MicroPhoneInput>();  
  47.         }  
  48.         return m_instance;  
  49.     }  
  50.   
  51.     void OnGUI()  
  52.         //GUI.Label(new Rect(10,10,200,100),"loudness = "+loudness);   
  53.         //GUI.Label(new Rect(10,210,200,100),"Microphone.GetPosition = "+Microphone.GetPosition(null));   
  54.     }  
  55.   
  56.     public void StartRecord()  
  57.     {  
  58.         audio.Stop();  
  59.         if (micArray.Length == 0)  
  60.         {  
  61.             Debug.Log("No Record Device!");  
  62.             return;  
  63.         }  
  64.         audio.loop = false;  
  65.         audio.mute = true;  
  66.         audio.clip = Microphone.Start(nullfalse, RECORD_TIME, 44100); //22050    
  67.         while (!(Microphone.GetPosition(null)>0)) {  
  68.         }  
  69.         audio.Play ();  
  70.         Debug.Log("StartRecord");  
  71.         //倒计时   
  72.         StartCoroutine(TimeDown());  
  73.          
  74.     }  
  75.   
  76.     public  void StopRecord()  
  77.     {  
  78.         if (micArray.Length == 0)  
  79.         {  
  80.             Debug.Log("No Record Device!");  
  81.             return;  
  82.         }  
  83.         if (!Microphone.IsRecording(null))  
  84.         {  
  85.             return;  
  86.         }  
  87.         Microphone.End (null);  
  88.         audio.Stop();  
  89.   
  90.         Debug.Log("StopRecord");  
  91.        // PlayRecord();   
  92.   
  93.         //调试Int16[] 数据的转化与播放   
  94.         //PlayClipData(GetClipData());   
  95.   
  96.     }  
  97.   
  98.     public Byte[] GetClipData()  
  99.     {  
  100.         if (audio.clip == null)  
  101.         {  
  102.             Debug.Log("GetClipData audio.clip is null");  
  103.             return null;   
  104.         }  
  105.   
  106.         float[] samples = new float[audio.clip.samples];  
  107.   
  108.         audio.clip.GetData(samples, 0);  
  109.   
  110.   
  111.         Byte[] outData = new byte[samples.Length * 2];  
  112.         //Int16[] intData = new Int16[samples.Length];   
  113.         //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]   
  114.   
  115.         int rescaleFactor = 32767; //to convert float to Int16   
  116.   
  117.         for (int i = 0; i < samples.Length; i++)  
  118.         {  
  119.             short temshort = (short)(samples[i] * rescaleFactor);  
  120.   
  121.             Byte[] temdata=System.BitConverter.GetBytes(temshort);  
  122.   
  123.             outData[i*2]=temdata[0];  
  124.             outData[i*2+1]=temdata[1];  
  125.   
  126.   
  127.         }  
  128.         if (outData == null || outData.Length <= 0)  
  129.         {  
  130.             Debug.Log("GetClipData intData is null");  
  131.             return null;   
  132.         }  
  133.         //return intData;   
  134.         return outData;  
  135.     }  
  136.     public void PlayClipData(Int16[] intArr)  
  137.     {  
  138.   
  139.         string aaastr = intArr.ToString();  
  140.         long  aaalength=aaastr.Length;  
  141.         Debug.LogError("aaalength=" + aaalength);  
  142.   
  143.         string aaastr1 = Convert.ToString (intArr);  
  144.         aaalength = aaastr1.Length;  
  145.         Debug.LogError("aaalength=" + aaalength);  
  146.   
  147.         if (intArr.Length == 0)  
  148.         {  
  149.             Debug.Log("get intarr clipdata is null");  
  150.             return;  
  151.         }  
  152.         //从Int16[]到float[]   
  153.         float[] samples = new float[intArr.Length];  
  154.         int rescaleFactor = 32767;  
  155.         for (int i = 0; i < intArr.Length; i++)  
  156.         {  
  157.             samples[i] = (float)intArr[i] / rescaleFactor;  
  158.         }  
  159.           
  160.         //从float[]到Clip   
  161.         AudioSource audioSource = this.GetComponent<AudioSource>();  
  162.         if (audioSource.clip == null)  
  163.         {  
  164.             audioSource.clip = AudioClip.Create("playRecordClip", intArr.Length, 1, 44100, falsefalse);  
  165.         }  
  166.         audioSource.clip.SetData(samples, 0);  
  167.         audioSource.mute = false;  
  168.         audioSource.Play();  
  169.     }PlayRecord()  
  170.     {  
  171.         if (audio.clip == null)  
  172.         {  
  173.             Debug.Log("audio.clip=null");  
  174.             return;  
  175.         }  
  176.         audio.mute = false;  
  177.         audio.loop = false;  
  178.         audio.Play ();  
  179.         Debug.Log("PlayRecord");  
  180.   
  181.     }  
  182.   
  183.     public void LoadAndPlayRecord()  
  184.     {  
  185.         string recordPath ="your path";  
  186.   
  187.         SavWav.LoadAndPlay (recordPath);  
  188.     }  
  189.   
  190.   
  191.     public  float GetAveragedVolume()  
  192.     {  
  193.         float[] data=new float[256];  
  194.         float a=0;  
  195.         audio.GetOutputData(data,0);  
  196.         foreach(float s in data)  
  197.         {  
  198.             a+=Mathf.Abs(s);  
  199.         }  
  200.         return a/256;  
  201.     }  
  202.       
  203.     // Update is called once per frame   
  204.     void Update ()  
  205.     {  
  206.         loudness = GetAveragedVolume () * sensitivity;  
  207.         if (loudness > 1)   
  208.         {  
  209.             Debug.Log("loudness = "+loudness);  
  210.         }  
  211.     }  
  212.   
  213.     private IEnumerator TimeDown()  
  214.     {  
  215.         Debug.Log(" IEnumerator TimeDown()");  
  216.   
  217.         int time = 0;  
  218.         while (time < RECORD_TIME)  
  219.         {  
  220.             if (!Microphone.IsRecording (null))   
  221.             { //如果没有录制   
  222.                 Debug.Log ("IsRecording false");  
  223.                 yield break;  
  224.             }  
  225.             Debug.Log("yield return new WaitForSeconds "+time);  
  226.             yield return new WaitForSeconds(1);  
  227.             time++;  
  228.         }  
  229.         if (time >= 10)  
  230.         {  
  231.             Debug.Log("RECORD_TIME is out! stop record!");  
  232.             StopRecord();  
  233.         }  
  234.         yield return 0;  
  235.     }  
  236. }  

目标实现录音功能、保存为wav、播放、保存为int16[]发送网络数据流、接收Int16[]播放数据流


MicroPhoneInput类


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using UnityEngine;  
  7. using System.Collections;  
  8. [RequireComponent (typeof(AudioSource))]  
  9.   
  10. public class MicroPhoneInput : MonoBehaviour {  
  11.   
  12.     private static MicroPhoneInput m_instance;  
  13.   
  14.     public float sensitivity=100;  
  15.     public float loudness=0;  
  16.   
  17.     private static string[] micArray=null;  
  18.   
  19.     const int HEADER_SIZE = 44;  
  20.   
  21.     const int RECORD_TIME = 10;  
  22.   
  23.     // Use this for initialization   
  24.     void Start () {  
  25.     }  
  26.   
  27.     public static MicroPhoneInput getInstance()  
  28.     {  
  29.         if (m_instance == null)   
  30.         {  
  31.             micArray = Microphone.devices;  
  32.             if (micArray.Length == 0)  
  33.             {  
  34.                 Debug.LogError ("Microphone.devices is null");  
  35.             }  
  36.             foreach (string deviceStr in Microphone.devices)  
  37.             {  
  38.                 Debug.Log("device name = " + deviceStr);  
  39.             }  
  40.             if(micArray.Length==0)  
  41.             {  
  42.                 Debug.LogError("no mic device");  
  43.             }  
  44.   
  45.             GameObject MicObj=new GameObject("MicObj");  
  46.             m_instance= MicObj.AddComponent<MicroPhoneInput>();  
  47.         }  
  48.         return m_instance;  
  49.     }  
  50.   
  51.     void OnGUI()  
  52.         //GUI.Label(new Rect(10,10,200,100),"loudness = "+loudness);   
  53.         //GUI.Label(new Rect(10,210,200,100),"Microphone.GetPosition = "+Microphone.GetPosition(null));   
  54.     }  
  55.   
  56.     public void StartRecord()  
  57.     {  
  58.         audio.Stop();  
  59.         if (micArray.Length == 0)  
  60.         {  
  61.             Debug.Log("No Record Device!");  
  62.             return;  
  63.         }  
  64.         audio.loop = false;  
  65.         audio.mute = true;  
  66.         audio.clip = Microphone.Start(nullfalse, RECORD_TIME, 44100); //22050    
  67.         while (!(Microphone.GetPosition(null)>0)) {  
  68.         }  
  69.         audio.Play ();  
  70.         Debug.Log("StartRecord");  
  71.         //倒计时   
  72.         StartCoroutine(TimeDown());  
  73.          
  74.     }  
  75.   
  76.     public  void StopRecord()  
  77.     {  
  78.         if (micArray.Length == 0)  
  79.         {  
  80.             Debug.Log("No Record Device!");  
  81.             return;  
  82.         }  
  83.         if (!Microphone.IsRecording(null))  
  84.         {  
  85.             return;  
  86.         }  
  87.         Microphone.End (null);  
  88.         audio.Stop();  
  89.   
  90.         Debug.Log("StopRecord");  
  91.        // PlayRecord();   
  92.   
  93.         //调试Int16[] 数据的转化与播放   
  94.         //PlayClipData(GetClipData());   
  95.   
  96.     }  
  97.   
  98.     public Byte[] GetClipData()  
  99.     {  
  100.         if (audio.clip == null)  
  101.         {  
  102.             Debug.Log("GetClipData audio.clip is null");  
  103.             return null;   
  104.         }  
  105.   
  106.         float[] samples = new float[audio.clip.samples];  
  107.   
  108.         audio.clip.GetData(samples, 0);  
  109.   
  110.   
  111.         Byte[] outData = new byte[samples.Length * 2];  
  112.         //Int16[] intData = new Int16[samples.Length];   
  113.         //converting in 2 float[] steps to Int16[], //then Int16[] to Byte[]   
  114.   
  115.         int rescaleFactor = 32767; //to convert float to Int16   
  116.   
  117.         for (int i = 0; i < samples.Length; i++)  
  118.         {  
  119.             short temshort = (short)(samples[i] * rescaleFactor);  
  120.   
  121.             Byte[] temdata=System.BitConverter.GetBytes(temshort);  
  122.   
  123.             outData[i*2]=temdata[0];  
  124.             outData[i*2+1]=temdata[1];  
  125.   
  126.   
  127.         }  
  128.         if (outData == null || outData.Length <= 0)  
  129.         {  
  130.             Debug.Log("GetClipData intData is null");  
  131.             return null;   
  132.         }  
  133.         //return intData;   
  134.         return outData;  
  135.     }  
  136.     public void PlayClipData(Int16[] intArr)  
  137.     {  
  138.   
  139.         string aaastr = intArr.ToString();  
  140.         long  aaalength=aaastr.Length;  
  141.         Debug.LogError("aaalength=" + aaalength);  
  142.   
  143.         string aaastr1 = Convert.ToString (intArr);  
  144.         aaalength = aaastr1.Length;  
  145.         Debug.LogError("aaalength=" + aaalength);  
  146.   
  147.         if (intArr.Length == 0)  
  148.         {  
  149.             Debug.Log("get intarr clipdata is null");  
  150.             return;  
  151.         }  
  152.         //从Int16[]到float[]   
  153.         float[] samples = new float[intArr.Length];  
  154.         int rescaleFactor = 32767;  
  155.         for (int i = 0; i < intArr.Length; i++)  
  156.         {  
  157.             samples[i] = (float)intArr[i] / rescaleFactor;  
  158.         }  
  159.           
  160.         //从float[]到Clip   
  161.         AudioSource audioSource = this.GetComponent<AudioSource>();  
  162.         if (audioSource.clip == null)  
  163.         {  
  164.             audioSource.clip = AudioClip.Create("playRecordClip", intArr.Length, 1, 44100, falsefalse);  
  165.         }  
  166.         audioSource.clip.SetData(samples, 0);  
  167.         audioSource.mute = false;  
  168.         audioSource.Play();  
  169.     }PlayRecord()  
  170.     {  
  171.         if (audio.clip == null)  
  172.         {  
  173.             Debug.Log("audio.clip=null");  
  174.             return;  
  175.         }  
  176.         audio.mute = false;  
  177.         audio.loop = false;  
  178.         audio.Play ();  
  179.         Debug.Log("PlayRecord");  
  180.   
  181.     }  
  182.   
  183.     public void LoadAndPlayRecord()  
  184.     {  
  185.         string recordPath ="your path";  
  186.   
  187.         SavWav.LoadAndPlay (recordPath);  
  188.     }  
  189.   
  190.   
  191.     public  float GetAveragedVolume()  
  192.     {  
  193.         float[] data=new float[256];  
  194.         float a=0;  
  195.         audio.GetOutputData(data,0);  
  196.         foreach(float s in data)  
  197.         {  
  198.             a+=Mathf.Abs(s);  
  199.         }  
  200.         return a/256;  
  201.     }  
  202.       
  203.     // Update is called once per frame   
  204.     void Update ()  
  205.     {  
  206.         loudness = GetAveragedVolume () * sensitivity;  
  207.         if (loudness > 1)   
  208.         {  
  209.             Debug.Log("loudness = "+loudness);  
  210.         }  
  211.     }  
  212.   
  213.     private IEnumerator TimeDown()  
  214.     {  
  215.         Debug.Log(" IEnumerator TimeDown()");  
  216.   
  217.         int time = 0;  
  218.         while (time < RECORD_TIME)  
  219.         {  
  220.             if (!Microphone.IsRecording (null))   
  221.             { //如果没有录制   
  222.                 Debug.Log ("IsRecording false");  
  223.                 yield break;  
  224.             }  
  225.             Debug.Log("yield return new WaitForSeconds "+time);  
  226.             yield return new WaitForSeconds(1);  
  227.             time++;  
  228.         }  
  229.         if (time >= 10)  
  230.         {  
  231.             Debug.Log("RECORD_TIME is out! stop record!");  
  232.             StopRecord();  
  233.         }  
  234.         yield return 0;  
  235.     }  
  236. }  

猜你喜欢

转载自blog.csdn.net/summerhust/article/details/45219683