DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
StateBlob.h File Reference

Versioned key-value state serialization for processor presets. More...

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
Include dependency graph for StateBlob.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  dspark::StateWriter
 Serializes key/value parameters into a versioned blob. More...
 
class  dspark::StateReader
 Tolerant reader: missing keys yield defaults, unknown keys are skipped. More...
 
struct  dspark::StateReader::Entry
 Access for generic tooling (JSON rendering, preset browsers). More...
 

Namespaces

namespace  dspark
 Main namespace for the DSPark framework.
 
namespace  dspark::detail
 

Functions

void dspark::detail::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._] convention (so this normally emits verbatim), but the convention is not enforced on the writer or reader, and untrusted blobs can carry arbitrary key bytes; escaping keeps the emitted JSON valid and non-injectable in every case.
 
void dspark::detail::appendStateJsonNumber (std::string &out, float v)
 Locale-independent float rendering ("%.9g" with a forced dot). Non-finite payloads (NaN/Inf – e.g. a corrupt blob or a bugged parameter) are emitted as JSON-legal 0 so the output always parses and round-trips.
 
const char * dspark::detail::parseStateJsonNumber (const char *s, const char *end, double &out) noexcept
 Locale-independent number parse over [s, end); nullptr on failure.
 
std::string dspark::detail::stateToJsonImpl (const std::vector< uint8_t > &blob, int depth)
 
std::vector< uint8_t > dspark::detail::stateFromJsonImpl (const std::string &json, int depth)
 
std::string dspark::stateToJson (const std::vector< uint8_t > &blob)
 Renders a state blob as a flat JSON object string.
 
std::vector< uint8_t > dspark::stateFromJson (const std::string &json)
 Parses the JSON produced by stateToJson() back into a blob.
 
constexpr uint32_t dspark::stateId (const char(&tag)[5]) noexcept
 Builds a FOURCC processor id, e.g. dspark::stateId("COMP").
 

Variables

constexpr int dspark::detail::kMaxStateJsonDepth = 32
 

Detailed Description

Versioned key-value state serialization for processor presets.

The uniform getState()/setState() backbone: processors serialize their parameters as readable key/value entries into a compact, versioned binary blob, and restore tolerantly – unknown keys are ignored, missing keys keep their defaults, so presets survive both directions of version drift.

Layout (little-endian):

"DSPK" | format u16 | processorId u32 | processorVersion u16 | count u16 count x ( keyLen u8 | key bytes | type u8 | payload 4 bytes )

Types: 0 = float, 1 = int32, 2 = bool. Keys are short ASCII strings (letters, digits, '.', '_' – no quotes or backslashes, so the JSON helpers never need escaping), which keeps blobs self-describing: stateToJson() renders any blob as a flat JSON object and stateFromJson() parses that same subset back – no external libraries. Both are locale-independent: a host process running with a decimal-comma LC_NUMERIC (the classic plugin bug) cannot corrupt the number formatting or parsing.

Serialization is a setup/UI-thread operation (it allocates); never call it from the audio callback. That matches every plugin host's preset flow.

Dependencies: C++20 standard library only.

Definition in file StateBlob.h.