gpt4 book ai didi

java - 字节数组到短数组并在java中再次返回

转载 作者:IT老高 更新时间:2023-10-28 20:42:09 26 4
gpt4 key购买 nike

我在获取存储在字节数组中的音频数据、将其转换为大端短数组、对其进行编码、然后将其更改回字节数组时遇到了一些问题。这就是我所拥有的。原始音频数据存储在 audioBytes2 中。我使用相同的格式进行解码,但在 cos 函数上加减号。不幸的是,更改字节和短数据类型是不可协商的。

    short[] audioData = null;
int nlengthInSamples = audioBytes2.length / 2;
audioData = new short[nlengthInSamples];

for (int i = 0; i < nlengthInSamples; i++) {
short MSB = (short) audioBytes2[2*i+1];
short LSB = (short) audioBytes2[2*i];
audioData[i] = (short) (MSB << 8 | (255 & LSB));
}

int i = 0;
while (i < audioData.length) {
audioData[i] = (short)(audioData[i] + (short)5*Math.cos(2*Math.PI*i/(((Number)EncodeBox.getValue()).intValue())));
i++;
}

short x = 0;
i = 0;
while (i < audioData.length) {
x = audioData[i];
audioBytes2[2*i+1] = (byte)(x >>> 0);
audioBytes2[2*i] = (byte)(x >>> 8);
i++;
}

我已经尽我所能来完成这项工作,但我最接近的方法是让它在所有其他编码/解码中工作,我不知道为什么。感谢您的帮助。

最佳答案

我也建议你试试 ByteBuffer。

byte[] bytes = {};
short[] shorts = new short[bytes.length/2];
// to turn bytes to shorts as either big endian or little endian.
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

// to turn shorts back to bytes.
byte[] bytes2 = new byte[shortsA.length * 2];
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortsA);

关于java - 字节数组到短数组并在java中再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com