jaredcnance
jaredcnance

Reputation: 742

Java: Sound Capture and Waveform Plotting

Currently, I have developed a Java application in Eclipse based on the Audio Capture tutorial written by Richard G. Baldwin at http://www.developer.com/java/other/article.php/1579071/Java-Sound-Getting-Started-Part-2-Capture-Using-Specified-Mixer.htm

This works fine, from what I can tell.

I am able to collect data, and store it in a ByteOutputStreamObject.

I then want to take this data and plot it.

For plotting I am using the SWTChart plugin (swtchart.org).

However, all the values plotted are close to 0, -256, 256.

My collection AudioFormat is:

SampleRate = 8000.0F
SampleSize = 8 bits
Channels = 1
Signed = true
bigEndian = False

I am converting the ByteOutputStream through:

ySeries = toDoubleArray(byteArrayOutputStream.toByteArray());

...

public static double[] toDoubleArray(byte[] byteArray){
    int times = Double.SIZE / Byte.SIZE;
    double[] doubles = new double[byteArray.length / times];
    for(int i=0;i<doubles.length;i++){
        doubles[i] = ByteBuffer.wrap(byteArray, i*times, times).getShort();
    }
    return doubles;
}

A double format is the required input for

lineSeries.setYSeries(ySeries);

If I change the method above from getShort to getDouble, the resulting array is doubles but on the order of (+-) 10^300 - 10^320.

Can someone help me understand what I need to do here?

Upvotes: 0

Views: 372

Answers (1)

jaket
jaket

Reputation: 9341

Your audio format is 8 bit signed data (typically 8bit audio is unsigned so beware) so samples are in the range of -128 to 127. I assume that you are trying to rescale them to a double in the range of -1.0 to 1.0. To do so, for each byte you multiply each byte by 1/128.0. Note that due to the asymmetry you won't get all the way up to 1.0.

public static double[] toDoubleArray(byte[] byteArray){
    double[] doubles = new double[byteArray.length];
    for(int i=0;i<doubles.length;i++){
        doubles[i] = byteArray[i]/128.0;
    }
    return doubles;
}

Upvotes: 1

Related Questions