Skip to content

Commit

Permalink
Clamp stereoPan instead of throwing an error
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Feb 6, 2024
1 parent 405a08d commit 1a7baf9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cacophony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class Cacophony {
if (periodicWave) {
oscillator.setPeriodicWave(periodicWave);
}
oscillator.frequency.value = frequency!;
oscillator.frequency.setValueAtTime(frequency || 440, this.context.currentTime);
oscillator.connect(this.globalGainNode);
return oscillator
}
Expand Down
9 changes: 6 additions & 3 deletions src/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export class Playback extends FilterManager implements BaseSound {
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.');
if (!this.panner) {
throw new Error('Cannot set stereo pan of a sound that has been cleaned up');
}
(this.panner as StereoPannerNode).pan.setValueAtTime(value, this.context.currentTime);
(this.panner as StereoPannerNode).pan.setValueAtTime(clamp(value, -1, 1), this.context.currentTime);
}

get duration() {
Expand Down Expand Up @@ -453,3 +453,6 @@ export class Playback extends FilterManager implements BaseSound {
}
}

function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}

0 comments on commit 1a7baf9

Please sign in to comment.