13 Spectrum spectrum,
double base_decay)
14 : m_spectrum(spectrum)
15 , m_fundamental(fundamental)
25 double fundamental,
double base_decay)
27 , m_fundamental(fundamental)
42 std::vector<double> ratios;
43 ratios.reserve(
count);
48 for (
size_t i = 0; i <
count; ++i) {
49 ratios.push_back(
static_cast<double>(i + 1));
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);
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);
77 for (
size_t i = 0; i <
count; ++i) {
78 ratios.push_back(
static_cast<double>(i + 1));
96 for (
size_t i = 0; i < ratios.size(); ++i) {
99 mode.frequency_ratio = ratios[i];
101 mode.current_frequency = mode.base_frequency;
103 mode.decay_time = base_decay / ratios[i];
105 mode.initial_amplitude = 1.0 / double(i + 1);
106 mode.amplitude = 0.0;
108 mode.oscillator = std::make_shared<Generator::Sine>(
static_cast<float>(mode.current_frequency));
109 mode.oscillator->set_in_network(
true);
111 mode.decay_coefficient = std::exp(-1.0 / (base_decay *
m_sample_rate));
113 m_modes.push_back(std::move(mode));
120 mode.amplitude = 0.0;
122 mode.current_frequency = mode.base_frequency;
217 position = std::clamp(position, 0.0, 1.0);
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);
229 m_modes[i].amplitude =
m_modes[i].initial_amplitude * strength * spatial_amp;
235 if (distribution.size() !=
m_modes.size()) {
246 for (
size_t i = 0; i <
m_modes.size(); ++i) {
257 if (mode_a >=
m_modes.size() || mode_b >=
m_modes.size() || mode_a == mode_b) {
261 strength = std::clamp(strength, 0.0, 1.0);
265 m_couplings.push_back({ mode_a, mode_b, strength });
280 auto& mode_a =
m_modes[coupling.mode_a];
281 auto& mode_b =
m_modes[coupling.mode_b];
283 double energy_diff = (mode_a.amplitude - mode_b.amplitude) * coupling.strength;
285 mode_a.amplitude -= energy_diff * 0.5;
286 mode_b.amplitude += energy_diff * 0.5;
310 nb.reserve(num_samples);
312 for (
size_t i = 0; i < num_samples; ++i) {
319 for (
size_t m = 0; m <
m_modes.size(); ++m) {
322 if (mode.amplitude > 0.0001) {
323 mode.amplitude *= mode.decay_coefficient;
325 mode.amplitude = 0.0;
328 double sample = mode.oscillator->process_sample(0.0) * mode.amplitude;
346 double value = mapping.broadcast_source->get_last_output();
357 if (param ==
"frequency") {
359 }
else if (param ==
"decay") {
361 }
else if (param ==
"amplitude") {
363 mode.amplitude *= value;
365 }
else if (param ==
"scale") {
371 const std::string& param,
const std::shared_ptr<NodeNetwork>& source)
373 if (source->get_node_count() !=
m_modes.size()) {
377 if (param ==
"amplitude") {
378 for (
size_t i = 0; i <
m_modes.size(); ++i) {
379 auto val = source->get_node_output(i);
384 }
else if (param ==
"detune") {
385 for (
size_t i = 0; i <
m_modes.size(); ++i) {
386 auto val = source->get_node_output(i);
388 double detune_cents = *val * 100.0;
389 double ratio = std::pow(2.0, detune_cents / 1200.0);
391 m_modes[i].oscillator->set_frequency(
m_modes[i].current_frequency);
398 const std::shared_ptr<Node>& source,
413 const std::string& param_name,
414 const std::shared_ptr<NodeNetwork>& source_network)
430 [&](
const auto& m) {
return m.param_name == param_name; });
442 mode.amplitude = mode.initial_amplitude * strength;
448 if (mode_index <
m_modes.size()) {
449 m_modes[mode_index].amplitude =
m_modes[mode_index].initial_amplitude * strength;
456 mode.amplitude *= damping_factor;
466 mode.current_frequency = mode.base_frequency;
468 mode.oscillator->set_frequency(mode.current_frequency);
476std::unordered_map<std::string, std::string>
481 metadata[
"fundamental"] = std::to_string(
m_fundamental) +
" Hz";
482 metadata[
"spectrum"] = [
this]() {
498 double avg_amplitude = 0.0;
499 for (
const auto& mode :
m_modes) {
500 avg_amplitude += mode.amplitude;
502 avg_amplitude /= (double)
m_modes.size();
503 metadata[
"avg_amplitude"] = std::to_string(avg_amplitude);
505 metadata[
"exciter_type"] = [
this]() {
510 return "NOISE_BURST";
512 return "FILTERED_NOISE";
523 metadata[
"coupling_count"] = std::to_string(
m_couplings.size());
531 return m_modes[index].oscillator->get_last_output();
#define B(method_name, full_type_name)
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.
@ NOISE_BURST
Short white noise burst.
@ SAMPLE
User-provided excitation waveform.
ExciterType m_exciter_type
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 ¶m, 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 ¶m, double value)
Apply broadcast parameter to all modes.
double m_exciter_duration
void compute_mode_coupling()
Apply modal coupling energy transfer.
size_t m_exciter_sample_position
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< ModalNode > m_modes
std::shared_ptr< Node > m_exciter_node
void unmap_parameter(const std::string ¶m_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.
double m_decay_multiplier
void remove_mode_coupling(size_t mode_a, size_t mode_b)
Remove specific coupling.
void set_fundamental(double frequency)
Set base frequency (fundamental)
std::vector< double > m_exciter_sample
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 ¶m_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
size_t m_exciter_samples_remaining
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.
Represents a single resonant mode.
std::shared_ptr< Node > broadcast_source
std::shared_ptr< NodeNetwork > network_source