From 6f1fcefe69ae38e29de3989eb66019b435551f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xe=CC=81fir=20Destiny?= Date: Wed, 9 Dec 2015 15:45:19 +0100 Subject: [PATCH] Implement int24 for bitDepth24 --- .gitignore | 1 + lib/input.js | 22 ++++++++++++++++------ lib/mixer.js | 15 ++++++++++++--- package.json | 3 +++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index a72b52e..9eadfec 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ results npm-debug.log node_modules +.idea diff --git a/lib/input.js b/lib/input.js index 11a2d5d..4fe3462 100644 --- a/lib/input.js +++ b/lib/input.js @@ -1,6 +1,7 @@ var Writable = require('stream').Writable, - util = require('util') + util = require('util'), + int24 = require('int24') ; function Input(args) { @@ -25,6 +26,15 @@ function Input(args) { this.writeSample = this.buffer.writeInt32LE; this.sampleByteLength = 4; } + else if (args.bitDepth == 24) { + this.readSample = function (offset) { + return int24.readInt24LE(this.buffer, offset); + }; + this.writeSample = function (offset, value) { + int24.writeInt24LE(this.buffer, offset, value); + }; + this.sampleByteLength = 3; + } else { args.bitDepth = 16; this.readSample = this.buffer.readInt16LE; @@ -54,7 +64,7 @@ Input.prototype.read = function (samples) { } return r; -} +}; Input.prototype.readMono = function (samples) { // This function will be overridden by this.read, if input already is mono. @@ -67,7 +77,7 @@ Input.prototype.readMono = function (samples) { this.writeSample.call(monoBuffer, Math.round((l + r) / 2), i * this.sampleByteLength); } return monoBuffer; -} +}; Input.prototype.readStereo = function (samples) { // This function will be overridden by this.read, if input already is stereo. @@ -80,12 +90,12 @@ Input.prototype.readStereo = function (samples) { this.writeSample.call(stereoBuffer, m, (i * this.sampleByteLength * 2) + this.sampleByteLength); } return stereoBuffer; -} +}; Input.prototype.availSamples = function (length) { if (typeof length === 'undefined') length = this.buffer.length; return Math.floor(length / ((this.bitDepth / 8) * this.channels)); -} +}; Input.prototype._write = function (chunk, encoding, next) { @@ -102,6 +112,6 @@ Input.prototype._write = function (chunk, encoding, next) { } -} +}; module.exports = Input; diff --git a/lib/mixer.js b/lib/mixer.js index 954f296..1572d58 100644 --- a/lib/mixer.js +++ b/lib/mixer.js @@ -1,7 +1,7 @@ var Readable = require('stream').Readable, util = require('util'), - + int24 = require('int24'), Input = require('./input.js') ; @@ -25,6 +25,15 @@ function Mixer(args) { this.writeSample = this.buffer.writeInt32LE; this.sampleByteLength = 4; } + else if (args.bitDepth == 24) { + this.readSample = function (offset) { + return int24.readInt24LE(this.buffer, offset); + }; + this.writeSample = function (offset, value) { + int24.writeInt24LE(this.buffer, offset, value); + }; + this.sampleByteLength = 3; + } else { args.bitDepth = 16; this.readSample = this.buffer.readInt16LE; @@ -66,7 +75,7 @@ Mixer.prototype._read = function() { } else { setImmediate(this._read.bind(this)); } -} +}; Mixer.prototype.input = function (args) { if (typeof args === 'undefined') args = {}; @@ -80,6 +89,6 @@ Mixer.prototype.input = function (args) { this.inputs.push(input); return input; -} +}; module.exports = Mixer; diff --git a/package.json b/package.json index e4b541a..5b16ea3 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "main": "index.js", "scripts": { }, + "dependencies": { + "int24": "*" + }, "repository": { "type": "git", "url": "git://github.com/alfreddatakillen/audio-mixer.git"