68 lines
3.0 KiB
JavaScript
68 lines
3.0 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');
|
|
|
|
var _toArraybuffer = require('to-arraybuffer');
|
|
|
|
var _toArraybuffer2 = _interopRequireDefault(_toArraybuffer);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
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 DecoderStream = function (_Transform) {
|
|
_inherits(DecoderStream, _Transform);
|
|
|
|
function DecoderStream() {
|
|
_classCallCheck(this, DecoderStream);
|
|
|
|
return _possibleConstructorReturn(this, (DecoderStream.__proto__ || Object.getPrototypeOf(DecoderStream)).call(this, { objectMode: true }));
|
|
}
|
|
|
|
_createClass(DecoderStream, [{
|
|
key: '_transform',
|
|
value: function _transform(chunk, encoding, callback) {
|
|
if (chunk.codec === 'Opus') {
|
|
if (!this._opus) {
|
|
this._opus = new _nodeOpus.OpusEncoder(48000, 1);
|
|
}
|
|
callback(null, {
|
|
target: chunk.target,
|
|
pcm: int16ToFloat32(this._opus.decode(chunk.frame)),
|
|
numberOfChannels: 1,
|
|
position: chunk.position
|
|
});
|
|
} else {
|
|
callback();
|
|
}
|
|
}
|
|
}]);
|
|
|
|
return DecoderStream;
|
|
}(_stream.Transform);
|
|
|
|
function int16ToFloat32(raw) {
|
|
var asInt16 = new Int16Array((0, _toArraybuffer2.default)(raw));
|
|
var len = asInt16.length;
|
|
var asFloat32 = new Float32Array(len);
|
|
for (var i = 0; i < len; i++) {
|
|
var val = asInt16[i];
|
|
val = val < 0 ? val / 32768 : val / 32767;
|
|
asFloat32[i] = Math.max(-1, Math.min(1, val));
|
|
}
|
|
return asFloat32;
|
|
}
|
|
|
|
exports.default = DecoderStream; |