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 Tyler 2 · Dec 18, 2010 at 01:04 AM · audiovolume

Volume increase?

Hello, I got this script from this thread I started. How can I use it to detect a sudden increase in volume, (like a drum beat) and then tie it to another function (like increasing the velocity of a particle emitter or spawning an enemy)?

var vol: float; var sampleRate: float; var timeWindow: float;

private var numSamples: int; private var window: float[];

function Start() { numSamples = sampleRate * timeWindow; window = new float[numSamples]; InvokeRepeating("UpdateVolume", 0, timeWindow); }

function UpdateVolume() { window = audio.GetOutputData(numSamples, 0); vol = RMS(window); }

function RMS(data: float[]) { var result = 0.0;

 for (i = 0; i < data.Length; i++) {
     result += data[i] * data[i];
 }

 result /= data.Length;

 return Mathf.Sqrt(result);

}

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

Answer by BerggreenDK · Dec 18, 2010 at 01:14 AM

I havent tried it, but perhaps you could sum all the samples within a RMS and check if it makes an abnormally large jump in the sum?

The RMS function seems to be summing your samples already? so the "vol" variable, isnt that the thing you should be watching?

perhaps if you store the value from each update in a "previousVol" somewhere globally and then compare the previous to the new vol. If the difference is... lets say 30 then you have a spike. Then you call your "volumeter trigger"?

Wouldnt that do the trick?

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 Tyler 2 · Dec 18, 2010 at 01:48 AM 0
Share

How would I do that?

avatar image BerggreenDK · Dec 18, 2010 at 02:19 AM 0
Share

How would you do what?

avatar image Tyler 2 · Dec 18, 2010 at 02:29 AM 0
Share

"sum all the samples within a R$$anonymous$$S and check if it makes an abnormally large jump in the sum?"

avatar image BerggreenDK · Dec 18, 2010 at 02:44 AM 0
Share

I believe the R$$anonymous$$S function already does it for you. Do you understand what the script does or how much practice do you have? Not to be rude, but I am trying to understand your question.

avatar image Tyler 2 · Dec 18, 2010 at 06:18 AM 0
Share

You're not being rude.I think the script I posted takes the volume of of the audio in the scene every interval? I am not very good at scripting. I dont know how I would "store the value from each update in a "previousVol" somewhere globally and then compare the previous to the new vol."

Show more comments
avatar image
0

Answer by _Petroz · Dec 18, 2010 at 11:54 AM

If you want to detect a sudden peak just check the absolute value of the samples:

function ExceedsVolumeThreshold(data: float[]) { var threshold = 0.7;

 for (i = 0; i < data.Length; i++) {
     if (Mathf.Abs(data[i]) > threshold) {
          return true;
     }
 }
 return false;

}

This is simplified, better analysis would require more complex code. You would probably have to develop some heuristics to distinguish beats from other sounds in the music.

Comment
Add comment · Show 4 · 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 Tyler 2 · Dec 18, 2010 at 05:50 PM 0
Share

Thanks. I added that to the script I posted above, and even though I am getting no error, I cannot get anything to happen when the audio increases (even if I set the threshold to .1). Tyler 0 secs ago

avatar image _Petroz · Dec 18, 2010 at 10:49 PM 0
Share

This is a function that you call like the R$$anonymous$$S funcion. something like: if (ExceedsVolumeThreshold(window))

avatar image Tyler 2 · Dec 19, 2010 at 06:46 AM 0
Share

Thank you. Is there a way to analyze audio BEFORE it plays? I know that is odd, but in music generated games like audio surf and rhythm zone, that is done (I presume it would help performance)?

avatar image Tyler 2 · Dec 19, 2010 at 07:29 AM 0
Share

Also, this is going to need some fine tunning, do you have any idea on how I could accuratly test to see if it is detecting beats (I want to eventualy make something like this youtube.com/watch?v=xH20F6Q3trY or this http://www.youtube.com/watch?v=cInI1DDpVj$$anonymous$$&feature=related if that helps put it into context)

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

No one has followed this question yet.

Related Questions

sound distance between camera and audio source 2 Answers

Rigidbody crate collide sound. 0 Answers

Music volume decreases after one loop 1 Answer

AudioListener.volume doesn't change the volume 4 Answers

Urgent! Need to animate volume 1 Answer


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