| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- // Round 190tg (0.1.33, QUEUE item 85): the disk caches' integrity check (core/CacheCheck.h), through the real
- // SampleLibrary and FL plugin database reader on files made here. For the time-stretched copies (*.str, stereo and
- // mono) and the decoded long samples (*.L.pcm / *.R.pcm): a flipped byte in the samples, a flipped byte in the header,
- // a file cut short and a file of the older format (no hash) are each found, the file is made again and logged, and
- // the audio read back is bit for bit the fresh one. FL's plugin database cache: a flipped byte is found and the database
- // read is the plain one. No window is opened. Included in the app class of Main.cpp; --cachecheck190tgtest <report>
- // and --selftest "cachecheck190tg".
- juce::String cachecheck190tgCheck (juce::String& log)
- {
- using namespace lynx;
- juce::String result = "OK";
- const auto check = [&] (const juce::String& what, bool ok) {
- log << (ok ? "ok " : "FAIL ") << what << "\n";
- if (! ok && result == "OK")
- result = "ERROR " + what;
- };
- const auto dir = SampleLibrary::cacheFolder().getParentDirectory().getChildFile ("selftest-190tg-" + juce::String::toHexString (juce::Random::getSystemRandom().nextInt64()));
- dir.createDirectory();
- const auto writeWav = [&dir] (const juce::String& name, int channels, int frames) {
- const auto wav = dir.getChildFile (name);
- juce::WavAudioFormat format;
- juce::AudioBuffer<float> audio (channels, frames);
- for (int i = 0; i < frames; ++i)
- {
- audio.setSample (0, i, 0.4f * std::sin (0.031f * (float) i) + 0.2f * std::sin (0.0071f * (float) i));
- if (channels > 1)
- audio.setSample (1, i, 0.3f * std::sin (0.017f * (float) i) - 0.25f * std::sin (0.0123f * (float) i));
- }
- std::unique_ptr<juce::OutputStream> out (wav.createOutputStream().release());
- if (auto* w = format.createWriterFor (out.get(), 44100.0, (unsigned int) channels, 16, {}, 0))
- {
- out.release();
- std::unique_ptr<juce::AudioFormatWriter> writer (w);
- writer->writeFromAudioSampleBuffer (audio, 0, frames);
- }
- return wav;
- };
- const auto samplesOf = [] (const SampleData& d) {
- std::vector<float> v (d.leftData(), d.leftData() + d.frames());
- if (d.isStereo())
- v.insert (v.end(), d.rightData(), d.rightData() + d.frames());
- return v;
- };
- const auto flipByte = [] (const juce::File& f, juce::int64 at, std::uint8_t mask) {
- juce::MemoryBlock b;
- if (! f.loadFileAsData (b) || at < 0 || at >= (juce::int64) b.getSize())
- return false;
- static_cast<std::uint8_t*> (b.getData())[at] ^= mask;
- return f.replaceWithData (b.getData(), b.getSize());
- };
- const auto cutShort = [] (const juce::File& f, juce::int64 by) {
- juce::MemoryBlock b;
- return f.loadFileAsData (b) && (juce::int64) b.getSize() > by && f.replaceWithData (b.getData(), b.getSize() - (std::size_t) by);
- };
- const auto cache = SampleLibrary::cacheFolder();
- SampleLibrary library;
- SampleLibrary::setInlineCacheChecks (true); // (the decoded copies checked before load() returns; the background way below)
- // --- the time-stretched copies -------------------------------------------------------------------------------
- const auto strCase = [&] (const juce::String& label, const juce::File& wav, double ratio) {
- const auto path = wav.getFullPathName();
- const auto since = juce::Time::getCurrentTime() - juce::RelativeTime::seconds (2.0);
- std::vector<float> fresh;
- std::size_t frames = 0;
- {
- const auto made = library.loadStretched (path, ratio);
- if (made == nullptr)
- {
- check (label + ": stretched", false);
- return;
- }
- fresh = samplesOf (*made);
- frames = made->frames();
- }
- // its file, once the cache's writer has written it (checked: the hash is right)
- const auto findFile = [&] {
- for (int tries = 0; tries < 600; ++tries)
- {
- for (const auto& f : cache.findChildFiles (juce::File::findFiles, false, "*.str"))
- {
- juce::MemoryBlock b;
- cachecheck::str::Info info;
- if (f.getLastModificationTime() >= since && f.loadFileAsData (b)
- && cachecheck::str::verify (b.getData(), b.getSize(), info) == cachecheck::Verdict::ok && (std::size_t) info.frames == frames
- && std::memcmp (static_cast<const char*> (b.getData()) + cachecheck::str::headerBytes, fresh.data(), frames * sizeof (float)) == 0)
- return f;
- }
- juce::Thread::sleep (50);
- }
- return juce::File();
- };
- auto file = findFile();
- check (label + ": the copy's file written with its hash (version 2)", file != juce::File());
- if (file == juce::File())
- return;
- const auto readBack = [&] (const juce::String& what, cachecheck::Verdict expected) {
- library.forget (path); // (the next load reads the disk)
- const auto before = cachecheck::rebuiltCount();
- const auto read = library.loadStretched (path, ratio);
- const auto logged = cachecheck::rebuiltCount() - before;
- const auto same = read != nullptr && samplesOf (*read) == fresh;
- library.forget (path); // (unmapped: Windows can't replace a mapped file)
- const auto line = juce::String (cachecheck::lastReport());
- check (label + ", " + what + ": found (" + juce::String (logged) + " logged: " + line.fromFirstOccurrenceOf ("(", false, false).upToFirstOccurrenceOf (")", false, false)
- + "), made again, the audio bit for bit the fresh one",
- logged == 1 && same && line.contains (file.getFileName()) && line.contains (cachecheck::describe (expected)));
- // the file written again by the writer, valid
- file = findFile();
- check (label + ", " + what + ": the file written again, valid", file != juce::File());
- };
- // a clean read: nothing logged, the same audio
- {
- library.forget (path);
- const auto before = cachecheck::rebuiltCount();
- const auto read = library.loadStretched (path, ratio);
- check (label + ": read back from the disk, checked, the same audio, nothing logged",
- read != nullptr && samplesOf (*read) == fresh && cachecheck::rebuiltCount() == before);
- library.forget (path);
- }
- const auto payload = (juce::int64) cachecheck::str::headerBytes;
- if (flipByte (file, payload + (file.getSize() - payload) / 3, 0x40)) // (bit 6, as round 190sm's)
- readBack ("a flipped bit in the samples", cachecheck::Verdict::mismatch);
- if (file != juce::File() && flipByte (file, 9, 0x01)) // (the sample rate's low byte)
- readBack ("a flipped bit in the header", cachecheck::Verdict::mismatch);
- if (file != juce::File() && cutShort (file, 1000))
- readBack ("a file cut short", cachecheck::Verdict::truncated);
- if (file != juce::File())
- {
- // the older format: a 32-byte header "LYNXSTR1", no hash (rounds 165i..190tf)
- juce::MemoryBlock b, v1;
- if (file.loadFileAsData (b))
- {
- v1.append (cachecheck::str::magicV1, 8);
- v1.append (static_cast<const char*> (b.getData()) + 8, 24);
- v1.append (static_cast<const char*> (b.getData()) + cachecheck::str::headerBytes, b.getSize() - cachecheck::str::headerBytes);
- if (file.replaceWithData (v1.getData(), v1.getSize()))
- readBack ("an older file without the hash", cachecheck::Verdict::oldFormat);
- }
- }
- };
- strCase ("stretched stereo", writeWav ("stereo 190tg.wav", 2, 44100 * 2), 1.3);
- strCase ("stretched mono", writeWav ("mono 190tg.wav", 1, 44100), 0.8);
- // --- the decoded long samples (over 30 s: mapped from the disk cache) -----------------------------------------
- {
- const auto wav = writeWav ("long 190tg.wav", 2, 44100 * 31);
- const auto path = wav.getFullPathName();
- const auto since = juce::Time::getCurrentTime() - juce::RelativeTime::seconds (2.0);
- std::vector<float> fresh;
- {
- const auto made = library.load (path);
- check ("long sample: mapped from the disk cache, stereo", made != nullptr && made->isMapped() && made->isStereo());
- if (made != nullptr)
- fresh = samplesOf (*made);
- }
- library.forget (path); // (nothing holds it: unmapped, the files can be changed)
- juce::File left, right;
- for (const auto& f : cache.findChildFiles (juce::File::findFiles, false, "*.L.pcm"))
- if (f.getLastModificationTime() >= since && f.getSize() == (juce::int64) cachecheck::pcm::headerBytes + (juce::int64) (fresh.size() / 2) * (juce::int64) sizeof (float))
- left = f, right = f.getSiblingFile (f.getFileName().replace (".L.pcm", ".R.pcm"));
- const auto valid = [] (const juce::File& f, std::size_t frames) {
- juce::MemoryBlock b;
- return f.loadFileAsData (b) && cachecheck::pcm::verify (b.getData(), b.getSize(), 44100.0, (std::int64_t) frames) == cachecheck::Verdict::ok;
- };
- const auto frames = fresh.size() / 2;
- check ("long sample: both channels' files written with their hash", ! fresh.empty() && valid (left, frames) && valid (right, frames));
- const auto readBack = [&] (const juce::String& what, const juce::File& damaged, cachecheck::Verdict expected) {
- const auto before = cachecheck::rebuiltCount();
- std::vector<float> read;
- {
- const auto again = library.load (path);
- if (again != nullptr)
- read = samplesOf (*again);
- }
- library.forget (path);
- const auto logged = cachecheck::rebuiltCount() - before;
- const auto line = juce::String (cachecheck::lastReport());
- check ("long sample, " + what + ": found (" + juce::String (logged) + " logged), decoded again, bit for bit the fresh one, both files valid again",
- logged == 1 && read == fresh && line.contains (damaged.getFileName()) && line.contains (cachecheck::describe (expected))
- && valid (left, frames) && valid (right, frames));
- };
- {
- const auto before = cachecheck::rebuiltCount();
- std::vector<float> read;
- {
- const auto again = library.load (path);
- if (again != nullptr)
- read = samplesOf (*again);
- }
- library.forget (path);
- check ("long sample: mapped again, checked, the same audio, nothing logged", read == fresh && cachecheck::rebuiltCount() == before);
- }
- if (flipByte (left, left.getSize() / 2, 0x40))
- readBack ("a flipped bit in the left channel's samples", left, cachecheck::Verdict::mismatch);
- if (flipByte (right, 12, 0x04)) // (the sample rate)
- readBack ("a flipped bit in the right channel's header", right, cachecheck::Verdict::mismatch);
- if (cutShort (right, 4096))
- readBack ("the right channel's file cut short", right, cachecheck::Verdict::truncated);
- {
- // the older format: "LYNXPCM1", bytes 24..31 zero
- juce::MemoryBlock b;
- if (left.loadFileAsData (b))
- {
- std::memcpy (b.getData(), cachecheck::pcm::magicV1, 8);
- std::memset (static_cast<char*> (b.getData()) + 24, 0, 8);
- if (left.replaceWithData (b.getData(), b.getSize()))
- readBack ("an older left file without the hash", left, cachecheck::Verdict::oldFormat);
- }
- }
- }
- // --- the decoded copies' check after the open (the app's way: load() checks the header and size; the samples are
- // checked on the checker thread; a damaged copy is decoded again and swapped in where it plays) -------------
- {
- SampleLibrary::setInlineCacheChecks (false);
- const auto wav = writeWav ("later 190tg.wav", 2, 44100 * 32);
- const auto path = wav.getFullPathName();
- const auto since = juce::Time::getCurrentTime() - juce::RelativeTime::seconds (2.0);
- std::vector<float> fresh;
- {
- const auto made = library.load (path);
- if (made != nullptr)
- fresh = samplesOf (*made);
- }
- library.forget (path);
- juce::File left;
- for (const auto& f : cache.findChildFiles (juce::File::findFiles, false, "*.L.pcm"))
- if (f.getLastModificationTime() >= since && f.getSize() == (juce::int64) cachecheck::pcm::headerBytes + (juce::int64) (fresh.size() / 2) * (juce::int64) sizeof (float))
- left = f;
- auto before = cachecheck::rebuiltCount();
- std::vector<float> read;
- {
- const auto again = library.load (path);
- if (again != nullptr)
- read = samplesOf (*again);
- }
- const auto idle = SampleLibrary::waitForCacheChecks (60000);
- library.forget (path);
- check ("after the open: a clean copy is mapped again, checked in the background, nothing logged",
- ! fresh.empty() && read == fresh && idle && cachecheck::rebuiltCount() == before);
- // the swap where it plays (the coordinator, 30 Nov): a sampler channel of an engine plays the sample; its decoded
- // copy is damaged; the check after the open finds it, decodes it again, and the message thread swaps the new copy
- // into the channel (AudioEngine::reloadSampleEverywhere), logged "was damaged (...); rebuilt and reloaded"
- {
- AudioEngine engine (false);
- auto song = Song::makeDefault();
- song.channels.edit()[0].instrument = InstrumentKind::sampler;
- song.channels.edit()[0].samplePath = path.toStdString();
- engine.setSong (song);
- SampleLibrary::waitForCacheChecks (60000);
- engine.setSong (Song::makeDefault()); // (nothing holds the copy: the file can be changed, also on Windows)
- library.forget (path);
- const auto flipped = left != juce::File() && flipByte (left, left.getSize() / 2, 0x40);
- before = cachecheck::rebuiltCount();
- engine.setSong (song); // (the open: header and size checked, the samples later)
- auto playing = library.findLoaded (path);
- const auto damagedNow = playing != nullptr && samplesOf (*playing) != fresh;
- const auto done = SampleLibrary::waitForCacheChecks (60000);
- const auto pump = [] (int ms, const std::function<bool()>& until) {
- const auto start = juce::Time::getMillisecondCounter();
- while (juce::Time::getMillisecondCounter() - start < (juce::uint32) ms && ! until())
- {
- #if JUCE_LINUX || JUCE_WINDOWS
- for (int i = 0; i < 256 && juce::detail::dispatchNextMessageOnSystemQueue (true); ++i) {}
- #elif JUCE_MAC
- for (int i = 0; i < 256 && CFRunLoopRunInMode (kCFRunLoopDefaultMode, 0, true) == kCFRunLoopRunHandledSource; ++i) {}
- #endif
- juce::Thread::sleep (5);
- }
- };
- pump (60000, [before] { return cachecheck::rebuiltCount() > before; });
- const auto line = juce::String (cachecheck::lastReport());
- const auto now = library.findLoaded (path);
- check ("after the open: the damaged copy that plays (" + juce::String (damagedNow ? "damaged" : "NOT damaged") + ") is found by the background check: \""
- + line.fromFirstOccurrenceOf ("was damaged", true, false) + "\"",
- flipped && damagedNow && done && cachecheck::rebuiltCount() == before + 1 && line.contains (left.getFileName())
- && line.contains ("was damaged (its content hash does not match") && line.contains ("rebuilt and reloaded (1 channel)"));
- // the damaged copy is let go by the engine (its rack holds the new one): nothing but this check holds it
- std::weak_ptr<const SampleData> old = playing;
- const auto oldPtr = playing.get();
- playing.reset();
- pump (5000, [&old, &engine] { engine.dispatchPendingMessages(); return old.expired(); });
- check ("after the open: the channel now plays the copy decoded again, bit for bit the fresh one; the engine let the damaged one go",
- now != nullptr && now.get() != oldPtr && samplesOf (*now) == fresh && old.expired());
- }
- library.forget (path);
- // the next load (nothing holds the old copy now; on Windows the damaged file goes only now) and its files
- read.clear();
- {
- const auto again = library.load (path);
- if (again != nullptr)
- read = samplesOf (*again);
- }
- SampleLibrary::waitForCacheChecks (60000);
- library.forget (path);
- juce::MemoryBlock b;
- check ("after the open: a later load is bit for bit the fresh one, from a valid file, nothing more logged, no marker left",
- read == fresh && cachecheck::rebuiltCount() == before + 1 && left.loadFileAsData (b)
- && cachecheck::pcm::verify (b.getData(), b.getSize(), 44100.0, (std::int64_t) (fresh.size() / 2)) == cachecheck::Verdict::ok
- && ! left.getSiblingFile (left.getFileName() + ".bad").exists());
- SampleLibrary::setInlineCacheChecks (true);
- }
- // --- FL's plugin database cache (text, its last line the hash) ------------------------------------------------
- {
- FlStudioInstall fl;
- fl.userData = dir.getChildFile ("FL user data");
- const auto nfo = fl.pluginDatabase().getChildFile ("Generators").getChildFile ("VST3").getChildFile ("Alpha190tg.nfo");
- const auto cacheFile = dir.getChildFile ("fl-plugin-database-cache.txt");
- auto ok = nfo.getParentDirectory().createDirectory()
- && nfo.replaceWithText ("ps_name=Alpha190tg\r\nps_file_name_0=C:\\VST3\\Alpha190tg.vst3\r\nps_file_vendorname_0=Vendor\r\nps_file_type_0=2\r\n");
- const auto plain = readFlPluginDatabase (fl);
- readFlPluginDatabase (fl, nullptr, cacheFile);
- juce::MemoryBlock b;
- std::string_view body;
- ok = ok && cacheFile.loadFileAsData (b)
- && cachecheck::unsealText ({ static_cast<const char*> (b.getData()), b.getSize() }, body) == cachecheck::Verdict::ok;
- check ("FL plugin database cache: written with its hash line", ok && plain.entries().size() == 1);
- // a flipped bit in the entry's vendor ("Vendor" -> "Vendgr"): without the check the cache would give it as it is
- const auto text = cacheFile.loadFileAsString();
- const auto at = text.indexOf ("Vendor") + 4;
- const auto before = cachecheck::rebuiltCount();
- const auto read = at > 4 && flipByte (cacheFile, at, 0x08) ? readFlPluginDatabase (fl, nullptr, cacheFile) : FlPluginDb();
- const auto logged = cachecheck::rebuiltCount() - before;
- check ("FL plugin database cache: a flipped bit is found (" + juce::String (logged) + " logged, at " + juce::String (at) + "), the database is the plain read ("
- + juce::String ((int) read.entries().size()) + " entries, vendor \"" + (read.entries().empty() ? juce::String() : juce::String (read.entries()[0].vendor)) + "\"), the cache written again",
- logged == 1 && read.entries().size() == 1 && read.entries()[0].vendor == "Vendor"
- && [&cacheFile] {
- juce::MemoryBlock again; // (loadFileAsData appends to a block: a fresh one)
- std::string_view rest;
- return cacheFile.loadFileAsData (again)
- && cachecheck::unsealText ({ static_cast<const char*> (again.getData()), again.getSize() }, rest) == cachecheck::Verdict::ok;
- }());
- }
- // the log line in the settings folder
- const auto logText = platform::settingsFile ("cache-check.log").loadFileAsString();
- check ("the log (cache-check.log in the settings folder) has the lines", logText.contains ("failed its check") && logText.contains ("and was rebuilt"));
- library.forget (dir.getChildFile ("stereo 190tg.wav").getFullPathName());
- library.forget (dir.getChildFile ("mono 190tg.wav").getFullPathName());
- library.forget (dir.getChildFile ("long 190tg.wav").getFullPathName());
- library.forget (dir.getChildFile ("later 190tg.wav").getFullPathName());
- SampleLibrary::setInlineCacheChecks (false); // (the app's way again)
- dir.deleteRecursively();
- return result;
- }
|