gpt4 book ai didi

javascript - 网络录音,预设: 16000Hz 16bit

转载 作者:数据小太阳 更新时间:2023-10-29 04:29:55 52 4
gpt4 key购买 nike

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 和 16 位录制音频。我想更改录制设置以录制 16000Hz 和 16 位的音频。我尝试将函数 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);

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

最佳答案

编辑:

另一种选择(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;

关于javascript - 网络录音,预设: 16000Hz 16bit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28969304/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com