63 appendU16(kFormatVersion);
64 appendU32(processorId);
65 appendU16(processorVersion);
66 countPos_ = blob_.size();
71 void write(
const char* key,
float value)
74 std::memcpy(&bits, &value, 4);
75 writeEntry(key, 0, bits);
79 void write(
const char* key, int32_t value)
81 writeEntry(key, 1,
static_cast<uint32_t
>(value));
85 void write(
const char* key,
bool value)
87 writeEntry(key, 2, value ? 1u : 0u);
91 void write(
const char* key,
const std::vector<uint8_t>& nested)
93 const size_t len = std::strlen(key);
94 assert(len <= 255 &&
"state key longer than 255 chars");
95 assert(count_ < 0xFFFF &&
"state entry count overflows u16");
96 if (len > 255 || count_ == 0xFFFF) { overflow_ =
true;
return; }
97 blob_.push_back(
static_cast<uint8_t
>(len));
100 appendU32(
static_cast<uint32_t
>(nested.size()));
101 blob_.insert(blob_.end(), nested.begin(), nested.end());
112 [[nodiscard]] std::vector<uint8_t>
blob()
const
114 if (overflow_)
return {};
115 std::vector<uint8_t> out = blob_;
116 out[countPos_] =
static_cast<uint8_t
>(count_ & 0xFF);
117 out[countPos_ + 1] =
static_cast<uint8_t
>((count_ >> 8) & 0xFF);
122 static constexpr uint16_t kFormatVersion = 1;
124 void writeEntry(
const char* key, uint8_t type, uint32_t payload)
126 const size_t len = std::strlen(key);
127 assert(len <= 255 &&
"state key longer than 255 chars");
128 assert(count_ < 0xFFFF &&
"state entry count overflows u16");
129 if (len > 255 || count_ == 0xFFFF) { overflow_ =
true;
return; }
130 blob_.push_back(
static_cast<uint8_t
>(len));
132 blob_.push_back(type);
137 void append(
const char* data,
size_t n)
139 blob_.insert(blob_.end(), data, data + n);
141 void appendU16(uint16_t v)
143 blob_.push_back(
static_cast<uint8_t
>(v & 0xFF));
144 blob_.push_back(
static_cast<uint8_t
>(v >> 8));
146 void appendU32(uint32_t v)
148 for (
int i = 0; i < 4; ++i)
149 blob_.push_back(
static_cast<uint8_t
>((v >> (8 * i)) & 0xFF));
152 std::vector<uint8_t> blob_;
153 size_t countPos_ = 0;
155 bool overflow_ =
false;
175 if (data ==
nullptr || size < 14 || std::memcmp(data,
"DSPK", 4) != 0)
177 const uint16_t fmt = readU16(data + 4);
178 if (fmt != 1)
return;
179 processorId_ = readU32(data + 6);
180 processorVersion_ = readU16(data + 10);
181 const uint16_t count = readU16(data + 12);
184 for (uint16_t i = 0; i < count; ++i)
186 if (pos + 1 > size)
return;
187 const size_t keyLen = data[pos++];
188 if (pos + keyLen + 5 > size)
return;
190 e.
key.assign(
reinterpret_cast<const char*
>(data + pos), keyLen);
192 e.
type = data[pos++];
193 e.
payload = readU32(data + pos);
200 if (e.
payload > size - pos)
return;
204 entries_.push_back(std::move(e));
209 [[nodiscard]]
bool isValid() const noexcept {
return valid_; }
210 [[nodiscard]] uint32_t
processorId() const noexcept {
return processorId_; }
214 [[nodiscard]]
float read(
const char* key,
float defaultValue)
const
216 if (
const Entry* e = find(key, 0))
219 std::memcpy(&v, &e->payload, 4);
226 [[nodiscard]] int32_t
read(
const char* key, int32_t defaultValue)
const
228 if (
const Entry* e = find(key, 1))
229 return static_cast<int32_t
>(e->payload);
234 [[nodiscard]]
bool read(
const char* key,
bool defaultValue)
const
236 if (
const Entry* e = find(key, 2))
237 return e->payload != 0;
242 [[nodiscard]] std::vector<uint8_t>
readBlob(
const char* key)
const
244 if (
const Entry* e = find(key, 3))
257 [[nodiscard]]
const std::vector<Entry>&
entries() const noexcept {
return entries_; }
260 [[nodiscard]]
const Entry* find(
const char* key, uint8_t type)
const
262 for (
const auto& e : entries_)
263 if (e.type == type && e.key == key)
268 static uint16_t readU16(
const uint8_t* p)
270 return static_cast<uint16_t
>(p[0] | (p[1] << 8));
272 static uint32_t readU32(
const uint8_t* p)
274 return static_cast<uint32_t
>(p[0]) | (
static_cast<uint32_t
>(p[1]) << 8)
275 | (
static_cast<uint32_t
>(p[2]) << 16) | (
static_cast<uint32_t
>(p[3]) << 24);
279 uint32_t processorId_ = 0;
280 uint16_t processorVersion_ = 0;
281 std::vector<Entry> entries_;
302 for (
const unsigned char c : s)
306 case '"': out +=
"\\\"";
break;
307 case '\\': out +=
"\\\\";
break;
308 case '\b': out +=
"\\b";
break;
309 case '\f': out +=
"\\f";
break;
310 case '\n': out +=
"\\n";
break;
311 case '\r': out +=
"\\r";
break;
312 case '\t': out +=
"\\t";
break;
317 std::snprintf(esc,
sizeof(esc),
"\\u%04x",
static_cast<unsigned>(c));
322 out +=
static_cast<char>(c);
335 if (!std::isfinite(v)) { out +=
'0';
return; }
337 std::snprintf(num,
sizeof(num),
"%.9g",
static_cast<double>(v));
338 for (
char* c = num; *c !=
'\0'; ++c)
339 if (*c ==
',') *c =
'.';
346 bool negative =
false;
347 if (s < end && (*s ==
'-' || *s ==
'+'))
349 negative = (*s ==
'-');
354 while (s < end && *s >=
'0' && *s <=
'9')
356 v = v * 10.0 + (*s -
'0');
360 if (s < end && *s ==
'.')
364 while (s < end && *s >=
'0' && *s <=
'9')
372 if (!any)
return nullptr;
373 if (s < end && (*s ==
'e' || *s ==
'E'))
376 bool expNegative =
false;
377 if (s < end && (*s ==
'-' || *s ==
'+'))
379 expNegative = (*s ==
'-');
384 while (s < end && *s >=
'0' && *s <=
'9')
386 e = e * 10 + (*s -
'0');
389 if (e > 308) { e = 309; }
391 if (!eAny)
return nullptr;
393 for (
int k = 0; k < (e > 309 ? 309 : e); ++k)
395 v = expNegative ? v / scale : v * scale;
397 out = negative ? -v : v;
408 std::string out =
"{\"id\":" + std::to_string(r.
processorId())
412 for (
const auto& e : r.
entries())
414 if (!first) out +=
',';
421 std::memcpy(&v, &e.payload, 4);
424 else if (e.type == 1)
426 out += std::to_string(
static_cast<int32_t
>(e.payload));
428 else if (e.type == 3)
434 out += (e.payload != 0) ?
"true" :
"false";
445 auto skipWs = [&](
size_t& p) {
while (p < json.size() && (json[p] ==
' ' || json[p] ==
'\n'
446 || json[p] ==
'\t' || json[p] ==
'\r')) ++p; };
447 auto expect = [&](
size_t& p,
char c) {
449 if (p >= json.size() || json[p] != c)
return false;
453 auto parseString = [&](
size_t& p, std::string& out) {
455 if (p >= json.size() || json[p] !=
'"')
return false;
458 while (p < json.size() && json[p] !=
'"')
463 if (p >= json.size())
return false;
464 const char e = json[p++];
467 case '"': out +=
'"';
break;
468 case '\\': out +=
'\\';
break;
469 case '/': out +=
'/';
break;
470 case 'b': out +=
'\b';
break;
471 case 'f': out +=
'\f';
break;
472 case 'n': out +=
'\n';
break;
473 case 'r': out +=
'\r';
break;
474 case 't': out +=
'\t';
break;
477 if (p + 4 > json.size())
return false;
479 for (
int k = 0; k < 4; ++k)
481 const char h = json[p++];
483 if (h >=
'0' && h <=
'9') code |= unsigned(h -
'0');
484 else if (h >=
'a' && h <=
'f') code |= unsigned(h -
'a' + 10);
485 else if (h >=
'A' && h <=
'F') code |= unsigned(h -
'A' + 10);
490 out +=
static_cast<char>(code & 0xFF);
493 default:
return false;
501 if (p >= json.size())
return false;
505 auto parseNumber = [&](
size_t& p,
double& out) {
507 const char* start = json.c_str() + p;
508 const char* endOfJson = json.c_str() + json.size();
510 if (stop ==
nullptr)
return false;
511 p =
static_cast<size_t>(stop - json.c_str());
519 uint16_t version = 0;
521 if (!expect(p,
'{'))
return {};
522 struct Param { std::string key; uint8_t type; uint32_t payload;
523 std::vector<uint8_t> bytes; };
524 std::vector<Param> params;
528 if (!parseString(p, key) || !expect(p,
':'))
return {};
529 if (key ==
"id" && parseNumber(p, num))
534 if (!(num >= 0.0 && num <= 4294967295.0))
return {};
535 id =
static_cast<uint32_t
>(num);
537 else if (key ==
"version" && parseNumber(p, num))
539 if (!(num >= 0.0 && num <= 65535.0))
return {};
540 version =
static_cast<uint16_t
>(num);
542 else if (key ==
"params")
544 if (!expect(p,
'{'))
return {};
546 if (p < json.size() && json[p] ==
'}') { ++p; }
552 if (!parseString(p, pk) || !expect(p,
':'))
return {};
554 Param prm { pk, 0, 0, {} };
555 if (p < json.size() && json[p] ==
'{')
558 size_t braceDepth = 0, end = p;
559 for (; end < json.size(); ++end)
561 if (json[end] ==
'{') ++braceDepth;
562 else if (json[end] ==
'}' && --braceDepth == 0) { ++end;
break; }
564 if (braceDepth != 0)
return {};
567 if (prm.bytes.empty())
return {};
570 else if (json.compare(p, 4,
"true") == 0)
572 prm.type = 2; prm.payload = 1; p += 4;
574 else if (json.compare(p, 5,
"false") == 0)
576 prm.type = 2; prm.payload = 0; p += 5;
578 else if (parseNumber(p, num))
587 constexpr double kFloatMax =
static_cast<double>(
588 std::numeric_limits<float>::max());
589 constexpr double kFloatLimit = 3.4028235677973366e38;
590 if (!(std::abs(num) < kFloatLimit))
592 const auto f =
static_cast<float>(std::clamp(num, -kFloatMax, kFloatMax));
593 std::memcpy(&prm.payload, &f, 4);
595 if (num >= -2147483648.0 && num <= 2147483647.0
596 && num ==
static_cast<double>(
static_cast<int32_t
>(num)))
598 params.push_back({ pk, 1,
599 static_cast<uint32_t
>(
static_cast<int32_t
>(num)), {} });
603 params.push_back(std::move(prm));
605 if (p < json.size() && json[p] ==
',') { ++p;
continue; }
608 if (!expect(p,
'}'))
return {};
613 if (p < json.size() && json[p] ==
',') { ++p;
continue; }
616 if (!expect(p,
'}'))
return {};
618 if (p != json.size())
return {};
620 if (params.size() > 0xFFFF)
return {};
621 for (
const auto& prm : params)
622 if (prm.key.size() > 255)
return {};
625 for (
const auto& prm : params)
630 std::memcpy(&v, &prm.payload, 4);
631 w.
write(prm.key.c_str(), v);
633 else if (prm.type == 1)
635 w.
write(prm.key.c_str(),
static_cast<int32_t
>(prm.payload));
637 else if (prm.type == 3)
639 w.
write(prm.key.c_str(), prm.bytes);
643 w.
write(prm.key.c_str(), prm.payload != 0);
652[[nodiscard]]
inline std::string
stateToJson(
const std::vector<uint8_t>& blob)
666[[nodiscard]]
inline std::vector<uint8_t>
stateFromJson(
const std::string& json)
672[[nodiscard]]
constexpr uint32_t
stateId(
const char (&tag)[5])
noexcept
676 return static_cast<uint32_t
>(
static_cast<uint8_t
>(tag[0]))
677 | (
static_cast<uint32_t
>(
static_cast<uint8_t
>(tag[1])) << 8)
678 | (
static_cast<uint32_t
>(
static_cast<uint8_t
>(tag[2])) << 16)
679 | (
static_cast<uint32_t
>(
static_cast<uint8_t
>(tag[3])) << 24);
Tolerant reader: missing keys yield defaults, unknown keys are skipped.
std::vector< uint8_t > readBlob(const char *key) const
Reads a nested blob; empty when the key is absent.
StateReader(const uint8_t *data, size_t size)
int32_t read(const char *key, int32_t defaultValue) const
Reads an int32, or defaultValue when the key is absent.
uint16_t processorVersion() const noexcept
float read(const char *key, float defaultValue) const
Reads a float, or defaultValue when the key is absent.
bool isValid() const noexcept
uint32_t processorId() const noexcept
const std::vector< Entry > & entries() const noexcept
bool read(const char *key, bool defaultValue) const
Reads a bool, or defaultValue when the key is absent.
Serializes key/value parameters into a versioned blob.
void write(const char *key, const std::vector< uint8_t > &nested)
Writes a nested blob (composite processors: bands, slots...).
StateWriter(uint32_t processorId, uint16_t processorVersion)
std::vector< uint8_t > blob() const
Finalizes and returns the blob.
void write(const char *key, float value)
Writes a float parameter.
void write(const char *key, bool value)
Writes a boolean parameter.
void write(const char *key, int32_t value)
Writes an integer parameter (enums, counts, modes).
const char * parseStateJsonNumber(const char *s, const char *end, double &out) noexcept
Locale-independent number parse over [s, end); nullptr on failure.
std::string stateToJsonImpl(const std::vector< uint8_t > &blob, int depth)
std::vector< uint8_t > stateFromJsonImpl(const std::string &json, int depth)
void appendStateJsonNumber(std::string &out, float v)
Locale-independent float rendering ("%.9g" with a forced dot). Non-finite payloads (NaN/Inf – e....
constexpr int kMaxStateJsonDepth
void appendStateJsonString(std::string &out, const std::string &s)
Appends a JSON string literal, escaping the characters JSON forbids. Keys follow the [A-Za-z0-9....
Main namespace for the DSPark framework.
std::string stateToJson(const std::vector< uint8_t > &blob)
Renders a state blob as a flat JSON object string.
constexpr uint32_t stateId(const char(&tag)[5]) noexcept
Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
std::vector< uint8_t > stateFromJson(const std::string &json)
Parses the JSON produced by stateToJson() back into a blob.
Access for generic tooling (JSON rendering, preset browsers).
std::vector< uint8_t > bytes
Nested blob content (type 3).