作为起点,我有一种有效的方法。但似乎行太多或动作重叠。如果您是音频向导,则可以立即看到如何将其简化。那么如何把它煮下来呢?
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;
}
我发现这样的想法首先将浮点数转换为整数。而且有效!此方法提供了两个字节的数组,可以准备写入javax.sound.sampled.SourceDataLine。这两个字节代表一个单帧。SourceDataLine需要了解的一件事是从物理声卡的角度给出名称。声卡中的那个小男人正在等待将样本传递给连接器。从用户的角度来看,这意味着输出到声卡。
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;
}