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) {
this.source.stop();
}
this.source = this.manager.context.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.muteNode);
this.applyConfig();
this.source.start(0, 0, this.currentConfig.duration);
// TODO include config offset and marker start
this.createAndStartBufferSource(0, this.currentConfig.duration);
this.startTime = this.manager.context.currentTime;
this.pausedTime = 0;
return this;
@ -79,11 +76,9 @@ var WebAudioSound = new Class({
},
resume: function () {
BaseSound.prototype.resume.call(this);
this.source = this.manager.context.createBufferSource();
this.source.buffer = this.audioBuffer;
this.source.connect(this.muteNode);
this.applyConfig();
this.source.start(0, this.pausedTime, this.currentConfig.duration - this.pausedTime);
var offset = this.pausedTime; // TODO include marker start time
var duration = this.currentConfig.duration - this.pausedTime;
this.createAndStartBufferSource(offset, duration);
this.startTime = this.manager.context.currentTime - this.pausedTime;
this.pausedTime = 0;
return this;
@ -96,6 +91,20 @@ var WebAudioSound = new Class({
this.pausedTime = 0;
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 () {
},
destroy: function () {