[Audio processing] wav音频文件读取int和double数组的关系
直接读取wav文件是int数组,但是有一些实现返回的是double数组,还有些输入是double数组;那我们要互相调用的时候还是要看看两者到底有什么关系,其实很简单。
以单身道,16bit为例
/** * For Mono, 16bit, only! * @author karl * */ public class BasicIntDoubleConvetor { private static final float floatScale = 32768; public double[] arrayIntToDouble (int[] data) { int numFrames = data.length; double[] postdata = new double[numFrames]; for (int f=0 ; f<numFrames ; f++) { postdata[f] = (double) data[f] / floatScale; } return postdata; } public int[] arrayDoubleToInt (int[] data) { int numFrames = data.length; int[] postdata = new int[numFrames]; for (int f=0 ; f<numFrames ; f++) { postdata[f] = (int) (data[f] * floatScale); } return postdata; } }
对于更广泛的情况来说,floatScale并不是2^16 = 32768
// Calculate the scaling factor for converting to a normalised double if (wavFile.validBits > 8) { // If more than 8 validBits, data is signed // Conversion required multiplying by magnitude of max positive value wavFile.floatOffset = 0; wavFile.floatScale = Long.MAX_VALUE >> (64 - wavFile.validBits); } else { // Else if 8 or less validBits, data is unsigned // Conversion required dividing by max positive value wavFile.floatOffset = 1; wavFile.floatScale = 0.5 * ((1 << wavFile.validBits) - 1); }