0

I have a function which plays a sound in Java using a variable vol as you can see in the code below. This variable vol is a decimal between 0 and 1. When I play a 0, the sound is basically the lowest volume possible, and when I play it with a 1, it is as loud as possible. How can I know what decibles the program's sound is making? I want to know the decibles for a given vol.

public static final AudioFormat DEFAULTAUDIOFORMAT = new AudioFormat(44100, 16, 2, true, true);

private void generateTone() {
        int channels = DEFAULTAUDIOFORMAT.getChannels();
        int sampleSizeInBytes = DEFAULTAUDIOFORMAT.getSampleSizeInBits() /8;
        float sampleRate = DEFAULTAUDIOFORMAT.getSampleRate();
        int timedSample = (int) (sampleRate * msecs / 1000f);
        int len = timedSample * channels * sampleSizeInBytes;
        int shapeLen = (int) (.05 * timedSample);
        buf = new byte[len];
        double scale;
        for ( int i = 0, j = 0; i < timedSample; ++i )
        {
            int wave = (int)(vol * 32767.0 * Math.sin(2.0 * Math.PI * hz * i / sampleRate));
            scale = 1.0;
            if (i <= shapeLen) {
                scale = i/(double)shapeLen;
            }
            else if (i >= timedSample - shapeLen) {
                scale = 1.0-((i- timedSample + shapeLen)/(double)(shapeLen-1));
            }
            wave = (int)(scale * wave);
            buf[j++] = (byte)(wave >>> 8);
            buf[j++] = (byte) wave;
            buf[j++] = (byte)(wave >>> 8);
            buf[j++] = (byte) wave;
         }
    }
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 2
    By putting a meter in front of the speaker? Java has no way of knowing how much amplification is applied after generating the signal.... – Affe Nov 15 '12 at 19:30

2 Answers2

1

Step 1: Find an SPL meter. If you have any kind of manufacturing or lab operations, the safety guy should have one or know who does. If not, you could hang out in the parking lot after work and see who has a car with a stupidly loud stereo and ask them.

Step 2: Establish a reference that is meaningful to the users. Mobile phone ringer? Maybe measure in open air, one meter from the device. (perhaps also in the pocket of a pair of jeans... :) .) Automotive Device? measure inside a car at the common distance from device to driver's seat. Manufacturing? Measure over a cemet floor at the common distance the opeartor sits. You get the idea.

Step 3: Set up your meter and take reference measurements. Since it's just a guesstimate I'd probably go for around ten readings from min to max then just interpolate straight lines between the points. You could fit a curve if you're a math geek.

Step 4: Put a lookup-table in your software that finds reference-SPL based on Vol level using your data from before.

Step 5: Explain somewhere in something like your about box the reference environment used for measurements.

Step 6: Put a HUGE disclaimer on your product that sound output values are reference estimates only and do not certify that output levels meet any occupational safety guidelines.

Not really anything to do with java, but:

Decibels of Sound Pressure Level is a ratio of the pressure generated in the air at a particular spot relative to the quiestest sound a normal person can hear.

It is not some absolute quantity you can state about a sound system like voltage or power dissipated. Saying "This is a 90 dB-SPL signal" is meaningless. A statement for reference would have to be more like:

"This is a 90dB-SPL signal measured at 2 meters from the loudspeaker, on axis, in a 3mx3m deadened studio."

You would need to measure the complete system in a controlled environment, then you would be able to develop an empirical model for predicting how your control signal in source code relates to pressure at the system's output.

Affe
  • 47,174
  • 11
  • 83
  • 83
0

Decibels are defined as 10 * Math.log10(v1/v2), where v1 is the value of the current sound volume and v2 is the reference lowest sound level.

remigio
  • 4,101
  • 1
  • 26
  • 28