MayaFlux 0.1.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
9/**
10 * @enum TemporalOperation
11 * @brief Specific temporal operations supported
12 */
13enum class TemporalOperation : uint8_t {
14 TIME_REVERSE, ///< Reverse temporal order
15 TIME_STRETCH, ///< Change playback speed
16 DELAY, ///< Add temporal delay
17 FADE_IN_OUT, ///< Apply fade envelope
18 SLICE, ///< Extract temporal slice
19 INTERPOLATE ///< Temporal interpolation
20};
21
22/**
23 * @class TemporalTransformer
24 * @brief Concrete transformer for time-domain operations
25 *
26 * Handles transformations that operate in the temporal domain:
27 * - Time reversal, stretching, delay
28 * - Envelope shaping (fade in/out)
29 * - Temporal slicing and repositioning
30 * - Rhythm and timing manipulations
31 */
32template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
33class MAYAFLUX_API TemporalTransformer final : public UniversalTransformer<InputType, OutputType> {
34public:
37
38 /**
39 * @brief Constructs a TemporalTransformer with specified operation
40 * @param op The temporal operation to perform (default: TIME_REVERSE)
41 */
42 explicit TemporalTransformer(TemporalOperation op = TemporalOperation::TIME_REVERSE)
43 : m_operation(op)
44 {
45 set_default_parameters();
46 }
47
48 /**
49 * @brief Gets the transformation type
50 * @return TransformationType::TEMPORAL
51 */
52 [[nodiscard]] TransformationType get_transformation_type() const override
53 {
54 return TransformationType::TEMPORAL;
55 }
56
57 /**
58 * @brief Gets the transformer name including the operation type
59 * @return String representation of the transformer name
60 */
61 [[nodiscard]] std::string get_transformer_name() const override
62 {
63 return std::string("TemporalTransformer_").append(Utils::enum_to_string(m_operation));
64 }
65
66protected:
67 /**
68 * @brief Core transformation implementation for temporal operations
69 * @param input Input data to transform in the time domain
70 * @return Transformed output data
71 *
72 * Performs the temporal operation specified by m_operation on the input data.
73 * Operations modify the temporal characteristics of the data including timing,
74 * duration, ordering, and envelope shaping. Supports both in-place and
75 * out-of-place transformations based on transformer settings.
76 */
78 {
79 switch (m_operation) {
80 case TemporalOperation::TIME_REVERSE: {
81 if (this->is_in_place()) {
82 return create_output(transform_time_reverse(input));
83 }
84
85 return create_output(transform_time_reverse(input, m_working_buffer));
86 }
87
88 case TemporalOperation::TIME_STRETCH: {
89 auto stretch_factor = get_parameter_or<double>("stretch_factor", 1.0);
90 if (this->is_in_place()) {
91 return create_output(transform_time_stretch(input, stretch_factor));
92 }
93
94 return create_output(transform_time_stretch(input, stretch_factor, m_working_buffer));
95 }
96
97 case TemporalOperation::DELAY: {
98 auto delay_samples = get_parameter_or<uint32_t>("delay_samples", 1000);
99 auto fill_value = get_parameter_or<double>("fill_value", 0.0);
100 if (this->is_in_place()) {
101 return create_output(transform_delay(input, delay_samples, fill_value));
102 }
103
104 return create_output(transform_delay(input, delay_samples, fill_value, m_working_buffer));
105 }
106
107 case TemporalOperation::FADE_IN_OUT: {
108 auto fade_in_ratio = get_parameter_or<double>("fade_in_ratio", 0.1);
109 auto fade_out_ratio = get_parameter_or<double>("fade_out_ratio", 0.1);
110 if (this->is_in_place()) {
111 return create_output(transform_fade(input, fade_in_ratio, fade_out_ratio));
112 }
113
114 return create_output(transform_fade(input, fade_in_ratio, fade_out_ratio, m_working_buffer));
115 }
116
117 case TemporalOperation::SLICE: {
118 auto start_ratio = get_parameter_or<double>("start_ratio", 0.0);
119 auto end_ratio = get_parameter_or<double>("end_ratio", 1.0);
120 if (this->is_in_place()) {
121 return create_output(transform_slice(input, start_ratio, end_ratio));
122 }
123
124 return create_output(transform_slice(input, start_ratio, end_ratio, m_working_buffer));
125 }
126
127 case TemporalOperation::INTERPOLATE: {
128 auto target_size = get_parameter_or<size_t>("target_size", 0);
129 auto use_cubic = get_parameter_or<bool>("use_cubic", false);
130 if (target_size > 0) {
131 if (use_cubic) {
132 if (this->is_in_place()) {
133 return create_output(interpolate_cubic(input, target_size));
134 }
135
136 return create_output(interpolate_cubic(input, target_size, m_working_buffer));
137 }
138
139 if (this->is_in_place()) {
140 return create_output(interpolate_linear(input, target_size));
141 }
142 return create_output(interpolate_linear(input, target_size, m_working_buffer));
143 }
144 return create_output(input);
145 }
146
147 default:
148 return create_output(input);
149 }
150 }
151
152 /**
153 * @brief Sets transformation parameters
154 * @param name Parameter name
155 * @param value Parameter value
156 *
157 * Handles setting of temporal operation type and delegates other parameters to base class.
158 * Supports both enum and string values for the "operation" parameter.
159 *
160 * Common parameters include:
161 * - "stretch_factor": Time stretching factor (1.0 = no change, >1.0 = slower, <1.0 = faster)
162 * - "delay_samples": Number of samples to delay
163 * - "fill_value": Value to fill delayed regions with
164 * - "fade_in_ratio", "fade_out_ratio": Fade envelope ratios (0.0-1.0)
165 * - "start_ratio", "end_ratio": Slice boundaries as ratios of total length
166 * - "target_size": Target size for interpolation
167 * - "use_cubic": Whether to use cubic interpolation (vs linear)
168 */
169 void set_transformation_parameter(const std::string& name, std::any value) override
170 {
171 if (name == "operation") {
172 if (auto op_result = safe_any_cast<TemporalOperation>(value)) {
173 m_operation = *op_result.value;
174 return;
175 }
176 if (auto str_result = safe_any_cast<std::string>(value)) {
177 if (auto op_enum = Utils::string_to_enum_case_insensitive<TemporalOperation>(*str_result.value)) {
178 m_operation = *op_enum;
179 return;
180 }
181 }
182 }
183
185 }
186
187private:
188 TemporalOperation m_operation; ///< Current temporal operation
189 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place temporal operations
190
191 /**
192 * @brief Sets default parameter values for all temporal operations
193 *
194 * Initializes all possible parameters with sensible defaults to ensure
195 * the transformer works correctly regardless of the selected operation.
196 * Default values are chosen for typical temporal processing scenarios.
197 */
199 {
200 this->set_parameter("stretch_factor", 1.0);
201 this->set_parameter("delay_samples", uint32_t { 1000 });
202 this->set_parameter("fill_value", 0.0);
203 this->set_parameter("fade_in_ratio", 0.1);
204 this->set_parameter("fade_out_ratio", 0.1);
205 this->set_parameter("start_ratio", 0.0);
206 this->set_parameter("end_ratio", 1.0);
207 this->set_parameter("target_size", size_t { 0 });
208 this->set_parameter("use_cubic", false);
209 }
210
211 /**
212 * @brief Gets a parameter value with fallback to default
213 * @tparam T Parameter type
214 * @param name Parameter name
215 * @param default_value Default value if parameter not found or wrong type
216 * @return Parameter value or default
217 */
218 template <typename T>
219 T get_parameter_or(const std::string& name, const T& default_value) const
220 {
221 auto param = this->get_transformation_parameter(name);
222 if (!param.has_value())
223 return default_value;
224
225 auto result = safe_any_cast<T>(param);
226 return result.value_or(default_value);
227 }
228
229 /**
230 * @brief Creates output with proper type conversion
231 * @param data Input data to convert
232 * @return Output with converted data type
233 *
234 * Handles type conversion between InputType and OutputType when necessary,
235 * or direct assignment when types match. Ensures temporal processing results
236 * maintain the correct output type and temporal characteristics.
237 */
239 {
240 if constexpr (std::is_same_v<InputType, OutputType>) {
241 return input;
242 } else {
243 auto [result_data, metadata] = OperationHelper::extract_structured_double(input);
244 return this->convert_result(result_data, metadata);
245 }
246 }
247};
248
249}
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 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.
DataType interpolate_cubic(DataType &input, size_t target_size)
Cubic interpolation between data points using C++20 ranges (IN-PLACE)
DataType interpolate_linear(DataType &input, size_t target_size)
Linear interpolation between data points using C++20 ranges (IN-PLACE)
DataType transform_time_reverse(DataType &input)
Time reversal transformation using C++20 ranges (IN-PLACE)
DataType transform_delay(DataType &input, uint32_t delay_samples, double fill_value=0.0)
Delay transformation that extends buffer size (IN-PLACE)
DataType transform_time_stretch(DataType &input, double stretch_factor)
Simple time stretching via resampling using C++20 ranges (IN-PLACE)
DataType transform_slice(DataType &input, double start_ratio, double end_ratio)
Slice transformation to extract a portion of the data based on ratios (IN-PLACE)
DataType transform_fade(DataType &input, double fade_in_duration_ratio=0.0, double fade_out_duration_ratio=0.0)
Fade transformation (linear fade-in and fade-out) using C++20 ranges (IN-PLACE)
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