MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ModalNetwork.cpp
Go to the documentation of this file.
1#include "ModalNetwork.hpp"
2
6
8
9//-----------------------------------------------------------------------------
10// Construction
11//-----------------------------------------------------------------------------
12
13ModalNetwork::ModalNetwork(size_t num_modes, double fundamental,
14 Spectrum spectrum, double base_decay)
15 : m_spectrum(spectrum)
16 , m_fundamental(fundamental)
17{
20
21 auto ratios = generate_spectrum_ratios(spectrum, num_modes);
22 initialize_modes(ratios, base_decay);
23}
24
25ModalNetwork::ModalNetwork(const std::vector<double>& frequency_ratios,
26 double fundamental, double base_decay)
27 : m_spectrum(Spectrum::CUSTOM)
28 , m_fundamental(fundamental)
29{
32
33 initialize_modes(frequency_ratios, base_decay);
34}
35
36//-----------------------------------------------------------------------------
37// Spectrum Generation
38//-----------------------------------------------------------------------------
39
41 size_t count)
42{
43 std::vector<double> ratios;
44 ratios.reserve(count);
45
46 switch (spectrum) {
48 // Perfect integer harmonics: 1, 2, 3, 4...
49 for (size_t i = 0; i < count; ++i) {
50 ratios.push_back(static_cast<double>(i + 1));
51 }
52 break;
53
55 // Bell-like spectrum (approximate mode ratios for circular plates)
56 // Based on Bessel function zeros
57 ratios = { 1.0, 2.756, 5.404, 8.933, 13.344, 18.64, 24.81, 31.86 };
58 while (ratios.size() < count) {
59 double last = ratios.back();
60 ratios.push_back(last + 6.8);
61 }
62 ratios.resize(count);
63 break;
64
66 // f_n = n * f_0 * sqrt(1 + B * n^2)
67 // Using small B = 0.0001 for moderate stretching
68 {
69 constexpr double B = 0.0001;
70 for (size_t n = 1; n <= count; ++n) {
71 auto dn = static_cast<double>(n);
72 double ratio = dn * std::sqrt(1.0 + B * dn * dn);
73 ratios.push_back(ratio);
74 }
75 }
76 break;
77
79 for (size_t i = 0; i < count; ++i) {
80 ratios.push_back(static_cast<double>(i + 1));
81 }
82 break;
83 }
84
85 return ratios;
86}
87
88//-----------------------------------------------------------------------------
89// Mode Initialization
90//-----------------------------------------------------------------------------
91
92void ModalNetwork::initialize_modes(const std::vector<double>& ratios,
93 double base_decay)
94{
95 m_modes.clear();
96 m_modes.reserve(ratios.size());
97
98 for (size_t i = 0; i < ratios.size(); ++i) {
99 ModalNode mode;
100 mode.index = i;
101 mode.frequency_ratio = ratios[i];
102 mode.base_frequency = m_fundamental * ratios[i];
103 mode.current_frequency = mode.base_frequency;
104
105 mode.decay_time = base_decay / ratios[i];
106
107 mode.initial_amplitude = 1.0 / double(i + 1);
108 mode.amplitude = 0.0;
109
110 mode.oscillator = std::make_shared<Generator::Sine>(static_cast<float>(mode.current_frequency));
111 mode.oscillator->set_in_network(true);
112
113 mode.decay_coefficient = std::exp(-1.0 / (base_decay * m_sample_rate));
114
115 m_modes.push_back(std::move(mode));
116 }
117}
118
120{
121 for (auto& mode : m_modes) {
122 mode.amplitude = 0.0;
123 mode.phase = 0.0;
124 mode.current_frequency = mode.base_frequency;
125 }
126}
127
128//-----------------------------------------------------------------------------
129// Exciter System
130//-----------------------------------------------------------------------------
131
133{
134 m_exciter_duration = std::max(0.001, seconds);
135}
136
138{
139 m_exciter_type = type;
140 m_exciter_active = false;
142 for (auto& mode : m_modes)
143 mode.amplitude = 0.0;
144}
145
146void ModalNetwork::set_exciter_sample(const std::vector<double>& sample)
147{
148 m_exciter_sample = sample;
149}
150
152{
153 m_exciter_active = true;
154 m_exciter_strength = strength;
156
157 switch (m_exciter_type) {
160 break;
161
164 m_exciter_samples_remaining = static_cast<size_t>(
166 break;
167
170 break;
171
173 m_exciter_samples_remaining = std::numeric_limits<size_t>::max();
174 break;
175 }
176}
177
179{
181 m_exciter_active = false;
182 return 0.0;
183 }
184
185 const size_t idx = m_exciter_node_buffer_pos < m_exciter_node_buffer.size()
187 : 0;
189 double sample = 0.0;
190
191 switch (m_exciter_type) {
193 sample = 1.0;
194 break;
195
197 sample = m_random_generator(-1.0, 1.0);
198 break;
199
201 double noise = m_random_generator(-1.0, 1.0);
202 sample = m_exciter_filter
203 ? m_exciter_filter->process_sample(noise)
204 : noise;
205 break;
206 }
207
211 break;
212
214 if (m_exciter_node)
215 sample = m_exciter_node_buffer[idx];
216 break;
217 }
218
219 return sample;
220}
221
227
228//-----------------------------------------------------------------------------
229// Spatial Excitation
230//-----------------------------------------------------------------------------
231
232void ModalNetwork::excite_at_position(double position, double strength)
233{
234 position = std::clamp(position, 0.0, 1.0);
235
236 if (m_spatial_distribution.empty()) {
238 }
239
240 initialize_exciter(strength);
241
242 for (size_t i = 0; i < m_modes.size(); ++i) {
243 double spatial_amp = std::sin((i + 1) * M_PI * position);
244 spatial_amp = std::abs(spatial_amp);
245
246 m_modes[i].amplitude = m_modes[i].initial_amplitude * strength * spatial_amp;
247 }
248}
249
250void ModalNetwork::set_spatial_distribution(const std::vector<double>& distribution)
251{
252 if (distribution.size() != m_modes.size()) {
253 return;
254 }
255 m_spatial_distribution = distribution;
256}
257
259{
261 m_spatial_distribution.reserve(m_modes.size());
262
263 for (size_t i = 0; i < m_modes.size(); ++i) {
264 m_spatial_distribution.push_back(1.0);
265 }
266}
267
268//-----------------------------------------------------------------------------
269// Modal Coupling
270//-----------------------------------------------------------------------------
271
272void ModalNetwork::set_mode_coupling(size_t mode_a, size_t mode_b, double strength)
273{
274 if (mode_a >= m_modes.size() || mode_b >= m_modes.size() || mode_a == mode_b) {
275 return;
276 }
277
278 strength = std::clamp(strength, 0.0, 1.0);
279
280 remove_mode_coupling(mode_a, mode_b);
281
282 m_couplings.push_back({ .mode_a = mode_a,
283 .mode_b = mode_b,
284 .strength = strength });
285}
286
287void ModalNetwork::remove_mode_coupling(size_t mode_a, size_t mode_b)
288{
289 std::erase_if(m_couplings,
290 [mode_a, mode_b](const ModeCoupling& c) {
291 return (c.mode_a == mode_a && c.mode_b == mode_b)
292 || (c.mode_a == mode_b && c.mode_b == mode_a);
293 });
294}
295
297{
298 for (const auto& coupling : m_couplings) {
299 auto& mode_a = m_modes[coupling.mode_a];
300 auto& mode_b = m_modes[coupling.mode_b];
301
302 double energy_diff = (mode_a.amplitude - mode_b.amplitude) * coupling.strength;
303
304 mode_a.amplitude -= energy_diff * 0.5;
305 mode_b.amplitude += energy_diff * 0.5;
306 }
307}
308
309//-----------------------------------------------------------------------------
310// Processing
311//-----------------------------------------------------------------------------
312
313void ModalNetwork::process_batch(unsigned int num_samples)
314{
316
317 if (!is_enabled()) {
318 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
319 std::this_thread::yield();
320
321 m_last_audio_buffer.assign(num_samples, 0.0);
322 m_audio_buffer_lock.clear(std::memory_order_release);
323 return;
324 }
325
326 thread_local std::vector<double> scratch;
327 scratch.assign(num_samples, 0.0);
328
330
331 m_node_buffers.assign(m_modes.size(), {});
332 for (auto& nb : m_node_buffers)
333 nb.reserve(num_samples);
334
335 if (m_exciter_node) {
337 m_exciter_node_buffer_pos, num_samples);
338
341 if (!peaks.empty()) {
342 const double peak_scale = 1.0 / static_cast<double>(peaks.size());
343 for (size_t peak_idx : peaks) {
344 const double strength = std::abs(m_exciter_node_buffer[peak_idx])
345 * m_exciter_strength * peak_scale;
346 for (auto& mode : m_modes) {
347 const double target = mode.initial_amplitude * strength;
348 mode.amplitude = std::max(mode.amplitude, target);
349 }
350 }
351 }
354 if (m_exciter_active) {
356 if (!peaks.empty()) {
357 const auto max_peak = *std::ranges::max_element(peaks,
358 [this](size_t a, size_t b) {
359 return std::abs(m_exciter_node_buffer[a]) < std::abs(m_exciter_node_buffer[b]);
360 });
361 const double strength = std::abs<double>(m_exciter_node_buffer[max_peak]) * m_exciter_strength;
362 for (auto& mode : m_modes) {
363 const double target = mode.initial_amplitude * strength;
364 mode.amplitude = std::max<double>(mode.amplitude, target);
365 }
366 }
367 }
368 }
369 }
370
371 for (size_t i = 0; i < num_samples; ++i) {
372 double exciter_signal = generate_exciter_sample();
373
374 if (exciter_signal != 0.0) {
376 for (auto& mode : m_modes) {
377 mode.amplitude += exciter_signal * mode.initial_amplitude
379 }
380 }
381 }
382
383 if (m_coupling_enabled && !m_couplings.empty())
385
386 for (size_t m = 0; m < m_modes.size(); ++m) {
387 auto& mode = m_modes[m];
388
389 if (mode.amplitude > 0.0001) {
390 mode.amplitude *= mode.decay_coefficient;
391 } else {
392 mode.amplitude = 0.0;
393 }
394
395 double sample = mode.oscillator->process_sample(0.0) * mode.amplitude;
396 m_node_buffers[m].push_back(sample);
397 scratch[i] += sample;
398 }
399 }
400
401 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
402 std::this_thread::yield();
403
404 m_last_audio_buffer.assign(scratch.begin(), scratch.end());
406 m_audio_buffer_lock.clear(std::memory_order_release);
407}
408
409//-----------------------------------------------------------------------------
410// Parameter Mapping
411//-----------------------------------------------------------------------------
412
414{
415 for (const auto& mapping : m_parameter_mappings) {
416 if (mapping.mode == MappingMode::BROADCAST && mapping.broadcast_source) {
417 double value = mapping.broadcast_source->get_last_output();
418 apply_broadcast_parameter(mapping.param_name, value);
419 } else if (mapping.mode == MappingMode::ONE_TO_ONE && mapping.network_source) {
420 apply_one_to_one_parameter(mapping.param_name, mapping.network_source);
421 }
422 }
423}
424
425void ModalNetwork::apply_broadcast_parameter(const std::string& param,
426 double value)
427{
428 if (param == "frequency") {
429 set_fundamental(value);
430 } else if (param == "decay") {
431 m_decay_multiplier = std::max(0.01, value);
432 } else if (param == "amplitude") {
433 for (auto& mode : m_modes) {
434 mode.amplitude *= value;
435 }
436 } else if (param == "scale") {
437 m_output_scale = std::max(0.0, value);
438 }
439}
440
442 const std::string& param, const std::shared_ptr<NodeNetwork>& source)
443{
444 if (source->get_node_count() != m_modes.size()) {
445 return;
446 }
447
448 if (param == "amplitude") {
449 for (size_t i = 0; i < m_modes.size(); ++i) {
450 auto val = source->get_node_output(i);
451 if (val) {
452 m_modes[i].amplitude *= *val;
453 }
454 }
455 } else if (param == "detune") {
456 for (size_t i = 0; i < m_modes.size(); ++i) {
457 auto val = source->get_node_output(i);
458 if (val) {
459 double detune_cents = *val * 100.0; // ±100 cents
460 double ratio = std::pow(2.0, detune_cents / 1200.0);
461 m_modes[i].current_frequency = m_modes[i].base_frequency * ratio;
462 m_modes[i].oscillator->set_frequency(m_modes[i].current_frequency);
463 }
464 }
465 }
466}
467
468void ModalNetwork::map_parameter(const std::string& param_name,
469 const std::shared_ptr<Node>& source,
470 MappingMode mode)
471{
472 unmap_parameter(param_name);
473
474 ParameterMapping mapping;
475 mapping.param_name = param_name;
476 mapping.mode = mode;
477 mapping.broadcast_source = source;
478 mapping.network_source = nullptr;
479
480 m_parameter_mappings.push_back(std::move(mapping));
481}
482
484 const std::string& param_name,
485 const std::shared_ptr<NodeNetwork>& source_network)
486{
487 unmap_parameter(param_name);
488
489 ParameterMapping mapping;
490 mapping.param_name = param_name;
492 mapping.broadcast_source = nullptr;
493 mapping.network_source = source_network;
494
495 m_parameter_mappings.push_back(std::move(mapping));
496}
497
498void ModalNetwork::unmap_parameter(const std::string& param_name)
499{
500 std::erase_if(m_parameter_mappings,
501 [&](const auto& m) { return m.param_name == param_name; });
502}
503
504//-----------------------------------------------------------------------------
505// Modal Control
506//-----------------------------------------------------------------------------
507
508void ModalNetwork::excite(double strength)
509{
510 initialize_exciter(strength);
511
512 for (auto& mode : m_modes) {
513 mode.amplitude = mode.initial_amplitude * strength;
514 }
515}
516
517void ModalNetwork::excite_mode(size_t mode_index, double strength)
518{
519 if (mode_index < m_modes.size()) {
520 m_modes[mode_index].amplitude = m_modes[mode_index].initial_amplitude * strength;
521 }
522}
523
524void ModalNetwork::damp(double damping_factor)
525{
526 for (auto& mode : m_modes) {
527 mode.amplitude *= damping_factor;
528 }
529}
530
532{
534
535 for (auto& mode : m_modes) {
536 mode.base_frequency = m_fundamental * mode.frequency_ratio;
537 mode.current_frequency = mode.base_frequency;
538
539 mode.oscillator->set_frequency(mode.current_frequency);
540 }
541}
542
543//-----------------------------------------------------------------------------
544// Metadata
545//-----------------------------------------------------------------------------
546
547std::unordered_map<std::string, std::string>
549{
550 auto metadata = NodeNetwork::get_metadata();
551
552 metadata["fundamental"] = std::to_string(m_fundamental) + " Hz";
553 metadata["spectrum"] = [this]() {
554 switch (m_spectrum) {
556 return "HARMONIC";
558 return "INHARMONIC";
560 return "STRETCHED";
561 case Spectrum::CUSTOM:
562 return "CUSTOM";
563 default:
564 return "UNKNOWN";
565 }
566 }();
567 metadata["decay_multiplier"] = std::to_string(m_decay_multiplier);
568
569 double avg_amplitude = 0.0;
570 for (const auto& mode : m_modes) {
571 avg_amplitude += mode.amplitude;
572 }
573 avg_amplitude /= (double)m_modes.size();
574 metadata["avg_amplitude"] = std::to_string(avg_amplitude);
575
576 metadata["exciter_type"] = [this]() {
577 switch (m_exciter_type) {
579 return "IMPULSE";
581 return "NOISE_BURST";
583 return "FILTERED_NOISE";
585 return "SAMPLE";
587 return "CONTINUOUS";
588 default:
589 return "UNKNOWN";
590 }
591 }();
592
593 metadata["coupling_enabled"] = m_coupling_enabled ? "true" : "false";
594 metadata["coupling_count"] = std::to_string(m_couplings.size());
595
596 return metadata;
597}
598
599[[nodiscard]] std::optional<double> ModalNetwork::get_node_output(size_t index) const
600{
601 if (m_modes.size() > index) {
602 return m_modes[index].oscillator->get_last_output();
603 }
604 return std::nullopt;
605}
606
607std::optional<std::span<const double>> ModalNetwork::get_node_audio_buffer(size_t index) const
608{
609 if (index >= m_node_buffers.size() || m_node_buffers[index].empty())
610 return std::nullopt;
611 return std::span<const double>(m_node_buffers[index]);
612}
613
614} // namespace MayaFlux::Nodes::Network
Discrete sequence analysis primitives for MayaFlux::Kinesis.
#define B(method_name, full_type_name)
Definition Creator.hpp:130
size_t a
size_t b
double frequency
size_t count
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()
ExciterType
Excitation signal types for modal synthesis.
@ 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.
void release()
Release continuous excitation and allow modes to decay naturally.
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 set_exciter_type(ExciterType type)
Set exciter type.
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.
std::vector< double > m_exciter_node_buffer
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.
static void extract_node_samples(const std::shared_ptr< Nodes::Node > &node, std::vector< double > &buffer, size_t &buffer_pos, size_t num_samples)
Extract num_samples from node into buffer using snapshot guard.
bool is_enabled() const
Check if network is enabled.
double m_output_scale
Post-processing scalar applied to m_last_audio_buffer each batch.
std::atomic_flag m_audio_buffer_lock
Spinlock guarding m_last_audio_buffer.
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.
std::vector< size_t > peak_positions(std::span< const double > data, double threshold, size_t min_distance)
Sample indices of local peak maxima in the full span.
Definition Analysis.cpp:669
@ 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.