Add GainNode
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
parent
c48f9a69cc
commit
0c3dab8dd2
1 changed files with 11 additions and 2 deletions
|
@ -208,6 +208,7 @@ export const useSpatialMediaStream = (
|
||||||
localVolume
|
localVolume
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const gainNodeRef = useRef<GainNode>();
|
||||||
const pannerNodeRef = useRef<PannerNode>();
|
const pannerNodeRef = useRef<PannerNode>();
|
||||||
const sourceRef = useRef<MediaStreamAudioSourceNode>();
|
const sourceRef = useRef<MediaStreamAudioSourceNode>();
|
||||||
|
|
||||||
|
@ -224,12 +225,18 @@ export const useSpatialMediaStream = (
|
||||||
refDistance: 3,
|
refDistance: 3,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (!gainNodeRef.current) {
|
||||||
|
gainNodeRef.current = new GainNode(audioContext, {
|
||||||
|
gain: localVolume,
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!sourceRef.current) {
|
if (!sourceRef.current) {
|
||||||
sourceRef.current = audioContext.createMediaStreamSource(stream);
|
sourceRef.current = audioContext.createMediaStreamSource(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tile = tileRef.current;
|
const tile = tileRef.current;
|
||||||
const source = sourceRef.current;
|
const source = sourceRef.current;
|
||||||
|
const gainNode = gainNodeRef.current;
|
||||||
const pannerNode = pannerNodeRef.current;
|
const pannerNode = pannerNodeRef.current;
|
||||||
|
|
||||||
const updatePosition = () => {
|
const updatePosition = () => {
|
||||||
|
@ -244,8 +251,9 @@ export const useSpatialMediaStream = (
|
||||||
pannerNodeRef.current.positionZ.value = -2;
|
pannerNodeRef.current.positionZ.value = -2;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
gainNode.gain.value = localVolume;
|
||||||
updatePosition();
|
updatePosition();
|
||||||
source.connect(pannerNode).connect(audioDestination);
|
source.connect(gainNode).connect(pannerNode).connect(audioDestination);
|
||||||
// HACK: We abuse the CSS transitionrun event to detect when the tile
|
// HACK: We abuse the CSS transitionrun event to detect when the tile
|
||||||
// moves, because useMeasure, IntersectionObserver, etc. all have no
|
// moves, because useMeasure, IntersectionObserver, etc. all have no
|
||||||
// ability to track changes in the CSS transform property
|
// ability to track changes in the CSS transform property
|
||||||
|
@ -254,10 +262,11 @@ export const useSpatialMediaStream = (
|
||||||
return () => {
|
return () => {
|
||||||
tile.removeEventListener("transitionrun", updatePosition);
|
tile.removeEventListener("transitionrun", updatePosition);
|
||||||
source.disconnect();
|
source.disconnect();
|
||||||
|
gainNode.disconnect();
|
||||||
pannerNode.disconnect();
|
pannerNode.disconnect();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [stream, spatialAudio, audioContext, audioDestination, mute]);
|
}, [stream, spatialAudio, audioContext, audioDestination, mute, localVolume]);
|
||||||
|
|
||||||
return [tileRef, mediaRef];
|
return [tileRef, mediaRef];
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue