three#PositionalAudio TypeScript Examples
The following examples show how to use
three#PositionalAudio.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Audio.tsx From spacesvr with MIT License | 5 votes |
export function Audio(props: AudioProps) {
const {
url,
dCone = new Vector3(180, 230, 0.1),
rollOff = 1,
volume = 1,
setAudioAnalyser,
fftSize = 128,
...rest
} = props;
const [speaker, setSpeaker] = useState<PositionalAudio>();
const camera = useThree((state) => state.camera);
const audio = useMemo(() => {
const a = document.createElement("audio");
a.src = url;
a.autoplay = false;
a.preload = "auto";
a.crossOrigin = "Anonymous";
a.loop = true;
return a;
}, []);
useEffect(() => {
const setupAudio = () => {
if (!audio.paused && !speaker) {
const listener = new AudioListener();
camera.add(listener);
const speak = new PositionalAudio(listener);
speak.setMediaElementSource(audio);
speak.setRefDistance(0.75);
speak.setRolloffFactor(rollOff);
speak.setVolume(volume);
speak.setDirectionalCone(dCone.x, dCone.y, dCone.z);
if (setAudioAnalyser) {
setAudioAnalyser(new AudioAnalyser(speak, fftSize));
}
setSpeaker(speak);
}
};
const playAudio = () => audio.play().then(() => setupAudio());
if (audio) {
audio.setAttribute("src", url);
audio.play().then(() => setupAudio());
document.addEventListener("click", playAudio);
return () => {
document.removeEventListener("click", playAudio);
};
}
}, [speaker, audio, url]);
useEffect(() => {
if (!speaker) return;
speaker.setRolloffFactor(rollOff);
speaker.setVolume(volume);
speaker.setDirectionalCone(dCone.x, dCone.y, dCone.z);
}, [dCone, rollOff, volume]);
return (
<group name="spacesvr-audio" {...rest}>
{speaker && <primitive object={speaker} />}
</group>
);
}
Example #2
Source File: Video.tsx From spacesvr with MIT License | 4 votes |
export function Video(props: Props) {
const {
src,
size = 1,
framed,
muted,
volume = 1,
frameMaterial,
frameWidth = 1,
...rest
} = props;
const camera = useThree((state) => state.camera);
const listener = useRef<THREE.AudioListener>();
const [speaker, setSpeaker] = useState<THREE.PositionalAudio>();
const [dims, setDims] = useState<Vector2 | null>();
const video = useMemo(() => {
const v = document.createElement("video");
// @ts-ignore
v.playsInline = true;
v.crossOrigin = "Anonymous";
v.loop = true;
v.src = src;
v.autoplay = false;
v.muted = muted ? muted : false;
return v;
}, []);
useEffect(() => {
const setupAudio = () => {
if (!muted && !video.paused && !speaker) {
const listener = new AudioListener();
camera.add(listener);
const speak = new PositionalAudio(listener);
speak.setMediaElementSource(video);
speak.setRefDistance(0.75);
speak.setRolloffFactor(1);
speak.setVolume(volume);
speak.setDirectionalCone(180, 230, 0.1);
setSpeaker(speak);
}
};
const playVideo = () => {
video
.play()
.then(() => setDims(new Vector2(video.videoWidth, video.videoHeight)));
setupAudio();
};
if (video) {
video.play().then(() => {
setDims(new Vector2(video.videoWidth, video.videoHeight));
setupAudio();
});
document.addEventListener("click", playVideo);
return () => {
document.removeEventListener("click", playVideo);
};
}
}, [speaker, video, muted]);
useEffect(() => {
return () => {
if (listener.current) {
camera.remove(listener.current);
listener.current.clear();
listener.current = undefined;
}
if (speaker) {
speaker.clear();
speaker.disconnect();
setSpeaker(undefined);
}
if (video) {
video.pause();
video.remove();
}
};
}, []);
if (!dims || !video) {
return null;
}
const max = Math.max(dims.x, dims.y);
const width = (dims.x / max) * size;
const height = (dims.y / max) * size;
return (
<group name="spacesvr-video" {...rest}>
<mesh>
<planeBufferGeometry attach="geometry" args={[width, height]} />
<meshBasicMaterial side={DoubleSide}>
<videoTexture attach="map" args={[video]} encoding={sRGBEncoding} />
</meshBasicMaterial>
</mesh>
{speaker && <primitive object={speaker} />}
{framed && (
<Frame
width={width}
height={height}
thickness={frameWidth}
material={frameMaterial}
/>
)}
</group>
);
}