Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SampleID to MD5Sum #1372

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/engine/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ void Zone::setupOnUnstream(const engine::Engine &e)
auto nbSampleLoaded{getNumSampleLoaded()};
for (auto i = 0; i < nbSampleLoaded; ++i)
{
auto oid = variantData.variants[i].sampleID;
auto nid = e.getSampleManager()->resolveAlias(oid);

if (nid != oid)
{
SCLOG("Relabeling zone on change : " << oid.to_string() << " -> " << nid.to_string());
variantData.variants[i].sampleID = nid;
}

attachToSample(*(e.getSampleManager()), i, Zone::NONE);
}
for (int p = 0; p < processorCount; ++p)
Expand Down
2 changes: 1 addition & 1 deletion src/json/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void unstreamEngineState(engine::Engine &e, const std::string &data, bool msgPac

void unstreamPartState(engine::Engine &e, int part, const std::string &data, bool msgPack)
{
e.clearAll();
e.getPatch()->getPart(part)->clearGroups();
if (msgPack)
{
tao::json::events::transformer<tao::json::events::to_basic_value<scxt_traits>> consumer;
Expand Down
25 changes: 25 additions & 0 deletions src/json/utils_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,30 @@ template <int idType> struct scxt_traits<ID<idType>>
}
}
};

SC_STREAMDEF(scxt::SampleID, SC_FROM(v = {{"m", from.md5}, {"a", from.multiAddress}});
, SC_TO(
auto vs = v.find("m"); if (vs) {
std::string m5;
vs->to(m5);
strncpy(to.md5, m5.c_str(), scxt::SampleID::md5len + 1);
findIf(v, "a", to.multiAddress);
} else {
std::string legType{"t"}, legID{"i"};
if (SC_UNSTREAMING_FROM_PRIOR_TO(0x2024'08'06))
{
legType = "idType";
legID = "id";
}

if (6 != v.at(legType).template as<int32_t>())
{
SCLOG("Unstreamed a non-6 type from legacy");
}
int32_t id;
v.at(legID).to(id);
to.setAsLegacy(id);
}));

} // namespace scxt::json
#endif // SHORTCIRCUIT_UTILS_TRAITS_H
8 changes: 3 additions & 5 deletions src/patch_io/patch_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ bool savePart(const fs::path &p, const scxt::engine::Engine &e, int part)
try
{
auto sg = scxt::engine::Engine::StreamGuard(engine::Engine::FOR_PART);
// auto msg =
// tao::json::msgpack::to_string(json::scxt_value(*(e.getPatch()->getPart(part))));
auto msg = tao::json::to_string(json::scxt_value(*(e.getPatch()->getPart(part))));
auto msg = tao::json::msgpack::to_string(json::scxt_value(*(e.getPatch()->getPart(part))));

auto f = std::make_unique<RIFF::File>('SCXT');
f->SetByteOrder(RIFF::endian_little);
Expand Down Expand Up @@ -261,7 +259,7 @@ bool loadPartInto(const fs::path &p, scxt::engine::Engine &engine, int part)
try
{
nonconste.stopAllSounds();
scxt::json::unstreamPartState(nonconste, part, payload, false);
scxt::json::unstreamPartState(nonconste, part, payload, true);
auto &cont = *e.getMessageController();
cont.restartAudioThreadFromSerial();
}
Expand All @@ -276,7 +274,7 @@ bool loadPartInto(const fs::path &p, scxt::engine::Engine &engine, int part)
try
{
engine.stopAllSounds();
scxt::json::unstreamPartState(engine, part, payload, false);
scxt::json::unstreamPartState(engine, part, payload, true);
}
catch (std::exception &err)
{
Expand Down
2 changes: 1 addition & 1 deletion src/sample/multisample_support/multisample_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool importMultisample(const fs::path &p, engine::Engine &engine)
&ssize, 0);

auto lsid = engine.getSampleManager()->setupSampleFromMultifile(
p, fileToIndex[fc->Attribute("file")], data, ssize);
p, md5, fileToIndex[fc->Attribute("file")], data, ssize);
free(data);

if (!lsid.has_value())
Expand Down
12 changes: 12 additions & 0 deletions src/sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ bool Sample::load(const fs::path &path)
mFileName = path;
displayName = fmt::format("{}", path.filename().u8string());
type = WAV_FILE;

id.setAsMD5(md5Sum);

return true;
}
else if (extensionMatches(path, ".flac"))
Expand All @@ -80,6 +83,9 @@ bool Sample::load(const fs::path &path)
type = FLAC_FILE;
mFileName = path;
displayName = fmt::format("{}", path.filename().u8string());

id.setAsMD5(md5Sum);

return true;
}
}
Expand All @@ -91,6 +97,9 @@ bool Sample::load(const fs::path &path)
type = MP3_FILE;
mFileName = path;
displayName = fmt::format("{}", path.filename().u8string());

id.setAsMD5(md5Sum);

return true;
}
}
Expand All @@ -108,6 +117,9 @@ bool Sample::load(const fs::path &path)
sample_loaded = true;
mFileName = path;
displayName = fmt::format("{}", path.filename().u8string());

id.setAsMD5(md5Sum);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sample/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct alignas(16) Sample : MoveableOnly<Sample>
MULTISAMPLE_FILE,
} type{WAV_FILE};

Sample() : id(SampleID::next()) {}
Sample() {}
Sample(const SampleID &sid) : displayName(sid.to_string()), id(sid) {}
virtual ~Sample();

Expand Down
79 changes: 39 additions & 40 deletions src/sample/sample_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,66 +42,58 @@ void SampleManager::restoreFromSampleAddressesAndIDs(const sampleAddressesAndIds
}
else
{
std::optional<SampleID> nid;
switch (addr.type)
{
case Sample::WAV_FILE:
case Sample::FLAC_FILE:
case Sample::MP3_FILE:
case Sample::AIFF_FILE:
{
loadSampleByPathToID(addr.path, id);
nid = loadSampleByPath(addr.path);
}
break;
case Sample::SF2_FILE:
{
loadSampleFromSF2ToID(addr.path, nullptr, addr.preset, addr.instrument, addr.region,
id);
nid = loadSampleFromSF2(addr.path, nullptr, addr.preset, addr.instrument,
addr.region);
}
break;
case Sample::MULTISAMPLE_FILE:
{
loadSampleFromMultiSample(addr.path, addr.region, id);
nid = loadSampleFromMultiSample(addr.path, addr.region, id);
}
break;
}

if (nid.has_value())
{
if (*nid != id)
{
SCLOG("Adding Alias ID [" << id.to_string() << "] -> [" << nid->to_string()
<< "]");
idAliases[id] = *nid;
}
}
}
}
}

SampleManager::~SampleManager() { SCLOG("Destroying Sample Manager"); }

std::optional<SampleID> SampleManager::loadSampleByPath(const fs::path &p)
{
for (const auto &[alreadyId, sm] : samples)
{
if (sm->getPath() == p)
{
return alreadyId;
}
}

return loadSampleByPathToID(p, SampleID::next());
}

std::optional<SampleID> SampleManager::loadSampleByPathToID(const fs::path &p, const SampleID &id)
{
assert(threadingChecker.isSerialThread());
SampleID::guaranteeNextAbove(id);

for (const auto &[alreadyId, sm] : samples)
{
if (sm->getPath() == p)
{
SCLOG("Potential concern: Asked to load '"
<< p.u8string() << "' into " << id.to_string() << " but it already exists at "
<< alreadyId.to_string());
return alreadyId;
}
}

SCLOG("Loading [" << p.u8string() << "] @ [" << id.to_string() << "]");

auto sp = std::make_shared<Sample>(id);
auto sp = std::make_shared<Sample>();

if (!sp->load(p))
{
Expand All @@ -110,19 +102,15 @@ std::optional<SampleID> SampleManager::loadSampleByPathToID(const fs::path &p, c
}

samples[sp->id] = sp;
SCLOG("Loading : " << p.u8string());
SCLOG(" : " << sp->id.to_string());

updateSampleMemory();
return sp->id;
}

std::optional<SampleID> SampleManager::loadSampleFromSF2(const fs::path &p, sf2::File *f,
int preset, int instrument, int region)
{
return loadSampleFromSF2ToID(p, f, preset, instrument, region, SampleID::next());
}

std::optional<SampleID> SampleManager::loadSampleFromSF2ToID(const fs::path &p, sf2::File *f,
int preset, int instrument, int region,
const SampleID &sid)
{
if (!f)
{
Expand All @@ -134,8 +122,7 @@ std::optional<SampleID> SampleManager::loadSampleFromSF2ToID(const fs::path &p,

auto riff = std::make_unique<RIFF::File>(p.u8string());
auto sf = std::make_unique<sf2::File>(riff.get());
sf2FilesByPath[p.u8string()] = {std::move(riff), std::move(sf),
infrastructure::createMD5SumFromFile(p)};
sf2FilesByPath[p.u8string()] = {std::move(riff), std::move(sf)};
}
catch (RIFF::Exception e)
{
Expand All @@ -145,6 +132,9 @@ std::optional<SampleID> SampleManager::loadSampleFromSF2ToID(const fs::path &p,
f = std::get<1>(sf2FilesByPath[p.u8string()]).get();
}

if (sf2MD5ByPath.find(p.u8string()) == sf2MD5ByPath.end())
sf2MD5ByPath[p.u8string()] = infrastructure::createMD5SumFromFile(p);

assert(f);
for (const auto &[id, sm] : samples)
{
Expand All @@ -156,23 +146,29 @@ std::optional<SampleID> SampleManager::loadSampleFromSF2ToID(const fs::path &p,
}
}

auto sp = std::make_shared<Sample>(sid);
auto sp = std::make_shared<Sample>();

if (!sp->loadFromSF2(p, f, preset, instrument, region))
return {};

sp->md5Sum = std::get<2>(sf2FilesByPath[p.u8string()]);
sp->md5Sum = sf2MD5ByPath[p.u8string()];
assert(!sp->md5Sum.empty());
sp->id.setAsMD5WithAddress(sp->md5Sum, preset, instrument, region);

SCLOG("Loading : " << p.u8string());
SCLOG(" : " << sp->id.to_string());

samples[sp->id] = sp;
updateSampleMemory();
return sp->id;
}

std::optional<SampleID> SampleManager::setupSampleFromMultifile(const fs::path &p, int idx,
std::optional<SampleID> SampleManager::setupSampleFromMultifile(const fs::path &p,
const std::string &md5, int idx,
void *data, size_t dataSize)
{
auto sid = SampleID::next();
auto sp = std::make_shared<Sample>(sid);
auto sp = std::make_shared<Sample>();
sp->id.setAsMD5WithAddress(md5, idx, -1, -1);

sp->parse_riff_wave(data, dataSize);
sp->type = Sample::MULTISAMPLE_FILE;
Expand Down Expand Up @@ -208,6 +204,9 @@ std::optional<SampleID> SampleManager::loadSampleFromMultiSample(const fs::path

free(data);

SCLOG("Loading : " << p.u8string());
SCLOG(" : " << sp->id.to_string());

return sp->id;
}

Expand All @@ -220,8 +219,8 @@ void SampleManager::purgeUnreferencedSamples()
auto ct = b->second.use_count();
if (ct <= 1)
{
SCLOG("Purging sample " << b->first.to_string() << " from "
<< b->second->mFileName.u8string())
SCLOG("Purging : " << b->second->mFileName.u8string());
SCLOG(" : " << b->first.to_string());
b = samples.erase(b);
}
else
Expand Down
31 changes: 22 additions & 9 deletions src/sample/sample_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,13 @@ struct SampleManager : MoveableOnly<SampleManager>
const SampleID &);

std::optional<SampleID> loadSampleByPath(const fs::path &);
std::optional<SampleID> loadSampleByPathToID(const fs::path &, const SampleID &id);

std::optional<SampleID> loadSampleFromSF2(const fs::path &,
sf2::File *f, // if this is null I will re-open it
int preset, int instrument, int region);
std::optional<SampleID> loadSampleFromSF2ToID(const fs::path &,
sf2::File *f, // if this is null I will re-open it
int preset, int instrument, int region,
const SampleID &id);

std::optional<SampleID> setupSampleFromMultifile(const fs::path &, int idx, void *data,
size_t dataSize);
std::optional<SampleID> setupSampleFromMultifile(const fs::path &, const std::string &md5,
int idx, void *data, size_t dataSize);
std::optional<SampleID> loadSampleFromMultiSample(const fs::path &, int idx,
const SampleID &id);

Expand All @@ -101,6 +96,12 @@ struct SampleManager : MoveableOnly<SampleManager>
auto p = samples.find(id);
if (p != samples.end())
return p->second;

auto alias = idAliases.find(id);
if (alias != idAliases.end())
{
return getSample(alias->second);
}
return {};
}

Expand Down Expand Up @@ -136,13 +137,25 @@ struct SampleManager : MoveableOnly<SampleManager>

std::atomic<uint64_t> sampleMemoryInBytes{0};

void addIdAlias(const SampleID &from, const SampleID &to) { idAliases[from] = to; }

SampleID resolveAlias(const SampleID &a)
{
auto ap = idAliases.find(a);
if (ap == idAliases.end())
return a;
return resolveAlias(ap->second);
}

private:
void updateSampleMemory();

std::unordered_map<SampleID, SampleID> idAliases;
std::unordered_map<SampleID, std::shared_ptr<Sample>> samples;

std::unordered_map<std::string, std::tuple<std::unique_ptr<RIFF::File>,
std::unique_ptr<sf2::File>, std::string>>
std::unique_ptr<sf2::File>>>
sf2FilesByPath; // last is the md5sum
std::unordered_map<std::string, std::string> sf2MD5ByPath;

std::unordered_map<std::string, std::unique_ptr<ZipArchiveHolder>> zipArchives;
};
Expand Down
Loading
Loading