|
| 1 | +// Copyright 2026 Davide Faconti |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +#pragma once |
| 4 | + |
| 5 | +// Filter Editor transform contract. Plugins provide the concrete strategies; |
| 6 | +// the host consumes them by id through FilterTransformFactory. |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <optional> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "pj_base/point2.hpp" |
| 14 | + |
| 15 | +namespace PJ::sdk { |
| 16 | + |
| 17 | +/// Strategy interface for a Filter Editor transform. |
| 18 | +/// |
| 19 | +/// Two streaming surfaces: |
| 20 | +/// - `calculateNextPoint` — SISO, one input -> at most one output (PJ3 mirror). |
| 21 | +/// - `appendTail` — process a chunk; default loops `calculateNextPoint`. Override |
| 22 | +/// when running state (sliding sum, deque) makes it O(Δsamples) per call. |
| 23 | +/// |
| 24 | +/// `clone()` is the host hand-off across the plugin DSO boundary. |
| 25 | +class FilterTransform { |
| 26 | + public: |
| 27 | + virtual ~FilterTransform() = default; |
| 28 | + |
| 29 | + // Catalog identity used in saveConfig JSON, the factory registry, and the |
| 30 | + // legend. |
| 31 | + [[nodiscard]] virtual const char* id() const = 0; |
| 32 | + [[nodiscard]] virtual const char* label() const = 0; |
| 33 | + [[nodiscard]] virtual const char* bracketLabel() const = 0; |
| 34 | + |
| 35 | + // True if the transform can extend its output as new samples arrive. |
| 36 | + [[nodiscard]] virtual bool isStreamSafe() const = 0; |
| 37 | + |
| 38 | + /// Drop accumulated state. Must be called before the first `calculateNextPoint` |
| 39 | + /// after a series replace / clear. |
| 40 | + virtual void reset() = 0; |
| 41 | + |
| 42 | + /// One input -> optional output. Inputs MUST arrive in x-ascending order. |
| 43 | + /// nullopt suppresses the output (e.g. Derivative drops the first sample). |
| 44 | + [[nodiscard]] virtual std::optional<Point2> calculateNextPoint(const Point2& in) = 0; |
| 45 | + |
| 46 | + /// Process the tail of points since the previous call. Default loops |
| 47 | + /// `calculateNextPoint`; override per-transform when O(Δsamples) is possible. |
| 48 | + virtual void appendTail(const std::vector<Point2>& new_raw, std::vector<Point2>& out) { |
| 49 | + out.reserve(out.size() + new_raw.size()); |
| 50 | + for (const auto& p : new_raw) { |
| 51 | + if (auto r = calculateNextPoint(p); r) { |
| 52 | + out.push_back(*r); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// Run from scratch over a whole series. Default: reset + appendTail. |
| 58 | + virtual std::vector<Point2> applyBatch(const std::vector<Point2>& input) { |
| 59 | + reset(); |
| 60 | + std::vector<Point2> out; |
| 61 | + out.reserve(input.size()); |
| 62 | + appendTail(input, out); |
| 63 | + return out; |
| 64 | + } |
| 65 | + |
| 66 | + /// JSON for the parameter set this transform owns (not the source binding). |
| 67 | + [[nodiscard]] virtual std::string saveParams() const { |
| 68 | + return "{}"; |
| 69 | + } |
| 70 | + virtual void loadParams(const std::string& /*json_str*/) {} |
| 71 | + |
| 72 | + /// Deep copy. The host calls this so the kept instance is independent of the |
| 73 | + /// plugin DSO (the cloned vtable lives in the plugin's code, which stays |
| 74 | + /// loaded for the app session). |
| 75 | + [[nodiscard]] virtual std::unique_ptr<FilterTransform> clone() const = 0; |
| 76 | +}; |
| 77 | + |
| 78 | +} // namespace PJ::sdk |
0 commit comments