Created
June 24, 2019 22:51
-
-
Save oshoham/02b74a7794595ed7fe6dbc882d89c31f to your computer and use it in GitHub Desktop.
ES5 AudioWorklet Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function RecorderProcessor(options) { | |
AudioWorkletProcessor.call(this); | |
var processorOptions = options.processorOptions || {}; | |
this.numInputChannels = processorOptions.numInputChannels || 2; | |
this.recording = false; | |
this.clear(); | |
this.port.onmessage = function(event) { | |
var data = event.data; | |
if (data.name === 'start') { | |
this.record(data.duration); | |
} else if (data.name === 'stop') { | |
this.stop(); | |
} | |
}.bind(this); | |
} | |
RecorderProcessor.prototype = Object.create(AudioWorkletProcessor.prototype); | |
RecorderProcessor.prototype.constructor = RecorderProcessor; | |
RecorderProcessor.prototype.process = function(inputs, outputs) { | |
if (!this.recording) { | |
return true; | |
} else if (this.sampleLimit && this.recordedSamples >= this.sampleLimit) { | |
this.stop(); | |
} | |
var input = inputs[0]; | |
var output = outputs[0]; | |
for (var channel = 0; channel < output.length; ++channel) { | |
var inputChannel = input[channel]; | |
if (channel === 0) { | |
this.leftBuffers.push(inputChannel); | |
if (this.numInputChannels === 1) { | |
this.rightBuffers.push(inputChannel); | |
} | |
} else if (channel === 1 && this.numInputChannels > 1) { | |
this.rightBuffers.push(inputChannel); | |
} | |
} | |
this.recordedSamples += output[0].length; | |
return true; | |
}; | |
RecorderProcessor.prototype.record = function(duration) { | |
if (duration) { | |
this.sampleLimit = Math.round(duration * sampleRate); | |
} | |
this.recording = true; | |
}; | |
RecorderProcessor.prototype.stop = function() { | |
this.recording = false; | |
const buffers = this.getBuffers(); | |
const leftBuffer = buffers[0].buffer; | |
const rightBuffer = buffers[1].buffer; | |
this.port.postMessage( | |
{ | |
name: 'buffers', | |
leftBuffer: leftBuffer, | |
rightBuffer: rightBuffer | |
}, | |
[leftBuffer, rightBuffer] | |
); | |
this.clear(); | |
}; | |
RecorderProcessor.prototype.getBuffers = function() { | |
var buffers = []; | |
buffers.push(this.mergeBuffers(this.leftBuffers)); | |
buffers.push(this.mergeBuffers(this.rightBuffers)); | |
return buffers; | |
}; | |
RecorderProcessor.prototype.mergeBuffers = function(channelBuffer) { | |
var result = new Float32Array(this.recordedSamples); | |
var offset = 0; | |
var lng = channelBuffer.length; | |
for (var i = 0; i < lng; i++) { | |
var buffer = channelBuffer[i]; | |
result.set(buffer, offset); | |
offset += buffer.length; | |
} | |
return result; | |
}; | |
RecorderProcessor.prototype.clear = function() { | |
this.leftBuffers = []; | |
this.rightBuffers = []; | |
this.recordedSamples = 0; | |
this.sampleLimit = null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment