audio.clip.GetData() works in a strange way.

Alright, I am making a game where I need to retrieve the samples on a song in order to perform an audio analysis. I have a function that stores the samples of an audio clip into an array:

public void FillSamples(ref float[] array, int timeInSamples) {
	AudioSource audio = GetComponent<AudioSource>();
	audio.clip.GetData(array, timeInSamples);
}

The way I understand it, audio.clip.GetData() fills an array with the samples of the audio clip, starting at a specified position.

That would mean that if I specified the timeInSamples to be 10,000, the 0th float in the array would be equal to the 10,000th sample in the audio clip. The 1st float would then be the 10,001st sample and so on.

This doesn’t seem to be the case, though. When I execute the following code:

float[] tArr = new float[64];
FillSamples(ref tArr, 99490 + 0);
print("tArr[10] = " + tArr[10]);
FillSamples(ref tArr, 99490 + 10);
print("tArr[0] = " + tArr[0]);

I get two different values for each print()-call, even though both values should be equal to the sample at position 99500 in the audio clip.

Is there something I’m missing?

Thanks in advance,
ThePC007

I guess that your clip is a stereo clip? In this case you get the left and right channel interleaved. You may want to check clip.channels.

Something like that:

 L   R
 0   1
 2   3
 4   5
 6   7
 8   9
10  11

Though i can’t remember if the first sample is left or right, it could be the other way round.