android音频波形图绘制

时间:2022-04-17 12:04:47

  给链接吧,这是个直接使用pcm数据来绘制图形.

http://*.com/questions/8325241/android-audio-fft-to-display-fundamental-frequency?rq=1 

关键代码贴出来吧:

/Conversion from short to double
double[] micBufferData = new double[bufferSizeInBytes];//size may need to change
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 1.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < bufferSizeInBytes - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v
&= 0xFF;
}
sample
+= v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData
[floatIndex] = sample32;
}



 很有意思,这个事声道16位的绘制.

                   1 由于是单声道所以直接去这2个8位的数据哦(bytesPerSample = 2),sample就是振幅的值哦

                   2 振幅的运算,由于是16位的,所以我们要知道,前八位(一个byte)就是低位的(是这样定义的哦),