node-audio-mixer
Version:
PCM audio mixer with customizable parameters
27 lines (26 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeBitDepth = changeBitDepth;
const ModifiedDataView_1 = require("../../ModifiedDataView/ModifiedDataView");
const IsLittleEndian_1 = require("../General/IsLittleEndian");
const GetMethodName_1 = require("../General/GetMethodName");
const GetScaledSample_1 = require("../General/GetScaledSample");
function changeBitDepth(audioData, inputParams, mixerParams) {
const oldBytesPerElement = inputParams.bitDepth / 8;
const newBytesPerElement = mixerParams.bitDepth / 8;
const scalingFactor = 2 ** (mixerParams.bitDepth - inputParams.bitDepth);
const isLe = (0, IsLittleEndian_1.isLittleEndian)(inputParams.endianness);
const dataSize = audioData.byteLength * (mixerParams.bitDepth / inputParams.bitDepth);
const allocData = new Uint8Array(dataSize);
const allocDataView = new ModifiedDataView_1.ModifiedDataView(allocData.buffer);
const getSampleMethod = `get${(0, GetMethodName_1.getMethodName)(inputParams.bitDepth, inputParams.unsigned, inputParams.float)}`;
const setSampleMethod = `set${(0, GetMethodName_1.getMethodName)(mixerParams.bitDepth, mixerParams.unsigned, mixerParams.float)}`;
for (let index = 0; index < audioData.byteLength; index += oldBytesPerElement) {
const audioSample = audioData[getSampleMethod](index, isLe);
const scaledSample = (0, GetScaledSample_1.getScaledSample)(audioSample, scalingFactor, inputParams, mixerParams);
const newSamplePosition = Math.floor(index * (newBytesPerElement / oldBytesPerElement));
allocDataView[setSampleMethod](newSamplePosition, scaledSample, isLe);
}
inputParams.bitDepth = mixerParams.bitDepth;
return allocDataView;
}