usingSystem.Collections.Generic;
usingSystem.Linq;
usingUnityEngine;
usingSystem.Collections;
[RequireComponent(typeof(AudioSource))]
publicclassRecord:MonoBehaviour
{
publicAudioSourceaudio;
//每秒种产生0.4M数据量
privateLinkedList recordData =newLinkedList();
privateAudioClipmicrophoneClip;
privatefloatvoiceDetectLevel = 0.01f;
privatevoidStart()
{
startMicRecord();
}
privatevoidstartMicRecord()
{
recordData.Clear();
isRecordingMic =false;
microphoneClip =Microphone.Start(null,true, 600, 44100);
while(!(Microphone.GetPosition(null) > 0))
{
}
StartCoroutine("getMicData");
}
privateintlastReadPositio = 0;
privatefloatmyMax;
privateboolisRecordingMic =false;
privatefloatclipPlayTime;
IEnumeratorgetMicData()
{
yieldreturnnewWaitForSeconds(0.4f);
//todo 加载数据并分析
intcurrentPosition =Microphone.GetPosition(null);
intsz = currentPosition - lastReadPositio;
if(sz > 1)
{
float[] d =newfloat[(sz - 1)];
microphoneClip.GetData(d, lastReadPositio);
floatmaxValue = 0;
foreach(floatsind)
{
maxValue = maxValue >Mathf.Abs(s) ? maxValue :Mathf.Abs(s);
}
myMax = maxValue;
Debug.LogError(myMax);
if(isRecordingMic)
{
if(myMax < voiceDetectLevel)//每个采集数据的最大值与接近无声音比较来是否放进采集队列里面。
{
//nobody is talking.
//todo maybe to relay the recored data.
StartCoroutine("replayRecoredData");
//Loom.DispatchToMainThread(replayRecoredData);
StopCoroutine("getMicData");
}
foreach(floatfind)
{
recordData.AddLast(f);
}
}
else
{
if(myMax > voiceDetectLevel)
{
isRecordingMic =true;
recordData.Clear();
foreach(floatfind)
{
recordData.AddLast(f);
}
}
}
}
lastReadPositio = currentPosition;
StartCoroutine("getMicData");
}
///
///采集数据 生成clip ,设置属性 并播放
///
publicvoidreplayRecoredData()
{
Microphone.End(null);
float[] clipFloats =newfloat[recordData.Count];
inti = 0;
foreach(varfinrecordData)
{
clipFloats[i] = f;
i++;
}
AudioClipmyClip =AudioClip.Create("tom", clipFloats.Length, 1, 44100,false,false);
myClip.SetData(clipFloats, 0);
audio.clip = myClip;
audio.Play();
audio.loop =false;
//audio.pitch = 1.2f;
clipPlayTime = clipFloats.Length / 44100.0f;
StartCoroutine("waitToRestartMic");
//Loom.StartSingleThread(waitToRestartMic);
}
///
///等待播放时间结束: 后重新录
///
///
IEnumeratorwaitToRestartMic()
{
yieldreturnnewWaitForSeconds(clipPlayTime);
//Loom.WaitForSeconds(clipPlayTime);
startMicRecord();
}
}
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Record : MonoBehaviour
{
public AudioSource audio;
//每秒种产生0.4M数据量
private LinkedList recordData = new LinkedList();
private AudioClip microphoneClip;
private float voiceDetectLevel = 0.01f;
private void Start()
{
startMicRecord();
}
private void startMicRecord()
{
recordData.Clear();
isRecordingMic = false;
microphoneClip = Microphone.Start(null, true, 600, 44100);
while (!(Microphone.GetPosition(null) > 0))
{
}
StartCoroutine("getMicData");
}
private int lastReadPositio = 0;
private float myMax;
private bool isRecordingMic = false;
private float clipPlayTime;
IEnumerator getMicData()
{
yield return new WaitForSeconds(0.4f);
//todo加载数据并分析
int currentPosition = Microphone.GetPosition(null);
int sz = currentPosition - lastReadPositio;
if (sz > 1)
{
float[] d = new float[(sz - 1)];
microphoneClip.GetData(d, lastReadPositio);
float maxValue = 0;
foreach (float s in d)
{
maxValue = maxValue > Mathf.Abs(s) ? maxValue : Mathf.Abs(s);
}
myMax = maxValue;
Debug.LogError(myMax);
if (isRecordingMic)
{
if (myMax < voiceDetectLevel) //每个采集数据的最大值与接近无声音比较来是否放进采集队列里面。
{
//nobody is talking.
//todo maybe to relay the recored data.
StartCoroutine("replayRecoredData");
//Loom.DispatchToMainThread(replayRecoredData);
StopCoroutine("getMicData");
}
foreach (float f in d)
{
recordData.AddLast(f);
}
}
else
{
if (myMax > voiceDetectLevel)
{
isRecordingMic = true;
recordData.Clear();
foreach (float f in d)
{
recordData.AddLast(f);
}
}
}
}
lastReadPositio = currentPosition;
StartCoroutine("getMicData");
}
///
///采集数据 生成clip,设置属性 并播放
///
public void replayRecoredData()
{
Microphone.End(null);
float[] clipFloats = new float[recordData.Count];
int i = 0;
foreach (var f in recordData)
{
clipFloats[i] = f;
i++;
}
AudioClip myClip = AudioClip.Create("tom", clipFloats.Length, 1, 44100, false, false);
myClip.SetData(clipFloats, 0);
audio.clip = myClip;
audio.Play();
audio.loop = false;
//audio.pitch = 1.2f;
clipPlayTime = clipFloats.Length / 44100.0f;
StartCoroutine("waitToRestartMic");
//Loom.StartSingleThread(waitToRestartMic);
}
///
///等待播放时间结束: 后重新录
///
///
IEnumerator waitToRestartMic()
{
yield return new WaitForSeconds(clipPlayTime);
//Loom.WaitForSeconds(clipPlayTime);
startMicRecord();
}
}
网友评论