简体   繁体   中英

Why can't my program detect decibels over 90?

So, my main problem here is I don't yet know what the actual problem is. I have a two microphones, and when using either one, my program that detects the volume in db will only detect 90 max. I'm not sure if this is a limit on the microphone, or if it's the program, but it clearly shows that it's 90db and it won't go any higher, regardless of whether or not it is. If this proves helpful, the microphones I have are a cheap logitech webcam and a blue yeti microphone (not cheap).

In the event that it is the microphone, why exactly does this happen, and if not, well then here's the code I have for figuring out what the db level is:

AudioFormat audioFormat = getAudioFormat();
TargetDataLine targetDataLine;
try {
    targetDataLine = (TargetDataLine) AudioSystem.getTargetDataLine(audioFormat);
    targetDataLine.open();
    targetDataLine.start();
    byte [] buffer = new byte[2000];
    while (true) {
        int bytesRead = targetDataLine.read(buffer,0,buffer.length);
         if (bytesRead >= 0) {
             max = (int) (buffer[0] + (buffer[1] << 8));
             for (int p=2;p<bytesRead-1;p+=2) {
                 int thisValue = (int)(buffer[p] + (buffer[p+1] << 8));
                 if (thisValue > max) max = thisValue;
             }

         }
    }
} catch (LineUnavailableException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
currentDecibelLabel.setText(Integer.toString((int)(20 * Math.log10(max)))));

Any ideas? Thanks!!

EDIT: Just so you know, 'max' is a global int .

Your max variable is a short.

A short maximal value is 32767 (signed 16 bits/2 bytes value) according to oracle

20*log10(32767) = 90.3087

As you cast it to int, it's round to 90.

If the API gives you only 2 bytes to get the volume, and if ( and only if ) it's unsigned, you could define max as an int instead of double raising the maximum value to 96dB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM