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
3
Question by badescuga9 · Sep 27, 2013 at 09:37 AM · audiosourceaudioclipmicrophone

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?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

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;    

Comment
Add comment · Show 3 · 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 incorrect · Jan 08, 2016 at 05:23 PM 0
Share

Please, format your code.

avatar image robot55 · Aug 23, 2017 at 05:08 PM 2
Share

@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;   
     }

avatar image liuyang61 robot55 · Aug 12, 2020 at 12:40 AM 0
Share

Worked great. Thanks so much!

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

20 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

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


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