@awayjs/core
Version:
AwayJS core classes
235 lines (234 loc) • 9.13 kB
JavaScript
import { __extends } from "tslib";
import { BaseAudioChannel } from './BaseAudioChannel';
var StreamingAudioChannel = /** @class */ (function (_super) {
__extends(StreamingAudioChannel, _super);
function StreamingAudioChannel(groupID, groupVolume, groupPan) {
if (groupID === void 0) { groupID = 0; }
if (groupVolume === void 0) { groupVolume = 1; }
if (groupPan === void 0) { groupPan = 1; }
var _this = _super.call(this) || this;
_this._groupID = 0;
_this._groupVolume = 1;
_this._groupPan = 0;
_this._startTime = 0;
_this._groupID = groupID;
_this._groupVolume = groupVolume;
_this._groupPan = groupPan;
_this._sourceOpenDelegate = function (event) { return _this._sourceOpen(event); };
_this._updateEndDelegate = function (event) { return _this._updateEnd(event); };
_this._audio = new Audio();
_this._audio.ontimeupdate = function (event) { return _this._onTimeUpdate(event); };
_this._updateSource();
return _this;
}
StreamingAudioChannel.stopAllSounds = function (channelGroup) {
if (channelGroup === void 0) { channelGroup = -1; }
var len = StreamingAudioChannel._channels.length;
if (channelGroup < 0) {
for (var j = 0; j < len; j++) {
StreamingAudioChannel._channels[j].stop();
}
StreamingAudioChannel._channels.length = 0;
return;
}
var aliveChannels = [];
for (var j = 0; j < len; j++) {
if (StreamingAudioChannel._channels[j].groupID == channelGroup) {
StreamingAudioChannel._channels[j].stop();
}
else {
aliveChannels[aliveChannels.length] = StreamingAudioChannel._channels[j];
}
}
StreamingAudioChannel._channels = aliveChannels;
};
StreamingAudioChannel.setChannelGroupVolume = function (value, channelGroup) {
if (channelGroup === void 0) { channelGroup = -1; }
var len = StreamingAudioChannel._channels.length;
if (channelGroup < 0) {
for (var j = 0; j < len; j++) {
StreamingAudioChannel._channels[j].groupVolume = value;
}
return;
}
for (var j = 0; j < len; j++) {
if (StreamingAudioChannel._channels[j].groupID == channelGroup) {
StreamingAudioChannel._channels[j].groupVolume = value;
}
}
};
Object.defineProperty(StreamingAudioChannel.prototype, "duration", {
get: function () {
return this._duration;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StreamingAudioChannel.prototype, "currentTime", {
get: function () {
return this._audio.currentTime - this._startTime;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StreamingAudioChannel.prototype, "groupID", {
get: function () {
return this._groupID;
},
set: function (value) {
this._groupID = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StreamingAudioChannel.prototype, "groupVolume", {
get: function () {
return this._groupVolume;
},
set: function (value) {
if (this._groupVolume == value)
return;
this._groupVolume = value;
this._audio.volume = this._groupVolume * this._volume;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StreamingAudioChannel.prototype, "groupPan", {
get: function () {
return this._groupPan;
},
set: function (value) {
},
enumerable: false,
configurable: true
});
Object.defineProperty(StreamingAudioChannel.prototype, "volume", {
get: function () {
return this._volume;
},
set: function (value) {
if (this._volume == value)
return;
this._volume = value;
this._audio.volume = this._volume;
},
enumerable: false,
configurable: true
});
StreamingAudioChannel.prototype.isPlaying = function () {
return this._isPlaying;
};
StreamingAudioChannel.prototype.isLooping = function () {
return this._isLooping;
};
StreamingAudioChannel.prototype.isDecoding = function () {
return false;
};
Object.defineProperty(StreamingAudioChannel.prototype, "pan", {
get: function () {
//todo
return 0;
},
set: function (value) {
//todo
},
enumerable: false,
configurable: true
});
StreamingAudioChannel.prototype.restart = function () {
console.warn('[StreamingAudioChannel] Restart not implemented');
return false;
};
StreamingAudioChannel.prototype.play = function (buffer, offset, loop) {
if (offset === void 0) { offset = 0; }
if (loop === void 0) { loop = false; }
_super.prototype.play.call(this, buffer, offset, loop, 0);
this._isPlaying = true;
if (this._isLooping || this._isLooping != this._loops > 0) {
this._isLooping = this._loops > 0;
this._sourceDirty = true;
}
if (this._sourceDirty)
this._updateSource();
this._buffer = buffer;
this._offset = offset;
this._audio.volume = this._groupVolume * this._volume;
if (!this._isQueuing && !this._isOpening)
this._queueBuffer();
};
StreamingAudioChannel.prototype.stopInternally = function (emitComplete) {
if (emitComplete === void 0) { emitComplete = false; }
this._audio.pause();
this._isPlaying = false;
this._isLooping = false;
_super.prototype.completeInternally.call(this, emitComplete, emitComplete);
};
StreamingAudioChannel.prototype.stop = function () {
this.stopInternally(false);
this.dispatchStop(false);
};
StreamingAudioChannel.prototype._sourceOpen = function (event) {
this._isOpening = false;
//TODO: find out how in the name of all that is holy how this can be
//executed more than once on a MediaSource object
if (this._mediaSource.activeSourceBuffers.length) {
console.log('ERR: double sourceopen event called');
this.dispatchStop(true);
return;
}
this._sourceBuffer = this._mediaSource.addSourceBuffer('audio/mpeg');
this._sourceBuffer.addEventListener('updateend', this._updateEndDelegate);
if (this._isPlaying)
this._queueBuffer();
};
StreamingAudioChannel.prototype._queueBuffer = function () {
this._isQueuing = true;
this._startTime = this._sourceBuffer.timestampOffset;
this._sourceBuffer.appendBuffer(this._buffer);
};
StreamingAudioChannel.prototype._updateEnd = function (event) {
this._isQueuing = false;
if (this._isLooping)
this._mediaSource.endOfStream();
this._duration = this._sourceBuffer.timestampOffset - this._startTime;
this._audio.currentTime = this._startTime + this._offset;
this._audio.play();
};
StreamingAudioChannel.prototype._onTimeUpdate = function (event) {
//TODO: more accurate end detection
if (!this._isLooping && this._duration < this._audio.currentTime - this._startTime + 0.1) {
this.stopInternally(true);
}
};
StreamingAudioChannel.prototype._updateSource = function () {
if (this._mediaSource)
this._disposeSource();
this._isQueuing = false;
this._isOpening = true;
this._mediaSource = new MediaSource();
this._mediaSource.addEventListener('sourceopen', this._sourceOpenDelegate);
this._urlString = URL.createObjectURL(this._mediaSource);
this._audio.src = this._urlString;
this._audio.loop = this._isLooping;
this._sourceDirty = false;
};
StreamingAudioChannel.prototype._disposeSource = function () {
if (!this._isOpening) {
if (this._sourceBuffer.timestampOffset)
this._sourceBuffer.remove(0, this._sourceBuffer.timestampOffset);
this._sourceBuffer.removeEventListener('updateend', this._updateEndDelegate);
this._mediaSource.removeSourceBuffer(this._sourceBuffer);
delete this._sourceBuffer;
this._sourceBuffer = null;
}
this._mediaSource.removeEventListener('sourceopen', this._sourceOpenDelegate);
URL.revokeObjectURL(this._urlString);
delete this._mediaSource;
this._mediaSource = null;
};
StreamingAudioChannel.maxChannels = 4;
StreamingAudioChannel._channels = new Array();
return StreamingAudioChannel;
}(BaseAudioChannel));
export { StreamingAudioChannel };