Skip to content

Commit

Permalink
Upgrade taojson and start streaming optimization (#1126)
Browse files Browse the repository at this point in the history
1. Upgrade libs/taocppjson to latest
2. Start some streaming optimizations by making bus effects a bit
smarter at none and macro way smarter, cutting a blank engine json
stream down by about 2x. Addresses #1069 but still more to do.
  • Loading branch information
baconpaul authored Aug 16, 2024
1 parent 4e44cc7 commit 7850be6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 55 deletions.
16 changes: 9 additions & 7 deletions src/engine/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ struct Macro
{
float value{0.f}; // -1 .. 1

int16_t part{-1}, index{-1};

bool isBipolar{false}; // The value is expected in range -1..1
bool isStepped{false}; // The value has discrete stepped value
size_t stepCount{1}; // how many steps between min and max.
Expand Down Expand Up @@ -100,17 +102,17 @@ struct Macro
}

static std::string defaultNameFor(int index) { return "Macro " + std::to_string(index + 1); }
std::string pluginParameterNameFor(int part, int index) const
std::string pluginParameterNameFor(int p, int i) const
{
auto dn = defaultNameFor(index);
auto dn = defaultNameFor(i);
if (name == dn)
{
return std::string("P" + std::to_string(part + 1) + " " + name);
return std::string("P" + std::to_string(p + 1) + " " + name);
}
else
{
return std::string("P" + std::to_string(part + 1) + " M" + std::to_string(index + 1) +
" - " + name);
return std::string("P" + std::to_string(p + 1) + " M" + std::to_string(i + 1) + " - " +
name);
}
}

Expand All @@ -129,9 +131,9 @@ struct Macro
}
static int macroIDToPart(uint32_t id) { return (id - firstMacroParam) / macroParamPartStride; }
static int macroIDToIndex(uint32_t id) { return (id - firstMacroParam) % macroParamPartStride; }
static uint32_t partIndexToMacroID(uint32_t part, uint32_t index)
static uint32_t partIndexToMacroID(uint32_t pt, uint32_t id)
{
return firstMacroParam + part * macroParamPartStride + index;
return firstMacroParam + pt * macroParamPartStride + id;
}
};
} // namespace scxt::engine
Expand Down
2 changes: 2 additions & 0 deletions src/engine/part.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct Part : MoveableOnly<Part>, SampleRateSupport
int idx{0};
for (auto &m : macros)
{
m.index = idx;
m.part = partNumber;
m.name = Macro::defaultNameFor(idx);
idx++;
}
Expand Down
95 changes: 48 additions & 47 deletions src/json/engine_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,25 @@ SC_STREAMDEF(scxt::engine::Patch, SC_FROM({
}))

SC_STREAMDEF(scxt::engine::Macro, SC_FROM({
v = {
{"v", t.value}, {"bp", t.isBipolar}, {"st", t.isStepped},
{"sc", t.stepCount}, {"nm", t.name},
};
v = {{"p", t.part}, {"i", t.index}, {"v", t.value}};

if (t.isBipolar)
SC_INSERT(v, "bp", t.isBipolar);
if (t.isStepped)
SC_INSERT(v, "st", t.isStepped);
if (t.stepCount != 1)
SC_INSERT(v, "sc", t.stepCount);
if (t.name != scxt::engine::Macro::defaultNameFor(t.index))
SC_INSERT(v, "nm", t.name);
}),
SC_TO({
findIf(v, "v", result.value);
findIf(v, "bp", result.isBipolar);
findIf(v, "st", result.isStepped);
findIf(v, "sc", result.stepCount);
findIf(v, "nm", result.name);
findIf(v, "i", result.index);
findIf(v, "p", result.part);
findOrSet(v, "bp", false, result.isBipolar);
findOrSet(v, "st", false, result.isStepped);
findOrSet(v, "sc", 1, result.stepCount);
findOrSet(v, "nm", scxt::engine::Macro::defaultNameFor(result.index), result.name);
}));

SC_STREAMDEF(
Expand Down Expand Up @@ -509,45 +517,38 @@ template <> struct scxt_traits<engine::Bus::BusSendStorage>
}
};

template <> struct scxt_traits<engine::BusEffectStorage>
{
template <template <typename...> class Traits>
static void assign(tao::json::basic_value<Traits> &v, const engine::BusEffectStorage &t)
{
if (t.type == scxt::engine::AvailableBusEffects::none)
{
v = {{"type", scxt::engine::toStringAvailableBusEffects(t.type)}};
}
else
{
v = {{"type", scxt::engine::toStringAvailableBusEffects(t.type)},
{"isActive", t.isActive},
{"params", t.params},
{"deact", t.deact},
{"temposync", t.isTemposync}};
}
}

template <template <typename...> class Traits>
static void to(const tao::json::basic_value<Traits> &v, engine::BusEffectStorage &r)
{
std::string tp;
findIf(v, "type", tp);
r.type = scxt::engine::fromStringAvailableBusEffects(tp);
if (r.type == engine::AvailableBusEffects::none)
{
r = engine::BusEffectStorage();
r.type = engine::AvailableBusEffects::none;
}
else
{
findIf(v, "params", r.params);
findOrSet(v, "isActive", true, r.isActive);
findIf(v, "deact", r.deact);
findIf(v, "temposync", r.isTemposync);
}
}
};
SC_STREAMDEF(engine::BusEffectStorage, SC_FROM({
if (from.type == scxt::engine::AvailableBusEffects::none)
{
v = tao::json::empty_object;
}
else
{
v = {{"t", scxt::engine::toStringAvailableBusEffects(from.type)},
{"isActive", from.isActive},
{"p", from.params},
{"deact", from.deact},
{"temposync", from.isTemposync}};
}
}),
SC_TO({
std::string tp{
scxt::engine::toStringAvailableBusEffects(engine::AvailableBusEffects::none)};
findIf(v, {"t", "type"}, tp);
to.type = scxt::engine::fromStringAvailableBusEffects(tp);
if (to.type == engine::AvailableBusEffects::none)
{
to = engine::BusEffectStorage();
to.type = engine::AvailableBusEffects::none;
}
else
{
findIf(v, {"p", "params"}, to.params);
findOrSet(v, "isActive", true, to.isActive);
findIf(v, "deact", to.deact);
findIf(v, "temposync", to.isTemposync);
}
}));

template <> struct scxt_traits<engine::Patch::Busses>
{
Expand Down
24 changes: 24 additions & 0 deletions src/json/scxt_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#ifndef SCXT_SRC_JSON_SCXT_TRAITS_H
#define SCXT_SRC_JSON_SCXT_TRAITS_H

#include <initializer_list>
#include "tao/json/traits.hpp"

namespace scxt::json
Expand All @@ -49,6 +50,21 @@ template <typename V, typename R> bool findIf(V &v, const std::string &key, R &r
return false;
}

template <typename V, typename R>
bool findIf(V &v, const std::initializer_list<std::string> &keys, R &r)
{
for (const auto &key : keys)
{
auto vs = v.find(key);
if (vs)
{
vs->to(r);
return true;
}
}
return false;
}

template <typename V, typename R> bool findEnumIf(V &v, const std::string &key, R &r)
{
auto vs = v.find(key);
Expand Down Expand Up @@ -76,6 +92,13 @@ void findOrSet(V &v, const std::string &key, const D &d, R &r)
r = d;
}

template <typename V, typename D, typename R>
void findOrSet(V &v, const std::initializer_list<std::string> &keys, const D &d, R &r)
{
if (!findIf(v, keys, r))
r = d;
}

template <typename V, typename R> void findOrDefault(V &v, const std::string &key, R &r)
{
findOrSet(v, key, R{}, r);
Expand Down Expand Up @@ -105,6 +128,7 @@ template <typename V, typename R> void findOrDefault(V &v, const std::string &ke
} \
};

#define SC_INSERT(x, a, b) x.insert({{a, tao::json::basic_value<Traits>(b)}});
#define SC_FROM(...) __VA_ARGS__
#define SC_TO(...) __VA_ARGS__
#define SC_STREAMDEF(type, assignBlock, toBlock) \
Expand Down

0 comments on commit 7850be6

Please sign in to comment.