What does 'float[] sample' in GetOutputData represent?

I need to monitor a master sound level while playing the game. AFAIK there is no VU meter of sorts so I think about writing my own meter.

For master channel data I used

float[] samples = new float[1024];
int channel = 0;
AudioListener.GetOutputData(samples,channel);

To get the last 1024 of samples played. But I’m now wondering what does each value in samples represent? Since when I try to sum all 1024 samples I sometimes get negative value.

Only thing I found out is when I play a really loud sound each float value in array will be close to 1. But what is a negative value?

I guess the channel will be 0 for left and 1 for right. Is this true?

Positive samples (translated to voltage) push your speakers out, and negative ones pull them in. This is not really a Unity question; maybe you are confused, because you’re used to seeing half the total bit depth at zero, instead of the number zero?

You can generally count on 0 and 1 mapping to left and right; users can change this, but you don’t have any way of knowing about it.

It sounds like what you want is the value of the sound’s envelope normalized between 0 and 1. If you want to achieve that, you don’t need to mess about with the spectrum data (which incidentally is available via AudioListener.GetSpectrumData() so you don’t even need to really worry about Fourier transforming anything yourself). All you really need to do is take a number of samples, normalize them and then find the maximum value out of each set of samples. So for instance:

private AudioListener theSource;

void Update() {

  float[] samples = theSource.GetOutputData(1024, 0);
  float max = 0;
  foreach (float sample in samples) {
    sample = (sample + 1f) /2f;
    if (sample > max) max = sample;
  }

  //Use value of max to control your VU meter height for this frame

}

Be aware that the human ear perceives ‘loudness’ in a logarithmic fashion, so scaling your VU meter’s height probably won’t give an accurate representation of the perceived loudness of your audio. You’ll want to perform a Mathf.Log() on your values to get a better representation of actual perceived loudness.