Gameboy: make mute button work properly

This commit is contained in:
Alex Gleason 2023-11-24 23:18:15 -06:00
parent c17b193ca1
commit 5aa69c917f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -7,6 +7,8 @@ import { exitFullscreen, isFullscreen, requestFullscreen } from 'soapbox/feature
import { IconButton } from './ui'; import { IconButton } from './ui';
let gainNode: GainNode | undefined;
interface IGameboy extends Pick<React.HTMLAttributes<HTMLDivElement>, 'onFocus' | 'onBlur'> { interface IGameboy extends Pick<React.HTMLAttributes<HTMLDivElement>, 'onFocus' | 'onBlur'> {
/** Classname of the outer `<div>`. */ /** Classname of the outer `<div>`. */
className?: string; className?: string;
@ -65,11 +67,7 @@ const Gameboy: React.FC<IGameboy> = ({ className, src, aspect = 'normal', onFocu
}; };
const togglePaused = () => paused ? play() : pause(); const togglePaused = () => paused ? play() : pause();
const toggleMuted = () => setMuted(!muted);
const unmute = async () => {
await WasmBoy.resumeAudioContext();
setMuted(false);
};
const toggleFullscreen = () => { const toggleFullscreen = () => {
if (isFullscreen()) { if (isFullscreen()) {
@ -101,6 +99,12 @@ const Gameboy: React.FC<IGameboy> = ({ className, src, aspect = 'normal', onFocu
} }
}, [fullscreen]); }, [fullscreen]);
useEffect(() => {
if (gainNode) {
gainNode.gain.value = muted ? 0 : 1;
}
}, [gainNode, muted]);
return ( return (
<div <div
ref={node} ref={node}
@ -131,7 +135,7 @@ const Gameboy: React.FC<IGameboy> = ({ className, src, aspect = 'normal', onFocu
/> />
<IconButton <IconButton
className='text-white' className='text-white'
onClick={unmute} onClick={toggleMuted}
src={muted ? require('@tabler/icons/volume-3.svg') : require('@tabler/icons/volume.svg')} src={muted ? require('@tabler/icons/volume-3.svg') : require('@tabler/icons/volume.svg')}
/> />
<IconButton <IconButton
@ -158,7 +162,11 @@ const WasmBoyOptions = {
tileCaching: true, tileCaching: true,
gameboyFPSCap: 60, gameboyFPSCap: 60,
updateGraphicsCallback: false, updateGraphicsCallback: false,
updateAudioCallback: false, updateAudioCallback: (audioContext: AudioContext, audioBufferSourceNode: AudioBufferSourceNode) => {
gainNode = gainNode ?? audioContext.createGain();
audioBufferSourceNode.connect(gainNode);
return gainNode;
},
saveStateCallback: false, saveStateCallback: false,
}; };