How do you determine data type of AudioData samples?

So this is what I understand AudioData can be an AudioBuffer or and AudioStream this can easily be determined using getDataType(). Also it is verry easy to know how many bytes you need to read for each sample and each channel using getBitsPerSample() and getChannels().

if bits per sample is 8 then we are dealing with ubyte.
if bits per sample is 16 we are dealing with short.
if bits per sample is 32 we are dealing with an int or float ← this is the issue.
if bits per sample is 64 we are dealing with double.

Here is a example of how I am converting these values right now. but in the case that the value is 32 bit i need to know when to convert and when to just accept the value as float.

public static final float BYTE_SCALE = 1.0f / 128;
public static final float SHORT_SCALE = 1.0f / Short.MAX_VALUE;
public static final float INT_SCALE = 1.0f / Integer.MAX_VALUE;

public float sampleToFloat(byte sample) {
	return ((0x000000ff & sample) - 128) * BYTE_SCALE;
}

public float sampleToFloat(short sample) {
	return sample * SHORT_SCALE;
}

public float sampleToFloat(int sample) {
	return sample * INT_SCALE;
}

public float sampleToFloat(double sample) {
	return (float)sample;
}
/*
public float sampleToFloat(Double sample) {
	return sample.floatValue();
}
*/

EDIT:
I just looked at grepcode and noticed that the Double.floatValue() is only doing a simple cast to float…LOL

No please ignore that I am not taking into consideration that the scale for lower and upper bounds are not the same :stuck_out_tongue:

Any suggestions?

I may have found a solution but not really…

find the mantissa aka. Fractional Part

public boolean couldBeFloat(int sample) {
	return (sample & 0x001fffff) != 0;
}

jME3 only supports 8 / 16 bits per sample so you don’t need to worry about anything higher than that.

Oh wow I was looking at wav and ogg using PCM data and reading the specification… sorry for this topic.

While we are on the sound sample subject it is unsigned for 8-bit right?

My understanding is that all sound is signed because it is a wave. However I would recommend avoiding 8-bit sound samples in general as the quality is too low. Encode your samples in 16-bit with a high bitrate OGG/Vorbis and you’ll be fine.

Thanks I’ll figure something out as I am not going to be the one supping the audio :stuck_out_tongue: