MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ transform_implementation()

template<ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = InputType>
output_type MayaFlux::Yantra::SpectralTransformer< InputType, OutputType >::transform_implementation ( input_type input)
inlineoverrideprotectedvirtual

Core transformation implementation for spectral operations.

Parameters
inputInput data to transform in the frequency domain
Returns
Transformed output data

Performs the spectral operation specified by m_operation on the input data. Operations are performed using FFT/IFFT transforms with windowing for overlap-add processing. Supports both in-place and out-of-place transformations.

Implements MayaFlux::Yantra::UniversalTransformer< InputType, OutputType >.

Definition at line 74 of file SpectralTransformer.hpp.

75 {
76 switch (m_operation) {
78 auto shift_hz = get_parameter_or<double>("shift_hz", 0.0);
79 auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
80 auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
81 auto sample_rate = get_parameter_or<double>("sample_rate", 48000.0);
82
83 auto low_freq = std::max(0.0, shift_hz);
84 auto high_freq = sample_rate / 2.0 + shift_hz;
85
86 if (this->is_in_place()) {
87 return create_output(transform_spectral_filter(input, low_freq, high_freq, sample_rate, window_size, hop_size));
88 }
89 return create_output(transform_spectral_filter(input, low_freq, high_freq, sample_rate, window_size, hop_size, m_working_buffer));
90 }
91
93 auto pitch_ratio = get_parameter_or<double>("pitch_ratio", 1.0);
94 auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
95 auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
96
97 double semitones = 12.0 * std::log2(pitch_ratio);
98
99 if (this->is_in_place()) {
100 return create_output(transform_pitch_shift(input, semitones, window_size, hop_size));
101 }
102 return create_output(transform_pitch_shift(input, semitones, window_size, hop_size, m_working_buffer));
103 }
104
106 auto low_freq = get_parameter_or<double>("low_freq", 20.0);
107 auto high_freq = get_parameter_or<double>("high_freq", 20000.0);
108 auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
109 auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
110 auto sample_rate = get_parameter_or<double>("sample_rate", 48000.0);
111
112 if (this->is_in_place()) {
113 return create_output(transform_spectral_filter(input, low_freq, high_freq, sample_rate, window_size, hop_size));
114 }
115 return create_output(transform_spectral_filter(input, low_freq, high_freq, sample_rate, window_size, hop_size, m_working_buffer));
116 }
117
119 auto enhancement_factor = get_parameter_or<double>("enhancement_factor", 2.0);
120 auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
121 auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
122
123 if (this->is_in_place()) {
124 auto [data_span, structure_info] = OperationHelper::extract_structured_double(input);
125
126 auto processor = [enhancement_factor](Eigen::VectorXcd& spectrum, size_t) {
127 for (int i = 1; i < spectrum.size() / 2; ++i) {
128 double freq_factor = 1.0 + (enhancement_factor - 1.0) * (double(i) / (spectrum.size() / 2));
129 spectrum[i] *= freq_factor;
130 spectrum[spectrum.size() - i] *= freq_factor;
131 }
132 };
133
134 for (auto& span : data_span) {
135 auto result = process_spectral_windows(span, window_size, hop_size, processor);
136 std::ranges::copy(result, span.begin());
137 }
138
139 auto reconstructed_data = data_span
140 | std::views::transform([](const auto& span) {
141 return std::vector<double>(span.begin(), span.end());
142 })
143 | std::ranges::to<std::vector>();
144
145 return create_output(OperationHelper::reconstruct_from_double<InputType>(reconstructed_data, structure_info));
146 }
147
148 auto [target_data, structure_info] = OperationHelper::setup_operation_buffer(input, m_working_buffer);
149 auto processor = [enhancement_factor](Eigen::VectorXcd& spectrum, size_t) {
150 for (int i = 1; i < spectrum.size() / 2; ++i) {
151 double freq_factor = 1.0 + (enhancement_factor - 1.0) * (double(i) / (spectrum.size() / 2));
152 spectrum[i] *= freq_factor;
153 spectrum[spectrum.size() - i] *= freq_factor;
154 }
155 };
156
157 for (size_t i = 0; i < target_data.size(); ++i) {
158 auto result = process_spectral_windows(target_data[i], window_size, hop_size, processor);
159 m_working_buffer[i] = std::move(result);
160 }
161
162 return create_output(OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
163 }
164
166 auto threshold = get_parameter_or<double>("threshold", -40.0);
167 auto window_size = get_parameter_or<uint32_t>("window_size", 1024);
168 auto hop_size = get_parameter_or<uint32_t>("hop_size", 512);
169
170 double linear_threshold = std::pow(10.0, threshold / 20.0);
171
172 if (this->is_in_place()) {
173 auto [data_span, structure_info] = OperationHelper::extract_structured_double(input);
174
175 auto processor = [linear_threshold](Eigen::VectorXcd& spectrum, size_t) {
176 std::ranges::transform(spectrum, spectrum.begin(), [linear_threshold](const std::complex<double>& bin) {
177 return (std::abs(bin) > linear_threshold) ? bin : std::complex<double>(0.0, 0.0);
178 });
179 };
180
181 for (auto& span : data_span) {
182 auto result = process_spectral_windows(span, window_size, hop_size, processor);
183 std::ranges::copy(result, span.begin());
184 }
185
186 auto reconstructed_data = data_span
187 | std::views::transform([](const auto& span) {
188 return std::vector<double>(span.begin(), span.end());
189 })
190 | std::ranges::to<std::vector>();
191
192 return create_output(OperationHelper::reconstruct_from_double<InputType>(reconstructed_data, structure_info));
193 }
194
195 auto [target_data, structure_info] = OperationHelper::setup_operation_buffer(input, m_working_buffer);
196 auto processor = [linear_threshold](Eigen::VectorXcd& spectrum, size_t) {
197 std::ranges::transform(spectrum, spectrum.begin(), [linear_threshold](const std::complex<double>& bin) {
198 return (std::abs(bin) > linear_threshold) ? bin : std::complex<double>(0.0, 0.0);
199 });
200 };
201
202 for (size_t i = 0; i < target_data.size(); ++i) {
203 auto result = process_spectral_windows(target_data[i], window_size, hop_size, processor);
204 m_working_buffer[i] = std::move(result);
205 }
206
207 return create_output(OperationHelper::reconstruct_from_double<InputType>(m_working_buffer, structure_info));
208 }
209
210 default:
211 return create_output(input);
212 }
213 }
static std::tuple< std::vector< std::span< double > >, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from IO container or direct ComputeData with automatic container handl...
static auto setup_operation_buffer(T &input, std::vector< std::vector< double > > &working_buffer)
Setup operation buffer from IO or ComputeData type.
output_type create_output(const input_type &input)
Creates output with proper type conversion.
SpectralOperation m_operation
Current spectral operation.
std::vector< std::vector< double > > m_working_buffer
Buffer for out-of-place spectral operations.
virtual bool is_in_place() const
Indicates whether the transformation modifies the input data directly.
std::vector< double > process_spectral_windows(std::span< double > data, uint32_t window_size, uint32_t hop_size, ProcessorFunc &&processor)
Common spectral processing helper to eliminate code duplication.
@ SPECTRAL_FILTER
Filter frequency bands.
@ HARMONIC_ENHANCE
Enhance harmonics.
@ PITCH_SHIFT
Pitch-preserving shift.
@ FREQUENCY_SHIFT
Shift entire spectrum.
DataType transform_spectral_filter(DataType &input, double low_freq, double high_freq, double sample_rate=48000.0, uint32_t window_size=1024, uint32_t hop_size=256)
Spectral filtering using existing FFT infrastructure with C++20 ranges (IN-PLACE)
DataType transform_pitch_shift(DataType &input, double semitones, uint32_t window_size=1024, uint32_t hop_size=256)
Pitch shifting using existing FFT from AnalysisHelper with C++20 ranges (IN-PLACE)

References MayaFlux::Yantra::process_spectral_windows(), MayaFlux::Yantra::transform_pitch_shift(), and MayaFlux::Yantra::transform_spectral_filter().

+ Here is the call graph for this function: