1

我正在尝试将 WAV 文件放在 AudioBuffer 中,以便我可以操作它。我之前从 AudioBuffer 创建了 WAV 文件,这需要将 Float32Array 转换为包含 Int16 值的 DataView。我使用了我拿起的这个方便的功能:

function floatTo16BitPCM(output, offset, input){
    for (var i = 0; i < input.length; i++, offset+=2){
        var s = Math.max(-1, Math.min(1, input[i]));
        output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
    }
}

好吧,我现在需要做的就是反转这个(WAV 文件是从服务器加载的,所以我没有原始数据了)。我无法弄清楚该函数中实际发生了什么或数据是如何转换的。

4

2 回答 2

8

这似乎有效。我在响应类型为“arraybuffer”的 ajax 调用中加载数据。否则,响应最终会变成一个字符串,并且处理起来很混乱。然后我转换为 16 位数组。然后我以与 WAV 编码一起使用的方式将其转换为 Float32 数组。我还需要扔掉 WAV 的标题,以及它末尾的一些元数据。

// These are ready to be copied into an AudioBufferSourceNode's channel data.
var theWavDataInFloat32;

function floatTo16Bit(inputArray, startIndex){
    var output = new Uint16Array(inputArray.length-startIndex);
    for (var i = 0; i < inputArray.length; i++){
        var s = Math.max(-1, Math.min(1, inputArray[i]));
        output[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
    }
    return output;
}

// This is passed in an unsigned 16-bit integer array. It is converted to a 32-bit float array.
// The first startIndex items are skipped, and only 'length' number of items is converted.
function int16ToFloat32(inputArray, startIndex, length) {
    var output = new Float32Array(inputArray.length-startIndex);
    for (var i = startIndex; i < length; i++) {
        var int = inputArray[i];
        // If the high bit is on, then it is a negative number, and actually counts backwards.
        var float = (int >= 0x8000) ? -(0x10000 - int) / 0x8000 : int / 0x7FFF;
        output[i] = float;
    }
    return output;
}

// TEST
var data = [ 65424, 18, 0, 32700, 33000, 1000, 50000 ];
var testDataInt = new Uint16Array(data);
var testDataFloat = int16ToFloat32(testDataInt, 0, data.length);
var testDataInt2 = floatTo16Bit(testDataFloat, 0);
// At this point testDataInt2 should be pretty close to the original data array (there is a little rounding.)

var xhr = new XMLHttpRequest();
xhr.open('GET', '/my-sound.wav', true);
xhr.responseType = 'arraybuffer';

xhr.onload = function(e) {
    if (this.status === 200) {
        // This retrieves the entire wav file. We're only interested in the data portion.
        // At the beginning is 44 bytes (22 words) of header, and at the end is some metadata about the file.
        // The actual data length is held in bytes 40 - 44.
        var data = new Uint16Array(this.response);
        var length = (data[20] + data[21] * 0x10000) / 2; // The length is in bytes, but the array is 16 bits, so divide by 2.
        theWavDataInFloat32 = int16ToFloat32(data, 22, length);
    }
};

xhr.send();
于 2016-02-07T01:56:07.773 回答
0

数组输入的每一个真实数据都缩减到区间 [-1, 1]

  • 如果 x<=1,Math.min(1, x) 给出 x,否则给出 1
  • 如果 y>=-1,Math.max(-1, y) 给出 y,否则给出 -1

然后这个介于 -1 和 1 之间的实数被转换为有符号的 16 位整数。

无论是正数还是负数乘以 32767 或 -32768,它都只保留整数部分。这相当于在二进制表示中仅保留小数点后的 16 个有效位。

16位整数在缓冲区的两个字节上存储在little Endian中,一个接一个(根据将二进二的偏移量)

对于反向操作,只需将两个连续字节放入 int16 中。将其转换为真正的区分符号后除以32768或32767的两种情况。但是操作会丢失数据。

于 2016-02-05T23:03:25.267 回答