qambi
Version:
MIDI sequencer, loads MIDI files, can record and playback MIDI, uses WebMIDI and WebAudio
212 lines (188 loc) • 7.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.configureMasterCompressor = exports.enableMasterCompressor = exports.getCompressionReduction = exports.getMasterVolume = exports.setMasterVolume = exports.masterCompressor = exports.unlockWebAudio = exports.masterGain = exports.context = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /*
Sets up the basic audio routing, tests which audio formats are supported and parses the samples for the metronome ticks.
*/
exports.initAudio = initAudio;
exports.getInitData = getInitData;
var _samples = require('./samples');
var _samples2 = _interopRequireDefault(_samples);
var _parse_audio = require('./parse_audio');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var data = void 0;
var masterGain = void 0;
var compressor = void 0;
var initialized = false;
var context = exports.context = function () {
//console.log('init AudioContext')
var ctx = void 0;
if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object') {
var AudioContext = window.AudioContext || window.webkitAudioContext;
if (AudioContext !== 'undefined') {
ctx = new AudioContext();
}
}
if (typeof ctx === 'undefined') {
//@TODO: create dummy AudioContext for use in node, see: https://www.npmjs.com/package/audio-context
exports.context = context = {
createGain: function createGain() {
return {
gain: 1
};
},
createOscillator: function createOscillator() {}
};
}
return ctx;
}();
function initAudio() {
if (typeof context.createGainNode === 'undefined') {
context.createGainNode = context.createGain;
}
// check for older implementations of WebAudio
data = {};
var source = context.createBufferSource();
data.legacy = false;
if (typeof source.start === 'undefined') {
data.legacy = true;
}
// set up the elementary audio nodes
exports.masterCompressor = compressor = context.createDynamicsCompressor();
compressor.connect(context.destination);
exports.masterGain = masterGain = context.createGain();
masterGain.connect(context.destination);
masterGain.gain.value = 0.5;
initialized = true;
return new Promise(function (resolve, reject) {
(0, _parse_audio.parseSamples)(_samples2.default).then(function onFulfilled(buffers) {
//console.log(buffers)
// data.ogg = typeof buffers.emptyOgg !== 'undefined'
// data.mp3 = typeof buffers.emptyMp3 !== 'undefined'
data.lowtick = buffers.lowtick;
data.hightick = buffers.hightick;
if (data.ogg === false && data.mp3 === false) {
reject('No support for ogg nor mp3!');
} else {
resolve(data);
}
}, function onRejected() {
reject('Something went wrong while initializing Audio');
});
});
}
var _setMasterVolume = function setMasterVolume() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0.5;
if (initialized === false) {
console.warn('please call qambi.init() first');
} else {
exports.setMasterVolume = _setMasterVolume = function setMasterVolume() {
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0.5;
if (value > 1) {
console.info('maximal volume is 1.0, volume is set to 1.0');
}
value = value < 0 ? 0 : value > 1 ? 1 : value;
masterGain.gain.value = value;
};
_setMasterVolume(value);
}
};
var _getMasterVolume = function getMasterVolume() {
if (initialized === false) {
console.warn('please call qambi.init() first');
} else {
exports.getMasterVolume = _getMasterVolume = function getMasterVolume() {
return masterGain.gain.value;
};
return _getMasterVolume();
}
};
var _getCompressionReduction = function getCompressionReduction() {
if (initialized === false) {
console.warn('please call qambi.init() first');
} else {
exports.getCompressionReduction = _getCompressionReduction = function getCompressionReduction() {
return compressor.reduction.value;
};
return _getCompressionReduction();
}
};
var _enableMasterCompressor = function enableMasterCompressor() {
if (initialized === false) {
console.warn('please call qambi.init() first');
} else {
exports.enableMasterCompressor = _enableMasterCompressor = function enableMasterCompressor(flag) {
if (flag) {
masterGain.disconnect(0);
masterGain.connect(compressor);
compressor.disconnect(0);
compressor.connect(context.destination);
} else {
compressor.disconnect(0);
masterGain.disconnect(0);
masterGain.connect(context.destination);
}
};
_enableMasterCompressor();
}
};
var _configureMasterCompressor = function configureMasterCompressor(cfg) {
/*
readonly attribute AudioParam attack; // in Seconds
readonly attribute AudioParam knee; // in Decibels
readonly attribute AudioParam ratio; // unit-less
readonly attribute AudioParam reduction; // in Decibels
readonly attribute AudioParam release; // in Seconds
readonly attribute AudioParam threshold; // in Decibels
@see: http://webaudio.github.io/web-audio-api/#the-dynamicscompressornode-interface
*/
if (initialized === false) {
console.warn('please call qambi.init() first');
} else {
exports.configureMasterCompressor = _configureMasterCompressor = function configureMasterCompressor(cfg) {
var _cfg$attack = cfg.attack;
compressor.attack = _cfg$attack === undefined ? 0.003 : _cfg$attack;
var _cfg$knee = cfg.knee;
compressor.knee = _cfg$knee === undefined ? 30 : _cfg$knee;
var _cfg$ratio = cfg.ratio;
compressor.ratio = _cfg$ratio === undefined ? 12 : _cfg$ratio;
var _cfg$reduction = cfg.reduction;
compressor.reduction = _cfg$reduction === undefined ? 0 : _cfg$reduction;
var _cfg$release = cfg.release;
compressor.release = _cfg$release === undefined ? 0.250 : _cfg$release;
var _cfg$threshold = cfg.threshold;
compressor.threshold = _cfg$threshold === undefined ? -24 : _cfg$threshold;
};
_configureMasterCompressor(cfg);
}
};
function getInitData() {
return data;
}
// this doesn't seem to be necessary anymore on iOS anymore
var _unlockWebAudio = function unlockWebAudio() {
var src = context.createOscillator();
var gainNode = context.createGain();
gainNode.gain.value = 0;
src.connect(gainNode);
gainNode.connect(context.destination);
if (typeof src.noteOn !== 'undefined') {
src.start = src.noteOn;
src.stop = src.noteOff;
}
src.start(0);
src.stop(0.001);
exports.unlockWebAudio = _unlockWebAudio = function unlockWebAudio() {
//console.log('already done')
};
};
exports.masterGain = masterGain;
exports.unlockWebAudio = _unlockWebAudio;
exports.masterCompressor = compressor;
exports.setMasterVolume = _setMasterVolume;
exports.getMasterVolume = _getMasterVolume;
exports.getCompressionReduction = _getCompressionReduction;
exports.enableMasterCompressor = _enableMasterCompressor;
exports.configureMasterCompressor = _configureMasterCompressor;