recorder-audio-worklet-processor
Version:
The AudioWorkletProcessor which is used by the recorder-audio-worklet package.
133 lines (129 loc) • 6.22 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('@babel/runtime/helpers/slicedToArray'), require('@babel/runtime/helpers/classCallCheck'), require('@babel/runtime/helpers/createClass'), require('@babel/runtime/helpers/possibleConstructorReturn'), require('@babel/runtime/helpers/getPrototypeOf'), require('@babel/runtime/helpers/inherits'), require('@babel/runtime/helpers/wrapNativeSuper')) :
typeof define === 'function' && define.amd ? define(['@babel/runtime/helpers/slicedToArray', '@babel/runtime/helpers/classCallCheck', '@babel/runtime/helpers/createClass', '@babel/runtime/helpers/possibleConstructorReturn', '@babel/runtime/helpers/getPrototypeOf', '@babel/runtime/helpers/inherits', '@babel/runtime/helpers/wrapNativeSuper'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global._slicedToArray, global._classCallCheck, global._createClass, global._possibleConstructorReturn, global._getPrototypeOf, global._inherits, global._wrapNativeSuper));
})(this, (function (_slicedToArray, _classCallCheck, _createClass, _possibleConstructorReturn, _getPrototypeOf, _inherits, _wrapNativeSuper) { 'use strict';
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var RecorderAudioWorkletProcessor = /*#__PURE__*/function (_AudioWorkletProcesso) {
function RecorderAudioWorkletProcessor() {
var _this;
_classCallCheck(this, RecorderAudioWorkletProcessor);
_this = _callSuper(this, RecorderAudioWorkletProcessor);
_this._encoderPort = null;
_this._numberOfChannels = 0;
_this._state = 'inactive';
_this.port.onmessage = function (_ref) {
var data = _ref.data;
if (data.method === 'pause') {
if (_this._state === 'active' || _this._state === 'recording') {
_this._state = 'paused';
_this._sendAcknowledgement(data.id);
} else {
_this._sendUnexpectedStateError(data.id);
}
} else if (data.method === 'record') {
if (_this._state === 'inactive') {
_this._encoderPort = data.params.encoderPort;
_this._state = 'active';
_this._sendAcknowledgement(data.id);
} else {
_this._sendUnexpectedStateError(data.id);
}
} else if (data.method === 'resume') {
if (_this._state === 'paused') {
_this._state = 'active';
_this._sendAcknowledgement(data.id);
} else {
_this._sendUnexpectedStateError(data.id);
}
} else if (data.method === 'stop') {
if ((_this._state === 'active' || _this._state === 'paused' || _this._state === 'recording') && _this._encoderPort !== null) {
_this._stop(_this._encoderPort);
_this._sendAcknowledgement(data.id);
} else {
_this._sendUnexpectedStateError(data.id);
}
} else if (typeof data.id === 'number') {
_this.port.postMessage({
error: {
code: -32601,
message: 'The requested method is not supported.'
},
id: data.id
});
}
};
return _this;
}
_inherits(RecorderAudioWorkletProcessor, _AudioWorkletProcesso);
return _createClass(RecorderAudioWorkletProcessor, [{
key: "process",
value: function process(_ref2) {
var _ref3 = _slicedToArray(_ref2, 1),
input = _ref3[0];
if (this._state === 'inactive' || this._state === 'paused') {
return true;
}
if (this._state === 'active') {
if (input === undefined) {
throw new Error('No channelData was received for the first input.');
}
if (input.length === 0) {
return true;
}
this._state = 'recording';
}
if (this._state === 'recording' && this._encoderPort !== null) {
if (input === undefined) {
throw new Error('No channelData was received for the first input.');
}
if (input.length === 0) {
this._encoderPort.postMessage(Array.from({
length: this._numberOfChannels
}, function () {
return 128;
}));
} else {
this._encoderPort.postMessage(input, input.map(function (_ref4) {
var buffer = _ref4.buffer;
return buffer;
}));
this._numberOfChannels = input.length;
}
return true;
}
return false;
}
}, {
key: "_sendAcknowledgement",
value: function _sendAcknowledgement(id) {
this.port.postMessage({
id: id,
result: null
});
}
}, {
key: "_sendUnexpectedStateError",
value: function _sendUnexpectedStateError(id) {
this.port.postMessage({
error: {
code: -32603,
message: 'The internal state does not allow to process the given message.'
},
id: id
});
}
}, {
key: "_stop",
value: function _stop(encoderPort) {
encoderPort.postMessage([]);
encoderPort.close();
this._encoderPort = null;
this._state = 'stopped';
}
}]);
}(/*#__PURE__*/_wrapNativeSuper(AudioWorkletProcessor));
RecorderAudioWorkletProcessor.parameterDescriptors = [];
registerProcessor('recorder-audio-worklet-processor', RecorderAudioWorkletProcessor);
}));