今天 2020+ 可以通过 audioWorklet 节点
https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/AudioWorkletProcessor
在 AudioWorkletContext 中运行,只有这样你才能通过消息传递二进制原始数据
// test-processor.js
class RecorderWorkletProcessor extends AudioWorkletProcessor {
constructor (options) {
super()
console.log(options.numberOfInputs)
console.log(options.processorOptions.someUsefulVariable)
}
// @ts-ignore
process(inputs, output, parameters) {
/**
* @type {Float32Array} length 128 Float32Array(128)
* non-interleaved IEEE754 32-bit linear PCM
* with a nominal range between -1 and +1,
* with each sample between -1.0 and 1.0.
* the sample rate depends on the audioContext and is variable
*/
const inputChannel = inputs[0][0]; //inputChannel Float32Array(128)
const { postMessage } = this.port;
postMessage(inputChannel) // float32Array sent as byte[512]
return true; // always do this!
}
}
主要代码
const audioContext = new AudioContext()
const audioMediaElement = audioContext.createMediaElementSource(
/** @type {HTMLAudioElement} */ audio
);
await audioContext.audioWorklet.addModule('test-processor.js')
const recorder = new AudioWorkletNode(audioContext, 'test-processor',
{
processorOptions: {
someUsefulVariable: new Map([[1, 'one'], [2, 'two']])
}
});
/**
*
* Objects of these types are designed to hold small audio snippets,
* typically less than 45 s. For longer sounds, objects implementing
* the MediaElementAudioSourceNode are more suitable.
* The buffer contains data in the following format:
* non-interleaved IEEE754 32-bit linear PCM (LPCM)
* with a nominal range between -1 and +1, that is, a 32-bit floating point buffer,
* with each sample between -1.0 and 1.0.
* @param {ArrayBufferLike|Float32Array} data
*/
const convertFloatToAudioBuffer = (data) => {
const sampleRate = 8000 | audioContext.sampleRate
const channels = 1;
const sampleLength = 128 | data.length; // 1sec = sampleRate * 1
const audioBuffer = audioContext.createBuffer(channels, sampleLength, sampleRate); // Empty Audio
audioBuffer.copyToChannel(new Float32Array(data), 0); // depending on your processing this could be already a float32array
return audioBuffer;
}
let startAt = 0
const streamDestination = audioContext.createMediaStreamDestination();
/**
* Note this is a minimum example it plays only the first sound
* it uses the main audio context if it would use a
* streamDestination = context.createMediaStreamDestination();
*
* @param {ArrayBufferLike|Float32Array} data
*/
const play = (data) => {
const audioBufferSourceNoce = audioContext.createBufferSource();
audioBufferSourceNoce.buffer = convertFloatToAudioBuffer(data);
const context = audioContext; // streamDestination; // creates a MediaStream on streamDestination.stream property
audioBufferSourceNoce.connect(context);
// here you will need a hugh enqueue algo that is out of scope for this answer
startAt = Math.max(context.currentTime, startAt);
source.start(startAt);
startAt += buffer.duration;
audioBufferSourceNoce.start(startAt);
}
// Here is your raw arrayBuffer ev.data
recorder.port.onmessage = (ev) => play(ev.data);
// connect the processor with the source
audioMediaElement.connect(recorder);
笔记
这只是 console.log 记录来自记录器处理器的数据的最低要求,当您真的想要处理该数据时,我们只采用 1 个通道,您应该考虑在工作人员中再次注册一个将数据直接发布到的处理程序如果您进行大量处理,该工作人员以及您的主进程可能会变得无响应。