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.

1 Like

@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.

Hope this helps!