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

◆ process_batch()

void MayaFlux::Nodes::Network::ModalNetwork::process_batch ( unsigned int  num_samples)
overridevirtual

Process the network for the given number of samples.

Parameters
num_samplesNumber of samples/frames to process

Subclasses implement their specific processing logic:

  1. Update internal state (physics, relationships, etc.)
  2. Process individual nodes
  3. Apply inter-node interactions
  4. Aggregate outputs if needed

Called by NodeGraphManager during token processing.

Implements MayaFlux::Nodes::Network::NodeNetwork.

Definition at line 335 of file ModalNetwork.cpp.

336{
338
339 if (!is_enabled()) {
340 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
341 std::this_thread::yield();
342
343 m_last_audio_buffer.assign(num_samples, 0.0);
344 m_audio_buffer_lock.clear(std::memory_order_release);
345 return;
346 }
347
348 thread_local std::vector<double> scratch;
349 scratch.assign(num_samples, 0.0);
350
352
353 m_node_buffers.assign(m_modes.size(), {});
354 for (auto& nb : m_node_buffers)
355 nb.reserve(num_samples);
356
357 if (m_exciter_node) {
359 m_exciter_node_buffer_pos, num_samples);
360
363 if (!peaks.empty()) {
364 const double peak_scale = 1.0 / static_cast<double>(peaks.size());
365 for (size_t peak_idx : peaks) {
366 const double strength = std::abs(m_exciter_node_buffer[peak_idx])
367 * m_exciter_strength * peak_scale;
368 for (auto& mode : m_modes) {
369 const double target = mode.initial_amplitude * strength;
370 mode.amplitude = std::max(mode.amplitude, target);
371 }
372 }
373 }
376 if (m_exciter_active) {
378 if (!peaks.empty()) {
379 const auto max_peak = *std::ranges::max_element(peaks,
380 [this](size_t a, size_t b) {
381 return std::abs(m_exciter_node_buffer[a]) < std::abs(m_exciter_node_buffer[b]);
382 });
383 const double strength = std::abs(m_exciter_node_buffer[max_peak]) * m_exciter_strength;
384 for (auto& mode : m_modes) {
385 const double target = mode.initial_amplitude * strength;
386 mode.amplitude = std::max<double>(mode.amplitude, target);
387 }
388 }
389 }
390 }
391 }
392
393 for (size_t i = 0; i < num_samples; ++i) {
394 double exciter_signal = generate_exciter_sample();
395
396 if (exciter_signal != 0.0) {
398 for (auto& mode : m_modes) {
399 mode.amplitude += exciter_signal * mode.initial_amplitude
401 }
402 }
403 }
404
405 if (m_coupling_enabled && !m_couplings.empty())
407
408 for (size_t m = 0; m < m_modes.size(); ++m) {
409 auto& mode = m_modes[m];
410
411 if (mode.amplitude > 0.0001) {
412 mode.amplitude *= mode.decay_coefficient;
413 } else {
414 mode.amplitude = 0.0;
415 }
416
417 double sample = mode.oscillator->process_sample(0.0) * mode.amplitude;
418 m_node_buffers[m].push_back(sample);
419 scratch[i] += sample;
420 }
421 }
422
423 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
424 std::this_thread::yield();
425
426 m_last_audio_buffer.assign(scratch.begin(), scratch.end());
428 m_audio_buffer_lock.clear(std::memory_order_release);
429}
uint32_t mode
size_t a
size_t b
std::vector< std::vector< double > > m_node_buffers
Per-mode sample buffers populated each process_batch()
@ FILTERED_NOISE
Spectrally-shaped noise burst.
@ CONTINUOUS
External node as continuous exciter.
std::vector< ModeCoupling > m_couplings
void compute_mode_coupling()
Apply modal coupling energy transfer.
double generate_exciter_sample()
Generate exciter signal for current sample.
std::vector< double > m_exciter_node_buffer
void update_mapped_parameters()
Update mapped parameters before processing.
void apply_output_scale()
Apply m_output_scale to m_last_audio_buffer.
bool is_enabled() const
Check if network is enabled.
std::atomic_flag m_audio_buffer_lock
Spinlock guarding m_last_audio_buffer.
void ensure_initialized()
Ensure initialize() is called exactly once.
std::vector< double > m_last_audio_buffer
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
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.

References a, MayaFlux::Nodes::Network::NodeNetwork::apply_output_scale(), b, compute_mode_coupling(), CONTINUOUS, MayaFlux::Nodes::Network::NodeNetwork::ensure_initialized(), MayaFlux::Nodes::Network::extract_node_samples(), FILTERED_NOISE, generate_exciter_sample(), MayaFlux::Nodes::Network::NodeNetwork::is_enabled(), MayaFlux::Nodes::Network::NodeNetwork::m_audio_buffer_lock, m_coupling_enabled, m_couplings, m_exciter_active, m_exciter_node, m_exciter_node_buffer, m_exciter_node_buffer_pos, m_exciter_strength, m_exciter_type, MayaFlux::Nodes::Network::NodeNetwork::m_last_audio_buffer, m_modes, m_node_buffers, mode, NOISE_BURST, MayaFlux::Kinesis::Discrete::peak_positions(), and update_mapped_parameters().

+ Here is the call graph for this function: