Microphone Level in Java

I am trying to access the microphone level through Java. I don’t need to record anything, I just want to know the relative scale of the sound level.

Is this possible in real time?

If this is not possible, it may work: Start recording when the level exceeds a certain value, stop when the level drops at a certain level within a certain time. Record a quarter of a second bit and read its volume, and if it is under a threshold recording stop.

Thank you in advance

+4
source share
2 answers

You can access the microphones through the Sound API, but this will not give you a simple volume level. You just need to capture the data and make your own decision about how loud it is.

http://download.oracle.com/javase/tutorial/sound/capturing.html

Recording involves saving data, but here you can drop the data as soon as you finish determining its volume.

Method The RMS method is a good way to calculate the amplitude of the wave data section.

In response to your comment, yes, you would capture a small data length (maybe just a few milliseconds) and calculate the amplitude of this. Repeat this periodically depending on how often you need updates. If you want to track previous volumes and compare them, then up to you - at the moment it's just a comparison of numbers. You can use the average of the latest volumes to calculate the external volume in the room so that you can detect a sharp increase in noise.

I don't know how much overhead it takes to turn audio capture on and off, but you might be better off keeping TargetDataLine open all the time and just calculating the volume when you need it. While the line is open, you need to continue to call read (), although otherwise the application freezes, waiting for you to read the data.

+4
source

http://www.technogumbo.com/tutorials/Java-Microphone-Selection-And-Level-Monitoring/Java-Microphone-Selection-And-Level-Monitoring.php

Pretty good article about this. Helped me a lot.

From what I can tell, this uses the root RMS stuff that was talked about in @Nick answer

Basically:

 public int calculateRMSLevel(byte[] audioData) { long lSum = 0; for(int i=0; i < audioData.length; i++) lSum = lSum + audioData[i]; double dAvg = lSum / audioData.length; double sumMeanSquare = 0d; for(int j=0; j < audioData.length; j++) sumMeanSquare += Math.pow(audioData[j] - dAvg, 2d); double averageMeanSquare = sumMeanSquare / audioData.length; return (int)(Math.pow(averageMeanSquare,0.5d) + 0.5); } 

and use:

 int level = 0; byte tempBuffer[] = new byte[6000]; stopCapture = false; try { while (!stopCapture) { if (targetRecordLine.read(tempBuffer, 0, tempBuffer.length) > 0) { level = calculateRMSLevel(tempBuffer); } } targetRecordLine.close(); } catch (Exception e) { System.out.println(e); System.exit(0); } 
+6
source

All Articles