Skip to content

Commit

Permalink
Fix stereo-only panning
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Feb 6, 2024
1 parent 7935cd8 commit 1c59364
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cacophony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Cacophony {
const audio = new Audio();
audio.src = url;
audio.crossOrigin = 'anonymous';
return new Sound(url, undefined, this.context, this.globalGainNode, SoundType.HTML);
return new Sound(url, undefined, this.context, this.globalGainNode, SoundType.HTML, panType);
}
return CacheManager.getAudioBuffer(url, this.context).then(buffer => new Sound(url, buffer, this.context, this.globalGainNode, soundType, panType));
}
Expand Down
16 changes: 9 additions & 7 deletions src/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
* including buffer sources and media elements. It also provides detailed control over the audio's
* spatial characteristics when using 3D audio.
*/
import { AudioContext, IAudioBuffer, IGainNode, IPannerNode, IPannerOptions, IStereoPannerNode } from "standardized-audio-context";


import { AudioContext, IAudioBuffer, IPannerNode, IPannerOptions, IStereoPannerNode } from "standardized-audio-context";
import { BaseSound, FadeType, LoopCount, PanType, Position } from "./cacophony";
import { BiquadFilterNode, SourceNode } from "./context";
import { BiquadFilterNode, GainNode, SourceNode } from "./context";
import { FilterManager } from "./filters";

type GainNode = IGainNode<AudioContext>;

type PannerNode = IPannerNode<AudioContext>;
type StereoPannerNode = IStereoPannerNode<AudioContext>;

Expand Down Expand Up @@ -65,20 +67,20 @@ export class Playback extends FilterManager implements BaseSound {
}

get stereoPan(): number | null {
if (this.panType === 'stereo' && this.panner instanceof StereoPannerNode) {
return this.panner.pan.value;
if (this.panType === 'stereo') {
return (this.panner as StereoPannerNode).pan.value;
}
return null;
}

set stereoPan(value: number) {
if (this.panType !== 'stereo' || !(this.panner instanceof StereoPannerNode)) {
if (this.panType !== 'stereo') {
throw new Error('Stereo panning is not available when using HRTF.');
}
if (value < -1 || value > 1) {
throw new Error('Stereo pan value must be between -1 and 1.');
}
this.panner.pan.setValueAtTime(value, this.context.currentTime);
(this.panner as StereoPannerNode).pan.setValueAtTime(value, this.context.currentTime);
}

get duration() {
Expand Down
1 change: 0 additions & 1 deletion src/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class Sound extends FilterManager implements BaseSound {
this.buffer = buffer;
this.context = context;
this.globalGainNode = globalGainNode;
this.panType = panType;
}

/**
Expand Down
Binary file added test.ogg
Binary file not shown.

0 comments on commit 1c59364

Please sign in to comment.