开发者

convert from floatbuffer to byte[]

开发者 https://www.devze.com 2023-04-02 02:35 出处:网络
I\'m trying to find a way to use jack-audio in java. I\'ve managed to create wrapper based on jnajack to get audio from jacks\' port to java app (the original jnajack is not working with jack 1.9.8),

I'm trying to find a way to use jack-audio in java. I've managed to create wrapper based on jnajack to get audio from jacks' port to java app (the original jnajack is not working with jack 1.9.8), but I can't find a way to manipulate data. I'm gettin' a List<FloatBuffer>, and for further data manipulation I need to convert it to byte[].

To put put it simple... firstly I wan开发者_StackOverflow社区t to save data to a file, but java sound api as I understand can only save data from TargetDataLine and/or byte[]. How can I convert FloatBuffer to byte[]? The only way I can find is floatbuffer.get(float[]) and somehow (don't know how) convert float[] to byte[].


How can I convert FloatBuffer to byte[]?

FloatBuffer floatbuffer;

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

You may have to change the byte order.


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

I don't see it in jnajack's specs, but I imagine it represents the audio as floats between -1 and 1.

I'm not too familiar with the Java Sound API, but I could imagine it uses bytes ranged between -128 and 127.

In other words, you'd have to convert between the two by multiplying by 128.

I'd say the neat way to do this would be byte converted = Float.valueOf(original * 128).byteValue(). If needed it's probably possible to do this with only primitives, which should be faster.

0

精彩评论

暂无评论...
验证码 换一张
取 消