我正在尝试创建一个基于节拍和频谱检测的游戏。我目前坚持认为我的函数返回的不是我想要的,因为数组没有完全分配。填写的最后一个元素是错误的,我不知道为什么。
数组大小为20,此代码仅填充12或更少
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;
}
}
}
}
问题是:
坦克为你的答案准备好了。
我很抱歉我的英语不好......
[编辑]
我发现if语句或我计算时间错误的方式: - (
因此,如果您更好地了解如何从4000个元素的数组中获取数据到20个数组,如果您在此发布您的建议,我将很高兴。