- Home /
Clear microphone data
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[i] * waveData[i];
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?
Answer by Cubist12 · Mar 28, 2017 at 04:39 AM
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.
Answer by Julien-Lynge · Dec 07, 2013 at 07:17 PM
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.
I tried, but if I call Destroy(_clip) the next call of StartRecord() causes an error
Yeah, if you're going to destroy the clip, you have to instantiate a new one. Can you just call
_clip = $$anonymous$$icrophone.Start ( null, false, 1, 44100);
again?
yes i tried call this after Destroy(_clip); even tried _clip = new AudioClip(); but still error
Can you post more of your code that's reading from the microphone into the clip? I have a feeling something is missing. For instance, you mention StartRecord(), but I don't see that anywhere in your posted code.
in first message
StartRecord() is simple, i use default unity3d class $$anonymous$$icrophone
public static void StartRecord()
{
if($$anonymous$$icrophone.devices.Length > 0)
{
_clip = $$anonymous$$icrophone.Start ( null, false, 1, 44100);
}
}
Answer by Shar1ngan · Dec 09, 2013 at 07:47 AM
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[i] * waveData[i];
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();
}
}
Your answer
Follow this Question
Related Questions
Generating an Echo effect in Unity 3 Answers
Voice Recording issue ?? 0 Answers
When is audio file loaded to memory 0 Answers
GetSpectrumData doesn't update 2 Answers
Mac built-in microphone stopped sending data to Unity. 0 Answers