android - how can i translate byte[] buffer to amplitude level -


i working library records audio , can listen each time new byte array if buffer if filled callback :

public void onvoicereceived(byte[] buffer) {  } 

now want take buffer , translate level can draw amplitude meter. how can translate data? dont want create recorder , use read() command.

this drawing code

    private void drawcircleview(canvas canvas, double ampvalue) {     // paint background color     canvas.drawcolor(android.r.color.holo_blue_bright);      // paint rectangular shape fill surface.     int border = 0;     rectf r = new rectf(border, border, canvas.getwidth(), canvas.getheight());     paint paint = new paint();     paint.setargb(255, 100, 0, 0); // paint color gray+semy transparent     canvas.drawrect(r, paint);      /*      * want paint circles, black , white. 1 of circles bounce, tile button 'swap' pressed , other circle begin bouncing.      */     calculateradiuses();     // paint left circle(black)     paint.setstrokewidth(0);     paint.setcolor(getresources().getcolor(android.r.color.holo_blue_light));     canvas.drawcircle(canvas.getwidth() / 2, canvas.getheight() / 2, ampvalue, paint); } 

thanks!

the byte[] buffer raw unformatted data. move forward, you'll need know format of data. how many bits per sample, endianness, , how many channels of data. 16 bits per sample common. assuming there 2 channels of data , 16 bits, bytes going arranged [ch1 hi byte, ch1 lo byte, ch2 hi byte, ch2 lo byte, ...] , on.

once information known can convert double. typically, double amplitude kept in range (-1.0, 1.0).

double[] samples = new double[buffer.length]; (int = 0; < buffer.length; ++i) {     int intsample = ((buffer[i*2] << 8) | buffer[i*2 + 1]) << 16;      samples[i] = intsample * (1/4294967296.0); // scale double (-1.0,1.0) } 

now crude level meter, first need decide if want peak meter or rms meter. peak meter find max of abs of samples.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -