qambi
Version:
MIDI sequencer, loads MIDI files, can record and playback MIDI, uses WebMIDI and WebAudio
130 lines (109 loc) • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Sample = undefined;
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; }; }();
exports.fadeOut = fadeOut;
var _init_audio = require('./init_audio.js');
var _util = require('./util.js');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Sample = exports.Sample = function () {
function Sample(sampleData, event) {
_classCallCheck(this, Sample);
this.event = event;
this.sampleData = sampleData;
}
_createClass(Sample, [{
key: 'start',
value: function start(time) {
var _sampleData = this.sampleData,
sustainStart = _sampleData.sustainStart,
sustainEnd = _sampleData.sustainEnd;
//console.log(sustainStart, sustainEnd)
if (sustainStart && sustainEnd) {
this.source.loop = true;
this.source.loopStart = sustainStart;
this.source.loopEnd = sustainEnd;
}
this.source.start(time);
}
}, {
key: 'stop',
value: function stop(time, cb) {
var _this = this;
var _sampleData2 = this.sampleData,
releaseDuration = _sampleData2.releaseDuration,
releaseEnvelope = _sampleData2.releaseEnvelope,
releaseEnvelopeArray = _sampleData2.releaseEnvelopeArray;
//console.log(releaseDuration, releaseEnvelope)
this.source.onended = cb;
if (releaseDuration && releaseEnvelope) {
this.startReleasePhase = time;
this.releaseFunction = function () {
fadeOut(_this.output, {
releaseDuration: releaseDuration,
releaseEnvelope: releaseEnvelope,
releaseEnvelopeArray: releaseEnvelopeArray
});
};
try {
this.source.stop(time + releaseDuration);
} catch (e) {
// in Firefox and Safari you can not call stop more than once
}
this.checkPhase();
} else {
try {
this.source.stop(time);
} catch (e) {
// in Firefox and Safari you can not call stop more than once
}
}
}
}, {
key: 'checkPhase',
value: function checkPhase() {
//console.log(context.currentTime, this.startReleasePhase)
if (_init_audio.context.currentTime >= this.startReleasePhase) {
this.releaseFunction();
return;
}
requestAnimationFrame(this.checkPhase.bind(this));
}
}]);
return Sample;
}();
function fadeOut(gainNode, settings) {
var now = _init_audio.context.currentTime;
var values = void 0,
i = void 0,
maxi = void 0;
//console.log(settings)
try {
switch (settings.releaseEnvelope) {
case 'linear':
gainNode.gain.linearRampToValueAtTime(gainNode.gain.value, now);
gainNode.gain.linearRampToValueAtTime(0.0, now + settings.releaseDuration);
break;
case 'equal power':
case 'equal_power':
values = (0, _util.getEqualPowerCurve)(100, 'fadeOut', gainNode.gain.value);
gainNode.gain.setValueCurveAtTime(values, now, settings.releaseDuration);
break;
case 'array':
maxi = settings.releaseEnvelopeArray.length;
values = new Float32Array(maxi);
for (i = 0; i < maxi; i++) {
values[i] = settings.releaseEnvelopeArray[i] * gainNode.gain.value;
}
gainNode.gain.setValueCurveAtTime(values, now, settings.releaseDuration);
break;
default:
}
} catch (e) {
// in Firefox and Safari you can not call setValueCurveAtTime and linearRampToValueAtTime more than once
//console.log(values, now, settings.releaseDuration)
//console.log(e, gainNode)
}
}