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 Supershandy · Sep 14, 2013 at 07:57 AM · audiomusicfrequencygetspectrumdata

Getting beats from a song using frequency range

To begin with, i'm not looking for exact beat matching as if using a midi file, all I want is to have something that will instantiate an object whenever a "Beat" is detected, to start with I'll want to use the bass range frequency which I believe is around 40 - 180 HZ.

This will be linked into a volume visualiser as well for some added effects, however, I have run into a problem. Doing a search around the forums I found this script by Aldonaletto to detect the volume of a song in a given range.

 private var freqData: float[];
 private var nSamples: int = 1024;
 private var fMax: float;
      
 function GetSpectrum(){
     // get spectrum: freqData[n] = vol of frequency n * fMax / nSamples
     audio.GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); 
 }
 
 function BandVol(fLow:float, fHigh:float): float {
     fLow = Mathf.Clamp(fLow, 20, fMax); // limit low...
     fHigh = Mathf.Clamp(fHigh, fLow, fMax); // and high frequencies
     var n1: int = Mathf.Floor(fLow * nSamples / fMax);
     var n2: int = Mathf.Floor(fHigh * nSamples / fMax);
     var sum: float = 0;
     // average the volumes of frequencies fLow to fHigh
     for (var i=n1; i]]

However, the script looks incomplete, in the description he puts that you have to add objects to the bass, mid and treble inputs in the inspector, however they don't show on the script and because it's incomplete I can't figure out where to begin fixing it, also i'm not an audio engineer, I'm just using knowledge I can gather to create an idea using music, but as I said at the start, it doesn't have to be exact.

"The function BandVol below calculates the volume of a given frequency range. You must call GetSpectrum to get a snapshot of the sound currently being played in the object's AudioSource, then you can analyse several different frequency bands. This script must be attached to an object with AudioSource, and you must drag three objects (cubes, spheres, whatever) to bass, mid and treb in the Inspector. When the song is playing, the Y coordinate of each object will be proportional to the frequency range it monitors. You can extend this script to several bands, until you find the ones you want."

A good example of what i'm trying to achieve is what Chemical Brothers did with their video for Star Guitar where objects flashed past a train window in beat to a song.

http://www.youtube.com/watch?v=0S43IwBF0uM

Comment
Add comment · Show 3
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 Supershandy · Sep 14, 2013 at 09:58 AM 0
Share

No that's pretty much it, from there I can work out how to instantiate an object when a certain volume is hit i.e "Beat".

I can then use this script to search different frequency levels and create beats from them....at least that's the idea in principle.

Unless you can think of a better way of doing it? As I said, I'm not exactly an audio engineer.

avatar image Benproductions1 · Sep 14, 2013 at 10:03 AM 0
Share

Neither am I, but there are so many better ways of figuring out the beat. However they would require a lot of work. With your current script it could end up spawning a "block" every frame because the music you have is currently building up to a massive Dubstep Base Drop with a couple thousand base drum beats per $$anonymous$$ute ;)

There are some good articles on finding the bpm of a song. Just search them up

avatar image Supershandy · Sep 14, 2013 at 03:15 PM 0
Share

That was part of the plan was to find the bpm which would set the speed of the level or the amount of times a block would be instantiated every second.

What I wanted here was to make a block to the bass beat, the other frequencies would do "other" things.

1 Reply

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

Answer by Benproductions1 · Sep 14, 2013 at 09:49 AM

Hello

That script shouldn't even compile, but it's quite easy to complete:

 for (var i = n1; i < n2; i++) {
     sum += drewData[i];
 }
 return sum/(n2 - n1);

Now it should return the average volume of the given range of frequencies.

Hope this helps,
Benproductions1

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 Benproductions1 · Sep 14, 2013 at 10:04 AM 0
Share

I've converted my comment to an answer since it seemed to answer your question. If it did, please accept it :)

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

16 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

Related Questions

GetSpectrumData over entire file 1 Answer

Unmuted sounds playing off-beat 0 Answers

Reliable way to analyze peaks in an audio file? 1 Answer

audio change object size 1 Answer

Audio signal plug-in? 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