UNPKG

tav-media

Version:

Cross platform media editing framework

2 lines (1 loc) 2.35 kB
export const movieAudioProcessorCode: "class MovieAudioProcessor extends AudioWorkletProcessor {\n static get parameterDescriptors() {\n return [\n { name: 'bitDepth', defaultValue: 16, minValue: 1, maxValue: 32 },\n ];\n }\n\n constructor() {\n super();\n this.bitDepth_ = 16;\n this.paused = false;\n this.inputIndexes = [];\n this.port.onmessage = (e) => {\n const { operation, index } = e.data;\n if (operation === 'pause') {\n this.paused = true;\n } else if (operation === 'play') {\n this.paused = false;\n } else if (operation === 'connect') {\n this.inputIndexes.push(index);\n } else if (operation === 'disconnect') {\n this.inputIndexes.splice(this.inputIndexes.indexOf(index), 1);\n }\n }\n }\n\n handleMessage() {\n this.port.postMessage('received');\n }\n\n process(inputs, outputs, parameters) {\n if (this.paused) return true;\n if (inputs.length === 0) {\n return true;\n }\n\n const { bitDepth } = parameters;\n const factor = 2 ** (bitDepth - 1);\n const range = 25000;\n\n const outBuffers = [];\n const outDataList = {};\n for (const inputIndex of this.inputIndexes) {\n const input = inputs[inputIndex];\n if (!input || input.length === 0) {\n continue;\n };\n const sampleCount = input[0].length;\n const outData = [];\n for (let channel = 0; channel < input.length; ++channel) {\n for (let dataIndex = 0; dataIndex < input[channel].length; dataIndex++) {\n let transformedValue = Math.floor(input[channel][dataIndex] * factor + 0.5);\n if (transformedValue >= range ) {\n transformedValue = range;\n } else\n if (transformedValue <= -range) {\n transformedValue = -range;\n }\n outData[input.length * dataIndex + channel] = transformedValue;\n }\n }\n const arrayBuffer = new Int16Array(outData);\n outDataList[inputIndex] = {\n bytes: arrayBuffer.buffer,\n sampleCount,\n channelCount: input.length,\n };\n outBuffers.push(arrayBuffer.buffer);\n }\n\n this.port.postMessage({\n outDataList\n }, outBuffers);\n\n return true;\n }\n}\nregisterProcessor('movie-audio-processor', MovieAudioProcessor);";