Microphone Input Level

All your problems with Audio, Video and Images.

Microphone Input Level

Postby Skeniver » Thu Jan 21, 2010 2:39 pm

Hi there

I was wondering how I could check the volume level from the mic... If that makes sense. IE: In a quiet room say it returns a 0, but when someone speaks it would return 10!

A thread was started here, but hasn't had much luck: http://www.anddev.org/viewtopic.php?p=25656

Basically, I want to measure a normal level and then trigger an event when there is more noise.

I have tried a little of what was posted in the mentioned thread:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AudioRecord ar;
  3.  
  4.    
  5.  
  6.     boolean running;
  7.  
  8.        
  9.  
  10.     @Override
  11.  
  12.     public void onCreate(Bundle savedInstanceState) {
  13.  
  14.         super.onCreate(savedInstanceState);
  15.  
  16.         setContentView(R.layout.main);
  17.  
  18.        
  19.  
  20.         running = false;
  21.  
  22.        
  23.  
  24.         int src = AudioSource.MIC;
  25.  
  26.         int sr = 44100;
  27.  
  28.         int ch = AudioFormat.CHANNEL_CONFIGURATION_DEFAULT;
  29.  
  30.         int af = AudioFormat.ENCODING_PCM_16BIT;
  31.  
  32.        
  33.  
  34.         try {
  35.  
  36.                 ar = new AudioRecord(src,sr,ch,af,AudioRecord.getMinBufferSize(sr, ch, af));
  37.  
  38.                 startListening();
  39.  
  40.         } catch (Exception e) {
  41.  
  42.                 e.printStackTrace();
  43.  
  44.         }
  45.  
  46.     }
  47.  
  48.    
  49.  
  50.     public void startListening() {
  51.  
  52.         ar.startRecording();
  53.  
  54.         Thread t = new Thread() {
  55.  
  56.                 public void run() {
  57.  
  58.                         int test = 2;
  59.  
  60.                         test++;
  61.  
  62.                         for (int y = 0; y < 5; y ++) {
  63.  
  64.                                 short[] sho = new short[1];
  65.  
  66.                                 sho[0] = 10;
  67.  
  68.                                 int x = ar.read(sho, 256, 256);
  69.  
  70.                                 if (x > 0)
  71.  
  72.                                         tHandler.sendMessage(Message.obtain(tHandler, 1));
  73.  
  74.                         }
  75.  
  76.                 }
  77.  
  78.         };
  79.  
  80.         t.start();
  81.  
  82.     }
  83.  
  84.    
  85.  
  86.     private Handler tHandler = new Handler() {
  87.  
  88.         @Override
  89.  
  90.         public void handleMessage(Message msg) {
  91.  
  92.                 switch (msg.what) {
  93.  
  94.                         case 1:
  95.  
  96.                                 TextView tv = (TextView)findViewById(R.id.textLabel);
  97.  
  98.                                 tv.setText("High");
  99.  
  100.                                 ar.stop();
  101.  
  102.                                 break;
  103.  
  104.                 }
  105.  
  106.         }
  107.  
  108.     };
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


The values (particularly the ones used in ar.read()), are all thumb sucked. I have no clue what I was inputing or what I was expecting to get out!

Does anyone have any insight for me?

Thanks in advance
Neil
Follow me on twitter -> http://twitter.com/Skeniver
Apps & blog -> http://www.formidableapps.co.uk
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Top

Postby kanulo » Fri Jan 29, 2010 1:10 am

Any idea? i have the same problem.

Thanks.
kanulo
Freshman
Freshman
 
Posts: 2
Joined: Fri Jan 29, 2010 1:08 am

Postby stealthcopter » Tue Mar 30, 2010 4:23 pm

When I run the code you have here I get an error on this line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ar = new AudioRecord(src,sr,ch,af,AudioRecord.getMinBufferSize(sr, ch, af));
  3.  
  4.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


and if I check the value of
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AudioRecord.getMinBufferSize(sr, ch, af)
  3.  
  4.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4


I get -2 which clearly wont work. Not sure what's going on, I will try just putting a manual buffer size in, although I don't know what kind of size is a good idea.
stealthcopter
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sun Jan 17, 2010 11:42 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 19 guests