byte[]和double[]如何互相转换
处理音频数据时,比如做频谱展示、音频滤波、3d音乐、音效均衡器的时候需要把pcm的byte数据转换为浮点数据(这里用的是double,也可以是short,float),java使用ByteBuffer就很简单了。
- 稍微讲解一下计算
这里为啥要除以32768,首先我简单的举个例子,假设我们是双声道的16位采样音频,每16位是一个声道,也就是两字节(java里就是一个short),这里使用的大端序BIG_ENDIAN,实际上就是高8位+低8位进行合并,之后需要除以16位的最大值即2的15次方 2^15=32768,得出的结果也就是double(也就是所谓的浮点数)
代码如下
/**
* byte转double
*
* @param data
* @return
*/
public double[] byteToDouble(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
int i = 0;
double[] dData = new double[data.length / 2];
while (buf.remaining() > 0) {
short s = buf.getShort();
dData[i] = (double) s / 32767f; // real
++i;
}
return dData;
}
/**
* double转byte
*
* @param data
* @return
*/
public static byte[] doubleToByte(double[] data) {
// 有的音频数据有问题,左右声道会缺一个,可以补全
// int length = data.length;
// if (length % 2 != 0) {
// length++;
// }
ByteBuffer buf = ByteBuffer.allocate(data.length * 2);
buf.order(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < data.length; i++) {
if (data[i] > 1f) {
data[i] = 1f;
}
if (data[i] < -1f) {
data[i] = -1f;
}
buf.putShort((short) (data[i] * 32767f));
}
return buf.array();
}
- 那么问题来了,双声道立体声想单独处理某个声道数据该怎么处理?
代码如下
/**
* 立体声
* @param data
* @return
*/
public static double[][] byteToDouble2(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
int i = 0;
double[][] dData = new double[2][data.length/2/2];
while (buf.remaining() > 0) {
short leftS = buf.getShort();
short rightS = buf.getShort();
dData[0][i] = (double) leftS / 32767f;
dData[1][i] = (double) rightS / 32767f;
++i;
}
return dData;
}
/**
* 立体声
* @param data
* @return
*/
public static byte[] doubleToByte2(double[][] data) {
ByteBuffer buf = ByteBuffer.allocate(data[0].length * 2 * 2);
buf.order(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < data[0].length; i++) {
if (data[0][i] > 1f) {
data[0][i] = 1f;
}
if (data[0][i] < -1f) {
data[0][i] = -1f;
}
if (data[1][i] > 1f) {
data[1][i] = 1f;
}
if (data[1][i] < -1f) {
data[1][i] = -1f;
}
buf.putShort((short) (data[0][i] * 32767f));
buf.putShort((short) (data[1][i] * 32767f));
}
return buf.array();
}
返回 double[][] 二维数组,第一维是左声道,第二维是右声道。
参考下图,以16位音频为例

两个8bit,分别低8bit+高8bit=16bit=1个声道,假如是左声道,那下一个16位则是右声道,以此类推。
续写:
上面代码已经优化了有噪音的bug,有很多人知道了转double,还是不知道怎么转float,其实和double一样的,今天干脆,把float也加上,代码如下
/**
* byte转float
*
* @param data
* @return
*/
public float[] byteToFloat(byte[] data) {
ByteBuffer buf = ByteBuffer.wrap(data);
buf.order(ByteOrder.BIG_ENDIAN);
int i = 0;
float[] dData = new float[data.length / 2];
while (buf.remaining() > 0) {
short s = buf.getShort();
dData[i] = (float) s / 32767f; // real
++i;
}
return dData;
}
/**
* float转byte
*
* @param data
* @return
*/
public static byte[] floatToByte(float[] data) {
// 有的音频数据有问题,左右声道会缺一个,可以补全
// int length = data.length;
// if (length % 2 != 0) {
// length++;
// }
ByteBuffer buf = ByteBuffer.allocate(data.length * 2);
buf.order(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < data.length; i++) {
if (data[i] > 1f) {
data[i] = 1f;
}
if (data[i] < -1f) {
data[i] = -1f;
}
buf.putShort((short) (data[i] * 32767f));
}
return buf.array();
}
java甚至还有几种写法,我再举个例子,代码如下
//可以这样
public static float[] byteToFloat(byte[] bytes) {
short[] shorts = new short[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
float[] fData = new float[shorts.length];
for (int i = 0; i < shorts.length; i++) {
fData[i] = (float) (shorts[i] / 32767f);
}
return fData;
}
public static byte[] floatToByte(float[] data) {
int length = data.length;
if (length % 2 != 0) {
length++;
}
ByteBuffer outBuffer = ByteBuffer.allocate(length * 2);
outBuffer.order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < data.length; i++) {
if (data[i] > 1f) {
data[i] = 1f;
}
if (data[i] < -1) {
data[i] = -1;
}
outBuffer.putShort((short) (data[i] * 32767f));
}
return outBuffer.array();
}
好了,有什么问题评论留言。
本文介绍了在音频处理场景下,Java中如何将PCM的byte数据转换为double或float浮点数,以及如何将浮点数转换回byte数据。涉及到双声道立体声的处理,并提供了优化的代码示例,确保数据在-1到1之间。同时,还给出了byte到float以及float到byte的转换方法。
1256

被折叠的 条评论
为什么被折叠?



