Get amplitude from MediaPlayer using Visualizer

寵の児 提交于 2021-02-07 17:24:27

问题


i've been reading another posts about calculate the amplitude in real time from a Mediaplayer, but i have no clear how to get a value useful for me. What i need is a linear amplitude value normalize between 0-100, but as i've watched in another posts they are performing a db calculation which has not much sense, cause they are not normalized to max 0dB value (from How to calculate the audio amplitude in real time (android)):

double amplitude = 0;
for (int i = 0; i < audioData.length/2; i++) {
    double y = (audioData[i*2] | audioData[i*2+1] << 8) / 32768.0
    // depending on your endianness:
    // double y = (audioData[i*2]<<8 | audioData[i*2+1]) / 32768.0
    amplitude += Math.abs(y);
}
amplitude = amplitude / audioData.length / 2;

I've watched that for calculate de dB, i should do as below (from How to compute decibel (dB) of Amplitude from Media Player? )

double sum=0;
for (int i = 0; i < audioData.length/2; i++) {
    double y = (audioData[i*2] | audioData[i*2+1] << 8) / 32768.0;
    sum += y * y;
}
double rms = Math.sqrt(sum / audioData.length/2);
dbAmp = 20.0*Math.log10(rms);

I've tried for that solution but the real time values are near to 0 but sometimes are over than 0, i mean, something between -Inifinit (no sound) to 1.2 (if i avoid 20.0* multiply) or anything else from than order. Anyway, i'd like to obtain a normalized value [0-100], not a dB value.

来源:https://stackoverflow.com/questions/40584680/get-amplitude-from-mediaplayer-using-visualizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!