Reading int values from audio input

hi all,

Is there any library which allows me to open an audio input (either a file or a real one) and read it as integers?
All the ways I’ve tried so far present the audio data as floats from -1.0 to +1.0.


1 Like

Here could be a solution :

You can use the function map() ( to map your audio data between -1.0 to 1.0 to an int range (for example 0 to 100):

int mapped_value = (int) map(0.5,-1.0,1.0,0,100);
//mapped_value = 75
1 Like

Or alternatively add 1 to value, multiply it by 50 and then round() it: :nerd_face:

final float value = .5;
final int mapped = round((value + 1.0) * 50.0);
println(mapped); // 75
1 Like

Use JavaSound directly and you’ll get integers. Well, raw bytes that are probably shorts. If you want integer values far better to get the raw value from the device/file than convert to float and back again.

But, why do you want integers out of interest? Normalised floats are in general far more useful, hence why most libraries at a higher level give you them.

1 Like

Thanks everyone. I managed to get what I wanted by doing

map (in.left.get(x), -1.0, 1.0, -32768, 32768));

I’m decoding non-audio data being sent over SPDIF, which is why the floats aren’t as useful. Maybe I’ll try the JavaSound approach.

1 Like

I assume you have some margin of error in the values, but note strictly speaking the range of a 16-bit signed sample is -32768 to 32767. There’s a good chance the float conversion maps -1.0:1.0 to -32767:32767.

1 Like

Oh yes, thank you. I should have realised that. I can feed it a self-generated file and see what numbers map to what.

At the moment it’s working fine for this exercise. The audio contains multiple streams of data that are one bit wide and I’m only looking at one of them at the moment, so I only need to detect one bit flipping back and forth.