android音訊波形圖繪製
阿新 • • 發佈:2019-01-07
給連結吧,這是個直接使用pcm資料來繪製圖形.
http://stackoverflow.com/questions/8325241/android-audio-fft-to-display-fundamental-frequency?rq=1
關鍵程式碼貼出來吧:
/Conversion from short to doubledouble[] micBufferData =newdouble[bufferSizeInBytes];//size may need to changefinalint bytesPerSample =2;// As it is 16bit PCMfinaldouble amplification =1.0;// choose a number as you likefor(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)就是低位的(是這樣定義的哦),