i am using unity3d to record some input from the microphone using:
getAudioSource.clip = Microphone.Start(null, true,20, 44100);
The problem is that this records for a standard time period of 20 secs, which i don’t want. I would like to record untill a finish a recording by:
Microphone.End(deviceName);
and the resulting clip should have the exact size i recorded, not a standard 20 secs. Right now, if i end the recording after 3 secs, the resulting audio is 20 secs in length. I would like it to have 3 secs. How can this be done?
This is really late, but I solved this predicament for anyone who may have this issue in the future.
You first need to capture the position of the last moment you record. Then, you have to create a new AudioClip and copy the recorded portion of the clip provided you by the Microphone class into the new AudioClip. EndRecording may look something like this, given recordedClip is the AudioClip returned by Microphone.Start() and deviceName is the string for the name of your device:
//Capture the current clip data
var position = Microphone.GetPosition(deviceName);
var soundData = new float[recordedClip.samples * recordedClip.channels];
recordedClip.GetData (soundData, 0);
//Create shortened array for the data that was used for recording
var newData = new float[position * recordedClip.channels];
//Copy the used samples to a new array
for (int i = 0; i < newData.Length; i++) {
newData _= soundData*;*_
}
//One does not simply shorten an AudioClip,
// so we make a new one with the appropriate length
var newClip = AudioClip.Create (recordedClip.name,
* position,*
* recordedClip.channels,*
* recordedClip.frequency,*
* false,*
* false);*
newClip.SetData (newData, 0); //Give it the data from the old clip
//Replace the old clip
AudioClip.Destroy (recordedClip);
*recordedClip = newClip; *