Convert from floatbuffer to byte []

I am trying to find a way to use jack-audio in java. I managed to create a shell based on jnajack to get sound from the jacks port in a java application (the original jnajack does not work with jack 1.9.8), but I can not find a way to manipulate the data. I gettin 'a List<FloatBuffer>, and for further data processing I need to convert it to byte[].

Simply put, first I want to save the data to a file, but the java sound api, as I understand it, can only save data from TargetDataLineand / or byte[]. How to convert FloatBufferto byte[]? The only way I can find is to floatbuffer.get(float[])somehow (do not know how) to convert float[]to byte[].

+2
source share
2 answers

How to convert FloatBufferto byte[]?

FloatBuffer floatbuffer;

ByteBuffer byteBuffer = ByteBuffer.allocate(floatbuffer.capacity() * 4);
byteBuffer.asFloatBuffer().put(floatbuffer);
byte[] bytearray = byteBuffer.array();

You may need to reorder the bytes .

+5
source

So jnajack uses a different presentation of audio data than the standard api java sound functions.

I don't see it in the jnajack specs, but I think it presents the audio as floating between -1 and 1.

I am not very familiar with the Java Sound API, but I can imagine that it uses bytes in the range from -128 to 127.

In other words, you will need to convert between them by multiplying by 128.

I would say that a neat way to do this would be byte converted = Float.valueOf(original * 128).byteValue(). If necessary, perhaps this can only be done with primitives, which should be faster.

+1
source

All Articles