Implement int24 for bitDepth24

This commit is contained in:
Michel Roux 2015-12-09 15:45:19 +01:00
parent 797d1f28a0
commit 6f1fcefe69
4 changed files with 32 additions and 9 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ results
npm-debug.log npm-debug.log
node_modules node_modules
.idea

View File

@ -1,6 +1,7 @@
var var
Writable = require('stream').Writable, Writable = require('stream').Writable,
util = require('util') util = require('util'),
int24 = require('int24')
; ;
function Input(args) { function Input(args) {
@ -25,6 +26,15 @@ function Input(args) {
this.writeSample = this.buffer.writeInt32LE; this.writeSample = this.buffer.writeInt32LE;
this.sampleByteLength = 4; 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 { else {
args.bitDepth = 16; args.bitDepth = 16;
this.readSample = this.buffer.readInt16LE; this.readSample = this.buffer.readInt16LE;
@ -54,7 +64,7 @@ Input.prototype.read = function (samples) {
} }
return r; return r;
} };
Input.prototype.readMono = function (samples) { Input.prototype.readMono = function (samples) {
// This function will be overridden by this.read, if input already is mono. // 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); this.writeSample.call(monoBuffer, Math.round((l + r) / 2), i * this.sampleByteLength);
} }
return monoBuffer; return monoBuffer;
} };
Input.prototype.readStereo = function (samples) { Input.prototype.readStereo = function (samples) {
// This function will be overridden by this.read, if input already is stereo. // 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); this.writeSample.call(stereoBuffer, m, (i * this.sampleByteLength * 2) + this.sampleByteLength);
} }
return stereoBuffer; return stereoBuffer;
} };
Input.prototype.availSamples = function (length) { Input.prototype.availSamples = function (length) {
if (typeof length === 'undefined') length = this.buffer.length; if (typeof length === 'undefined') length = this.buffer.length;
return Math.floor(length / ((this.bitDepth / 8) * this.channels)); return Math.floor(length / ((this.bitDepth / 8) * this.channels));
} };
Input.prototype._write = function (chunk, encoding, next) { Input.prototype._write = function (chunk, encoding, next) {
@ -102,6 +112,6 @@ Input.prototype._write = function (chunk, encoding, next) {
} }
} };
module.exports = Input; module.exports = Input;

View File

@ -1,7 +1,7 @@
var var
Readable = require('stream').Readable, Readable = require('stream').Readable,
util = require('util'), util = require('util'),
int24 = require('int24'),
Input = require('./input.js') Input = require('./input.js')
; ;
@ -25,6 +25,15 @@ function Mixer(args) {
this.writeSample = this.buffer.writeInt32LE; this.writeSample = this.buffer.writeInt32LE;
this.sampleByteLength = 4; 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 { else {
args.bitDepth = 16; args.bitDepth = 16;
this.readSample = this.buffer.readInt16LE; this.readSample = this.buffer.readInt16LE;
@ -66,7 +75,7 @@ Mixer.prototype._read = function() {
} else { } else {
setImmediate(this._read.bind(this)); setImmediate(this._read.bind(this));
} }
} };
Mixer.prototype.input = function (args) { Mixer.prototype.input = function (args) {
if (typeof args === 'undefined') args = {}; if (typeof args === 'undefined') args = {};
@ -80,6 +89,6 @@ Mixer.prototype.input = function (args) {
this.inputs.push(input); this.inputs.push(input);
return input; return input;
} };
module.exports = Mixer; module.exports = Mixer;

View File

@ -5,6 +5,9 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
}, },
"dependencies": {
"int24": "*"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/alfreddatakillen/audio-mixer.git" "url": "git://github.com/alfreddatakillen/audio-mixer.git"