YOUR  
AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding);  
returns -2 value which crashes while allocation of buffer = new 
short[buffersizebytes];

Can you Explain the reason?

On Saturday, May 22, 2010 11:23:34 AM UTC+6, BobG wrote:
>
> try this, turn on audio record permission 
>
> package com.aiti.vumeter; 
> //vumeter 
> //May 21 2010 Bob Gardner at aol.com 
>
> import java.util.Timer; 
> import java.util.TimerTask; 
>
> import android.app.Activity; 
> import android.content.Context; 
> import android.graphics.Canvas; 
> import android.graphics.Color; 
> import android.graphics.Paint; 
> import android.media.AudioFormat; 
> import android.media.AudioRecord; 
> import android.os.Bundle; 
> import android.os.SystemClock; 
> import android.util.DisplayMetrics; 
> import android.view.View; 
> import android.widget.TextView; 
>
> public class vumeter extends Activity { 
>         public AudioRecord audioRecord; 
>         public VuView vuview; 
>     public Timer timer; 
>     public int channelConfiguration = 
> AudioFormat.CHANNEL_CONFIGURATION_MONO; 
>     public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 
>         public int mSamplesRead;        //how many samples read 
>         public int buffersizebytes; 
>         public int buflen; 
>         public int scrwidth; 
>         public int scrheight; 
>         public int cursamprate; 
>         public final int SAMPPERSEC = 48000; //samp per sec 8000, 11025, 
> 22050 44100 or 48000 
>         public float vin;    //input volts 
>         public float iavg;   //avg of input volts 
>         public float ipk;    //peak input volts 
>         public float isum;   //sum of ans of input volts 
>         public float idbavg; //db below 0 of avg input volts 
>         public float idbpk; 
>         public float pixperdb; 
>         public float pixperdiv; 
>         public final float rangedb=60.0f; 
>         public final float dbperdiv=3.0f; 
>         public final float barwid=30f; 
>         public float scrbot; 
>         public long t1,t2,dt, t3,t4,dt2; 
>         public short[] buffer; //+-32767 
>
>     /** Called when the activity is first created. */ 
>     @Override 
>     public void onCreate(Bundle savedInstanceState){ 
>         super.onCreate(savedInstanceState); 
> //      setContentView(R.layout.main); 
>         DisplayMetrics metrics = new DisplayMetrics(); 
>         getWindowManager().getDefaultDisplay().getMetrics(metrics); 
>         scrwidth=metrics.widthPixels;   // 
>         scrheight=metrics.heightPixels; // 
>         scrbot=scrheight-100;     //480-100->380 
>         pixperdb=scrbot/rangedb;  //380/60->5 
>               pixperdiv=dbperdiv*pixperdb; //3*5->15 
>
>         buffersizebytes = 
> AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding); 
> // 
> 4096 on ion 
>         buffer = new short[buffersizebytes]; //4096 
>         buflen=buffersizebytes/2;            //2048 
>         audioRecord = new 
> AudioRecord(android.media.MediaRecorder.AudioSource.MIC,SAMPPERSEC, 
>                     channelConfiguration, audioEncoding, 
> buffersizebytes); //constructor 
>         cursamprate=audioRecord.getSampleRate(); 
>         vuview = new VuView(this); 
>             setContentView(vuview); 
>
>             timer = new Timer(); 
>             timer.schedule(new TimerTask(){ 
>                     @Override 
>                     public void run(){ 
>                             TimerMethod(); 
>                     } 
>             }, 0, 200); //every 200 ms 
> //            trigger(); //grab samples and calc db 
>     }//oncreate 
>
>     private void TimerMethod(){ 
>             //This method is called directly by the timer 
>             //and runs in the same thread as the timer. 
>             //We call the method that will work with the UI 
>             //through the runOnUiThread method. 
>             this.runOnUiThread(Timer_Tick); 
>     } 
>
>     private Runnable Timer_Tick = new Runnable(){ 
>             public void run() { 
>                 //This method runs in the same thread as the UI. 
>                 //Do something to the UI thread here 
>                     trigger(); 
>             } 
>     }; 
>
>     //-------------------------------------- 
>     public void acquire(){ 
>         try { 
>             audioRecord.startRecording(); //48khz 
>         } catch (Throwable t) { 
> //          Log.e("AudioRecord", "startrecording Failed"); 
>         } 
> //            SystemClock.sleep(50L);       //one period of 20 hz 
>          mSamplesRead = audioRecord.read(buffer, 0, buflen); 
>          try{ 
>            audioRecord.stop(); 
>          } catch (Throwable t) { 
> //           Log.e("AudioRecord", "stoprecording Failed"); 
>          } 
>     } 
>
>     //----------------------- 
>     public void avg(){ 
>     int i; 
>     float ftmp; 
>
>         ipk=0f; 
>         isum=0f; 
>                 for(i=0; i < buflen; i++){      //2048 samples 
>                         vin=(float)buffer[i];       //get a sample 
>                         ftmp=Math.abs(vin);         //abs of input sample 
>                         isum += ftmp;               //sum of input samples 
>                         if(ftmp > ipk) ipk=ftmp;    //remember input peak 
>                 } 
>                 iavg=isum/buflen;                               //avg of 
> input 
> samples 
>                 idbavg= (float)(20.0*Math.log10(iavg/32767.0)); //db below 
> clip of 
> input signal 
>                 idbpk=  (float)(20.0*Math.log10(ipk/32767.0));  //db of 
> input signal 
>     }//avg 
>
>     //-------------------------------- 
>     public void trigger(){ 
>         t1=System.currentTimeMillis(); 
>         acquire();           //grab buffer full of samples 
>         avg();               //calc avg and db 
> //        dump(); 
>         vuview.invalidate(); //tell os to call ondraw 
>                t2=System.currentTimeMillis(); 
>         dt=t2-t1; 
>         t1=t2; 
>     }//trigger 
>
>     //-------------------------------------- 
>         void dump(){ 
>             TextView tv = new TextView(this); 
>             setContentView(tv); 
>             tv.setTextColor(Color.WHITE); 
>             tv.setText("buffersizebytes "+buffersizebytes+"\n"); 
>         for (int i = 0; i < buflen; i++){ 
>                 tv.append(" "+buffer[i]); 
>         } 
>     }//dump 
>
>     //-------lifecycle callbacks------------------- 
>     @Override 
>     public void onResume(){ 
>              super.onResume(); 
>              trigger(); 
>     }//onresume 
>
>     @Override 
>     public void onPause(){ 
>         super.onPause(); 
>         audioRecord.stop(); 
>     }//onpause 
>
>     @Override 
>     public void onStop(){ 
>         super.onStop(); 
>         audioRecord.release(); 
>     }//onstop 
>
> //    @Override 
> //    public boolean onTouchEvent(MotionEvent motionevent){ 
> //              if(motionevent.getAction()==MotionEvent.ACTION_DOWN){ 
> //                      trigger(); //acquire buffer full of samples 
> //              } 
> //              return true; 
> //    } 
>
>         //-------------------------------------- 
>         private class VuView extends View { 
>                 public VuView(Context context) { 
>                         super(context); 
>                 } 
>
>                 @Override 
>                 protected void onDraw(Canvas canvas) { 
>                         super.onDraw(canvas); 
>                      t3=System.currentTimeMillis(); 
>                         graticule(canvas); 
>                         drawvu(canvas); 
>                      t4=System.currentTimeMillis(); 
>                 dt2=t4-t3; 
>                 t3=t4; 
>                  }//ondraw 
>
>                 //------------------------------------------------ 
>                 public void graticule(Canvas canvas){ //draw graticule 
>                 int i; 
>         float TL=7; 
>
>             Paint paint = new Paint(); 
>                         paint.setColor(Color.BLACK); 
>                         canvas.drawPaint(paint); //clear screen 
>
>                         paint.setColor(Color.GRAY); 
>                         paint.setStyle(Paint.Style.STROKE); 
>                         paint.setStrokeWidth(1); 
>                         canvas.drawLine(0f,0f,0f,scrbot, paint); //y axis 
> (vertical) 
>                         for(i=0; i < (int)scrbot; i++){ 
>                                 if((i % (int)pixperdiv)==0){ 
>                                         canvas.drawLine(0,i,TL,i, paint); 
> //y axis tics (horizontal) 
>                                 } 
>                         } 
>                 }//graticule 
>
>                 //----------------------------------------- 
>                 public void drawvu(Canvas canvas){ //draw vu 
>                 float x; 
>                 float ftmp; 
>
>                         Paint paint = new Paint(); 
>                         paint.setColor(Color.GREEN); 
>                         paint.setStyle(Paint.Style.STROKE); 
>                         paint.setStrokeWidth(1); 
>                         x=20f; //left side of vu 
>                         ftmp=Math.abs(idbpk)*pixperdb; //abs of db, cvt to 
> pix 
>                         if(ftmp > scrbot) ftmp=scrbot; //clip at bottom 
>                         canvas.drawLine(x, ftmp, x+barwid, ftmp, paint); 
> //db peak bar 
>
>                         paint.setStyle(Paint.Style.FILL); 
>                     canvas.drawRect(x,(float)Math.abs(idbavg)*pixperdb, 
> x+barwid, 
> scrbot, paint); //input db avg 
>
>                         paint.setColor(Color.WHITE); 
>                         paint.setTextSize(15); 
>                     canvas.drawText("ms     " + dt,       100, 20, paint); 
>  // 
>                     canvas.drawText("ms     " + dt2,      100, 40, paint); 
> // 
>                     canvas.drawText("db/div " + dbperdiv, 100, 60, paint); 
>                     canvas.drawText("range  " + rangedb,  100, 80, paint); 
>                     canvas.drawText("avg    " + idbavg,   100, 100, 
> paint); 
> //                    canvas.drawText("ht " + scrheight,    100, 120, 
> paint); 
> //                    canvas.drawText("sr     " + cursamprate,100,120, 
> paint); 
>                 }//drawvus 
>         }//graphview 
> }//activity 
> //-----------------------eof----------------------- 
>
> -- 
> You received this message because you are subscribed to the Google 
> Groups "Android Developers" group. 
> To post to this group, send email to 
> android-d...@googlegroups.com<javascript:> 
> To unsubscribe from this group, send email to 
> android-developers+unsubscr...@googlegroups.com <javascript:> 
> For more options, visit this group at 
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to