0 votes
by (390 points)
Hello everyone!

Is there are way to "uncache" audio to clean up the RAM a bit? I already looked up on sugarcubes site but haven't found anything.

1 Answer

0 votes
by (44.7k points)

If we assume the variable _trackID equals the track ID of the track you want to uncache, then you would do this:

<<set _tracks = Macro.get("cacheaudio").tracks>>
<<if _tracks.hasOwnProperty(_trackID)>>
	<<if _tracks[_trackID].audio.duration>>
		<<run alert('Error: Cannot uncache track "' + _trackID + '".  Track currently queued for playing.')>>
	<<else>>
		<<run _tracks[_trackID].destroy()>>
	<</if>>
<<else>>
	<<run alert('Error: Track "' + _trackID + '" not found.')>>
<</if>>

That will display error popups if you attempt to delete a track that doesn't exist or a track that is currently queued for playing (currently playing or paused).  You can swap out those "alert" lines for your own error handling method if you prefer.

Note that this may not immediately free up that memory, however, it should make that memory available for JavaScript garbage collection.

Hope that helps!  :-)

[Note: I haven't tested the above code, but I believe it should work.]

by (390 points)

Thank you very much for your help. If i am reading the code correctly, then is this:

<<run _tracks[_trackID].destroy()>>

the actual part which deletes the track, right? Don't get me wrong. I appreciate the help but i actually only need a command to uncache music as soon as the player gets to the next chapter.

Second: The trackID is the name from the track right? Like:

<<cacheaudio "theme" "music/theme.mp3">>

In this example the trackID would be "theme", or?

...