MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ModalNetwork.cpp
Go to the documentation of this file.
1#include "ModalNetwork.hpp"
2
5
7
8//-----------------------------------------------------------------------------
9// Construction
10//-----------------------------------------------------------------------------
11
12ModalNetwork::ModalNetwork(size_t num_modes, double fundamental,
13 Spectrum spectrum, double base_decay)
14 : m_spectrum(spectrum)
15 , m_fundamental(fundamental)
16{
19
20 auto ratios = generate_spectrum_ratios(spectrum, num_modes);
21 initialize_modes(ratios, base_decay);
22}
23
24ModalNetwork::ModalNetwork(const std::vector<double>& frequency_ratios,
25 double fundamental, double base_decay)
26 : m_spectrum(Spectrum::CUSTOM)
27 , m_fundamental(fundamental)
28{
31
32 initialize_modes(frequency_ratios, base_decay);
33}
34
35//-----------------------------------------------------------------------------
36// Spectrum Generation
37//-----------------------------------------------------------------------------
38
40 size_t count)
41{
42 std::vector<double> ratios;
43 ratios.reserve(count);
44
45 switch (spectrum) {
47 // Perfect integer harmonics: 1, 2, 3, 4...
48 for (size_t i = 0; i < count; ++i) {
49 ratios.push_back(static_cast<double>(i + 1));
50 }
51 break;
52
54 // Bell-like spectrum (approximate mode ratios for circular plates)
55 // Based on Bessel function zeros
56 ratios = { 1.0, 2.756, 5.404, 8.933, 13.344, 18.64, 24.81, 31.86 };
57 while (ratios.size() < count) {
58 double last = ratios.back();
59 ratios.push_back(last + 6.8);
60 }
61 ratios.resize(count);
62 break;
63
65 // f_n = n * f_0 * sqrt(1 + B * n^2)
66 // Using small B = 0.0001 for moderate stretching
67 {
68 constexpr double B = 0.0001;
69 for (size_t n = 1; n <= count; ++n) {
70 double ratio = n * std::sqrt(1.0 + B * n * n);
71 ratios.push_back(ratio);
72 }
73 }
74 break;
75
77 for (size_t i = 0; i < count; ++i) {
78 ratios.push_back(static_cast<double>(i + 1));
79 }
80 break;
81 }
82
83 return ratios;
84}
85
86//-----------------------------------------------------------------------------
87// Mode Initialization
88//-----------------------------------------------------------------------------
89
90void ModalNetwork::initialize_modes(const std::vector<double>& ratios,
91 double base_decay)
92{
93 m_modes.clear();
94 m_modes.reserve(ratios.size());
95
96 for (size_t i = 0; i < ratios.size(); ++i) {
97 ModalNode mode;
98 mode.index = i;
99 mode.frequency_ratio = ratios[i];
100 mode.base_frequency = m_fundamental * ratios[i];
101 mode.current_frequency = mode.base_frequency;
102
103 mode.decay_time = base_decay / ratios[i];
104
105 mode.initial_amplitude = 1.0 / double(i + 1);
106 mode.amplitude = 0.0;
107
108 mode.oscillator = std::make_shared<Generator::Sine>(static_cast<float>(mode.current_frequency));
109 mode.oscillator->set_in_network(true);
110
111 mode.decay_coefficient = std::exp(-1.0 / (base_decay * m_sample_rate));
112
113 m_modes.push_back(std::move(mode));
114 }
115}
116
118{
119 for (auto& mode : m_modes) {
120 mode.amplitude = 0.0;
121 mode.phase = 0.0;
122 mode.current_frequency = mode.base_frequency;
123 }
124}
125
126//-----------------------------------------------------------------------------
127// Exciter System
128//-----------------------------------------------------------------------------
129
131{
132 m_exciter_duration = std::max(0.001, seconds);
133}
134
135void ModalNetwork::set_exciter_sample(const std::vector<double>& sample)
136{
137 m_exciter_sample = sample;
138}
139
140void ModalNetwork::initialize_exciter(double /*strength*/)
141{
142 m_exciter_active = true;
144
145 switch (m_exciter_type) {
148 break;
149
152 m_exciter_samples_remaining = static_cast<size_t>(
154 break;
155
158 break;
159
161 m_exciter_samples_remaining = std::numeric_limits<size_t>::max();
162 break;
163 }
164}
165
167{
169 m_exciter_active = false;
170 return 0.0;
171 }
172
174 double sample = 0.0;
175
176 switch (m_exciter_type) {
178 sample = 1.0;
179 break;
180
182 sample = m_random_generator(-1.0, 1.0);
183 break;
184
186 double noise = m_random_generator(-1.0, 1.0);
187 if (m_exciter_filter) {
188 sample = m_exciter_filter->process_sample(noise);
189 } else {
190 sample = noise;
191 }
192 break;
193 }
194
198 }
199 break;
200
202 if (m_exciter_node) {
203 sample = m_exciter_node->process_sample(0.0);
204 }
205 break;
206 }
207
208 return sample;
209}
210
211//-----------------------------------------------------------------------------
212// Spatial Excitation
213//-----------------------------------------------------------------------------
214
215void ModalNetwork::excite_at_position(double position, double strength)
216{
217 position = std::clamp(position, 0.0, 1.0);
218
219 if (m_spatial_distribution.empty()) {
221 }
222
223 initialize_exciter(strength);
224
225 for (size_t i = 0; i < m_modes.size(); ++i) {
226 double spatial_amp = std::sin((i + 1) * M_PI * position);
227 spatial_amp = std::abs(spatial_amp);
228
229 m_modes[i].amplitude = m_modes[i].initial_amplitude * strength * spatial_amp;
230 }
231}
232
233void ModalNetwork::set_spatial_distribution(const std::vector<double>& distribution)
234{
235 if (distribution.size() != m_modes.size()) {
236 return;
237 }
238 m_spatial_distribution = distribution;
239}
240
242{
244 m_spatial_distribution.reserve(m_modes.size());
245
246 for (size_t i = 0; i < m_modes.size(); ++i) {
247 m_spatial_distribution.push_back(1.0);
248 }
249}
250
251//-----------------------------------------------------------------------------
252// Modal Coupling
253//-----------------------------------------------------------------------------
254
255void ModalNetwork::set_mode_coupling(size_t mode_a, size_t mode_b, double strength)
256{
257 if (mode_a >= m_modes.size() || mode_b >= m_modes.size() || mode_a == mode_b) {
258 return;
259 }
260
261 strength = std::clamp(strength, 0.0, 1.0);
262
263 remove_mode_coupling(mode_a, mode_b);
264
265 m_couplings.push_back({ mode_a, mode_b, strength });
266}
267
268void ModalNetwork::remove_mode_coupling(size_t mode_a, size_t mode_b)
269{
270 std::erase_if(m_couplings,
271 [mode_a, mode_b](const ModeCoupling& c) {
272 return (c.mode_a == mode_a && c.mode_b == mode_b)
273 || (c.mode_a == mode_b && c.mode_b == mode_a);
274 });
275}
276
278{
279 for (const auto& coupling : m_couplings) {
280 auto& mode_a = m_modes[coupling.mode_a];
281 auto& mode_b = m_modes[coupling.mode_b];
282
283 double energy_diff = (mode_a.amplitude - mode_b.amplitude) * coupling.strength;
284
285 mode_a.amplitude -= energy_diff * 0.5;
286 mode_b.amplitude += energy_diff * 0.5;
287 }
288}
289
290//-----------------------------------------------------------------------------
291// Processing
292//-----------------------------------------------------------------------------
293
294void ModalNetwork::process_batch(unsigned int num_samples)
295{
297
298 m_last_audio_buffer.clear();
299
300 if (!is_enabled()) {
301 m_last_audio_buffer.assign(num_samples, 0.0);
302 return;
303 }
304
306 m_last_audio_buffer.reserve(num_samples);
307
308 m_node_buffers.assign(m_modes.size(), {});
309 for (auto& nb : m_node_buffers)
310 nb.reserve(num_samples);
311
312 for (size_t i = 0; i < num_samples; ++i) {
313 double exciter_signal = generate_exciter_sample();
314
315 if (m_coupling_enabled && !m_couplings.empty())
317
318 double sum = 0.0;
319 for (size_t m = 0; m < m_modes.size(); ++m) {
320 auto& mode = m_modes[m];
321
322 if (mode.amplitude > 0.0001) {
323 mode.amplitude *= mode.decay_coefficient;
324 } else {
325 mode.amplitude = 0.0;
326 }
327
328 double sample = mode.oscillator->process_sample(0.0) * mode.amplitude;
329 m_node_buffers[m].push_back(sample);
330 sum += sample;
331 }
332 m_last_audio_buffer.push_back(sum);
333 }
334
336}
337
338//-----------------------------------------------------------------------------
339// Parameter Mapping
340//-----------------------------------------------------------------------------
341
343{
344 for (const auto& mapping : m_parameter_mappings) {
345 if (mapping.mode == MappingMode::BROADCAST && mapping.broadcast_source) {
346 double value = mapping.broadcast_source->get_last_output();
347 apply_broadcast_parameter(mapping.param_name, value);
348 } else if (mapping.mode == MappingMode::ONE_TO_ONE && mapping.network_source) {
349 apply_one_to_one_parameter(mapping.param_name, mapping.network_source);
350 }
351 }
352}
353
354void ModalNetwork::apply_broadcast_parameter(const std::string& param,
355 double value)
356{
357 if (param == "frequency") {
358 set_fundamental(value);
359 } else if (param == "decay") {
360 m_decay_multiplier = std::max(0.01, value);
361 } else if (param == "amplitude") {
362 for (auto& mode : m_modes) {
363 mode.amplitude *= value;
364 }
365 } else if (param == "scale") {
366 m_output_scale = std::max(0.0, value);
367 }
368}
369
371 const std::string& param, const std::shared_ptr<NodeNetwork>& source)
372{
373 if (source->get_node_count() != m_modes.size()) {
374 return;
375 }
376
377 if (param == "amplitude") {
378 for (size_t i = 0; i < m_modes.size(); ++i) {
379 auto val = source->get_node_output(i);
380 if (val) {
381 m_modes[i].amplitude *= *val;
382 }
383 }
384 } else if (param == "detune") {
385 for (size_t i = 0; i < m_modes.size(); ++i) {
386 auto val = source->get_node_output(i);
387 if (val) {
388 double detune_cents = *val * 100.0; // ±100 cents
389 double ratio = std::pow(2.0, detune_cents / 1200.0);
390 m_modes[i].current_frequency = m_modes[i].base_frequency * ratio;
391 m_modes[i].oscillator->set_frequency(m_modes[i].current_frequency);
392 }
393 }
394 }
395}
396
397void ModalNetwork::map_parameter(const std::string& param_name,
398 const std::shared_ptr<Node>& source,
399 MappingMode mode)
400{
401 unmap_parameter(param_name);
402
403 ParameterMapping mapping;
404 mapping.param_name = param_name;
405 mapping.mode = mode;
406 mapping.broadcast_source = source;
407 mapping.network_source = nullptr;
408
409 m_parameter_mappings.push_back(std::move(mapping));
410}
411
413 const std::string& param_name,
414 const std::shared_ptr<NodeNetwork>& source_network)
415{
416 unmap_parameter(param_name);
417
418 ParameterMapping mapping;
419 mapping.param_name = param_name;
421 mapping.broadcast_source = nullptr;
422 mapping.network_source = source_network;
423
424 m_parameter_mappings.push_back(std::move(mapping));
425}
426
427void ModalNetwork::unmap_parameter(const std::string& param_name)
428{
429 std::erase_if(m_parameter_mappings,
430 [&](const auto& m) { return m.param_name == param_name; });
431}
432
433//-----------------------------------------------------------------------------
434// Modal Control
435//-----------------------------------------------------------------------------
436
437void ModalNetwork::excite(double strength)
438{
439 initialize_exciter(strength);
440
441 for (auto& mode : m_modes) {
442 mode.amplitude = mode.initial_amplitude * strength;
443 }
444}
445
446void ModalNetwork::excite_mode(size_t mode_index, double strength)
447{
448 if (mode_index < m_modes.size()) {
449 m_modes[mode_index].amplitude = m_modes[mode_index].initial_amplitude * strength;
450 }
451}
452
453void ModalNetwork::damp(double damping_factor)
454{
455 for (auto& mode : m_modes) {
456 mode.amplitude *= damping_factor;
457 }
458}
459
461{
463
464 for (auto& mode : m_modes) {
465 mode.base_frequency = m_fundamental * mode.frequency_ratio;
466 mode.current_frequency = mode.base_frequency;
467
468 mode.oscillator->set_frequency(mode.current_frequency);
469 }
470}
471
472//-----------------------------------------------------------------------------
473// Metadata
474//-----------------------------------------------------------------------------
475
476std::unordered_map<std::string, std::string>
478{
479 auto metadata = NodeNetwork::get_metadata();
480
481 metadata["fundamental"] = std::to_string(m_fundamental) + " Hz";
482 metadata["spectrum"] = [this]() {
483 switch (m_spectrum) {
485 return "HARMONIC";
487 return "INHARMONIC";
489 return "STRETCHED";
490 case Spectrum::CUSTOM:
491 return "CUSTOM";
492 default:
493 return "UNKNOWN";
494 }
495 }();
496 metadata["decay_multiplier"] = std::to_string(m_decay_multiplier);
497
498 double avg_amplitude = 0.0;
499 for (const auto& mode : m_modes) {
500 avg_amplitude += mode.amplitude;
501 }
502 avg_amplitude /= (double)m_modes.size();
503 metadata["avg_amplitude"] = std::to_string(avg_amplitude);
504
505 metadata["exciter_type"] = [this]() {
506 switch (m_exciter_type) {
508 return "IMPULSE";
510 return "NOISE_BURST";
512 return "FILTERED_NOISE";
514 return "SAMPLE";
516 return "CONTINUOUS";
517 default:
518 return "UNKNOWN";
519 }
520 }();
521
522 metadata["coupling_enabled"] = m_coupling_enabled ? "true" : "false";
523 metadata["coupling_count"] = std::to_string(m_couplings.size());
524
525 return metadata;
526}
527
528[[nodiscard]] std::optional<double> ModalNetwork::get_node_output(size_t index) const
529{
530 if (m_modes.size() > index) {
531 return m_modes[index].oscillator->get_last_output();
532 }
533 return std::nullopt;
534}
535
536std::optional<std::span<const double>> ModalNetwork::get_node_audio_buffer(size_t index) const
537{
538 if (index >= m_node_buffers.size() || m_node_buffers[index].empty())
539 return std::nullopt;
540 return std::span<const double>(m_node_buffers[index]);
541}
542
543} // namespace MayaFlux::Nodes::Network
#define B(method_name, full_type_name)
Definition Creator.hpp:203
Eigen::Index count
double frequency
std::shared_ptr< Filters::Filter > m_exciter_filter
void compute_spatial_distribution()
Compute spatial amplitude distribution.
std::optional< double > get_node_output(size_t index) const override
Get output of specific internal node (for ONE_TO_ONE mapping)
std::vector< std::vector< double > > m_node_buffers
Per-mode sample buffers populated each process_batch()
@ IMPULSE
Single-sample Dirac impulse (default)
@ FILTERED_NOISE
Spectrally-shaped noise burst.
@ CONTINUOUS
External node as continuous exciter.
@ SAMPLE
User-provided excitation waveform.
void process_batch(unsigned int num_samples) override
Process the network for the given number of samples.
void apply_one_to_one_parameter(const std::string &param, const std::shared_ptr< NodeNetwork > &source)
Apply one-to-one parameter from another network.
Kinesis::Stochastic::Stochastic m_random_generator
void excite_mode(size_t mode_index, double strength=1.0)
Excite specific mode.
void excite(double strength=1.0)
Excite all modes (strike/pluck)
void initialize_modes(const std::vector< double > &ratios, double base_decay)
Initialize modes with given frequency ratios.
std::optional< std::span< const double > > get_node_audio_buffer(size_t index) const override
Get output of specific internal node as audio buffer (for ONE_TO_ONE mapping)
std::vector< ModeCoupling > m_couplings
void reset() override
Reset network to initial state.
void apply_broadcast_parameter(const std::string &param, double value)
Apply broadcast parameter to all modes.
void compute_mode_coupling()
Apply modal coupling energy transfer.
double generate_exciter_sample()
Generate exciter signal for current sample.
void damp(double damping_factor=0.1)
Damp all modes (rapidly reduce amplitude)
std::unordered_map< std::string, std::string > get_metadata() const override
Get network metadata for debugging/visualization.
void unmap_parameter(const std::string &param_name) override
Remove parameter mapping.
void set_mode_coupling(size_t mode_a, size_t mode_b, double strength)
Define bidirectional coupling between two modes.
void remove_mode_coupling(size_t mode_a, size_t mode_b)
Remove specific coupling.
void set_fundamental(double frequency)
Set base frequency (fundamental)
void initialize_exciter(double strength)
Initialize exciter for new excitation event.
void excite_at_position(double position, double strength=1.0)
Excite modes based on normalized strike position.
void map_parameter(const std::string &param_name, const std::shared_ptr< Node > &source, MappingMode mode=MappingMode::BROADCAST) override
Map external node output to network parameter.
Spectrum
Predefined frequency relationship patterns.
@ STRETCHED
Piano-like stiffness: f, 2.01f, 3.02f, 4.04f...
@ INHARMONIC
Bell-like: f, 2.76f, 5.40f, 8.93f, 13.34f...
@ CUSTOM
User-provided frequency ratios.
@ HARMONIC
Integer harmonics: f, 2f, 3f, 4f...
void update_mapped_parameters()
Update mapped parameters before processing.
static std::vector< double > generate_spectrum_ratios(Spectrum spectrum, size_t count)
Generate frequency ratios for predefined spectra.
void set_exciter_sample(const std::vector< double > &sample)
Set custom excitation sample.
std::vector< double > m_spatial_distribution
ModalNetwork(size_t num_modes, double fundamental=220.0, Spectrum spectrum=Spectrum::HARMONIC, double base_decay=1.0)
Create modal network with predefined spectrum.
void set_exciter_duration(double seconds)
Set noise burst duration.
void set_spatial_distribution(const std::vector< double > &distribution)
Set custom spatial amplitude distribution.
std::vector< ParameterMapping > m_parameter_mappings
void apply_output_scale()
Apply m_output_scale to m_last_audio_buffer.
virtual void set_topology(Topology topology)
Set the network's topology.
bool is_enabled() const
Check if network is enabled.
double m_output_scale
Post-processing scalar applied to m_last_audio_buffer each batch.
void ensure_initialized()
Ensure initialize() is called exactly once.
virtual std::unordered_map< std::string, std::string > get_metadata() const
Get network metadata for debugging/visualization.
std::vector< double > m_last_audio_buffer
void set_output_mode(OutputMode mode)
Set the network's output routing mode.
@ INDEPENDENT
No connections, nodes process independently.
@ CUSTOM
User-defined arbitrary topology.
MappingMode
Defines how nodes map to external entities (e.g., audio channels, graphics objects)
@ ONE_TO_ONE
Node array/network → network nodes (must match count)
@ BROADCAST
One node → all network nodes.
@ AUDIO_SINK
Aggregated audio samples sent to output.