audio2wave
Version:
draw wave in canvas from audio element source
49 lines • 1.9 kB
JavaScript
import { Drawer } from './Drawer';
import { DataProcesser } from './DataProcesser';
var Audio2Wave = /** @class */ (function () {
function Audio2Wave(config) {
var _this = this;
this.stateChagneHandler = function (e) {
if (e.data === 'running') {
// this.start();s
}
else if (e.data === 'suspended') {
// this.stop();
}
};
this.config = config;
var fftSize = config.dataConfig && config.dataConfig.fftSize || 512;
this.drawer = new Drawer(config.container, fftSize, config.drawerConfig);
this.processer = new DataProcesser(config.audio, config.dataConfig);
window.queueMicrotask(function () {
_this.addEventListener();
});
}
Audio2Wave.prototype.start = function () {
var _this = this;
this.drawer.beforeDraw = function () {
_this.processer.getByteFrequenceData();
_this.drawer.waveData = _this.processer.byteFrequencyData.slice(0);
};
return this.processer.start().then(function (_) { return _this.drawer.start(); });
};
Audio2Wave.prototype.stop = function () {
var _this = this;
return this.drawer.stop().then(function (_) { return _this.processer.stop(); });
};
Audio2Wave.prototype.addEventListener = function () {
this.processer.addEventListener('statechange', this.stateChagneHandler);
};
Audio2Wave.prototype.removeEventListener = function () {
this.processer.removeEventListener('statechange', this.stateChagneHandler);
};
Audio2Wave.prototype.destroy = function () {
this.removeEventListener();
this.drawer.destroy();
this.processer.destroy();
return Promise.resolve();
};
return Audio2Wave;
}());
export { Audio2Wave };
//# sourceMappingURL=index.js.map