Hello,
How can I do to the rhythm of the music created objects?
I know I have to do this using Audio Spectrum, but I do not know what to do next
Please, tips, videos, links, whatever.
Hello,
How can I do to the rhythm of the music created objects?
I know I have to do this using Audio Spectrum, but I do not know what to do next
Please, tips, videos, links, whatever.
Hi!
The GetSpectrumData Method will give you what you need.
The example provided there should be enough to get you started.
Divide the returned data in bands if you want to move objects according to specific frequencies.
@WoozyBytes How can I do that? There he finds himself in this topic :c
The GetSpectrumData() Method will return you a block of the data currently playing this will be a float array of the specified size(1024 or 2048 samples should be enough) containing the audio data.
This is your frequency range.
Here’s an rough example of how to bounce an object according to the bass frequency
i hope it helps please let me know if you have any questions
This Line → " objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;"
Will move your object but you better of using Mathf.SmoothDamp() method to smoth out the movement.
var qSamples : int = 2048; // QWindow
var channels : int = 2;
var bassMinHz : int = 30; //Min bass Frequency in Hz
var bassMaxHz : int = 120; // Max bass Frequency in Hz
private var bassMinSamples: int ; //Min bass in samples
private var bassMaxSamples : int ; // Max bass in samples
private var spectrumSamples : float[ ];
var objectToMove : Transform; // Add you object here
var moveMultiplier : float = 10; // Adjust this to move more or less
var audioListener : AudioListener; // Assign one audioListener here
var audioisPlaying : boolean = false;
function Start()
{
spectrumSamples = new float[ qSamples];
bassMinSamples = (bassMinHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
bassMaxSamples = (bassMaxHz * qSamples)/(AudioSettings.outputSampleRate/channels) ;
}
function Update ()
{
if( audioListener != null && audioisPlaying == true)
AnalizeSpectrum() ;
}
function AnalizeSpectrum(){
audioListener.GetSpectrumData( spectrumSamples, 0, FFTWindow.Rectangular );
for ( var smplN = 0; smplN < spectrumSamples.Length ; smplN ++ ){
// If the current sample is withing the bass range move the object
if( smplN >= bassMinSamples && smplN <= bassMaxSamples ){
objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;
}
}
}
@WoozyBytes Thank you for your help. more keen on creating objects when there’s a bass. How can I use Instantiate in easiest method?
Instead of this – > objectToMove.position.y = spectrumSamples[smplN] * moveMultiplier ;
use Instantiate() if the spectrumSamples[smplN] value is higher than a pre defined threshold( you will need to play around to get the right value , also it will depend on the sound itself , some sounds have less/more energy at specific frequencies);
Be careful since AnalizeSpectrum() runs every frame you don’t want to instantiate stuff every frame.
@WoozyBytes thanks for Help <3 You could write a complete script? That is, lest objects formed by Instantiate only when the music beat?