UNPKG

@awayjs/core

Version:
225 lines (224 loc) 7.98 kB
import { __extends } from "tslib"; import { AssetBase } from '../library/AssetBase'; import { AudioManager } from '../managers/AudioManager'; import { ByteArray } from '../utils/ByteArray'; import { BaseAudioChannel } from '../managers/BaseAudioChannel'; // TODO: Audio should probably be an interface containing play/stop/seek functionality var WaveAudio = /** @class */ (function (_super) { __extends(WaveAudio, _super); /** * */ function WaveAudio(data, channelGroup) { if (channelGroup === void 0) { channelGroup = 0; } var _this = _super.call(this) || this; _this._volume = 1; _this._pan = 0; _this._audioChannels = []; _this._channelsPlaying = 0; _this._data = data; _this._channelGroup = channelGroup; _this.onChannelCompleteStopError = _this.onChannelCompleteStopError.bind(_this); return _this; } Object.defineProperty(WaveAudio.prototype, "isPlaying", { get: function () { return (this._channelsPlaying > 0); }, enumerable: false, configurable: true }); Object.defineProperty(WaveAudio.prototype, "assetType", { /** * * @returns {string} */ get: function () { return WaveAudio.assetType; }, enumerable: false, configurable: true }); WaveAudio.prototype.detachChannel = function (channel) { if (!channel) { return; } channel.removeEventListener(BaseAudioChannel.COMPLETE, this.onChannelCompleteStopError); channel.removeEventListener(BaseAudioChannel.STOP, this.onChannelCompleteStopError); channel.removeEventListener(BaseAudioChannel.ERROR, this.onChannelCompleteStopError); }; WaveAudio.prototype.attachChannel = function (channel) { if (!channel) { return; } channel.owner = this; channel.addEventListener(BaseAudioChannel.COMPLETE, this.onChannelCompleteStopError); channel.addEventListener(BaseAudioChannel.STOP, this.onChannelCompleteStopError); channel.addEventListener(BaseAudioChannel.ERROR, this.onChannelCompleteStopError); }; WaveAudio.prototype.onChannelCompleteStopError = function (e) { var channel = e.target; var index = this._audioChannels.indexOf(channel); if (index >= 0) { this.detachChannel(channel); this._audioChannels.splice(index, 1); this._channelsPlaying--; } if (this._channelsPlaying !== 0) { return; } // we stop channels this.stopInternal(false); }; Object.defineProperty(WaveAudio.prototype, "pan", { get: function () { return this._pan; }, set: function (value) { if (this._pan == value) return; this._pan = value; if (this._audioChannel) this._audioChannel.pan = this._pan; }, enumerable: false, configurable: true }); Object.defineProperty(WaveAudio.prototype, "channelGroup", { get: function () { return this._channelGroup; }, set: function (value) { if (this._channelGroup == value) return; this._channelGroup = value; var groupVolume = AudioManager.getVolume(value); for (var i = 0; i < this._audioChannels.length; i++) this._audioChannels[i].groupVolume = groupVolume; }, enumerable: false, configurable: true }); Object.defineProperty(WaveAudio.prototype, "volume", { get: function () { return this._volume; }, set: function (value) { if (this._volume == value) return; this._volume = value; if (this._audioChannel) this._audioChannel.volume = this._volume; for (var i = 0; i < this._audioChannels.length; i++) this._audioChannels[i].volume = this._volume; }, enumerable: false, configurable: true }); Object.defineProperty(WaveAudio.prototype, "currentTime", { get: function () { if (this._audioChannel) return this._audioChannel.currentTime; return 0; }, enumerable: false, configurable: true }); Object.defineProperty(WaveAudio.prototype, "duration", { get: function () { if (this._audioChannel) return this._audioChannel.duration; return 0; }, enumerable: false, configurable: true }); WaveAudio.prototype.dispose = function () { this.stop(); }; WaveAudio.prototype.play = function (offset, loop) { if (loop === void 0) { loop = false; } this._audioChannel = AudioManager.getChannel(this._data.size, this.channelGroup); if (this._audioChannel) { this._channelsPlaying++; this._audioChannels.push(this._audioChannel); this._audioChannel.volume = this._volume; this.attachChannel(this._audioChannel); this._data.play(this._audioChannel, offset, loop, this.id); } return this._audioChannel; }; WaveAudio.prototype.stopInternal = function (stopChannels) { if (stopChannels === void 0) { stopChannels = false; } // we not unlink channels for case when it can be restarted for (var _i = 0, _a = this._audioChannels; _i < _a.length; _i++) { var channel = _a[_i]; // detach died channels if (channel.stopped || stopChannels) { // detach channels, now it can't be restarted this.detachChannel(channel); } if (stopChannels) { channel.stop(); } } this._channelsPlaying = 0; this._audioChannels.length = 0; this._audioChannel = null; }; WaveAudio.prototype.stop = function () { this.stopInternal(true); }; WaveAudio.prototype.clone = function () { var newInstance = new WaveAudio(this._data); newInstance.name = this.name; return newInstance; }; WaveAudio.assetType = '[asset WaveAudio]'; return WaveAudio; }(AssetBase)); export { WaveAudio }; var WaveAudioData = /** @class */ (function () { function WaveAudioData(data, meta) { if (data instanceof Blob) { this._blob = data; } else if (data instanceof ByteArray) { this._buffer = data.arraybytes; } else if (ArrayBuffer.isView(data)) { this._buffer = data.buffer; } else { this._buffer = data; } this.meta = meta; } Object.defineProperty(WaveAudioData.prototype, "size", { get: function () { if (this._buffer) return this._buffer.byteLength; return this._blob.size; }, enumerable: false, configurable: true }); WaveAudioData.prototype.play = function (audioChannel, offset, loop, id) { var _this = this; if (this._buffer) { audioChannel.play(this._buffer, offset, loop, id, this.meta); } else if (!this._loading) { this._loading = true; var fileReader = new FileReader(); fileReader.onload = function (event) { return _this._blobConverted(event, audioChannel, offset, loop, id); }; fileReader.readAsArrayBuffer(this._blob); } }; WaveAudioData.prototype._blobConverted = function (event, audioChannel, offset, loop, id) { this._buffer = event.target.result; audioChannel.play(this._buffer, offset, loop, id, this.meta); }; return WaveAudioData; }()); export { WaveAudioData };