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 lorrotar · Dec 28, 2017 at 09:55 PM · audiofrequency

How can I use spectrum data to create a frequency based beat detection?

I have created a forum post about this, better write it here aswell

I've been thinking about making a rhythm game for a long time and I want to take action. The problem is though, after all the research and reading through old posts in unity forum/reddit etc., I still don't have a clue about how can I create what I aim for. At this point I'm not even sure if I'm approaching this the right way because I probably lack the technical knowledge.

So here's the thing as far as I understood ;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class something : MonoBehaviour {
 
 public AudioSource audioSource;
 
 public float[] freqData = new float[1024];
 
 void Start () 
 {
 audioSource = GetComponent<AudioSource> ();
 }
 
 void Update () 
 {
 audioSource.GetSpectrumData (freqData, 0, FFTWindow.BlackmanHarris);
 }
 }

Let's say I have this code in place. My sampling rate is at 48000 Hz. The array size determines the frequency range of each element. So with a size of 1024, my frequency resolution is 23.4 Hz. So freqData[0] represents the freq between 0-23.4 Hz. freqData[1] represents 23.4-46.8 Hz. so on and so forth. And I want to detect, let's say bass beats between 60Hz and 250 Hz.

How can I translate this data into "hey unity I want you to check those wiggling small numbers(relative amplitudes of frequency i think) from index 4 to 11, and if some of them is above a certain threshold(of decibel or whatever i don't even know), I want to register them as beats."

And everytime there is a SendMessage("Beat Detected") in place I can instantiate a cube to represent the beat for instance.

Even though it is an extremely complicated subject, I feel like what I want to achieve is relatively simple to accomplish yet I feel like an idiot not being able to do it.

Thanks for reading and thanks for help/tips in advance.

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
0

Answer by jkeogh1413 · Mar 08, 2018 at 02:57 AM

@lorrotar your understanding of the data returned by GetSpectrumData is spot on. You're on the right track. You know that you care most about indexes 4 through 11, so what you want to do is to compare the current output of GetSpectrumData for those indexes, which would be the spectrum for the most recently played audio, to some window or running average of samples to be able to say that "this set of samples is significantly more active than these other sets of samples, so it must be a beat".

I wrote an article on how to do this with an "onset detection using spectral flux" algorithm, and it can work for real-time audio or you can preprocess the entire audio file up front. https://medium.com/giant-scam/algorithmic-beat-mapping-in-unity-real-time-audio-analysis-using-the-unity-api-6e9595823ce4

Hope this helps!

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

88 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

Related Questions

Getting beats from a song using frequency range 1 Answer

Calculating reverberation time in Resonance Audio for Unity,Reverberation time in Resonance Audio (Unity) 0 Answers

Get the frequency from an AudioSource 1 Answer

Why is the data I pass into OnAudioFilterRead() resulting in glitchy audio? 1 Answer

How do I get the fundamental frequency pitch for audio input? 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