Skip to content

Commit

Permalink
feat: automatic audio resume (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
thenick775 authored Dec 1, 2024
1 parent aafec4d commit 12d5c73
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions gbajs3/src/emulator/mgba/mgba-emulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type GBAEmulator = {
quitEmulator: () => void;
quitGame: () => void;
remapKeyBindings: (keyBindings: KeyBinding[]) => void;
resume: () => void;
resume: () => Promise<void>;
run: (romPath: string) => boolean;
screenshot: (fileName?: string) => boolean;
setCurrentGameName: (gameName: string | undefined) => void;
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion gbajs3/src/hooks/emulator/use-background-emulator.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 12d5c73

Please sign in to comment.