MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
OSCNode.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "InputNode.hpp"
4
6
7/**
8 * @struct OSCConfig
9 * @brief Configuration for OSC input node argument extraction
10 *
11 * Determines which argument from an OSC message to extract as the
12 * node's scalar output. Address filtering is handled by InputBinding,
13 * not by the node. The node only extracts and interprets arguments
14 * from messages that already passed binding resolution.
15 */
17 size_t argument_index { 0 }; ///< Which argument to extract (0-based)
18
19 double range_min { 0.0 }; ///< Expected input minimum (for normalization)
20 double range_max { 1.0 }; ///< Expected input maximum (for normalization)
21 bool normalize { false }; ///< Map [range_min, range_max] to [0.0, 1.0]
22
23 std::function<double(const Core::InputValue::OSCArg&)> custom_extractor;
24
25 /**
26 * @brief Extract first float argument (most common case)
27 */
28 static OSCConfig value() { return {}; }
29
30 /**
31 * @brief Extract a specific argument by index
32 */
33 static OSCConfig arg(size_t index)
34 {
35 OSCConfig cfg;
36 cfg.argument_index = index;
37 return cfg;
38 }
39
40 /**
41 * @brief Extract and normalize to [0, 1]
42 */
43 static OSCConfig normalized(double min_val, double max_val, size_t index = 0)
44 {
45 OSCConfig cfg;
46 cfg.argument_index = index;
47 cfg.range_min = min_val;
48 cfg.range_max = max_val;
49 cfg.normalize = true;
50 return cfg;
51 }
52
53 /**
54 * @brief Use a custom extraction function
55 */
56 template <typename F>
57 static OSCConfig custom(F&& extractor)
58 {
59 OSCConfig cfg;
60 cfg.custom_extractor = std::forward<F>(extractor);
61 return cfg;
62 }
63
64 OSCConfig& with_range(double min_val, double max_val)
65 {
66 range_min = min_val;
67 range_max = max_val;
68 normalize = true;
69 return *this;
70 }
71
72 OSCConfig& at_index(size_t index)
73 {
74 argument_index = index;
75 return *this;
76 }
77};
78
79/**
80 * @class OSCNode
81 * @brief Specialized InputNode for OSC messages
82 *
83 * Extracts scalar values from OSC message arguments. Address pattern
84 * filtering is handled by InputBinding::osc_address_pattern at the
85 * InputManager routing level. The node receives only messages that
86 * already matched its binding.
87 *
88 * Supports typed argument callbacks for structured access to the
89 * full OSC message beyond the scalar output.
90 *
91 * Example usage:
92 * @code
93 * // Simple: first float argument from /sensor/pressure
94 * auto pressure = std::make_shared<OSCNode>(OSCConfig::value());
95 * register_input_node(pressure, InputBinding::osc("/sensor/pressure"));
96 *
97 * // Second argument, normalized from [0, 1023] to [0, 1]
98 * auto adc = std::make_shared<OSCNode>(
99 * OSCConfig::normalized(0.0, 1023.0, 1));
100 * register_input_node(adc, InputBinding::osc("/adc"));
101 *
102 * // With message callback for multi-argument access
103 * auto multi = std::make_shared<OSCNode>();
104 * multi->on_message([](const std::string& addr, const auto& args) {
105 * // access all arguments
106 * });
107 * register_input_node(multi, InputBinding::osc("/multi"));
108 *
109 * // Via Creator API
110 * auto node = vega.read_osc(OSCConfig::value(), InputBinding::osc("/fader/1"));
111 * @endcode
112 */
113class MAYAFLUX_API OSCNode : public InputNode {
114public:
115 using MessageCallback = std::function<void(
116 const std::string& address,
117 const std::vector<Core::InputValue::OSCArg>& arguments)>;
118
119 explicit OSCNode(OSCConfig config = {});
120
121 void save_state() override { }
122 void restore_state() override { }
123
124 /**
125 * @brief Register callback for full OSC message access
126 * @param callback Receives address and all typed arguments
127 *
128 * Fires on every matching message, independent of scalar extraction.
129 * Use for multi-argument messages or when you need the address.
130 */
132 {
133 m_message_callbacks.push_back(std::move(callback));
134 }
135
136protected:
137 double extract_value(const Core::InputValue& value) override;
138
139 void notify_tick(double value) override;
140
141private:
143 std::optional<Core::InputValue::OSCMessage> m_last_osc_message;
144
145 std::vector<MessageCallback> m_message_callbacks;
146
147 void fire_osc_callbacks(const Core::InputValue::OSCMessage& osc);
148};
149
150} // namespace MayaFlux::Nodes::Input
Abstract base class for nodes that receive external input.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
Definition OSCNode.hpp:121
std::optional< Core::InputValue::OSCMessage > m_last_osc_message
Definition OSCNode.hpp:143
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
Definition OSCNode.hpp:122
void on_message(MessageCallback callback)
Register callback for full OSC message access.
Definition OSCNode.hpp:131
std::function< void(const std::string &address, const std::vector< Core::InputValue::OSCArg > &arguments)> MessageCallback
Definition OSCNode.hpp:117
std::vector< MessageCallback > m_message_callbacks
Definition OSCNode.hpp:145
Specialized InputNode for OSC messages.
Definition OSCNode.hpp:113
std::variant< int32_t, float, std::string, std::vector< uint8_t > > OSCArg
OSC argument types.
Generic input value container.
Configuration for InputNode behavior.
Definition InputNode.hpp:79
double range_min
Expected input minimum (for normalization)
Definition OSCNode.hpp:19
bool normalize
Map [range_min, range_max] to [0.0, 1.0].
Definition OSCNode.hpp:21
static OSCConfig value()
Extract first float argument (most common case)
Definition OSCNode.hpp:28
static OSCConfig arg(size_t index)
Extract a specific argument by index.
Definition OSCNode.hpp:33
static OSCConfig normalized(double min_val, double max_val, size_t index=0)
Extract and normalize to [0, 1].
Definition OSCNode.hpp:43
size_t argument_index
Which argument to extract (0-based)
Definition OSCNode.hpp:17
double range_max
Expected input maximum (for normalization)
Definition OSCNode.hpp:20
std::function< double(const Core::InputValue::OSCArg &)> custom_extractor
Definition OSCNode.hpp:23
static OSCConfig custom(F &&extractor)
Use a custom extraction function.
Definition OSCNode.hpp:57
OSCConfig & at_index(size_t index)
Definition OSCNode.hpp:72
OSCConfig & with_range(double min_val, double max_val)
Definition OSCNode.hpp:64
Configuration for OSC input node argument extraction.
Definition OSCNode.hpp:16