|
|
@@ -13,6 +13,8 @@
|
|
|
#include <condition_variable>
|
|
|
#include <cstring>
|
|
|
#include <mutex>
|
|
|
+#include <functional>
|
|
|
+#include <map>
|
|
|
#include <set>
|
|
|
|
|
|
#include "core/AudioEdit.h"
|
|
|
@@ -720,7 +722,26 @@ juce::ThreadPool& cacheWriter()
|
|
|
return *writer;
|
|
|
}
|
|
|
|
|
|
-void writeStretchedLater (const juce::File& file, std::shared_ptr<const SampleData> data)
|
|
|
+// Round 190tg: a stretched copy made from a sample whose decoded copy on disk is still waiting for its check after the open
|
|
|
+// (checkPcmLater) is written only once that check passes: a copy stretched from damaged samples must not be kept on disk
|
|
|
+// with a hash that says it is good. A failed check drops it.
|
|
|
+std::mutex& unsettledLock()
|
|
|
+{
|
|
|
+ static std::mutex m;
|
|
|
+ return m;
|
|
|
+}
|
|
|
+std::map<juce::String, int>& unsettledPaths() // path -> checks still running
|
|
|
+{
|
|
|
+ static std::map<juce::String, int> m;
|
|
|
+ return m;
|
|
|
+}
|
|
|
+std::multimap<juce::String, std::function<void()>>& heldWrites()
|
|
|
+{
|
|
|
+ static std::multimap<juce::String, std::function<void()>> m;
|
|
|
+ return m;
|
|
|
+}
|
|
|
+
|
|
|
+void writeStretchedLater (const juce::File& file, std::shared_ptr<const SampleData> data, const juce::String& source = {})
|
|
|
{
|
|
|
if (file == juce::File() || data == nullptr || data->isMapped() || data->left.empty() || (! data->right.empty() && data->right.size() != data->left.size()))
|
|
|
return;
|
|
|
@@ -737,7 +758,16 @@ void writeStretchedLater (const juce::File& file, std::shared_ptr<const SampleDa
|
|
|
const loadtrace::Scope timing ("cache_seal"); // (the hash's own share of the write)
|
|
|
cachecheck::str::makeHeader (header.data(), info, data->left.data(), data->right.empty() ? nullptr : data->right.data());
|
|
|
}
|
|
|
- cacheWriter().addJob ([file, data = std::move (data), header] { writeStretched (file, *data, header); });
|
|
|
+ std::function<void()> write = [file, data = std::move (data), header] { writeStretched (file, *data, header); };
|
|
|
+ {
|
|
|
+ const std::scoped_lock l (unsettledLock());
|
|
|
+ if (source.isNotEmpty() && unsettledPaths().count (source) > 0)
|
|
|
+ {
|
|
|
+ heldWrites().emplace (source, std::move (write));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cacheWriter().addJob (std::move (write));
|
|
|
}
|
|
|
} // namespace
|
|
|
|
|
|
@@ -780,7 +810,7 @@ std::shared_ptr<const SampleData> SampleLibrary::loadStretched (const juce::Stri
|
|
|
}
|
|
|
stretched = std::move (made);
|
|
|
if (stretched != nullptr && onDisk != juce::File())
|
|
|
- writeStretchedLater (onDisk, stretched);
|
|
|
+ writeStretchedLater (onDisk, stretched, path);
|
|
|
}
|
|
|
|
|
|
// the other lengths of this file go when nothing uses them any more (changing the BPM replaces the stretched one).
|
|
|
@@ -937,20 +967,75 @@ juce::ThreadPool& pcmChecker()
|
|
|
// opening (the open's own reads come first). One that fails is logged, marked (<file>.bad) and deleted where the system
|
|
|
// lets it (a mapped file stays on Windows: the marker makes the next load decode again), and the next load() of the
|
|
|
// sample in this session reads the file again.
|
|
|
+SampleLibrary::DamagedHandler& damagedHandler()
|
|
|
+{
|
|
|
+ static SampleLibrary::DamagedHandler h;
|
|
|
+ return h;
|
|
|
+}
|
|
|
+std::mutex& damagedHandlerLock()
|
|
|
+{
|
|
|
+ static std::mutex m;
|
|
|
+ return m;
|
|
|
+}
|
|
|
+
|
|
|
void checkPcmLater (const juce::String& path, std::vector<juce::File> files)
|
|
|
{
|
|
|
++pendingPcmChecks;
|
|
|
+ {
|
|
|
+ const std::scoped_lock l (unsettledLock());
|
|
|
+ ++unsettledPaths()[path];
|
|
|
+ }
|
|
|
pcmChecker().addJob ([path, files = std::move (files)] {
|
|
|
for (int i = 0; i < 300 && loadtrace::active(); ++i) // (at most 30 s)
|
|
|
juce::Thread::sleep (100);
|
|
|
+ juce::File damaged;
|
|
|
+ auto why = cachecheck::Verdict::ok;
|
|
|
for (const auto& f : files)
|
|
|
- if (const auto why = checkPcmContent (f, f.getSize()); why != cachecheck::Verdict::ok)
|
|
|
+ if (const auto v = checkPcmContent (f, f.getSize()); v != cachecheck::Verdict::ok)
|
|
|
{
|
|
|
badMarker (f).create();
|
|
|
- f.deleteFile();
|
|
|
- cachecheck::reportRebuilt (f.getFullPathName().toStdString(), why);
|
|
|
- SampleLibrary::forgetShared (path);
|
|
|
+ f.deleteFile(); // (where the system lets it: Windows keeps a file mapped by the copy that plays now)
|
|
|
+ if (damaged == juce::File())
|
|
|
+ damaged = f, why = v;
|
|
|
+ }
|
|
|
+ // the stretched copies made from it meanwhile: written now, or dropped (they came from the damaged samples)
|
|
|
+ std::vector<std::function<void()>> writes;
|
|
|
+ {
|
|
|
+ const std::scoped_lock l (unsettledLock());
|
|
|
+ if (--unsettledPaths()[path] <= 0)
|
|
|
+ unsettledPaths().erase (path);
|
|
|
+ const auto [from, to] = heldWrites().equal_range (path);
|
|
|
+ for (auto it = from; it != to; ++it)
|
|
|
+ writes.push_back (std::move (it->second));
|
|
|
+ heldWrites().erase (from, to);
|
|
|
+ }
|
|
|
+ if (damaged == juce::File())
|
|
|
+ for (auto& w : writes)
|
|
|
+ cacheWriter().addJob (std::move (w));
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // rebuilt now, here (decoded again into new files: the marker), then swapped in where it plays: the app's
|
|
|
+ // handler reloads it in every channel on the message thread (AudioEngine::reloadSampleEverywhere) and logs
|
|
|
+ // what was done; the new copy is held until then, so the reload finds it decoded
|
|
|
+ SampleLibrary::forgetShared (path);
|
|
|
+ std::shared_ptr<const SampleData> fresh;
|
|
|
+ {
|
|
|
+ SampleLibrary library;
|
|
|
+ fresh = library.load (path);
|
|
|
+ }
|
|
|
+ SampleLibrary::DamagedHandler handler;
|
|
|
+ {
|
|
|
+ const std::scoped_lock l (damagedHandlerLock());
|
|
|
+ handler = damagedHandler();
|
|
|
}
|
|
|
+ const auto file = damaged.getFullPathName().toStdString();
|
|
|
+ if (fresh == nullptr)
|
|
|
+ cachecheck::reportDamaged (file, why, "it could not be decoded again (the source file is gone?); the copy loaded now plays until the project is opened again");
|
|
|
+ else if (handler == nullptr)
|
|
|
+ cachecheck::reportDamaged (file, why, "rebuilt; nothing here can swap it in, it plays from the next load");
|
|
|
+ else
|
|
|
+ handler (path, file, why, std::move (fresh));
|
|
|
+ }
|
|
|
--pendingPcmChecks;
|
|
|
});
|
|
|
}
|
|
|
@@ -1162,6 +1247,12 @@ std::shared_ptr<const SampleData> SampleLibrary::loadStreamed (const juce::File&
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+void SampleLibrary::setDamagedHandler (DamagedHandler handler)
|
|
|
+{
|
|
|
+ const std::scoped_lock l (damagedHandlerLock());
|
|
|
+ damagedHandler() = std::move (handler);
|
|
|
+}
|
|
|
+
|
|
|
void SampleLibrary::setInlineCacheChecks (bool checkInline) noexcept
|
|
|
{
|
|
|
inlinePcmChecks.store (checkInline);
|