Skip to content

Commit d13bdfd

Browse files
Added createBufferSource to reuse buffer source creation logic
1 parent 6481872 commit d13bdfd

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

v3/src/sound/webaudio/WebAudioSound.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ var WebAudioSound = new Class({
109109
this.stopAndRemoveBufferSource();
110110
return true;
111111
},
112+
/**
113+
* @private
114+
*/
115+
createBufferSource: function () {
116+
var _this = this;
117+
var source = this.manager.context.createBufferSource();
118+
source.buffer = this.audioBuffer;
119+
source.connect(this.muteNode);
120+
source.onended = function (ev) {
121+
if (ev.target === _this.source) {
122+
// sound ended
123+
_this.hasEnded = true;
124+
}
125+
// else was stopped
126+
};
127+
return source;
128+
},
112129
/**
113130
* Used internally to do what the name says.
114131
*
@@ -120,16 +137,7 @@ var WebAudioSound = new Class({
120137
var offset = (this.currentMarker ? this.currentMarker.start : 0) + seek;
121138
var duration = this.duration - seek;
122139
this.startTime = this.manager.context.currentTime - seek;
123-
this.source = this.manager.context.createBufferSource();
124-
this.source.buffer = this.audioBuffer;
125-
this.source.connect(this.muteNode);
126-
this.source.onended = function (ev) {
127-
if (ev.target === this.source) {
128-
// sound ended
129-
this.hasEnded = true;
130-
}
131-
// else was stopped
132-
}.bind(this);
140+
this.source = this.createBufferSource();
133141
this.applyConfig();
134142
this.source.start(0, Math.max(0, offset), Math.max(0, duration));
135143
this.resetConfig();
@@ -138,16 +146,7 @@ var WebAudioSound = new Class({
138146
* @private
139147
*/
140148
createLoopBufferSource: function () {
141-
this.loopSource = this.manager.context.createBufferSource();
142-
this.loopSource.buffer = this.audioBuffer;
143-
this.loopSource.connect(this.muteNode);
144-
this.loopSource.onended = function (ev) {
145-
if (ev.target === this.source) {
146-
// sound ended
147-
this.hasEnded = true;
148-
}
149-
// else was stopped
150-
}.bind(this);
149+
this.loopSource = this.createBufferSource();
151150
},
152151
/**
153152
* Used internally to do what the name says.
@@ -206,9 +205,6 @@ var WebAudioSound = new Class({
206205
if (this.source) {
207206
this.source.playbackRate.setValueAtTime(this.totalRate, 0);
208207
}
209-
if (this.loopSource) {
210-
this.loopSource.playbackRate.setValueAtTime(this.totalRate, 0);
211-
}
212208
if (this.isPlaying) {
213209
this.rateUpdates.push({
214210
time: this.manager.context.currentTime - this.startTime,

0 commit comments

Comments
 (0)