node-audio-mixer
Version:
PCM audio mixer with customizable parameters
23 lines (22 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.changeVolume = changeVolume;
const IsLittleEndian_1 = require("../General/IsLittleEndian");
const GetMethodName_1 = require("../General/GetMethodName");
const GetValueRange_1 = require("../General/GetValueRange");
function changeVolume(audioData, params) {
const bytesPerElement = params.bitDepth / 8;
const volume = params.volume / 100;
const isLe = (0, IsLittleEndian_1.isLittleEndian)(params.endianness);
const valueRange = (0, GetValueRange_1.getValueRange)(params.bitDepth, false, params.float);
const getSampleMethod = `get${(0, GetMethodName_1.getMethodName)(params.bitDepth, params.unsigned, params.float)}`;
const setSampleMethod = `set${(0, GetMethodName_1.getMethodName)(params.bitDepth, params.unsigned, params.float)}`;
for (let index = 0; index < audioData.byteLength; index += bytesPerElement) {
const sample = audioData[getSampleMethod](index, isLe);
const volumedSample = params.unsigned
? ((sample - valueRange.max) * volume) + valueRange.max
: sample * volume;
audioData[setSampleMethod](index, volumedSample, isLe);
}
params.volume = 100;
}