How to compute magnitude from raw pcm data?

527 views Asked by At

My Android application periodically captures raw pcm audio data from the speaker. The packet size is 128 bytes.

When there is no audio, all the bytes in the packet contain a value of -128. When there is audio, the max value for a byte is +127.

Here is my pseudo-code to compute the amplitude (between 0.0 and 1.0).

 long sum = 0;
 for(int i=0;i<wave.length;i++) {
    sum += ((int) wave[i] + 128);
 }

 float amp = ((float) sum)/((float) 256 * wave.length);

Using this algorithm, it appears the amplitude does not change much whether I speak softly or loudly. Makes me think this algorithm is not right.

I am wondering if my understanding of amplitude calculation is not correct. I would appreciate it if someone can suggest me a better algorithm.

For Android folks, I am receiving data via Visualizer.OnDataCaptureListener interface and its onWaveFormData method.

Thank you in advance for your help.

0

There are 0 answers