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.


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

for (var i = n1; i < n2; i++) {
    sum += drewData*;*

return sum/(n2 - n1);
Now it should return the average volume of the given range of frequencies.
Hope this helps,