audio2wave
Version:
draw wave in canvas from audio element source
95 lines • 3.74 kB
JavaScript
import { IEvent } from './interface/IDataProcesser';
import { Emitor } from './utils/Emitor/index';
var DEFAULT_CONFIG = {
fftSize: 512,
};
var DataProcesser = /** @class */ (function () {
function DataProcesser(audio, config) {
if (config === void 0) { config = DEFAULT_CONFIG; }
this.state = 'suspended';
this.running = false;
this.audio = audio;
this.config = config;
this.init();
}
DataProcesser.prototype.init = function () {
var _this = this;
this.emitor = new Emitor();
this.audioContext = this.createAudioContext();
this.audioContext.onstatechange = function (event) {
_this.state = event.target.state;
_this.emitor.emit('statchange', new IEvent('statechange', _this.state));
};
// this.audioContext.onstatechange
};
DataProcesser.prototype.createAudioContext = function () {
if (window.AudioContext) {
return new AudioContext();
}
else if (window.webkitAudioContext) {
return new webkitAudioContext();
}
else {
throw new Error('当前浏览器版本不支持AudioContext和webkitAudioContext,请升级或者更换浏览器');
}
};
DataProcesser.prototype.createAudioSource = function (elementOrStream) {
if (elementOrStream instanceof HTMLMediaElement) {
return this.audioContext.createMediaElementSource(elementOrStream);
}
return this.audioContext.createMediaStreamSource(elementOrStream);
};
DataProcesser.prototype.getByteFrequenceData = function () {
this.analyser.getByteFrequencyData(this.byteFrequencyData);
};
DataProcesser.prototype.start = function () {
if (this.running)
return Promise.reject('Processor still running');
this.running = true;
if (!this.analyser) {
this.analyser = this.audioContext.createAnalyser();
this.analyser.fftSize = this.config.fftSize || 512;
}
if (!this.audioSourceNode) {
var bufferLength = this.analyser.frequencyBinCount;
this.byteFrequencyData = new Uint8Array(bufferLength);
this.audioSourceNode = this.createAudioSource(this.audio);
}
this.audioSourceNode.connect(this.analyser);
this.analyser.connect(this.audioContext.destination);
return this.audioContext.resume();
};
DataProcesser.prototype.stop = function () {
var _this = this;
if (!this.running)
return Promise.reject('Processor was stoped');
this.running = false;
this.analyser.disconnect();
this.audioSourceNode.disconnect();
this.audioContext.suspend().catch(function (e) {
_this.emitor.emit('error', new IEvent('error', e));
}).finally(function () {
// this.analyser = null;
// this.audioSourceNode = null;
});
return Promise.resolve();
};
DataProcesser.prototype.destroy = function () {
var _this = this;
this.stop().finally(function () {
_this.audioContext.close();
_this.audioContext = null;
_this.emitor.emit('destroy', new IEvent('destroy', null));
});
return Promise.resolve();
};
DataProcesser.prototype.addEventListener = function (eventName, listener) {
this.emitor.addListener(eventName, listener);
};
DataProcesser.prototype.removeEventListener = function (eventName, listener) {
this.emitor.removeListener(eventName, listener);
};
return DataProcesser;
}());
export { DataProcesser };
//# sourceMappingURL=DataProcesser.js.map