MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SpectralTransformer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Yantra {
7
9
10/**
11 * @enum SpectralOperation
12 * @brief Spectral operations supported by SpectralTransformer
13 */
14enum class SpectralOperation : uint8_t {
15 FREQUENCY_SHIFT, ///< Bandpass repositioning (approximate frequency shift)
16 PITCH_SHIFT, ///< Phase-vocoder pitch shift, duration preserved
17 SPECTRAL_FILTER, ///< Hard bandpass filter
18 HARMONIC_ENHANCE, ///< Linear spectral tilt
19 SPECTRAL_GATE, ///< Hard magnitude gate
20};
21
22/** @brief Construction settings for SpectralTransformer. */
24
25/**
26 * @class SpectralTransformer
27 * @brief Concrete transformer for frequency-domain operations
28 *
29 * Handles transformations in the spectral domain:
30 * - Spectral filtering and shaping
31 * - Pitch shifting and harmonics
32 * - Spectral morphing and cross-synthesis
33 * - Frequency analysis and manipulation
34 */
35template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
36class SpectralTransformer final : public UniversalTransformer<InputType, OutputType> {
37public:
40
41 /**
42 * @brief Constructs a SpectralTransformer with specified operation
43 * @param op The spectral operation to perform (default: FREQUENCY_SHIFT)
44 */
50
51 /**
52 * @brief Construct and configure a spectral transformer in one step.
53 * @param config Initial transformation settings.
54 */
56 : SpectralTransformer(config.operation)
57 {
58 this->set_strategy(config.strategy);
59 this->set_quality(config.quality);
60 this->set_scope(config.scope);
61 this->set_intensity(config.intensity);
62
63 for (const auto& key : config.keys) {
64 this->add_transformation_key(key);
65 }
66
67 for (const auto& [name, value] : config.parameters) {
68 this->set_parameter(name, value);
69 }
70 }
71
72 /**
73 * @brief Gets the transformation type
74 * @return TransformationType::SPECTRAL
75 */
76 [[nodiscard]] TransformationType get_transformation_type() const override
77 {
79 }
80
81 /**
82 * @brief Gets the transformer name including the operation type
83 * @return String representation of the transformer name
84 */
85 [[nodiscard]] std::string get_transformer_name() const override
86 {
87 return std::string("SpectralTransformer_").append(Reflect::enum_to_string(m_operation));
88 }
89
90protected:
91 /**
92 * @brief Applies the configured spectral operation
93 * @param input Input data
94 * @return Transformed output
95 *
96 * Extracts per-channel double spans, calls the corresponding
97 * Kinesis::Discrete::Spectral primitive on each channel, then
98 * reconstructs the typed output via OperationHelper.
99 *
100 * FREQUENCY_SHIFT is implemented as a bandpass repositioning matching
101 * the prior behaviour. True single-sideband frequency
102 * shift (complex exponential modulation) is a separate primitive.
103 */
105 {
106 switch (m_operation) {
107
109 const auto shift_hz = get_parameter_or<double>("shift_hz", 0.0);
110 const auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
111 const auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
112 const auto sample_rate = get_parameter_or<double>("sample_rate", 48000.0);
113 const double lo = std::max(0.0, shift_hz);
114 const double hi = sample_rate / 2.0 + shift_hz;
115
117 [lo, hi, sample_rate, window_size, hop_size](std::span<double> ch) {
118 return D::spectral_filter(ch, lo, hi, sample_rate, window_size, hop_size);
119 });
120 }
121
123 const auto pitch_ratio = get_parameter_or<double>("pitch_ratio", 1.0);
124 const auto window_size = get_parameter_or<uint32_t>("window_size", 2048);
125 const auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
126 const double semitones = 12.0 * std::log2(pitch_ratio);
127
129 [semitones, window_size, hop_size](std::span<double> ch) {
130 return D::pitch_shift(ch, semitones, window_size, hop_size);
131 });
132 }
133
135 const auto lo_freq = get_parameter_or<double>("low_freq", 20.0);
136 const auto hi_freq = get_parameter_or<double>("high_freq", 20000.0);
137 const auto sample_rate = get_parameter_or<double>("sample_rate", 48000.0);
138 const auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
139 const auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
140
142 [lo_freq, hi_freq, sample_rate, window_size, hop_size](std::span<double> ch) {
143 return D::spectral_filter(ch, lo_freq, hi_freq, sample_rate, window_size, hop_size);
144 });
145 }
146
148 const auto factor = get_parameter_or<double>("enhancement_factor", 2.0);
149 const auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
150 const auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
151
153 [factor, window_size, hop_size](std::span<double> ch) {
154 return D::harmonic_enhance(ch, factor, window_size, hop_size);
155 });
156 }
157
159 const auto threshold = get_parameter_or<double>("threshold", -40.0);
160 const auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
161 const auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
162
164 [threshold, window_size, hop_size](std::span<double> ch) {
165 return D::spectral_gate(ch, threshold, window_size, hop_size);
166 });
167 }
168
169 default:
170 return create_output(input);
171 }
172 }
173
174 /**
175 * @brief Sets transformation parameters
176 * @param name Parameter name
177 * @param value Parameter value
178 *
179 * Handles setting of spectral operation type and delegates other parameters to base class.
180 * Supports both enum and string values for the "operation" parameter.
181 *
182 * Common parameters include:
183 * - "shift_hz": Frequency shift in Hz
184 * - "pitch_ratio": Pitch scaling factor (1.0 = no change)
185 * - "low_freq", "high_freq": Filter frequency bounds
186 * - "enhancement_factor": Harmonic enhancement strength
187 * - "threshold": Spectral gate threshold in dB
188 * - "window_size", "hop_size": FFT processing parameters
189 */
190 void set_transformation_parameter(const std::string& name, std::any value) override
191 {
192 if (name == "operation") {
193 if (auto r = safe_any_cast<SpectralOperation>(value)) {
194 m_operation = *r.value;
195 return;
196 }
197 if (auto r = safe_any_cast<std::string>(value)) {
198 if (auto e = Reflect::string_to_enum_case_insensitive<SpectralOperation>(*r.value)) {
199 m_operation = *e;
200 return;
201 }
202 }
203 }
204
206 }
207
208private:
209 SpectralOperation m_operation; ///< Current spectral operation
210 mutable std::vector<std::vector<double>> m_working_buffer; ///< Buffer for out-of-place spectral operations
211
212 /**
213 * @brief Extracts per-channel spans, applies func to each, and reconstructs.
214 * @tparam Func Callable matching std::vector<double>(std::span<double>)
215 *
216 * In-place: results are written back into the channel spans of @p input before
217 * reconstruction, leaving no second allocation of channel data live at the same time.
218 * Out-of-place: results land in m_working_buffer; the original channel data in
219 * @p input is not touched.
220 */
221 template <typename Func>
223 {
225 m_working_buffer.resize(channels.size());
226 for (size_t i = 0; i < channels.size(); ++i)
227 m_working_buffer[i] = func(channels[i]);
228
229 if (this->is_in_place())
230 for (size_t i = 0; i < channels.size(); ++i)
231 std::ranges::copy(m_working_buffer[i], channels[i].begin());
232
233 return create_output(
234 OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
235 }
236
237 /**
238 * @brief Sets default parameter values for all spectral operations
239 *
240 * Initializes all possible parameters with sensible defaults to ensure
241 * the transformer works correctly regardless of the selected operation.
242 * Default values are chosen for typical audio processing scenarios.
243 */
245 {
246 this->set_parameter("shift_hz", 0.0);
247 this->set_parameter("pitch_ratio", 1.0);
248 this->set_parameter("low_freq", 20.0);
249 this->set_parameter("high_freq", 20000.0);
250 this->set_parameter("enhancement_factor", 2.0);
251 this->set_parameter("threshold", -40.0);
252 this->set_parameter("time_stretch", 1.0);
253 this->set_parameter("window_size", uint32_t { 1024 });
254 this->set_parameter("hop_size", uint32_t { 512 });
255 }
256
257 /**
258 * @brief Gets a parameter value with fallback to default
259 * @tparam T Parameter type
260 * @param name Parameter name
261 * @param default_value Default value if parameter not found or wrong type
262 * @return Parameter value or default
263 */
264 template <typename T>
265 T get_parameter_or(const std::string& name, const T& default_value) const
266 {
267 auto param = this->get_transformation_parameter(name);
268 if (!param.has_value())
269 return default_value;
270
271 auto result = safe_any_cast<T>(param);
272 return result.value_or(default_value);
273 }
274
275 /**
276 * @brief Creates output with proper type conversion
277 * @param data Input data to convert
278 * @return Output with converted data type
279 *
280 * Handles type conversion between InputType and OutputType when necessary,
281 * or direct assignment when types match. Ensures spectral processing results
282 * are properly converted back to the expected output type.
283 */
285 {
286 if constexpr (std::is_same_v<InputType, OutputType>) {
287 return input;
288 } else {
290 return this->convert_result(result_data, metadata);
291 }
292 }
293};
294}
Core::GlobalInputConfig input
Definition Config.cpp:42
std::unordered_map< std::string, std::string > metadata
Short-time Fourier domain primitives for MayaFlux::Kinesis.
Float Processing Guidelines.
glm::vec3 value
std::string name
Definition VKDevice.cpp:123
uint32_t channels
float threshold
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...
output_type apply_per_channel(input_type &input, Func &&func)
Extracts per-channel spans, applies func to each, and reconstructs.
SpectralTransformer(const SpectralTransformerConfig &config)
Construct and configure a spectral transformer in one step.
std::string get_transformer_name() const override
Gets the transformer name including the operation type.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
SpectralOperation m_operation
Current spectral operation.
output_type transform_implementation(input_type &input) override
Applies the configured spectral operation.
TransformationType get_transformation_type() const override
Gets the transformation type.
std::vector< std::vector< double > > m_working_buffer
Buffer for out-of-place spectral operations.
SpectralTransformer(SpectralOperation op=SpectralOperation::FREQUENCY_SHIFT)
Constructs a SpectralTransformer with specified operation.
T get_parameter_or(const std::string &name, const T &default_value) const
Gets a parameter value with fallback to default.
void set_default_parameters()
Sets default parameter values for all spectral operations.
void set_transformation_parameter(const std::string &name, std::any value) override
Sets transformation parameters.
Concrete transformer for frequency-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 > harmonic_enhance(std::span< const double > src, double enhancement_factor, uint32_t window_size, uint32_t hop_size)
Linear spectral tilt: scale each bin by a factor that rises linearly from 1 at bin 0 to enhancement_f...
Definition Spectral.cpp:155
std::vector< double > spectral_gate(std::span< const double > src, double threshold_db, uint32_t window_size, uint32_t hop_size)
Hard spectral gate: zero bins whose magnitude is below the threshold.
Definition Spectral.cpp:172
std::vector< double > spectral_filter(std::span< const double > src, double lo_hz, double hi_hz, double sample_rate, uint32_t window_size, uint32_t hop_size)
Hard bandpass filter: zero all bins outside [lo_hz, hi_hz].
Definition Spectral.cpp:123
std::vector< double > pitch_shift(std::span< const double > src, double semitones, uint32_t window_size, uint32_t analysis_hop)
Pitch-shift by resampling around a phase vocoder stretch.
Definition Spectral.cpp:282
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
SpectralOperation
Spectral operations supported by SpectralTransformer.
@ SPECTRAL_FILTER
Hard bandpass filter.
@ SPECTRAL_GATE
Hard magnitude gate.
@ HARMONIC_ENHANCE
Linear spectral tilt.
@ PITCH_SHIFT
Phase-vocoder pitch shift, duration preserved.
@ FREQUENCY_SHIFT
Bandpass repositioning (approximate frequency shift)
TransformationType
Categories of transformation operations for discovery and organization.
@ SPECTRAL
Frequency domain transformations (pitch-shift, spectral filtering)
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.