From a851c52a738500ee594f6ae35c039a3cdb10a28d Mon Sep 17 00:00:00 2001 From: wntiv-main <60457971+wntiv-main@users.noreply.github.com> Date: Sun, 19 Jan 2025 16:39:38 +1300 Subject: [PATCH] fix memory leak --- clients/web/src/audio.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/clients/web/src/audio.ts b/clients/web/src/audio.ts index 64361a4..9d9ad0c 100644 --- a/clients/web/src/audio.ts +++ b/clients/web/src/audio.ts @@ -14,7 +14,7 @@ let startSeek: number; const songs: Record> = {}; export const history = reactive>([]); -export const volume = ref(JSON.parse(localStorage.getItem("volume") ?? (10 ** (-10/20) + ""))); +export const volume = ref(JSON.parse(localStorage.getItem("volume") ?? (10 ** (-10 / 20) + ""))); // default volume of -10dBFS is approximately 0.31 linearly (or exactly 1/sqrt(10)!) export const volumeDbfs = computed({ @@ -71,6 +71,14 @@ export async function play(song: Song, seek: number) { buffer: await (songs[url] ?? preload(url)), }); + audioSource.addEventListener("ended", function () { + // important: disconnect called in event after stop(): https://stackoverflow.com/a/53263710/13160456 + this.disconnect(); + // this is meant to act as cache? its a lot of stuff to keep in memory + // despite being so infrequently re-accessed + delete songs[url]; + }); + audioSource.connect(audioAnalyser).connect(audioGain).connect(audioCtx.destination); seek = Math.max(0, seek + currentTimestamp() - then); // time to create the audio node should be counted