Skip to content

Commit 00a39d2

Browse files
ehsankripken
authored andcommitted
Implement proper pause/replay support
1 parent 837404f commit 00a39d2

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/library_openal.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ var LibraryOpenAL = {
8686
gain.connect(AL.currentContext.ctx.destination);
8787
}
8888
gain.gain.value = 1; // work around a Firefox bug (bug 850970)
89-
AL.currentContext.src.push({loop: false, buffer: null, gain: gain, panner: panner});
89+
AL.currentContext.src.push({
90+
loop: false,
91+
buffer: null,
92+
gain: gain,
93+
panner: panner,
94+
paused: false,
95+
playTime: 0,
96+
pausedTime: 0
97+
});
9098
{{{ makeSetValue('sources', 'i', 'AL.currentContext.src.length', 'i32') }}};
9199
}
92100
},
@@ -265,16 +273,23 @@ var LibraryOpenAL = {
265273
console.error("alSourcePlay called with an invalid source");
266274
return;
267275
}
276+
var offset = 0;
268277
if ("src" in AL.currentContext.src[source - 1]) {
269278
// If the source is already playing, we need to resume from beginning.
270279
// We do that by stopping the current source and replaying it.
271280
_alSourceStop(source);
281+
} else if (AL.currentContext.src[source - 1].paused) {
282+
// So now we have to resume playback, remember the offset here.
283+
offset = AL.currentContext.src[source - 1].pausedTime -
284+
AL.currentContext.src[source - 1].playTime;
272285
}
273286
var src = AL.currentContext.ctx.createBufferSource();
274287
src.loop = AL.currentContext.src[source - 1].loop;
275288
src.buffer = AL.currentContext.src[source - 1].buffer;
276289
src.connect(AL.currentContext.src[source - 1].gain);
277-
src.start(0);
290+
src.start(0, offset);
291+
AL.currentContext.src[source - 1].playTime = AL.currentContext.ctx.currentTime;
292+
AL.currentContext.src[source - 1].paused = false;
278293
AL.currentContext.src[source - 1]['src'] = src;
279294
},
280295

@@ -288,7 +303,25 @@ var LibraryOpenAL = {
288303
return;
289304
}
290305
if ("src" in AL.currentContext.src[source - 1]) {
291-
AL.currentContext.src[source - 1].src.stop(0);
306+
AL.currentContext.src[source - 1]["src"].stop(0);
307+
delete AL.currentContext.src[source - 1]["src"];
308+
}
309+
},
310+
311+
alSourcePause: function(source) {
312+
if (!AL.currentContext) {
313+
console.error("alSourcePause called without a valid context");
314+
return;
315+
}
316+
if (source > AL.currentContext.src.length) {
317+
console.error("alSourcePause called with an invalid source");
318+
return;
319+
}
320+
if ("src" in AL.currentContext.src[source - 1] &&
321+
!AL.currentContext.src[source - 1].paused) {
322+
AL.currentContext.src[source - 1].paused = true;
323+
AL.currentContext.src[source - 1].pausedTime = AL.currentContext.ctx.currentTime;
324+
AL.currentContext.src[source - 1]["src"].stop(0);
292325
delete AL.currentContext.src[source - 1].src;
293326
}
294327
},

0 commit comments

Comments
 (0)