Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 snifo · Apr 16, 2018 at 12:57 PM · audioaudioclipcuttrim

How to detect volume change and trim the beginning of AudioClip?

Hello! I made a drumpad app, which records audio from the device microphone and then plays the recorded audio. I'm using SavWav to trim the end of the clip, but I don't know how to trim the beginning of the clip. I want to cut the silence at the beginning where the volume is close to 0 and create a new clip that begins at the point when the volume gets higher. Any idea on how to do that? Here's the part of SavWav which deals with the trimming.

 public static AudioClip TrimSilence(AudioClip clip, float min) {
     var samples = new float[clip.samples];

     clip.GetData(samples, 0);

     return TrimSilence(new List<float>(samples), min, clip.channels, clip.frequency);

 }

 public static AudioClip TrimSilence(List<float> samples, float min, int channels, int hz) {
     return TrimSilence(samples, min, channels, hz, false, false);
 }

 public static AudioClip TrimSilence(List<float> samples, float min, int channels, int hz, bool _3D, bool stream) {
     int i;

     for (i=0; i<samples.Count; i++) {
         if (Mathf.Abs(samples[i]) > min) {
             break;
         }
     }

     samples.RemoveRange(0, i);

     for (i=samples.Count - 1; i>0; i--) {
         if (Mathf.Abs(samples[i]) > min) {
             break;
         }
     }

     samples.RemoveRange(i, samples.Count - i);

     var clip = AudioClip.Create("TempClip", samples.Count, channels, hz, _3D, stream);

     clip.SetData(samples.ToArray(), 0);

     return clip;
 }
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
0
Best Answer

Answer by Lukas-Wendt · Apr 18, 2018 at 03:53 PM

The TrimSilence method in SavWav already trims both beginning and end (see line 15-20), what is not working for you?

Also, this way om trimming does not trim at a zero-crossing, which means that you will most likely hear clicks at the beginning and end of your trimmed clips.

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
avatar image
0

Answer by snifo · Apr 18, 2018 at 05:00 PM

I thought that it cuts both beginning and end but I was not sure. The thing is that there is some static noise at the beginning, which is inevitable when you're using the mic, but TrimSilence does not cut it, it recognizes it as important. Now that I know it cuts both ends I think the problem can be solved with a noise reduction plugin or something relevant. Thank you!

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 Lukas-Wendt · Apr 18, 2018 at 06:03 PM 0
Share

I would think that adjusting the $$anonymous$$ parameter of TrimSilence can help you get rid of the noise. It should just be a question of finding a good value.

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

98 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 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 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 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.clip.GetData() returns no data. 0 Answers

Keep playing audio on android even if screen dim or sleep 0 Answers

Unity 5 low audio volume 1 Answer

Playing many audioclips with PlayOneShot causes all sound to cut out. 0 Answers

Adding multiple audio clips to a single game object 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