UNPKG

node-audio-mixer

Version:

PCM audio mixer with customizable parameters

25 lines (24 loc) • 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.changeChannelsCount = changeChannelsCount; const ModifiedDataView_1 = require("../../ModifiedDataView/ModifiedDataView"); const IsLittleEndian_1 = require("../General/IsLittleEndian"); const GetMethodName_1 = require("../General/GetMethodName"); function changeChannelsCount(audioData, params, channels) { const bytesPerElement = params.bitDepth / 8; const isLe = (0, IsLittleEndian_1.isLittleEndian)(params.endianness); const dataSize = Math.round((audioData.byteLength / params.channels) * channels); const allocData = new Uint8Array(dataSize); const allocDataView = new ModifiedDataView_1.ModifiedDataView(allocData.buffer); 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 oldPosition = 0, newPosition = 0; oldPosition < audioData.byteLength; oldPosition += (bytesPerElement * params.channels)) { const sample = audioData[getSampleMethod](oldPosition, isLe); const nextPosition = newPosition + (bytesPerElement * channels); for (newPosition; newPosition < nextPosition; newPosition += bytesPerElement) { allocDataView[setSampleMethod](newPosition, sample, isLe); } } params.channels = channels; return allocDataView; }