How do I correctly use AudioSettings.outputSampleRate

In the new features section of the release notes for 3.1, it mentions that they added `AudioSettings.outputSampleRate` which "Returns the mixer's output rate; use this to calculate the precise hertz range returned from GetSpectrumData()," but there's no documentation in the web version of the scripting reference and no description in the local version. I would think that dividing it by the number of samples I'm using in GetSpectrumData would give me the frequency range for each bucket in the float array, but that doesn't appear to be the case.

For example, I'm playing a 500Hz tone and taking 128 samples. AudioSettings.outputSampleRate returns 44100. This value gives me a bucket frequency range of 344Hz/index, putting 500Hz in bucket 1 (zero-indexed), but in reality, the tone triggers index 3.

Am I doing something wrong or misunderstanding how I should be using this value?

In a general sense, how do I convert a frequency to its expected index in the output array from GetSpectrumData?

This deserves a BUMP!

2 Answers

2

It works in pairs of channels, thus the 3(second pair) as the minimum sampling rate to reproduce a signal is twice the wave form's original frequency.

http://www.pcguide.com/art/soundSampleRate-c.html

660/334 hz goes to bucket 2, not 3. pairing starting from index 0 stores at index 3. It saves the channels separately (as far as I tested).

Does it round up at x.5 to the next index-pair (as you imply would happen w/ 660Hz) rather than simply dropping the remainder/casting the result to an int? Is that where I'm going wrong? So, 500/344 ~= 1.45 => 1 and 660/344 ~= 1.92 =>2 ?

both would go to index 2, even though it's not a full int, it is rounded up

So it's the ceiling? I'm sorry, I'm still confused as to how to interpret what I'm seeing here: http://dl.dropbox.com/u/2109263/SpectralAnalysisWebplayer/SpectralAnalysisWebplayer.html If I want to fire something when a certain frequency hits some threshold, how do I convert a frequency to its expected index in the output of GetSpectrumData?

It appears to be proper rounding rather than the ceiling. Looks like the formula is Mathf.RoundToInt (Mathf.Clamp (frequency * 2, 0, AudioSettings.outputSampleRate) / (AudioSettings.outputSampleRate / numberOfSamples)) Thanks for the guidance. Marked as answered, but please let me know if I've missed something.

I am trying to get the hertz out of my code to determine.

AudioListener.GetOutputData(number, 0);

        for(int i=0; i < numSamples; i++)
        {
            peakData.Hertz = Mathf.RoundToInt (Mathf.Clamp (number _* 2, 0, AudioSettings.outputSampleRate) / (AudioSettings.outputSampleRate / numSamples));}_
*```*
*<p>What am i doing Wrong as Hertz which is an int is returning 0?              </p>*