单个浮点转换为java签名的PCM wav是javax的缩写.sampled SourceDataLine有太多行?



作为起点,我有一个有效的方法。但它似乎有太多的台词或重叠的动作。 如果您是音频向导,那么您会立即看到如何将其归结。那么如何归结呢?

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;
}

我在 sof 上发现了将放大浮点数首先转换为 int 的想法。它奏效了! 此方法给出了两个字节的数组,准备写入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;
}

最新更新