65,187
社区成员




typedef signed short sshort;
typedef unsigned short ushort;
ushort CalcVolume(sshort* samples, unsigned long sampleCount)
{
uint accum = 0;
for (unsigned long i = 0; i < sampleCount; i++)
{
if (samples[i] >= 0)
accum += samples[i];
else
accum += -samples[i];
}
float average = accum / (float)sampleCount;
sint volume = average;
return (ushort)volume;
}
int OnGetVolume(ushort volume)
{
const static double p = 2.0;
const static double p32 = pow(1 / 32.0, 1 / p);
double root = 31.0 / (32*32767) * volume + 1.0 / 32;
double rooted = pow(root, 1 / p);
rooted -= p32;
rooted *= 32767;
rooted /= 1 - p32;
volume = (ushort)rooted;
volume = volume > 16383 ? 16383 : volume;
return volume;//计算得到声音的能量
}
void main()
{
//......
while(not file eof)//假设一次读160字节
//读文件
//......
sshort volume = CalcVolume(pInput, frameCount);
volume = OnGetVolume(volume);
//...
}