| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #pragma once
- #include <map>
- #include <functional>
- #include <memory>
- #include <mutex>
- #include <juce_audio_formats/juce_audio_formats.h>
- #include "core/CacheCheck.h"
- #include "core/Instruments.h"
- #include "core/TimeStretch.h"
- namespace lynx
- {
- // Decodes audio files into SampleData, cached by path (one cache for the whole process).
- // Any thread except the audio thread.
- class SampleLibrary
- {
- public:
- SampleLibrary();
- std::shared_ptr<const SampleData> load (const juce::String& path);
- // The sample when something already holds it loaded (nullptr otherwise): never reads the file (round 149: the
- // stream prefetcher looked its clips up with load(), which decoded again every audio clip whose channel plays a
- // time-stretched copy, seconds on every edit of a song with many of them)
- std::shared_ptr<const SampleData> findLoaded (const juce::String& path);
- // The file changed on disk (the audio editor saved over it): the next load() reads it again.
- void forget (const juce::String& path);
- // the same for the process's one cache, without a library (round 190tg: a cache file that failed its check)
- static void forgetShared (const juce::String& path);
- // The sample time-stretched without changing pitch (core/TimeStretch), cached by (path, ratio, method).
- // `map` (round 175a, warp markers): stretched along that warp map instead of at one ratio (its own cache entries).
- std::shared_ptr<const SampleData> loadStretched (const juce::String& path, double ratio, StretchMethod method = StretchMethod::general,
- double semitones = 0.0, // semitones: Beats only (core/TimeStretch)
- const StretchMap& map = {}, const StretchOptions& options = {});
- // A channel's precomputed span (its region after its precomputed effects and Trim, core/SamplePrecompute.h) stretched
- // by `ratio` (core/TimeStretch timeStretchSpan), cached in memory by (path, variantKey, ratio, method): FL stretches
- // that span to TIME, not the whole file (round 180d). Dropped with the file's other unused lengths.
- std::shared_ptr<const SampleData> loadStretchedSpan (const juce::String& path, const std::string& variantKey,
- const std::shared_ptr<const SampleData>& span, double ratio,
- StretchMethod method = StretchMethod::general, double semitones = 0.0,
- const StretchOptions& options = {});
- // Above this, audio goes to a memory-mapped disk cache instead of staying entirely
- // in RAM ("disk streaming"). The cache is reused across sessions.
- static constexpr double streamAboveSeconds = 30.0;
- static juce::File cacheFolder();
- // The decoded copies (*.pcm) above `limitBytes`: the least recently used are deleted down to 3/4 of it (round 167e;
- // done once per session in the background). Returns the bytes left.
- static juce::int64 prunePcmCache (juce::int64 limitBytes);
- // Round 190tg (the caches' integrity check, core/CacheCheck.h): a decoded copy (*.pcm) found again on disk is checked in
- // full in the background once the project has opened (reading it whole at once cost seconds on a hard disk when it
- // was not in the system's memory any more; the open only checks its header and size). A copy that fails is logged and
- // marked, and the next load decodes the file again. `inline` true: checked in full before load() returns (--export,
- // tests: a render that must be exact). The stretched copies (*.str) are always checked before they are used.
- static void setInlineCacheChecks (bool checkInline) noexcept;
- // Waits (at most `timeoutMs`) for the background checks; true when none is left
- static bool waitForCacheChecks (int timeoutMs);
- // What the app does with a sample whose decoded copy failed the background check, once it is decoded again (`fresh`,
- // held until the call returns): AudioEngine swaps it in where it plays and logs it. Called on the checker thread.
- using DamagedHandler = std::function<void (const juce::String& path, const std::string& cacheFile, cachecheck::Verdict why,
- std::shared_ptr<const SampleData> fresh)>;
- static void setDamagedHandler (DamagedHandler handler);
- // What the decoded samples of the process hold now (round 190gd: the load log says where a project's megabytes go):
- // samples in RAM (float, both channels) and mapped from the disk cache, and the time-stretched copies kept
- struct MemoryUse
- {
- int samples = 0, mappedSamples = 0, stretched = 0;
- std::int64_t sampleBytes = 0, mappedBytes = 0, stretchedBytes = 0;
- // round 190gn (A2): the stretched copies mapped from the disk cache (locked in RAM: not the process's memory)
- int stretchedMapped = 0;
- std::int64_t stretchedMappedBytes = 0;
- };
- static MemoryUse memoryUse();
- // the bytes of the mapped stretched copies that are resident and locked right now (-1: can't tell); the load log
- // shows it next to stretchedMappedBytes: equal = no page of them can fault on the audio thread
- static std::int64_t lockedStretchedBytes();
- // Filter for FileChooser ("*.wav;*.flac;...")
- juce::String wildcard() const;
- // The audio files LYNX loads as samples (core/SampleFiles.h: wav, aif(f), flac, ogg, mp3 and FL's WavPack .wv).
- // Every drop target (Channel Rack, Playlist, Audio editor...) asks this, so they all take what the Browser lists.
- static bool isSampleFile (const juce::File& file);
- // The file's first `seconds` mixed to mono, read straight from the file: nothing is cached or kept (round 190as:
- // the Browser's "Find similar samples" analyses whole folders with it). False when the file can't be read.
- bool readMono (const juce::File& file, double seconds, std::vector<float>& mono, double& sampleRate);
- // Round 190kx (loop analysis, core/LoopAnalysis.h): readMono into `out` (mono, its rate, the WAV's ACID beats as a
- // load reads them) and the whole file's length in seconds (`totalSeconds`, also when only the first `seconds` are
- // read). Nothing is cached or kept. False when the file can't be read.
- bool readForAnalysis (const juce::File& file, double seconds, SampleData& out, double& totalSeconds);
- // Issue #193 (round als190ka): a video file's sound track (a Live set's video clips play their sound as audio
- // clips). The engine cannot decode video itself; the app hands it the system's decoder (ui/VideoMedia), which writes
- // the sound to a WAV in the cache and returns it (a non-existent file: no sound / no decoder). Set once at start-up.
- using VideoSoundDecoder = juce::File (*) (const juce::File& video);
- static void setVideoSoundDecoder (VideoSoundDecoder decoder) noexcept;
- private:
- std::unique_ptr<juce::AudioFormatReader> createReader (const juce::File& file);
- std::shared_ptr<const SampleData> loadStreamed (const juce::File& file, juce::AudioFormatReader& reader);
- std::shared_ptr<const SampleData> decode (const juce::String& path);
- juce::AudioFormatManager formats;
- };
- // Brings in from disk the range [from, to) (seconds) of a mapped sample. Any thread except the audio thread.
- void prefetchSample (const SampleData& data, double fromSeconds, double toSeconds);
- } // namespace lynx
|