- Home /
How to properly destroy a script-created AudioClip ?
Hello everybody,
I'm currently trying to create a Speech recognition system.. and it work pretty well. The only problem I have is that my AudioClip won't be freed.
To create my AudioClip, I'm using the Microphone looping AudioClip. When I think the user has spoken (using the sample array), I create a new AudioClip and give him the part of the sample array when the user speaks.
Something like that :
/// <summary>
/// Takes a part within a sample array and put it within an AudioClip
/// </summary>
/// <param name="samples">The sample array</param>
/// <param name="first">The first sample to take</param>
/// <param name="last">The last sample to take</param>
/// <param name="freq">The frequency of the AudioClip</param>
/// <returns>the cutted AudioClip</returns>
public static AudioClip TrimClip(
float[] samples, int first, int last, int freq)
{
int nSamples = last - first;
float[] newSamples = new float[nSamples];
for (int i = first; i < last; i++)
{
newSamples[i - first] = samples[i];
}
AudioClip ac = AudioClip.Create(
"ToRecognize", nSamples, 1, freq, false, false);
ac.SetData(newSamples, 0);
return ac;
}
When I don't need the AudioClip, I just use this method :
/// <summary>
/// Frees an AudioClip
/// </summary>
/// <param name="ac">
/// The AudioClip we want to remove from our memory
/// </param>
public static void FreeAudioClip(AudioClip ac)
{
AudioClip.DestroyImmediate(ac, false);
// AudioClip.Destroy(ac);
// Resources.UnloadAsset(ac);
// GameObject.Destroy(ac);
}
I'm sure I give those methods every AudioClip, I've already tested. No one among those for Destroy methods makes the Profiler drop the number of AudioClip.
So if I want to create a looot of AudioClips, my memory begins to hate me..
The only simillar I found was with Loaded assets, which is not my case, that's why I created this topic,
I thank you in advance and for reading :)
Answer by Sylafrs · Dec 19, 2013 at 06:32 PM
Ok, I've just found one of my issues : The Microphone clip wasn't released, but I was starting new clips..
So, I think this method works well
I'm sorry to have posted this one :/
Now, I must use coroutine and Stream the samples.. because for now, if I let the recording audioclip exceed 10 seconds, it becomes laggy (15 FPS peaks)