Skip to content

Commit 478656d

Browse files
moved source buffer creation and starting to separate method for reuse
1 parent e6f1774 commit 478656d

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

v3/src/sound/webaudio/WebAudioSound.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ var WebAudioSound = new Class({
6161
if (this.source) {
6262
this.source.stop();
6363
}
64-
this.source = this.manager.context.createBufferSource();
65-
this.source.buffer = this.audioBuffer;
66-
this.source.connect(this.muteNode);
67-
this.applyConfig();
68-
this.source.start(0, 0, this.currentConfig.duration);
64+
// TODO include config offset and marker start
65+
this.createAndStartBufferSource(0, this.currentConfig.duration);
6966
this.startTime = this.manager.context.currentTime;
7067
this.pausedTime = 0;
7168
return this;
@@ -79,11 +76,9 @@ var WebAudioSound = new Class({
7976
},
8077
resume: function () {
8178
BaseSound.prototype.resume.call(this);
82-
this.source = this.manager.context.createBufferSource();
83-
this.source.buffer = this.audioBuffer;
84-
this.source.connect(this.muteNode);
85-
this.applyConfig();
86-
this.source.start(0, this.pausedTime, this.currentConfig.duration - this.pausedTime);
79+
var offset = this.pausedTime; // TODO include marker start time
80+
var duration = this.currentConfig.duration - this.pausedTime;
81+
this.createAndStartBufferSource(offset, duration);
8782
this.startTime = this.manager.context.currentTime - this.pausedTime;
8883
this.pausedTime = 0;
8984
return this;
@@ -96,6 +91,20 @@ var WebAudioSound = new Class({
9691
this.pausedTime = 0;
9792
return this;
9893
},
94+
/**
95+
* Used internally to do what the name says.
96+
*
97+
* @private
98+
* @param {number} offset
99+
* @param {number} duration
100+
*/
101+
createAndStartBufferSource: function (offset, duration) {
102+
this.source = this.manager.context.createBufferSource();
103+
this.source.buffer = this.audioBuffer;
104+
this.source.connect(this.muteNode);
105+
this.applyConfig();
106+
this.source.start(0, offset, duration);
107+
},
99108
update: function () {
100109
},
101110
destroy: function () {

0 commit comments

Comments
 (0)