- Home /
How to use Microphone.Start/End for clips of unknown length?
I'm trying to add functionality that allows the user to record an audio clip, then run whatever analysis I've written on the audio once they've finished. The basic implementation seems obvious:
public AudioClip myClip;
void Update(){
if (Input.GetKey (KeyCode.P)) {
myClip = Microphone.Start(null, false, 10, samplerate); //Start recording (rewriting older recordings)
}
if (Input.GetKey (KeyCode.O)) { //Stop recording
Microphone.End(null);
MyAnalyzer(myClip); //Run whatever analysis I want on the recording
}
}
The problem is that Microphone.Start requires me to pass it a clip length when I call the method, and I would ideally like to let the user decide the recording length. The hacky solution is to leave the length at something far longer than the use case is likely to demand, but that's ugly and wastes memory. Is there a simple way to leave the length unspecified when I start recording?
Answer by pako · Jan 17, 2015 at 10:25 AM
For asking the user the recording length beforehand:
Ask the user to input the clip length. Add a field to hold the clip length and edit the code that runs after the user presses "P".
using System; //necessary for conversion of string to integer
private int clipLength; //number of seconds of the audio clip length
if (Input.GetKey(KeyCode.P))
{
//Ask the user to enter the length of the clip. A string will be returned
//that will be later converted to integer.
//The last parameter is the length of the string the user can input.
//So a value of 3 corresponds to a maximum valid number of 999 seconds. Adjust this as necessary.
//Also adjust the default to be offered to the user for editing (now "10")
string stringInput = GUI.TextField(new Rect(10, 10, 200, 20), "10", 3);
//Try to convert the input text to an integer. If the conversion is successful
//the function will return "true"
bool conversionSuccessful = Int32.TryParse(stringInput, out clipLength);
if (conversionSuccessful)
{
myClip = Microphone.Start(null, false, clipLength, samplerate); //Start recording (rewriting older recordings)
}
//else
//{
// //optionally output a suitable message to the user
//}
}
For determining the recording length at run time, and trimming a temporary audio clip to its non-empty part:
Use a temporary AudioClip "tempClip", to start the recording with Microphone.Start(). Say 60 seconds length, or whatever length you like. A maximum length for the temp clip would also be useful in controlling the use of system resources too.
Have the user press a key to stop the recording, say "S".
Before ending the recording call Microphone.GetPosition(). This will return an int with the the position in samples of the recording, say we call it "lastSample". You can use this to trim the useful, non-empty part of tempClip.
Use AudioClip.GetData() and AudioClip.SetData() in conjucntion with lastSample to store the useful part of tempClip in myClip for further analysis.
http://docs.unity3d.com/ScriptReference/Microphone.GetPosition.html
http://docs.unity3d.com/ScriptReference/AudioClip.GetData.html
http://docs.unity3d.com/ScriptReference/AudioClip.SetData.html
I hope this helps.
Is there a way to do it if the user doesn't know how long the clip will last, though? This is being used to record them pronouncing random words or phrases, so the duration will vary pretty drastically from speaker to speaker.
You said in your question "I would ideally like to let the user decide the recording length". This is why I gave this answer, which does just that.
Now you're saying "I would ideally like to to find the recording length at run time", or something, which is a different thing, and I wouldn't have answered at all since I have not used microphone at all.
However, if I wanted to do what you want to do, this what I would try (for all that's worth):
EDIT: moved the method inside the body of the answer.
Ack sorry, I was totally unclear with my phrasing: by "let the user decide" I was picturing them hitting a key to start, then stop recording, but I completely failed to articulate that. :)
Using the temporary clip is a great idea though, thank you for two extremely thorough answers!
I edited my answer and comment appropriately. Thank you for accepting my answer!
Of course! It was incredibly helpful, especially considering how unfocused my initial query was :)
Answer by ZXCZXCZXZXC · May 11, 2016 at 11:01 AM
code:
public void StopRecord() { int lastTime = Microphone.GetPosition(null); if (lastTime == 0) return;
Debuger.Log("lastTime =" + lastTime);
Microphone.End(null);
float[] samples = new float[AudioSource.clip.samples]; //
AudioSource.clip.GetData(samples, 0);
float[] ClipSamples = new float[lastTime];
Array.Copy(samples, ClipSamples, ClipSamples.Length - 1);
AudioSource.clip = AudioClip.Create("playRecordClip", ClipSamples.Length, 1, 44100, false, false);
AudioSource.clip.SetData(ClipSamples, 0);
}
Your answer
Follow this Question
Related Questions
Audio Record Limit? 1 Answer
Get the frequency from an AudioSource 1 Answer
Multiple Cars not working 1 Answer
How do I get the fundamental frequency pitch for audio input? 0 Answers
Distribute terrain in zones 3 Answers