moved source buffer creation and starting to separate method for reuse

This commit is contained in:
Pavle Goloskokovic 2017-11-17 17:01:12 +01:00
parent e6f17744b6
commit 478656df98

View file

@ -61,11 +61,8 @@ var WebAudioSound = new Class({
if (this.source) { if (this.source) {
this.source.stop(); this.source.stop();
} }
this.source = this.manager.context.createBufferSource(); // TODO include config offset and marker start
this.source.buffer = this.audioBuffer; this.createAndStartBufferSource(0, this.currentConfig.duration);
this.source.connect(this.muteNode);
this.applyConfig();
this.source.start(0, 0, this.currentConfig.duration);
this.startTime = this.manager.context.currentTime; this.startTime = this.manager.context.currentTime;
this.pausedTime = 0; this.pausedTime = 0;
return this; return this;
@ -79,11 +76,9 @@ var WebAudioSound = new Class({
}, },
resume: function () { resume: function () {
BaseSound.prototype.resume.call(this); BaseSound.prototype.resume.call(this);
this.source = this.manager.context.createBufferSource(); var offset = this.pausedTime; // TODO include marker start time
this.source.buffer = this.audioBuffer; var duration = this.currentConfig.duration - this.pausedTime;
this.source.connect(this.muteNode); this.createAndStartBufferSource(offset, duration);
this.applyConfig();
this.source.start(0, this.pausedTime, this.currentConfig.duration - this.pausedTime);
this.startTime = this.manager.context.currentTime - this.pausedTime; this.startTime = this.manager.context.currentTime - this.pausedTime;
this.pausedTime = 0; this.pausedTime = 0;
return this; return this;
@ -96,6 +91,20 @@ var WebAudioSound = new Class({
this.pausedTime = 0; this.pausedTime = 0;
return this; return this;
}, },
/**
* Used internally to do what the name says.
*
* @private
* @param {number} offset
* @param {number} duration
*/
createAndStartBufferSource: function (offset, duration) {
this.source = this.manager.context.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.muteNode);
this.applyConfig();
this.source.start(0, offset, duration);
},
update: function () { update: function () {
}, },
destroy: function () { destroy: function () {