Hey everyone, been losing my mind with this. What I’m trying to do is have the player hold the space button, and while its being held his voice is recorded. So as soon as the player lets go of space, that ends the recording, and the resulting audio clip is of the length that the player held the space button.
This clip will then be used to analyze the volume vs. a set value, but for now I simply cannot figure out how to get this recording to work.
The Microphone.Start function specifically wants an int value for length of recording.
Any help would be greatly appreciated, as information on this topic seems rather sparse.
Thanks in advance!
Unfortunately, the only way to do this with the current API is a huge pain since resizing AudioClips isn’t allowed:
- When space is pressed, call Microphone.Start(), passing some maximum length of time for a recording as the length. Start a real-time timer of some sort.
- When space is released, call Microphone.End(), and stop the timer.
- If the maximum length was reached, use it as is. Otherwise, continue on:
- Create a new array of size clip.frequencyclip.channelsrealTimedSeconds.
- Call GetData() on the clip with the array and an offset of 0.
- Create a new AudioClip using AudioClip.Create with the same sample rate and number of channels, and with number of samples equal to the length of the array.
- Call SetData() on the new clip with the array and an offset of 0.
- Destroy the old clip.
- Return the new clip.
Tested with 5.1.2f1.