Skip to content

Commit

Permalink
Implement AL_PITCH in AL Module
Browse files Browse the repository at this point in the history
  • Loading branch information
kidanger authored and juj committed Mar 1, 2016
1 parent 53266e1 commit fe96f0e
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/library_openal.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ var LibraryOpenAL = {
for (var i = src.buffersPlayed; i < src.queue.length; i++) {
var entry = src.queue[i];

var startOffset = startTime - currentTime;
var endTime = startTime + entry.buffer.duration;
var startOffset = (startTime - currentTime) / src.playbackRate;
var endTime = startTime + entry.buffer.duration / src.playbackRate;
if (entry.src) {
endTime = startTime + entry.src.duration;
}

// Clean up old buffers.
if (currentTime >= endTime) {
Expand All @@ -81,6 +84,8 @@ var LibraryOpenAL = {
entry.src = AL.currentContext.ctx.createBufferSource();
entry.src.buffer = entry.buffer;
entry.src.connect(src.gain);
entry.src.playbackRate.value = src.playbackRate;
entry.src.duration = entry.buffer.duration / src.playbackRate;
if (typeof(entry.src.start) !== 'undefined') {
entry.src.start(startTime, offset);
} else if (typeof(entry.src.noteOn) !== 'undefined') {
Expand Down Expand Up @@ -345,6 +350,7 @@ var LibraryOpenAL = {
state: 0x1011 /* AL_INITIAL */,
queue: [],
loop: false,
playbackRate: 1,
get refDistance() {
return this._refDistance || 1;
},
Expand Down Expand Up @@ -521,9 +527,32 @@ var LibraryOpenAL = {
}
switch (param) {
case 0x1003 /* AL_PITCH */:
#if OPENAL_DEBUG
console.log("alSourcef was called with 0x1003 /* AL_PITCH */, but Web Audio does not support static pitch changes");
#endif
if (value <= 0) {
AL.currentContext.err = 0xA003 /* AL_INVALID_VALUE */;
return;
}
src.playbackRate = value;

var currentTime = AL.currentContext.ctx.currentTime;
// update currently playing entry
var entry = src.queue[src.buffersPlayed];
if (entry.src) {
var oldrate = entry.src.playbackRate.value;
var offset = currentTime - src.bufferPosition;
entry.src.duration = (entry.src.duration - offset) * oldrate / src.playbackRate;
entry.src.playbackRate.value = src.playbackRate;
}
src.bufferPosition = currentTime;
// stop other buffers
for (var k = src.buffersPlayed + 1; k < src.queue.length; k++) {
var entry = src.queue[k];
if (entry.src) {
entry.src.stop();
entry.src = null;
}
}
// update the source to reschedule buffers with the new playbackRate
AL.updateSource(src);
break;
case 0x100A /* AL_GAIN */:
src.gain.gain.value = value;
Expand Down Expand Up @@ -1059,8 +1088,9 @@ var LibraryOpenAL = {
return;
}
switch (param) {
// case 0x1003 /* AL_PITCH */:
// break;
case 0x1003 /* AL_PITCH */:
{{{ makeSetValue('value', '0', 'src.playbackRate', 'float') }}}
break;
case 0x100A /* AL_GAIN */:
{{{ makeSetValue('value', '0', 'src.gain.gain.value', 'float') }}}
break;
Expand Down

0 comments on commit fe96f0e

Please sign in to comment.