Hello, i need call somthing metod if player say something in microphone.
I have a static class to work with a microphone.
Every second I start to record from the microphone
private static AudioClip _clip;
public static void StartRecord()
{
if(Microphone.devices.Length > 0)
{
_clip = Microphone.Start ( null, false, 1, 44100);
}
}
Then turn off the recording and take a sample
Microphone.End(null);
_clip.GetData(waveData, 0);
Then find the maximum volume for this time
float levelMax = 0;
for (int i = 0; i < _dec; i++)
{
float wavePeak = waveData _* waveData*;*_
* if (levelMax < wavePeak)*
* {*
* levelMax = wavePeak;*
* }*
}
return Mathf.Sqrt(levelMax);
And repeat this procedure every second
Everything works well, but the sound that I recorded not erased and stored.
And every second I have an extra 100 kb + one additional audioclip in stroage. (total i have 44.5 mb)
How to delete the data, and store only the actual sample?
I know this is a very old threat, but for everyone having still problems with this:
I had the same problem (clearing Microphone Data from memory) while programming a Musicvisualizer thats made to run 24/7.
If making a seperate Audioclip, I got an Unassigned reference Exeption after destroying it. But I found a solution in creating a Audiosource.clip instead of an Audioclip.
If you do: Destroy(Audiosource.clip);
and then call Audiosource.clip = Microphone.Start(null, false, 1, 44100);
it will remove the old clip from your memory and instanciate a new one.
Try Destroy()ing the previous clip before assigning a new clip.
You assign a reference to the clip variable with the line
_clip = Microphone.Start ( null, false, 1, 44100);
but you never explicitly destroy the clip that was previously referenced. There are some things like Materials (and I believe clips) that are not destroyed when you remove a reference to them. You have to do it explicitly.
All Code in static class MicMaster
public static void StartRecord()
{
if(Microphone.devices.Length > 0)
{
_clip = Microphone.Start ( null, false, 1, 44100);
}
else
{
Debug.Log("Mic is missing!");
}
}
public static float StopAndGetVolume()
{
if(Microphone.devices.Length > 0)
{
Microphone.End(null);
float[] waveData = new float[_dec];
_clip.GetData(waveData, 0);
float levelMax = 0;
for (int i = 0; i < _dec; i++)
{
float wavePeak = waveData _* waveData*;*_
* if (levelMax < wavePeak)*
* {*
* levelMax = wavePeak;*
* }*
* }*
* return Mathf.Sqrt(levelMax);*
* }*
* return 0;*
}
In Update
private void Update()
{
* _volumeTimer += Time.deltaTime;
if(_volumeTimer >= getVolumeInterval)
_ {*
* volumeTimer = 0;
_ float volume = MicMaster.StopAndGetVolume();*
* if(volume < Config.ThresholdOfSilence)*
* {*
* timer = 0;
_ MethodToCall()*
* }*
* MicMaster.StartRecord();*
* }*
}