在网络上录制音频,预设:16000Hz 16bit

时间:2015-03-10 16:39:32

标签: javascript audio record web-audio

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

function writeString(view, offset, string){
  for (var i = 0; i < string.length; i++){
    view.setUint8(offset + i, string.charCodeAt(i));
  }
}

function encodeWAV(samples){
  var buffer = new ArrayBuffer(44 + samples.length * 2);
  var view = new DataView(buffer);

  /* RIFF identifier */
  writeString(view, 0, 'RIFF');
  /* RIFF chunk length */
  view.setUint32(4, 36 + samples.length * 2, true);
  /* RIFF type */
  writeString(view, 8, 'WAVE');
  /* format chunk identifier */
  writeString(view, 12, 'fmt ');
  /* format chunk length */
  view.setUint32(16, 16, true);
  /* sample format (raw) */
  view.setUint16(20, 1, true);
  /* channel count */
  view.setUint16(22, 2, true);
  /* sample rate */
  view.setUint32(24, sampleRate, true);
  /* byte rate (sample rate * block align) */
  view.setUint32(28, sampleRate * 4, true);
  /* block align (channel count * bytes per sample) */
  view.setUint16(32, 4, true);
  /* bits per sample */
  view.setUint16(34, 16, true);
  /* data chunk identifier */
  writeString(view, 36, 'data');
  /* data chunk length */
  view.setUint32(40, samples.length * 2, true);

  floatTo16BitPCM(view, 44, samples);

  return view;
}

嗨,我正在使用此源代码为我的学校考试录制音频。它以44100Hz和16bit记录音频。我想更改录音设置以录制16000Hz和16bit的音频。 我尝试将函数encodeWAV中的44修改为16,但它不起作用。

function encodeWAV(samples){
  var buffer = new ArrayBuffer(44 + samples.length * 2);
  var view = new DataView(buffer)

此外,我还尝试更改floadRToBitPCM。我试图将44改为16但它也没有用。

floatTo16BitPCM(view, 44, samples);

你能帮我解决这个问题吗?我不知道如何修改这个源代码。

4 个答案:

答案 0 :(得分:7)

修改:

另一个选择(更好的一个IMO)只是用于HTML的MediaRecorder并以.ogg格式记录demo,并且git repo


我假设您使用this作为来源,就像jaket所说,行floatTo16BitPCM(view, 44, samples);与采样率无关......

如果要重新取样数据,可以修改:

function exportWAV(type){
    var buffers = [];
    for (var channel = 0; channel < numChannels; channel++){
        buffers.push(mergeBuffers(recBuffers[channel], recLength));
    }
    if (numChannels === 2){
        var interleaved = interleave(buffers[0], buffers[1]);
    } else {
        var interleaved = buffers[0];
    }
    var dataview = encodeWAV(interleaved);
    var audioBlob = new Blob([dataview], { type: type });
    this.postMessage(audioBlob);
}

进入这个:

function exportWAV(type, desiredSamplingRate){
    var buffers = [];
    for (var channel = 0; channel < numChannels; channel++){
        var buffer = mergeBuffers(recBuffers[channel], recLength);
        buffer = interpolateArray(buffer, desiredSamplingRate, sampleRate);
        buffers.push(buffer);
    }
    sampleRate = desiredSamplingRate;
    if (numChannels === 2){
        var interleaved = interleave(buffers[0], buffers[1]);
    } else {
        var interleaved = buffers[0];
    }
    var dataview = encodeWAV(interleaved);
    var audioBlob = new Blob([dataview], { type: type });
    this.postMessage(audioBlob);
}

重新采样数据的代码,

// for changing the sampling rate, data,
function interpolateArray(data, newSampleRate, oldSampleRate) {
    var fitCount = Math.round(data.length*(newSampleRate/oldSampleRate));
    var newData = new Array();
    var springFactor = new Number((data.length - 1) / (fitCount - 1));
    newData[0] = data[0]; // for new allocation
    for ( var i = 1; i < fitCount - 1; i++) {
    var tmp = i * springFactor;
    var before = new Number(Math.floor(tmp)).toFixed();
    var after = new Number(Math.ceil(tmp)).toFixed();
    var atPoint = tmp - before;
    newData[i] = this.linearInterpolate(data[before], data[after], atPoint);
    }
    newData[fitCount - 1] = data[data.length - 1]; // for new allocation
    return newData;
};
function linearInterpolate(before, after, atPoint) {
    return before + (after - before) * atPoint;
};

修改:如果你不想改变太多,你可以硬编码为

function exportWAV(type){
    var buffers = [], desiredSamplingRate = 16000;

答案 1 :(得分:3)

我不相信你可以使用Web Audio API来控制采样率...它会获取在浏览器之外定义的系统默认采样率...当然,在录制之后,你可以通过编程方式改变音频以重新采样任何采样率...大多数音频播放器只能播放标准采样率的媒体...能够呈现16 kHz的关闭采样率可能比从44.1到16 kHz的重采样更具挑战性

答案 2 :(得分:2)

您正在修改错误的字段。波形文件是标题,后跟数据。波头通常 44个字节长。您在示例代码中看到的44的值与此相关,而不是44100

在您未发布的代码中,sampleRate被定义为44100.您需要追踪该定义并将其更改为16000.根据代码的其余部分和样本来源,它可能会虽然不是那么简单。例如,如果正在从设备记录样本并且设备已配置为以44100记录,那么简单地将保存的波形标记为16000将不会产生期望的效果。它只会使播放2.75x太慢而巴里白色效果。

答案 3 :(得分:0)

您可以像这样更改init函数,在这里您可以将默认的浏览器采样率克服到16000以及指示单声道/立体声的通道我们也可以更改那个,  如果它是单声道,它将是1,其他明智的两个。

 function init(config) {
            //sampleRate = config.sampleRate;
            sampleRate = 16000;
            debugger;
            //numChannels = config.numChannels;
            numChannels = 1;
            initBuffers();
        }