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
0
Question by Shar1ngan · Dec 07, 2013 at 04:31 PM · soundmemorymicrophone

Clear microphone data

Hello, i need call somthing metod if player say something in microphone. I have a static class to work with a microphone. Every second I start to record from the microphone

private static AudioClip _clip;

 public static void StartRecord()
 {
     if(Microphone.devices.Length > 0)
     {
         _clip = Microphone.Start ( null, false, 1, 44100);
     }    
 }    

Then turn off the recording and take a sample Microphone.End(null); _clip.GetData(waveData, 0);

Then find the maximum volume for this time float levelMax = 0;

 for (int i = 0; i < _dec; i++) 
 {
     float wavePeak = waveData[i] * waveData[i];
     if (levelMax < wavePeak) 
     {
          levelMax = wavePeak;
     }
 }
 return Mathf.Sqrt(levelMax);


And repeat this procedure every second Everything works well, but the sound that I recorded not erased and stored. And every second I have an extra 100 kb + one additional audioclip in stroage. (total i have 44.5 mb)

How to delete the data, and store only the actual sample?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cubist12 · Mar 28, 2017 at 04:39 AM

I know this is a very old threat, but for everyone having still problems with this:

I had the same problem (clearing Microphone Data from memory) while programming a Musicvisualizer thats made to run 24/7. If making a seperate Audioclip, I got an Unassigned reference Exeption after destroying it. But I found a solution in creating a Audiosource.clip instead of an Audioclip.

If you do: Destroy(Audiosource.clip); and then call Audiosource.clip = Microphone.Start(null, false, 1, 44100);

it will remove the old clip from your memory and instanciate a new one.

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 yutoVR · Jul 29, 2018 at 03:56 AM 0
Share

$$anonymous$$y problem was solved. Thank you!

avatar image
0

Answer by Julien-Lynge · Dec 07, 2013 at 07:17 PM

Try Destroy()ing the previous clip before assigning a new clip.

You assign a reference to the clip variable with the line

 _clip = Microphone.Start ( null, false, 1, 44100);

but you never explicitly destroy the clip that was previously referenced. There are some things like Materials (and I believe clips) that are not destroyed when you remove a reference to them. You have to do it explicitly.

Comment
Add comment · Show 6 · 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 Shar1ngan · Dec 07, 2013 at 08:32 PM 0
Share

I tried, but if I call Destroy(_clip) the next call of StartRecord() causes an error

avatar image Julien-Lynge · Dec 07, 2013 at 09:11 PM 0
Share

Yeah, if you're going to destroy the clip, you have to instantiate a new one. Can you just call

 _clip = $$anonymous$$icrophone.Start ( null, false, 1, 44100);

again?

avatar image Shar1ngan · Dec 07, 2013 at 09:14 PM 0
Share

yes i tried call this after Destroy(_clip); even tried _clip = new AudioClip(); but still error

avatar image Julien-Lynge · Dec 08, 2013 at 01:11 AM 0
Share

Can you post more of your code that's reading from the microphone into the clip? I have a feeling something is missing. For instance, you mention StartRecord(), but I don't see that anywhere in your posted code.

avatar image Shar1ngan · Dec 08, 2013 at 09:39 AM 0
Share

in first message

StartRecord() is simple, i use default unity3d class $$anonymous$$icrophone

 public static void StartRecord()
 {
     if($$anonymous$$icrophone.devices.Length > 0)
     {
        _clip = $$anonymous$$icrophone.Start ( null, false, 1, 44100);
     }  
 }   
Show more comments
avatar image
0

Answer by Shar1ngan · Dec 09, 2013 at 07:47 AM

All Code in static class MicMaster

 public static void StartRecord()
 {
     if(Microphone.devices.Length > 0)
     {
         _clip = Microphone.Start ( null, false, 1, 44100);
     }
     else
     {
         Debug.Log("Mic is missing!");
     }
         
 }    
     
 public static float StopAndGetVolume()
 {
     if(Microphone.devices.Length > 0)
     {
         Microphone.End(null);
         float[] waveData = new float[_dec];
         _clip.GetData(waveData, 0);
         float levelMax = 0;
         
         for (int i = 0; i < _dec; i++) 
         {
             float wavePeak = waveData[i] * waveData[i];
             if (levelMax < wavePeak) 
                 {
                 levelMax = wavePeak;
             }
         }
     
         return Mathf.Sqrt(levelMax);
             
     }
     return 0;
 }


In Update

 private void Update()
 {
     _volumeTimer += Time.deltaTime;
     if(_volumeTimer >= _getVolumeInterval)
     {
         _volumeTimer = 0;
         float volume = MicMaster.StopAndGetVolume();
         
         if(volume < Config.ThresholdOfSilence)
         {
             _timer = 0;
             MethodToCall()
         }
         MicMaster.StartRecord();
     }
 }
Comment
Add comment · 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

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

19 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

Related Questions

Generating an Echo effect in Unity 3 Answers

Voice Recording issue ?? 0 Answers

When is audio file loaded to memory 0 Answers

GetSpectrumData doesn't update 2 Answers

Mac built-in microphone stopped sending data to Unity. 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