0

As starting point I have a method that works. But it seems to have too many lines or overlapping actions. If you are audio wizard then you see immediately how to boil this down. So how to boil this down?

public static byte[] float16toDualByte(byte[] twoPlaces, float f_val) {

    short val_as_short = (short) (f_val * 32768);//signed short.16bit.

    twoPlaces[0] = (byte) (val_as_short >>> 8);
    twoPlaces[1] = (byte) val_as_short;

    ByteBuffer buf = ByteBuffer.wrap(twoPlaces);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    short turned = buf.asShortBuffer().get(0);

    twoPlaces[0] = (byte) (turned >>> 8);
    twoPlaces[1] = (byte) turned;
    return twoPlaces;

}
Tonecops
  • 127
  • 9

1 Answers1

0

I found at sof the idea that turn upscaled float first to int. And it worked! This method gives array of two bytes ready to write into javax.sound.sampled.SourceDataLine. Those two bytes represent one mono frame. One thing to understand with SourceDataLine is that name is given from the physical soundcard standpoint. That little man inside soundcard waiting to pass sample to connector. From user perspective it means the output to soundcard.

  public byte[] simpleSwap(byte[] twoPlaces, float f_val) {

    int val_as_short = (int) (f_val * 32768);//signed short.16bit as int.
    int swapped = ((val_as_short >> 8) & 0xff) | ((val_as_short & 0xff) << 8);
    twoPlaces[0] = (byte) (swapped >>> 8);
    twoPlaces[1] = (byte) swapped;
    return twoPlaces;

}
Tonecops
  • 127
  • 9