Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion clients/web/src/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let startSeek: number;
const songs: Record<string, Promise<AudioBuffer>> = {};

export const history = reactive<Array<Song>>([]);
export const volume = ref<number>(JSON.parse(localStorage.getItem("volume") ?? (10 ** (-10/20) + "")));
export const volume = ref<number>(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({
Expand Down Expand Up @@ -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
Expand Down