we now handle the frame timing in C, audio for me is now VERY stable

This commit is contained in:
Corwin 2024-10-01 18:36:53 +01:00
parent 1a9d5901cc
commit 10ee738f88
No known key found for this signature in database
3 changed files with 2 additions and 54 deletions

View file

@ -8,7 +8,6 @@ import {
import mGBA, { mGBAEmulator } from "./vendor/mgba";
import { GbaKey, KeyBindings } from "./bindings";
import { styled } from "styled-components";
import { useFrameSkip } from "./useFrameSkip.hook";
import { useController } from "./useController.hook";
import { useLocalStorage } from "./useLocalStorage.hook";
@ -148,7 +147,6 @@ export const Mgba = forwardRef<MgbaHandle, MgbaProps>(
};
}, [state]);
useFrameSkip(mgbaModule);
useController(mgbaModule);
useEffect(() => {

View file

@ -1,50 +0,0 @@
import { MutableRefObject, useEffect } from "react";
import { mGBAEmulator } from "./vendor/mgba";
export function useFrameSkip(
mgbaModule: MutableRefObject<mGBAEmulator | undefined>
) {
useEffect(() => {
let previous: number | undefined = undefined;
let stopped = false;
let smoothedFrameTime = 60;
let totalTime = 0;
let paused = false;
function raf(time: DOMHighResTimeStamp) {
if (previous) {
const delta = time - previous;
smoothedFrameTime = (smoothedFrameTime * 3 + delta) / 4;
const smoothedFrameRate = Math.round(1 / (smoothedFrameTime / 1000));
totalTime += 1 / smoothedFrameRate;
if (totalTime >= 1 / 60) {
totalTime -= 1 / 60;
if (paused) {
mgbaModule.current?.resumeGame();
paused = false;
}
} else {
if (!paused) {
mgbaModule.current?.pauseGame();
paused = true;
}
}
}
previous = time;
if (!stopped) {
window.requestAnimationFrame(raf);
}
}
window.requestAnimationFrame(raf);
return () => {
stopped = true;
};
}, [mgbaModule]);
}

View file

@ -10,10 +10,10 @@ ENV BUILD_DIR=build-wasm
WORKDIR /
RUN git clone https://github.com/thenick775/mgba.git --filter=tree:0 -b feature/wasm
RUN git clone https://github.com/corwinkuiper/mgba.git --filter=tree:0 -b wasm
RUN mkdir /mgba/build-wasm
WORKDIR /mgba/build-wasm
RUN git checkout 27dede256b6de36303a87d0886f81505a0f30c28
RUN git checkout 26a2701cef6fcad47c67559fcb7892004f8d489c
RUN emcmake cmake .. && make install DESTDIR=install