Skip to content

Commit

Permalink
Directly set audio param values instead of using setValueAtTime to at…
Browse files Browse the repository at this point in the history
…tempt to avoid odd race conditions in calling code
  • Loading branch information
ctoth committed Feb 13, 2024
1 parent a43fa80 commit 65312e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
27 changes: 12 additions & 15 deletions src/cacophony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,12 @@ export class Cacophony {
const { forward, up } = orientation;
const [forwardX, forwardY, forwardZ] = forward;
const [upX, upY, upZ] = up;
const currentTime = this.context.currentTime;
this.listener.forwardX.setValueAtTime(forwardX, currentTime);
this.listener.forwardY.setValueAtTime(forwardY, currentTime);
this.listener.forwardZ.setValueAtTime(forwardZ, currentTime);
this.listener.upX.setValueAtTime(upX, currentTime);
this.listener.upY.setValueAtTime(upY, currentTime);
this.listener.upZ.setValueAtTime(upZ, currentTime);
this.listener.forwardX.value = forwardX;
this.listener.forwardY.value = forwardY;
this.listener.forwardZ.value = forwardZ;
this.listener.upX.value = upX;
this.listener.upY.value = upY;
this.listener.upZ.value = upZ;
}

get listenerUpOrientation(): Position {
Expand All @@ -339,10 +338,9 @@ export class Cacophony {

set listenerUpOrientation(up: Position) {
const [x, y, z] = up;
const currentTime = this.context.currentTime;
this.listener.upX.setValueAtTime(x, currentTime);
this.listener.upY.setValueAtTime(y, currentTime);
this.listener.upZ.setValueAtTime(z, currentTime);
this.listener.upX.value = x;
this.listener.upY.value = y;
this.listener.upZ.value = z;
}

get listenerForwardOrientation(): Position {
Expand All @@ -351,10 +349,9 @@ export class Cacophony {

set listenerForwardOrientation(forward: Position) {
const [x, y, z] = forward;
const currentTime = this.context.currentTime;
this.listener.forwardX.setValueAtTime(x, currentTime);
this.listener.forwardY.setValueAtTime(y, currentTime);
this.listener.forwardZ.setValueAtTime(z, currentTime);
this.listener.forwardX.value = x;
this.listener.forwardY.value = y;
this.listener.forwardZ.value = z;
}

get listenerPosition(): Position {
Expand Down
24 changes: 13 additions & 11 deletions src/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,11 @@ export class Playback extends FilterManager implements BaseSound {
}

/**
* Sets the position of the audio source in 3D space (HRTF panning only).
* @param {Position} position - The [x, y, z] coordinates of the audio source.
* @throws {Error} Throws an error if the sound has been cleaned up or if HRTF panning is not used.
*/
* Sets the position of the audio source in 3D space (HRTF panning only).
* @param {Position} position - The [x, y, z] coordinates of the audio source.
* @throws {Error} Throws an error if the sound has been cleaned up or if HRTF panning is not used.
*/

set position(position: Position) {
if (!this.panner) {
throw new Error('Cannot move a sound that has been cleaned up');
Expand All @@ -559,16 +560,17 @@ export class Playback extends FilterManager implements BaseSound {
}
const [x, y, z] = position;
const panner = this.panner as PannerNode;
panner.positionX.setValueAtTime(x, this.context.currentTime);
panner.positionY.setValueAtTime(y, this.context.currentTime);
panner.positionZ.setValueAtTime(z, this.context.currentTime);
panner.positionX.value = x;
panner.positionY.value = y;
panner.positionZ.value = z;
}

/**
* Gets the position of the audio source in 3D space (HRTF panning only).
* @returns {Position} The [x, y, z] coordinates of the audio source.
* @throws {Error} Throws an error if the sound has been cleaned up or if HRTF panning is not used.
*/
* Gets the position of the audio source in 3D space (HRTF panning only).
* @returns {Position} The [x, y, z] coordinates of the audio source.
* @throws {Error} Throws an error if the sound has been cleaned up or if HRTF panning is not used.
*/

get position(): Position {
if (!this.panner) {
throw new Error('Cannot get position of a sound that has been cleaned up');
Expand Down

0 comments on commit 65312e7

Please sign in to comment.