thumbnail_cache.h
AudioThumbnailCache: waveforms already computed, kept by source.
#include <sonore/thumbnail_cache.h>- 1type
On this page
Design notes
A sampler holds the thumbnail of the sample it has loaded for as long as it holds the sample, and needs nothing more. A BROWSER does: it draws a waveform for every file the user scrolls past, each of which means decoding the whole file, and scrolling back up means decoding them all again. This keeps what was computed, keyed by the source, so the second look is a lookup.
Keyed by identity, not by name
The key is a 64-bit hash the caller chooses. hashFile() makes one from a file's path, length and modification time, so a file the user edits and saves gets a NEW key and the old waveform is simply never asked for again -- there is no invalidation to get wrong, and the stale entry ages out of the budget like any other. (Windows reports modification times here to the second; a file saved twice within one second at the same length keeps its key. The length changes with nearly every real edit.) hashData() keys audio that never had a file.
Bounded by memory, least recently used out first
The reference bounds its cache by a COUNT of thumbnails. A count says nothing about memory: a thumbnail of a one-shot at 200 buckets and one of an hour-long stem at 20 000 differ a hundredfold. The budget here is bytes -- what the buckets occupy plus a fixed overhead per entry -- and storing past it evicts the least recently used. A lookup counts as a use, because the file the user keeps coming back to is the one worth keeping.
Kept across runs
saveTo() writes the whole cache, most recently used first, and loadFrom() reads it back -- so a browser opens with the waveforms it showed last time. The bytes come from disk, so they are parsed as untrusted: anything malformed is refused whole and the cache is left as it was.
Thread-safe: a loader thread stores while the UI thread looks up. Never on the audio thread -- every call can allocate.
Types 1
Computed AudioThumbnails kept under a 64-bit key, within a budget in bytes (16 MB unless told otherwise), least recently used out first, and savable across runs. Mirrors the reference's class of the same name, bounded by memory rather than by count -- see the file comment.
It stores and hands back COPIES: storeThumb copies in and loadThumb copies out, so nothing returned is tied to the cache's lifetime. Any thread but the audio one, since every call locks and may allocate. The key is the caller's to choose: hashFile() for a file, hashData() for anything else.