traffic light: every color has its own buzz sound volume
This commit is contained in:
parent
5dbe51d871
commit
254dbaf2fd
2 changed files with 18 additions and 13 deletions
|
|
@ -40,21 +40,23 @@ export const TrafficLight = memo(function TrafficLight({state, speed, raiseUIEve
|
||||||
|
|
||||||
// preloadAudio(sndAtmosphere);
|
// preloadAudio(sndAtmosphere);
|
||||||
|
|
||||||
// the traffic light makes sound too:
|
// play wind
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const stopPlaying = playURL(sndAtmosphere, true);
|
const stopPlaying = playURL(sndAtmosphere, true);
|
||||||
return () => stopPlaying();
|
return () => stopPlaying();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
// for added realism, every light color has its own buzzing noise volume
|
||||||
if (redOn || yellowOn || greenOn) {
|
for (const [color, gain] of [[redOn, 0.5], [yellowOn, 1], [greenOn, 0.3]] as [boolean, number][]) {
|
||||||
const stopPlaying = playURL(sndBuzz, true);
|
useEffect(() => {
|
||||||
return () => {
|
if (color) {
|
||||||
stopPlaying();
|
const stopPlaying = playURL(sndBuzz, true, gain);
|
||||||
};
|
return () => {
|
||||||
}
|
stopPlaying();
|
||||||
else return () => {};
|
};
|
||||||
}, [redOn || yellowOn || greenOn])
|
}
|
||||||
|
}, [color]);
|
||||||
|
}
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
<style>{`
|
<style>{`
|
||||||
|
|
|
||||||
|
|
@ -28,12 +28,15 @@ export function useAudioContext(speed: number) {
|
||||||
.then(buf => ref.current!.ctx.decodeAudioData(buf));
|
.then(buf => ref.current!.ctx.decodeAudioData(buf));
|
||||||
}), [ref.current]);
|
}), [ref.current]);
|
||||||
|
|
||||||
function play(url: string, loop: boolean) {
|
function play(url: string, loop: boolean, gain: number = 1) {
|
||||||
const srcPromise = url2AudioBuf(url)
|
const srcPromise = url2AudioBuf(url)
|
||||||
.then(audioBuf => {
|
.then(audioBuf => {
|
||||||
const src = ref.current!.ctx.createBufferSource();
|
const src = ref.current!.ctx.createBufferSource();
|
||||||
|
const gainNode = ref.current!.ctx.createGain();
|
||||||
|
gainNode.gain.value = gain;
|
||||||
|
gainNode.connect(ref.current!.hipass);
|
||||||
src.buffer = audioBuf;
|
src.buffer = audioBuf;
|
||||||
src.connect(ref.current!.hipass);
|
src.connect(gainNode);
|
||||||
src.playbackRate.value = speed;
|
src.playbackRate.value = speed;
|
||||||
src.loop = loop;
|
src.loop = loop;
|
||||||
src.start();
|
src.start();
|
||||||
|
|
@ -59,5 +62,5 @@ export function useAudioContext(speed: number) {
|
||||||
}
|
}
|
||||||
}, [speed]);
|
}, [speed]);
|
||||||
|
|
||||||
return [play, url2AudioBuf] as [(url: string, loop: boolean) => ()=>void, (url:string)=>void];
|
return [play, url2AudioBuf] as [(url: string, loop: boolean, gain?: number) => ()=>void, (url:string)=>void];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue