MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TemporalTransformer.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7namespace MayaFlux::Yantra {
8
10
11/**
12 * @enum TemporalOperation
13 * @brief Specific temporal operations supported
14 */
15enum class TemporalOperation : uint8_t {
16 TIME_REVERSE, ///< Reverse temporal order
17 TIME_STRETCH, ///< Change playback speed
18 DELAY, ///< Add temporal delay
19 FADE_IN_OUT, ///< Apply fade envelope
20 SLICE, ///< Extract temporal slice
21 INTERPOLATE ///< Temporal interpolation
22};
23
24/**
25 * @class TemporalTransformer
26 * @brief Concrete transformer for time-domain operations
27 *
28 * Handles transformations that operate in the temporal domain:
29 * - Time reversal, stretching, delay
30 * - Envelope shaping (fade in/out)
31 * - Temporal slicing and repositioning
32 * - Rhythm and timing manipulations
33 */
34template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
35class MAYAFLUX_API TemporalTransformer final : public UniversalTransformer<InputType, OutputType> {
36public:
39
40 /**
41 * @brief Constructs a TemporalTransformer with specified operation
42 * @param op The temporal operation to perform (default: TIME_REVERSE)
43 */
44 explicit TemporalTransformer(TemporalOperation op = TemporalOperation::TIME_REVERSE)
45 : m_operation(op)
46 {
47 set_default_parameters();
48 }
49
50 /**
51 * @brief Gets the transformation type
52 * @return TransformationType::TEMPORAL
53 */
54 [[nodiscard]] TransformationType get_transformation_type() const override
55 {
56 return TransformationType::TEMPORAL;
57 }
58
59 /**
60 * @brief Gets the transformer name including the operation type
61 * @return String representation of the transformer name
62 */
63 [[nodiscard]] std::string get_transformer_name() const override
64 {
65 return std::string("TemporalTransformer_").append(Reflect::enum_to_string(m_operation));
66 }
67
68protected:
69 /**
70 * @brief Core transformation implementation for temporal operations
71 * @param input Input data to transform in the time domain
72 * @return Transformed output data
73 *
74 * Performs the temporal operation specified by m_operation on the input data.
75 * Operations modify the temporal characteristics of the data including timing,
76 * duration, ordering, and envelope shaping. Supports both in-place and
77 * out-of-place transformations based on transformer settings.
78 */
80 {
81 switch (m_operation) {
82
83 case TemporalOperation::TIME_REVERSE:
84 return apply_per_channel(input, [](std::span<double> ch) {
85 D::reverse(ch);
86 });
87
88 case TemporalOperation::FADE_IN_OUT: {
89 const auto fi = get_parameter_or<double>("fade_in_ratio", 0.1);
90 const auto fo = get_parameter_or<double>("fade_out_ratio", 0.1);
91 return apply_per_channel(input, [fi, fo](std::span<double> ch) {
92 D::fade(ch, fi, fo);
93 });
94 }
95
96 case TemporalOperation::TIME_STRETCH: {
97 const auto factor = get_parameter_or<double>("stretch_factor", 1.0);
98 return apply_per_channel(input, [factor](std::span<const double> ch) {
99 return D::time_stretch(ch, factor);
100 });
101 }
102
103 case TemporalOperation::DELAY: {
104 const auto samples = get_parameter_or<uint32_t>("delay_samples", 1000);
105 const auto fill = get_parameter_or<double>("fill_value", 0.0);
106 return apply_per_channel(input, [samples, fill](std::span<const double> ch) {
107 return D::delay(ch, samples, fill);
108 });
109 }
110
111 case TemporalOperation::SLICE: {
112 const auto start = get_parameter_or<double>("start_ratio", 0.0);
113 const auto end = get_parameter_or<double>("end_ratio", 1.0);
114 return apply_per_channel(input, [start, end](std::span<const double> ch) {
115 return D::slice(ch, start, end);
116 });
117 }
118
119 case TemporalOperation::INTERPOLATE: {
120 const auto target = get_parameter_or<size_t>("target_size", size_t { 0 });
121 const auto cubic = get_parameter_or<bool>("use_cubic", false);
122 if (target == 0)
123 return create_output(input);
124 return apply_per_channel(input, [target, cubic](std::span<const double> ch) {
125 std::vector<double> out(target);
126 if (cubic)
127 D::interpolate_cubic(ch, out);
128 else
129 D::interpolate_linear(ch, out);
130 return out;
131 });
132 }
133
134 default:
135 return create_output(input);
136 }
137 }
138
139 /**
140 * @brief Sets transformation parameters
141 * @param name Parameter name
142 * @param value Parameter value
143 *
144 * Handles setting of temporal operation type and delegates other parameters to base class.
145 * Supports both enum and string values for the "operation" parameter.
146 *
147 * Common parameters include:
148 * - "stretch_factor": Time stretching factor (1.0 = no change, >1.0 = slower, <1.0 = faster)
149 * - "delay_samples": Number of samples to delay
150 * - "fill_value": Value to fill delayed regions with
151 * - "fade_in_ratio", "fade_out_ratio": Fade envelope ratios (0.0-1.0)
152 * - "start_ratio", "end_ratio": Slice boundaries as ratios of total length
153 * - "target_size": Target size for interpolation
154 * - "use_cubic": Whether to use cubic interpolation (vs linear)
155 */
156 void set_transformation_parameter(const std::string& name, std::any value) override
157 {
158 if (name == "operation") {
159 if (auto r = safe_any_cast<TemporalOperation>(value)) {
160 m_operation = *r.value;
161 return;
162 }
163 if (auto r = safe_any_cast<std::string>(value)) {
164 if (auto e = Reflect::string_to_enum_case_insensitive<TemporalOperation>(*r.value)) {
165 m_operation = *e;
166 return;
167 }
168 }
169 }
171 }
172
173private:
174 TemporalOperation m_operation; ///< Current temporal operation
175 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place temporal operations
176
177 /**
178 * @brief Extracts per-channel spans, applies func to each, and reconstructs.
179 * @tparam Func Callable matching either void(std::span<double>) for same-size
180 * operations or std::vector<double>(std::span<const double>) for
181 * operations that may resize the channel.
182 *
183 * In-place: results are copied back into the original channel spans of @p input.
184 * Out-of-place: input is not mutated.
185 */
186 template <typename Func>
188 {
189 auto [channels, structure_info] = OperationHelper::extract_structured_double(input);
190 m_working_buffer.resize(channels.size());
191 for (size_t i = 0; i < channels.size(); ++i) {
192 if constexpr (std::is_void_v<std::invoke_result_t<Func, std::span<double>>>) {
193 m_working_buffer[i].assign(channels[i].begin(), channels[i].end());
194 func(std::span<double> { m_working_buffer[i] });
195 } else {
196 m_working_buffer[i] = func(std::span<const double> { channels[i] });
197 }
198 }
199
200 if (this->is_in_place())
201 for (size_t i = 0; i < channels.size(); ++i)
202 std::ranges::copy(m_working_buffer[i], channels[i].begin());
203 return create_output(
204 OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
205 }
206
207 /**
208 * @brief Sets default parameter values for all temporal operations
209 *
210 * Initializes all possible parameters with sensible defaults to ensure
211 * the transformer works correctly regardless of the selected operation.
212 * Default values are chosen for typical temporal processing scenarios.
213 */
215 {
216 this->set_parameter("stretch_factor", 1.0);
217 this->set_parameter("delay_samples", uint32_t { 1000 });
218 this->set_parameter("fill_value", 0.0);
219 this->set_parameter("fade_in_ratio", 0.1);
220 this->set_parameter("fade_out_ratio", 0.1);
221 this->set_parameter("start_ratio", 0.0);
222 this->set_parameter("end_ratio", 1.0);
223 this->set_parameter("target_size", size_t { 0 });
224 this->set_parameter("use_cubic", false);
225 }
226
227 /**
228 * @brief Gets a parameter value with fallback to default
229 * @tparam T Parameter type
230 * @param name Parameter name
231 * @param default_value Default value if parameter not found or wrong type
232 * @return Parameter value or default
233 */
234 template <typename T>
235 T get_parameter_or(const std::string& name, const T& default_value) const
236 {
237 auto param = this->get_transformation_parameter(name);
238 if (!param.has_value())
239 return default_value;
240
241 auto result = safe_any_cast<T>(param);
242 return result.value_or(default_value);
243 }
244
245 /**
246 * @brief Creates output with proper type conversion
247 * @param data Input data to convert
248 * @return Output with converted data type
249 *
250 * Handles type conversion between InputType and OutputType when necessary,
251 * or direct assignment when types match. Ensures temporal processing results
252 * maintain the correct output type and temporal characteristics.
253 */
255 {
256 if constexpr (std::is_same_v<InputType, OutputType>) {
257 return input;
258 } else {
259 auto [result_data, metadata] = OperationHelper::extract_structured_double(input);
260 return this->convert_result(result_data, metadata);
261 }
262 }
263};
264
265}
Discrete sequence transformation primitives for MayaFlux::Kinesis.
Float Processing Guidelines.
void set_transformation_parameter(const std::string &name, std::any value) override
Sets transformation parameters.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
std::vector< std::vector< double > > m_working_buffer
Buffer for out-of-place temporal operations.
std::string get_transformer_name() const override
Gets the transformer name including the operation type.
TransformationType get_transformation_type() const override
Gets the transformation type.
output_type apply_per_channel(input_type &input, Func &&func)
Extracts per-channel spans, applies func to each, and reconstructs.
output_type transform_implementation(input_type &input) override
Core transformation implementation for temporal operations.
TemporalOperation m_operation
Current temporal operation.
TemporalTransformer(TemporalOperation op=TemporalOperation::TIME_REVERSE)
Constructs a TemporalTransformer with specified operation.
void set_default_parameters()
Sets default parameter values for all temporal operations.
T get_parameter_or(const std::string &name, const T &default_value) const
Gets a parameter value with fallback to default.
Concrete transformer for time-domain operations.
Template-flexible transformer base with instance-defined I/O types.
TransformationType
Categories of transformation operations for discovery and organization.
TemporalOperation
Specific temporal operations supported.
@ SLICE
Extract temporal slice.
@ TIME_REVERSE
Reverse temporal order.
@ TIME_STRETCH
Change playback speed.
@ FADE_IN_OUT
Apply fade envelope.
@ INTERPOLATE
Temporal interpolation.
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:24