I am trying to create a game based on beat and spectrum detection. I am currently stuck on that my function returns not quite what i want, beacose the array is not fully assigned. The last element which is filled is wrong and i don't know why.
The array size is 20 and this code fills only 12 or evven less
float[] rmsData;
public void spektrsw(int barrcount, bool lefton, out float[] spectrumData)
{
spectrumData = new float[barrcount];
//defining array in which will be spectrum data
if (isPlaying) //check if sound is playing
{
int lenght = (int)Bass.BASS_ChannelSeconds2Bytes(ActiveStreamHandle, 1.0); //gets and sets lenght
int rmsDataArraySize = Math.Abs(lenght / 4);
//calculating FFT data or RMS array size
if (rmsData == null || rmsData.Length < rmsDataArraySize)
rmsData = new float[rmsDataArraySize];
//defining new array if old one has lenght smaler
lenght = Bass.BASS_ChannelGetData(ActiveStreamHandle, rmsData, lenght); //geting lenght FFT data
rmsDataArraySize = lenght / 4; //calculating FFT data or RMS array size
int times = (int)(rmsDataArraySize / (barrcount * 2)) + 1;
//calculating how many times loop will continue until the loop will go to next index
int index = 0;
// creating index variable which will point to an element in array
for (int counter = 0; counter < rmsDataArraySize; counter++)
{
if (counter % 2 == 0 || counter == 0) //For left channel
{
if(lefton) //which channel am i analysing
spectrumData[index] = spectrumData[index] + Math.Abs(rmsData[counter]);
//adding data to array
}
else
{
if (!lefton) //which channel am i analysing
spectrumData[index] = spectrumData[index] + Math.Abs(rmsData[counter]);
//adding data to array
}
if (counter == times) //check to see if index needs to be increased
{
spectrumData[index] = spectrumData[index] / times;
//this is for calculating avarege in one bar since
//I combined lots of data in one bar
spectrumData[index] = spectrumData[index] * 1000f;
index++;//increasing index
times = times + counter;
}
}
}
}
Questions are:
- Am i doing right?
- Why it doesn't return all the values?
- What is the easyest way to get data i need?
Thanq for your answers allready.
And I am sorry for my bad english......
[EDIT]
I have found out that the if statement or the way i calculet Times is wrong:-(
So if you have a better idea how to get data from array of 4000 elements to an array of 20 i would be pleased if you posted your advice here.