Files
discord.js/lib/Voice/VolumeTransformer.js
2016-04-02 11:15:33 +02:00

85 lines
3.0 KiB
JavaScript

'use strict';
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Transform = require('stream').Transform;
/**
* @see https://github.com/reneraab/pcm-volume/blob/master/index.js Inspired by this script
*/
var Volume = (function (_Transform) {
_inherits(Volume, _Transform);
function Volume(volume) {
_classCallCheck(this, Volume);
_Transform.call(this);
this.set(volume);
}
// Set the volume so that a value of 0.5 is half the perceived volume and
// 2.0 is double the perceived volume.
Volume.prototype.setVolumeLogarithmic = function setVolumeLogarithmic(value) {
this.volume = Math.pow(value, 1.660964);
};
// Set the volume to a value specified as decibels.
Volume.prototype.setVolumeDecibels = function setVolumeDecibels(db) {
this.volume = Math.pow(10, db / 20);
};
Volume.prototype.get = function get() {
return this.volume;
};
Volume.prototype.set = function set(volume) {
this.volume = volume === undefined ? 1 : volume;
};
Volume.prototype._transform = function _transform(buffer, encoding, callback) {
var out = new Buffer(buffer.length);
for (var i = 0; i < buffer.length; i += 2) {
// Read Int16, multiple with multiplier and round down
//console.log(this.volume, this.multiplier, buffer.readInt16LE(i));
var uint = Math.floor(this.multiplier * buffer.readInt16LE(i));
// Ensure value stays within 16bit
uint = Math.min(32767, uint);
uint = Math.max(-32767, uint);
// Write 2 new bytes into other buffer;
out.writeInt16LE(uint, i);
}
this.push(out);
callback();
};
_createClass(Volume, [{
key: 'volume',
get: function get() {
return this._volume === undefined ? 1 : this._volume;
},
set: function set(value) {
this._volume = value;
}
}, {
key: 'multiplier',
get: function get() {
return this.volume;
}
}]);
return Volume;
})(Transform);
module.exports = Volume;