音频byte[]和double[]、short[]、float[]、浮点数转换

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

好了,有什么问题评论留言。

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值