- Home /
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.
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.
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
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.
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
I've converted my comment to an answer since it seemed to answer your question. If it did, please accept it :)
Your answer
Follow this Question
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