MayaFlux 1.0.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/** @brief Construction settings for TemporalTransformer. */
26
27/**
28 * @class TemporalTransformer
29 * @brief Concrete transformer for time-domain operations
30 *
31 * Handles transformations that operate in the temporal domain:
32 * - Time reversal, stretching, delay
33 * - Envelope shaping (fade in/out)
34 * - Temporal slicing and repositioning
35 * - Rhythm and timing manipulations
36 */
37template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
38class TemporalTransformer final : public UniversalTransformer<InputType, OutputType> {
39public:
42
43 /**
44 * @brief Constructs a TemporalTransformer with specified operation
45 * @param op The temporal operation to perform (default: TIME_REVERSE)
46 */
52
53 /**
54 * @brief Construct and configure a temporal transformer in one step.
55 * @param config Initial transformation settings.
56 */
58 : TemporalTransformer(config.operation)
59 {
60 this->set_strategy(config.strategy);
61 this->set_quality(config.quality);
62 this->set_scope(config.scope);
63 this->set_intensity(config.intensity);
64
65 for (const auto& key : config.keys) {
66 this->add_transformation_key(key);
67 }
68
69 for (const auto& [name, value] : config.parameters) {
70 this->set_parameter(name, value);
71 }
72 }
73
74 /**
75 * @brief Gets the transformation type
76 * @return TransformationType::TEMPORAL
77 */
78 [[nodiscard]] TransformationType get_transformation_type() const override
79 {
81 }
82
83 /**
84 * @brief Gets the transformer name including the operation type
85 * @return String representation of the transformer name
86 */
87 [[nodiscard]] std::string get_transformer_name() const override
88 {
89 return std::string("TemporalTransformer_").append(Reflect::enum_to_string(m_operation));
90 }
91
92protected:
93 /**
94 * @brief Core transformation implementation for temporal operations
95 * @param input Input data to transform in the time domain
96 * @return Transformed output data
97 *
98 * Performs the temporal operation specified by m_operation on the input data.
99 * Operations modify the temporal characteristics of the data including timing,
100 * duration, ordering, and envelope shaping. Supports both in-place and
101 * out-of-place transformations based on transformer settings.
102 */
104 {
105 switch (m_operation) {
106
108 return apply_per_channel(input, [](std::span<double> ch) {
109 D::reverse(ch);
110 });
111
113 const auto fi = get_parameter_or<double>("fade_in_ratio", 0.1);
114 const auto fo = get_parameter_or<double>("fade_out_ratio", 0.1);
115 return apply_per_channel(input, [fi, fo](std::span<double> ch) {
116 D::fade(ch, fi, fo);
117 });
118 }
119
121 const auto factor = get_parameter_or<double>("stretch_factor", 1.0);
122 return apply_per_channel(input, [factor](std::span<const double> ch) {
123 return D::time_stretch(ch, factor);
124 });
125 }
126
128 const auto samples = get_parameter_or<uint32_t>("delay_samples", 1000);
129 const auto fill = get_parameter_or<double>("fill_value", 0.0);
130 return apply_per_channel(input, [samples, fill](std::span<const double> ch) {
131 return D::delay(ch, samples, fill);
132 });
133 }
134
136 const auto start = get_parameter_or<double>("start_ratio", 0.0);
137 const auto end = get_parameter_or<double>("end_ratio", 1.0);
138 return apply_per_channel(input, [start, end](std::span<const double> ch) {
139 return D::slice(ch, start, end);
140 });
141 }
142
144 const auto target = get_parameter_or<size_t>("target_size", size_t { 0 });
145 const auto cubic = get_parameter_or<bool>("use_cubic", false);
146 if (target == 0)
147 return create_output(input);
148 return apply_per_channel(input, [target, cubic](std::span<const double> ch) {
149 std::vector<double> out(target);
150 if (cubic)
152 else
154 return out;
155 });
156 }
157
158 default:
159 return create_output(input);
160 }
161 }
162
163 /**
164 * @brief Sets transformation parameters
165 * @param name Parameter name
166 * @param value Parameter value
167 *
168 * Handles setting of temporal operation type and delegates other parameters to base class.
169 * Supports both enum and string values for the "operation" parameter.
170 *
171 * Common parameters include:
172 * - "stretch_factor": Time stretching factor (1.0 = no change, >1.0 = slower, <1.0 = faster)
173 * - "delay_samples": Number of samples to delay
174 * - "fill_value": Value to fill delayed regions with
175 * - "fade_in_ratio", "fade_out_ratio": Fade envelope ratios (0.0-1.0)
176 * - "start_ratio", "end_ratio": Slice boundaries as ratios of total length
177 * - "target_size": Target size for interpolation
178 * - "use_cubic": Whether to use cubic interpolation (vs linear)
179 */
180 void set_transformation_parameter(const std::string& name, std::any value) override
181 {
182 if (name == "operation") {
183 if (auto r = safe_any_cast<TemporalOperation>(value)) {
184 m_operation = *r.value;
185 return;
186 }
187 if (auto r = safe_any_cast<std::string>(value)) {
188 if (auto e = Reflect::string_to_enum_case_insensitive<TemporalOperation>(*r.value)) {
189 m_operation = *e;
190 return;
191 }
192 }
193 }
195 }
196
197private:
198 TemporalOperation m_operation; ///< Current temporal operation
199 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place temporal operations
200
201 /**
202 * @brief Extracts per-channel spans, applies func to each, and reconstructs.
203 * @tparam Func Callable matching either void(std::span<double>) for same-size
204 * operations or std::vector<double>(std::span<const double>) for
205 * operations that may resize the channel.
206 *
207 * In-place: results are copied back into the original channel spans of @p input.
208 * Out-of-place: input is not mutated.
209 */
210 template <typename Func>
212 {
214 m_working_buffer.resize(channels.size());
215 for (size_t i = 0; i < channels.size(); ++i) {
216 if constexpr (std::is_void_v<std::invoke_result_t<Func, std::span<double>>>) {
217 m_working_buffer[i].assign(channels[i].begin(), channels[i].end());
218 func(std::span<double> { m_working_buffer[i] });
219 } else {
220 m_working_buffer[i] = func(std::span<const double> { channels[i] });
221 }
222 }
223
224 if (this->is_in_place())
225 for (size_t i = 0; i < channels.size(); ++i)
226 std::ranges::copy(m_working_buffer[i], channels[i].begin());
227 return create_output(
228 OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
229 }
230
231 /**
232 * @brief Sets default parameter values for all temporal operations
233 *
234 * Initializes all possible parameters with sensible defaults to ensure
235 * the transformer works correctly regardless of the selected operation.
236 * Default values are chosen for typical temporal processing scenarios.
237 */
239 {
240 this->set_parameter("stretch_factor", 1.0);
241 this->set_parameter("delay_samples", uint32_t { 1000 });
242 this->set_parameter("fill_value", 0.0);
243 this->set_parameter("fade_in_ratio", 0.1);
244 this->set_parameter("fade_out_ratio", 0.1);
245 this->set_parameter("start_ratio", 0.0);
246 this->set_parameter("end_ratio", 1.0);
247 this->set_parameter("target_size", size_t { 0 });
248 this->set_parameter("use_cubic", false);
249 }
250
251 /**
252 * @brief Gets a parameter value with fallback to default
253 * @tparam T Parameter type
254 * @param name Parameter name
255 * @param default_value Default value if parameter not found or wrong type
256 * @return Parameter value or default
257 */
258 template <typename T>
259 T get_parameter_or(const std::string& name, const T& default_value) const
260 {
261 auto param = this->get_transformation_parameter(name);
262 if (!param.has_value())
263 return default_value;
264
265 auto result = safe_any_cast<T>(param);
266 return result.value_or(default_value);
267 }
268
269 /**
270 * @brief Creates output with proper type conversion
271 * @param data Input data to convert
272 * @return Output with converted data type
273 *
274 * Handles type conversion between InputType and OutputType when necessary,
275 * or direct assignment when types match. Ensures temporal processing results
276 * maintain the correct output type and temporal characteristics.
277 */
279 {
280 if constexpr (std::is_same_v<InputType, OutputType>) {
281 return input;
282 } else {
284 return this->convert_result(result_data, metadata);
285 }
286 }
287};
288
289}
Core::GlobalInputConfig input
Definition Config.cpp:42
std::unordered_map< std::string, std::string > metadata
Discrete sequence transformation primitives for MayaFlux::Kinesis.
Float Processing Guidelines.
std::vector< float > * out
glm::vec3 value
std::string name
Definition VKDevice.cpp:123
uint32_t channels
output_type convert_result(std::vector< std::vector< double > > &result_data, DataStructureInfo &metadata)
Convert processed double data back to OutputType using metadata and optional callback.
static std::tuple< std::vector< std::span< double > >, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from Datum container or direct ComputeData with automatic container ha...
TemporalTransformer(const TemporalTransformerConfig &config)
Construct and configure a temporal transformer in one step.
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.
void set_strategy(TransformationStrategy strategy)
Sets the transformation strategy.
void set_intensity(double intensity)
Set transformation intensity (0.0 = no transformation, 1.0 = full transformation)
void set_scope(TransformationScope scope)
Sets the transformation scope.
virtual bool is_in_place() const
Indicates whether the transformation modifies the input data directly.
virtual std::any get_transformation_parameter(const std::string &name) const
Gets a transformation-specific parameter value.
void set_parameter(const std::string &name, std::any value) override
Type-safe parameter management with transformation-specific defaults.
void set_quality(TransformationQuality quality)
Sets the transformation quality level.
void add_transformation_key(const TransformationKey &key)
Add transformation key for multi-dimensional transformations.
virtual void set_transformation_parameter(const std::string &name, std::any value)
Transformation-specific parameter handling (override for custom parameters)
Template-flexible transformer base with instance-defined I/O types.
std::vector< double > delay(std::span< const double > data, uint32_t delay_samples, double fill_value)
Prepend delay_samples zero-valued (or fill_value) samples, returning a new vector.
std::vector< double > time_stretch(std::span< const double > data, double stretch_factor)
Time-stretch via linear interpolation resampling Fast but alias-naive: no anti-aliasing pre-filter is...
void interpolate_cubic(std::span< const double > src, std::span< double > dst) noexcept
Catmull-Rom cubic interpolation from src into dst (caller sizes dst) Branchless boundary clamping; Ho...
std::vector< double > slice(std::span< const double > data, double start_ratio, double end_ratio)
Extract a contiguous slice by ratio, returning a new vector.
void fade(std::span< double > data, double fade_in_ratio, double fade_out_ratio) noexcept
Apply equal-power (cosine) fade-in then fade-out envelope in-place The cosine taper maintains constan...
Definition Transform.cpp:95
void reverse(std::span< double > data) noexcept
Reverse temporal order in-place.
Definition Transform.cpp:90
void interpolate_linear(std::span< const double > src, std::span< double > dst) noexcept
Linear interpolation from src into dst (caller sizes dst) Branchless inner loop with precomputed step...
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
TransformationType
Categories of transformation operations for discovery and organization.
@ TEMPORAL
Time-based transformations (time-stretch, reverse, delay)
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:26
TransformationQuality quality
Requested transformation quality.
TransformationScope scope
Portion of the input to transform.
TransformationStrategy strategy
How transformation work is performed.
std::vector< TransformationKey > keys
Transformation keys to register before processing.
std::map< std::string, std::any > parameters
Additional named parameters applied last.
double intensity
Overall transformation strength.
Initial settings shared by concrete transformer families.