- Home /
record dynamic length from microphone
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?
Answer by atolver · Jan 08, 2016 at 05:23 PM
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[i] = soundData[i];
}
//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;
@incorrect - this code almost work but doesn't. (at least on Unity 5.6 and up)
The problem seems to be the last 2 lines: AudioClip.Destroy (recordedClip); recordedClip = newClip;
It seems that once you destory recordedClip you can't use "recordedClip = newClip" and inspector show clip is missing.
I tweaked it to by calling the function with AudioSource and not Clip, and defining the AudioClip ("recordedClip") inside. Works now. thanks for the heavy lifting.
tweaked code below:
void EndRecording (AudioSource audS, string deviceName) {
//Capture the current clip data
AudioClip recordedClip = audS.clip;
var position = $$anonymous$$icrophone.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];
//$$anonymous$$icrophone.End (null);
//Copy the used samples to a new array
for (int i = 0; i < newData.Length; i++) {
newData[i] = soundData[i];
}
//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);
newClip.SetData (newData, 0); //Give it the data from the old clip
//Replace the old clip
AudioClip.Destroy (recordedClip);
audS.clip = newClip;
}
Your answer
Follow this Question
Related Questions
Microphone recording problems (double sample, audiosource stops working) 0 Answers
Movement speed based on audio input 1 Answer
Is it possible to select channels for the microphone input? 0 Answers
How to record audio in chunks and send through websocket? 0 Answers
Microphone — detect speech start & end 0 Answers