我正在从音频文件中提取样本以绘制波形 - 我已经生成了一个包含样本的数组(我的代码中的waveDisplayArray)。
我希望从waveDisplayArray中提取最大值,这是最好的方法吗?
(我最初定义了waveDisplayArray,如下所示:int waveDisplayArray [280] = {0};
我不确定这是解决这个问题的最好方法。
提前感谢:)
for( int y=0; y<convertedData.mNumberBuffers; y++ )
{
NSLog(@"buffer# %u", y);
AudioBuffer audioBuffer = convertedData.mBuffers[y];
int bufferSize = audioBuffer.mDataByteSize / sizeof(Float32);
Float32 *frame = audioBuffer.mData;
NSLog(@"Buffer Size is: %i", bufferSize);
int numberOfPixels = 280;
int waveDisplayArray[280]={ 0 };
int i;
for (i = 0; i<numberOfPixels; i++)
{
//NSLog(@"i is: %i", i);
int j;
int numberOfSamplesPerPixel = bufferSize/numberOfPixels;
float average = 0;
for (j=i*numberOfSamplesPerPixel; j<(i+1)*numberOfSamplesPerPixel; j++){
average += frame[j];
average = average/numberOfSamplesPerPixel;
}
waveDisplayArray[i] = average;
NSLog(@"Average %i is %f",i,average);
NSLog(@"waveDisplay Array %i: %f",i, waveDisplayArray[i]);
}
}
答案 0 :(得分:1)
我已编辑并仅发布相关部分以找到最大值。
int i;
float largest = 0;
for (i = 0; i<numberOfPixels; i++)
{
//NSLog(@"i is: %i", i);
int j;
int numberOfSamplesPerPixel = bufferSize/numberOfPixels;
float average = 0;
for (j=i*numberOfSamplesPerPixel; j<(i+1)*numberOfSamplesPerPixel; j++){
average += frame[j];
average = average/numberOfSamplesPerPixel;
}
waveDisplayArray[i] = average;
if( largest < average )
{
largest = average;
}
NSLog(@"Average %i is %f",i,average);
NSLog(@"waveDisplay Array %i: %f",i, waveDisplayArray[i]);
}
NSLog(@"Largest %f",largest);
答案 1 :(得分:1)
你有没有考虑过使用苹果Accelerate framework?例如,有一个函数可以计算整个向量(您的数组)的最大值。
一行替换你的for循环:
vDSP_maxmgv(&maxValue, 1, waveDisplayArray, 280);
还有用于计算mean,min或其他的有用函数。这应该快得多。