From 12d5c737bf3023c8c65317ff9cb410d21920a607 Mon Sep 17 00:00:00 2001 From: Nicholas VanCise <40526638+thenick775@users.noreply.github.com> Date: Sun, 1 Dec 2024 10:58:23 -0800 Subject: [PATCH] feat: automatic audio resume (#222) --- gbajs3/src/emulator/mgba/mgba-emulator.tsx | 25 +++++++++++-------- .../emulator/use-background-emulator.spec.tsx | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/gbajs3/src/emulator/mgba/mgba-emulator.tsx b/gbajs3/src/emulator/mgba/mgba-emulator.tsx index 30b164ff..841c50ca 100644 --- a/gbajs3/src/emulator/mgba/mgba-emulator.tsx +++ b/gbajs3/src/emulator/mgba/mgba-emulator.tsx @@ -57,7 +57,7 @@ export type GBAEmulator = { quitEmulator: () => void; quitGame: () => void; remapKeyBindings: (keyBindings: KeyBinding[]) => void; - resume: () => void; + resume: () => Promise; run: (romPath: string) => boolean; screenshot: (fileName?: string) => boolean; setCurrentGameName: (gameName: string | undefined) => void; @@ -194,6 +194,14 @@ export const mGBAEmulator = (mGBA: mGBAEmulatorTypeDef): GBAEmulator => { return gbaSDLKey; }; + const resumeAudio = async () => { + if ( + mGBA.SDL2.audioContext.state === 'suspended' || + mGBA.SDL2.audioContext.state === 'interrupted' + ) + await mGBA.SDL2.audioContext.resume(); + }; + return { addCoreCallbacks: mGBA.addCoreCallbacks, autoLoadCheats: mGBA.autoLoadCheats, @@ -214,15 +222,7 @@ export const mGBAEmulator = (mGBA: mGBAEmulatorTypeDef): GBAEmulator => { loadSaveState: mGBA.loadState, listSaveStates: () => mGBA.FS.readdir(paths.saveStatePath), listRoms: mGBA.listRoms, - setVolume: async (volumePercent) => { - if ( - mGBA.SDL2.audioContext.state === 'suspended' || - mGBA.SDL2.audioContext.state === 'interrupted' - ) - await mGBA.SDL2.audioContext.resume(); - - mGBA.setVolume(volumePercent); - }, + setVolume: mGBA.setVolume, getVolume: mGBA.getVolume, enableKeyboardInput: () => mGBA.toggleInput(true), disableKeyboardInput: () => mGBA.toggleInput(false), @@ -251,7 +251,10 @@ export const mGBAEmulator = (mGBA: mGBAEmulatorTypeDef): GBAEmulator => { }, deleteFile: mGBA.FS.unlink, pause: mGBA.pauseGame, - resume: mGBA.resumeGame, + resume: async () => { + await resumeAudio(); + mGBA.resumeGame(); + }, quitGame: mGBA.quitGame, quitEmulator: mGBA.quitMgba, quickReload: mGBA.quickReload, diff --git a/gbajs3/src/hooks/emulator/use-background-emulator.spec.tsx b/gbajs3/src/hooks/emulator/use-background-emulator.spec.tsx index 0e23d8ad..a3f5fc54 100644 --- a/gbajs3/src/hooks/emulator/use-background-emulator.spec.tsx +++ b/gbajs3/src/hooks/emulator/use-background-emulator.spec.tsx @@ -15,7 +15,7 @@ vi.mock('@uidotdev/usehooks', async (importOriginal) => { }; }); -describe('useQuickReload hook', () => { +describe('useBackgroundEmulator hook', () => { it('pauses emulator when entering background if running and not paused', () => { const emulatorPauseSpy: () => void = vi.fn(); const emulatorResumeSpy: () => void = vi.fn();