waveform-playlist-nartj
Version:
Multiple track web audio editor and player with waveform preview
174 lines (153 loc) • 5.33 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _fadeMaker = require("fade-maker");
var _default = /*#__PURE__*/function () {
function _default(ac, buffer) {
(0, _classCallCheck2["default"])(this, _default);
this.ac = ac;
this.gain = 1;
this.buffer = buffer;
this.destination = this.ac.destination;
this.ac.createStereoPanner = ac.createStereoPanner || ac.createPanner;
}
(0, _createClass2["default"])(_default, [{
key: "applyFade",
value: function applyFade(type, start, duration) {
var shape = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'logarithmic';
if (type === _fadeMaker.FADEIN) {
(0, _fadeMaker.createFadeIn)(this.fadeGain.gain, shape, start, duration);
} else if (type === _fadeMaker.FADEOUT) {
(0, _fadeMaker.createFadeOut)(this.fadeGain.gain, shape, start, duration);
} else {
throw new Error('Unsupported fade type');
}
}
}, {
key: "applyFadeIn",
value: function applyFadeIn(start, duration) {
var shape = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'logarithmic';
this.applyFade(_fadeMaker.FADEIN, start, duration, shape);
}
}, {
key: "applyFadeOut",
value: function applyFadeOut(start, duration) {
var shape = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'logarithmic';
this.applyFade(_fadeMaker.FADEOUT, start, duration, shape);
}
}, {
key: "isPlaying",
value: function isPlaying() {
return this.source !== undefined;
}
}, {
key: "getDuration",
value: function getDuration() {
return this.buffer.duration;
}
}, {
key: "setAudioContext",
value: function setAudioContext(ac) {
this.ac = ac;
this.ac.createStereoPanner = ac.createStereoPanner || ac.createPanner;
this.destination = this.ac.destination;
}
}, {
key: "setUpSource",
value: function setUpSource() {
var _this = this;
this.source = this.ac.createBufferSource();
this.source.buffer = this.buffer;
var sourcePromise = new Promise(function (resolve) {
// keep track of the buffer state.
_this.source.onended = function () {
_this.source.disconnect();
_this.fadeGain.disconnect();
_this.volumeGain.disconnect();
_this.shouldPlayGain.disconnect();
_this.panner.disconnect();
_this.masterGain.disconnect();
_this.source = undefined;
_this.fadeGain = undefined;
_this.volumeGain = undefined;
_this.shouldPlayGain = undefined;
_this.panner = undefined;
_this.masterGain = undefined;
resolve();
};
});
this.fadeGain = this.ac.createGain(); // used for track volume slider
this.volumeGain = this.ac.createGain(); // used for solo/mute
this.shouldPlayGain = this.ac.createGain();
this.masterGain = this.ac.createGain();
this.panner = this.ac.createStereoPanner();
this.source.connect(this.fadeGain);
this.fadeGain.connect(this.volumeGain);
this.volumeGain.connect(this.shouldPlayGain);
this.shouldPlayGain.connect(this.masterGain);
this.masterGain.connect(this.panner);
this.panner.connect(this.destination);
return sourcePromise;
}
}, {
key: "setVolumeGainLevel",
value: function setVolumeGainLevel(level) {
if (this.volumeGain) {
this.volumeGain.gain.value = level;
}
}
}, {
key: "setShouldPlay",
value: function setShouldPlay(bool) {
if (this.shouldPlayGain) {
this.shouldPlayGain.gain.value = bool ? 1 : 0;
}
}
}, {
key: "setMasterGainLevel",
value: function setMasterGainLevel(level) {
if (this.masterGain) {
this.masterGain.gain.value = level;
}
}
}, {
key: "setStereoPanValue",
value: function setStereoPanValue(value) {
var pan = value === undefined ? 0 : value;
if (this.panner) {
if (this.panner.pan !== undefined) {
this.panner.pan.value = pan;
} else {
this.panner.panningModel = 'equalpower';
this.panner.setPosition(pan, 0, 1 - Math.abs(pan));
}
}
}
/*
source.start is picky when passing the end time.
If rounding error causes a number to make the source think
it is playing slightly more samples than it has it won't play at all.
Unfortunately it doesn't seem to work if you just give it a start time.
*/
}, {
key: "play",
value: function play(when, start, duration) {
this.source.start(when, start, duration);
}
}, {
key: "stop",
value: function stop() {
var when = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
if (this.source) {
this.source.stop(when);
}
}
}]);
return _default;
}();
exports["default"] = _default;