- Home /
getting pitch
so i have an audio file that goes from 100Hz and goes down to 1Hz. i want to be able to get the pitch or frequency of the changing sound but i cant find a way to do it through unity. when i run this code.
var getfre : float;
var getpitch : float;
function Update () {
if(Input.GetButtonDown("k"){
getfre = audio.clip.frequency;
getpitch = audio.pitch;
Debug.Log(audio.clip.frequency);
}
}
i get an unchanging pitch of 1 and an unchanging frequency of 44100 everytime i hit "k". is there anyway to get the actual pitch or frequency throughout?
Answer by dju4ic · Jun 11, 2011 at 04:25 PM
Coming from an audio engeineering and production background, I believe this is the reason why:
Pitch - You are playing back the sound at a 1:1 ratio - meaning 1 pitch, if you played it back at half speed it would be .5, double would be 2 etc. Unity can not monitor the output pitch as far as i know of.
Frequency - This is the frequency the sound file is encoded in, usually would be 44100, 44800, 96000 etc.. all depending on what it is encoded in. The higher the frequency i9t is encoded, the more information per sample is stored in the sound file.
I havent looked into what FFT functions are available, but if this is for audio/visual work my best advice is to go with a workflow of vvvv (from vvvv.org - windows only though) - and OSC controls to unity. They do play very well together and vvvv is very versatile for FFT/Audio monitoring and control as well ad directX and Video..
Unfortunately you would not be able to build down including the vvvv application that I know of.. If there is a way, I am very intrigued.
It's correct. Unity's $$anonymous$$ch is intended to vary the sound pitch, not to show which is its real pitch. Finding the real pitch of an audio signal is very tricky, and involves FFT. It can be estimated using the function audio.GetSpectrumData. I didn't test this function yet, and the docs seem incomplete (they mention numSamples, but this parameter can't be found in the function arguments).