Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add panner node into play() pipeline #331

Merged
merged 3 commits into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/audio/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface AudioPlayOpt {
* The start time, in seconds.
*/
seek?: number;
/**
* The stereo pan of the sound.
* -1.0 means fully from the left channel, 0.0 means centered, 1.0 means fully right.
* Defaults to 0.0.
*/
pan?: number;
}

export interface AudioPlay {
Expand Down Expand Up @@ -91,6 +97,12 @@ export interface AudioPlay {
* Volume of the sound. 1.0 means full volume, 0.5 means half volume.
*/
volume: number;
/**
* The stereo pan of the sound.
* -1.0 means fully from the left channel, 0.0 means centered, 1.0 means fully right.
* Defaults to 0.0.
*/
pan?: number;
/**
* If the audio should start again when it ends.
*/
Expand Down Expand Up @@ -130,6 +142,7 @@ export function play(
let srcNode = ctx.createBufferSource();
const onEndEvents = new KEvent();
const gainNode = ctx.createGain();
const panNode = ctx.createStereoPanner();
const pos = opt.seek ?? 0;
let startTime = 0;
let stopTime = 0;
Expand All @@ -138,7 +151,7 @@ export function play(
srcNode.loop = Boolean(opt.loop);
srcNode.detune.value = opt.detune ?? 0;
srcNode.playbackRate.value = opt.speed ?? 1;
srcNode.connect(gainNode);
srcNode.connect(panNode);
srcNode.onended = () => {
if (
getTime()
Expand All @@ -147,6 +160,8 @@ export function play(
onEndEvents.trigger();
}
};
panNode.pan.value = opt.pan ?? 0;
panNode.connect(gainNode);
gainNode.connect(audio.masterNode);
gainNode.gain.value = opt.volume ?? 1;

Expand Down Expand Up @@ -263,6 +278,14 @@ export function play(
return gainNode.gain.value;
},

set pan(pan: number) {
panNode.pan.value = pan;
},

get pan() {
return panNode.pan.value;
},

set loop(l: boolean) {
srcNode.loop = l;
},
Expand Down
Loading