linx-simulator2/node_modules/mumble-client-codecs-node/lib/opus-encoder-stream.js
2019-09-18 11:11:16 +03:00

58 lines
2.7 KiB
JavaScript

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _nodeOpus = require('node-opus');
var _stream = require('stream');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
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 OpusEncoderStream = function (_Transform) {
_inherits(OpusEncoderStream, _Transform);
function OpusEncoderStream() {
_classCallCheck(this, OpusEncoderStream);
return _possibleConstructorReturn(this, (OpusEncoderStream.__proto__ || Object.getPrototypeOf(OpusEncoderStream)).call(this, { objectMode: true }));
}
_createClass(OpusEncoderStream, [{
key: '_transform',
value: function _transform(chunk, encoding, callback) {
if (!this._opus) {
this._opus = new _nodeOpus.OpusEncoder(48000, chunk.numberOfChannels);
}
callback(null, {
target: chunk.target,
codec: 'Opus',
frame: this._opus.encode(Buffer.from(float32ToInt16(chunk.pcm).buffer)),
position: chunk.position
});
}
}]);
return OpusEncoderStream;
}(_stream.Transform);
function float32ToInt16(asFloat32) {
var len = asFloat32.length;
var asInt16 = new Int16Array(len);
for (var i = 0; i < len; i++) {
var val = asFloat32[i];
val = val < 0 ? val * 32768 : val * 32767;
asInt16[i] = Math.max(-32768, Math.min(32767, val));
}
return asInt16;
}
exports.default = OpusEncoderStream;