Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Sendatsu_Yoshimitsu · Jan 17, 2015 at 08:22 AM · c#audiomicrophone

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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:

  1. 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.

  2. Have the user press a key to stop the recording, say "S".

  3. 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.

  4. 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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Jan 17, 2015 at 10:28 AM 0
Share

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.

avatar image pako · Jan 17, 2015 at 12:08 PM 0
Share

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.

avatar image Sendatsu_Yoshimitsu · Jan 17, 2015 at 12:12 PM 0
Share

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!

avatar image pako · Jan 17, 2015 at 12:45 PM 0
Share

I edited my answer and comment appropriately. Thank you for accepting my answer!

avatar image Sendatsu_Yoshimitsu · Jan 17, 2015 at 12:49 PM 0
Share

Of course! It was incredibly helpful, especially considering how unfocused my initial query was :)

avatar image
3

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);

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karmatha · Sep 05, 2018 at 01:01 PM 0
Share

Thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges