MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WaveguideNetwork.cpp
Go to the documentation of this file.
2
6
8
9//-----------------------------------------------------------------------------
10// Construction
11//-----------------------------------------------------------------------------
12
14 WaveguideType type,
15 double fundamental_freq,
16 double sample_rate)
17 : m_type(type)
18 , m_fundamental(fundamental_freq)
19{
20 m_sample_rate = static_cast<uint32_t>(sample_rate);
23
25
26 const auto prop_mode = (m_type == WaveguideType::TUBE)
29
30 m_segments.emplace_back(m_delay_length_integer + 2, prop_mode);
31
33}
34
35//-----------------------------------------------------------------------------
36// Initialization
37//-----------------------------------------------------------------------------
38
43
45{
46 for (auto& seg : m_segments) {
47 const auto len = seg.p_plus.capacity();
48 seg.p_plus = Memory::HistoryBuffer<double>(len);
49 seg.p_minus = Memory::HistoryBuffer<double>(len);
50 }
51
52 m_exciter_active = false;
55 m_last_output = 0.0;
56 m_last_audio_buffer.clear();
57}
58
59//-----------------------------------------------------------------------------
60// Delay Length Computation
61//-----------------------------------------------------------------------------
62
64{
65 double total_delay = static_cast<double>(m_sample_rate) / m_fundamental;
66
67 total_delay -= 0.5;
68
69 m_delay_length_integer = static_cast<size_t>(total_delay);
70 m_delay_length_fraction = total_delay - static_cast<double>(m_delay_length_integer);
71
73}
74
75//-----------------------------------------------------------------------------
76// Default Loop Filter
77//-----------------------------------------------------------------------------
78
80{
81 auto filter = std::make_shared<Filters::FIR>(
82 std::vector<double> { 0.5, 0.5 });
83
84 if (!m_segments.empty()) {
85 m_segments[0].loop_filter = filter;
86 }
87}
88
89//-----------------------------------------------------------------------------
90// Fractional Delay Interpolation
91//-----------------------------------------------------------------------------
92
95 size_t integer_part,
96 double fraction) const
97{
98 double s0 = delay[integer_part];
99 double s1 = delay[integer_part + 1];
100 return s0 + fraction * (s1 - s0);
101}
102
103//-----------------------------------------------------------------------------
104// Processing
105//-----------------------------------------------------------------------------
106
108{
110 return seg.p_plus[m_pickup_sample];
111 }
112
113 const double plus = seg.p_plus[m_pickup_sample];
114 const double minus = seg.p_minus[m_pickup_sample];
115
117 return plus + minus;
118
119 return plus - minus;
120}
121
122void WaveguideNetwork::process_batch(unsigned int num_samples)
123{
125 m_last_audio_buffer.clear();
126
127 if (!is_enabled() || m_segments.empty()) {
128 m_last_audio_buffer.assign(num_samples, 0.0);
129 m_last_output = 0.0;
130 return;
131 }
132
134 m_last_audio_buffer.reserve(num_samples);
135
136 auto& seg = m_segments[0];
137
139 process_unidirectional(seg, num_samples);
140 } else {
141 process_bidirectional(seg, num_samples);
142 }
143
146}
147
149 unsigned int num_samples)
150{
151 for (unsigned int i = 0; i < num_samples; ++i) {
152 const double exciter = generate_exciter_sample();
153
154 const double delayed = read_with_interpolation(
156
157 const double filtered = seg.loop_filter ? seg.loop_filter->process_sample(delayed)
158 : delayed;
159
160 seg.p_plus.push(
161 exciter + filtered * seg.loss_factor * seg.reflection_closed);
162
163 m_last_audio_buffer.push_back(observe_sample(seg));
164 }
165}
166
168 unsigned int num_samples)
169{
170 for (unsigned int i = 0; i < num_samples; ++i) {
171 const double exciter = generate_exciter_sample();
172
173 const double plus_end = read_with_interpolation(
175
176 const double minus_end = read_with_interpolation(
178
179 auto* filt_open = seg.loop_filter_open ? seg.loop_filter_open.get()
180 : seg.loop_filter.get();
181 auto* filt_closed = seg.loop_filter_closed ? seg.loop_filter_closed.get()
182 : seg.loop_filter.get();
183
184 const double filtered_plus = filt_open ? filt_open->process_sample(plus_end)
185 : plus_end;
186 const double filtered_minus = filt_closed ? filt_closed->process_sample(minus_end)
187 : minus_end;
188
189 seg.p_minus.push(filtered_plus * seg.loss_factor * seg.reflection_open);
190 seg.p_plus.push(exciter + filtered_minus * seg.loss_factor * seg.reflection_closed);
191
192 m_last_audio_buffer.push_back(observe_sample(seg));
193 }
194}
195
196//-----------------------------------------------------------------------------
197// Excitation
198//-----------------------------------------------------------------------------
199
200void WaveguideNetwork::pluck(double position, double strength)
201{
202 position = std::clamp(position, 0.01, 0.99);
203
204 if (m_segments.empty()) {
205 return;
206 }
207
208 auto& seg = m_segments[0];
209 const size_t len = m_delay_length_integer;
210 const auto pluck_sample = static_cast<size_t>(position * static_cast<double>(len));
211
212 seg.p_plus = Memory::HistoryBuffer<double>(seg.p_plus.capacity());
213 seg.p_minus = Memory::HistoryBuffer<double>(seg.p_minus.capacity());
214
215 for (size_t s = 0; s < len; ++s) {
216 double value = 0.0;
217 if (s <= pluck_sample) {
218 value = strength * static_cast<double>(s)
219 / static_cast<double>(pluck_sample);
220 } else {
221 value = strength * static_cast<double>(len - s)
222 / static_cast<double>(len - pluck_sample);
223 }
224 seg.p_plus.push(value);
225 }
226
227 m_exciter_active = false;
229}
230
231void WaveguideNetwork::strike(double position, double strength)
232{
233 position = std::clamp(position, 0.01, 0.99);
234
236 m_exciter_duration = 0.002;
237
239
240 if (m_segments.empty()) {
241 return;
242 }
243
244 auto& seg = m_segments[0];
245 const size_t len = m_delay_length_integer;
246 const auto strike_center = static_cast<size_t>(position * static_cast<double>(len));
247 const size_t burst_width = std::max<size_t>(len / 10, 4);
248
249 seg.p_plus = Memory::HistoryBuffer<double>(seg.p_plus.capacity());
250 seg.p_minus = Memory::HistoryBuffer<double>(seg.p_minus.capacity());
251
252 for (size_t s = 0; s < len; ++s) {
253 const double dist = std::abs(static_cast<double>(s)
254 - static_cast<double>(strike_center));
255 const double window = std::exp(-(dist * dist)
256 / (2.0 * static_cast<double>(burst_width * burst_width)));
257 seg.p_plus.push(strength * m_random_generator(-1.0, 1.0) * window);
258 }
259
260 m_exciter_active = false;
262}
263
265{
266 m_exciter_duration = std::max(0.001, seconds);
267}
268
269void WaveguideNetwork::set_exciter_sample(const std::vector<double>& sample)
270{
271 m_exciter_sample = sample;
272}
273
275{
276 m_exciter_active = true;
278
279 switch (m_exciter_type) {
282 break;
283
286 m_exciter_samples_remaining = static_cast<size_t>(
287 m_exciter_duration * static_cast<double>(m_sample_rate));
288 break;
289
292 break;
293
295 m_exciter_samples_remaining = std::numeric_limits<size_t>::max();
296 break;
297 }
298}
299
301{
303 m_exciter_active = false;
304 return 0.0;
305 }
306
308 double sample = 0.0;
309
310 switch (m_exciter_type) {
312 sample = 1.0;
313 break;
314
316 sample = m_random_generator(-1.0, 1.0);
317 break;
318
320 double noise = m_random_generator(-1.0, 1.0);
321 sample = m_exciter_filter ? m_exciter_filter->process_sample(noise) : noise;
322 break;
323 }
324
328 }
329 break;
330
332 if (m_exciter_node) {
333 sample = m_exciter_node->process_sample(0.0);
334 }
335 break;
336 }
337
338 return sample;
339}
340
341//-----------------------------------------------------------------------------
342// Waveguide Control
343//-----------------------------------------------------------------------------
344
346{
347 m_fundamental = std::max(20.0, freq);
349
350 if (!m_segments.empty()) {
351 const size_t required = m_delay_length_integer + 2;
352 auto& seg = m_segments[0];
353 if (seg.p_plus.capacity() < required) {
354 seg.p_plus = Memory::HistoryBuffer<double>(required);
355 seg.p_minus = Memory::HistoryBuffer<double>(required);
356 }
357 }
358}
359
361{
362 loss = std::clamp(loss, 0.0, 1.0);
363 for (auto& seg : m_segments) {
364 seg.loss_factor = loss;
365 }
366}
367
369{
370 return m_segments.empty() ? 0.996 : m_segments[0].loss_factor;
371}
372
373void WaveguideNetwork::set_loop_filter(const std::shared_ptr<Filters::Filter>& filter)
374{
375 if (!m_segments.empty()) {
376 m_segments[0].loop_filter = filter;
377 }
378}
379
381{
382 position = std::clamp(position, 0.0, 1.0);
383 m_pickup_sample = static_cast<size_t>(position * static_cast<double>(m_delay_length_integer));
384 m_pickup_sample = std::clamp(m_pickup_sample, size_t { 0 }, m_delay_length_integer);
385}
386
388{
389 if (m_delay_length_integer == 0) {
390 return 0.5;
391 }
392 return static_cast<double>(m_pickup_sample) / static_cast<double>(m_delay_length_integer);
393}
394
395void WaveguideNetwork::set_loop_filter_open(const std::shared_ptr<Filters::Filter>& filter)
396{
397 if (!m_segments.empty()) {
398 m_segments[0].loop_filter_open = filter;
399 }
400}
401
402void WaveguideNetwork::set_loop_filter_closed(const std::shared_ptr<Filters::Filter>& filter)
403{
404 if (!m_segments.empty()) {
405 m_segments[0].loop_filter_closed = filter;
406 }
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 WaveguideNetwork::apply_broadcast_parameter(const std::string& param, double value)
426{
427 if (param == "frequency") {
428 set_fundamental(value);
429 } else if (param == "damping" || param == "loss") {
430 set_loss_factor(value);
431 } else if (param == "position") {
432 set_pickup_position(value);
433 } else if (param == "scale") {
434 m_output_scale = std::max(0.0, value);
435 }
436}
437
439 const std::string& /*param*/,
440 const std::shared_ptr<NodeNetwork>& /*source*/)
441{
442}
443
445 const std::string& param_name,
446 const std::shared_ptr<Node>& source,
447 MappingMode mode)
448{
449 unmap_parameter(param_name);
450
451 ParameterMapping mapping;
452 mapping.param_name = param_name;
453 mapping.mode = mode;
454 mapping.broadcast_source = source;
455 mapping.network_source = nullptr;
456
457 m_parameter_mappings.push_back(std::move(mapping));
458}
459
461 const std::string& param_name,
462 const std::shared_ptr<NodeNetwork>& source_network)
463{
464 unmap_parameter(param_name);
465
466 ParameterMapping mapping;
467 mapping.param_name = param_name;
469 mapping.broadcast_source = nullptr;
470 mapping.network_source = source_network;
471
472 m_parameter_mappings.push_back(std::move(mapping));
473}
474
475void WaveguideNetwork::unmap_parameter(const std::string& param_name)
476{
477 std::erase_if(m_parameter_mappings,
478 [&](const auto& m) { return m.param_name == param_name; });
479}
480
481//-----------------------------------------------------------------------------
482// Metadata
483//-----------------------------------------------------------------------------
484
485std::unordered_map<std::string, std::string>
487{
488 auto metadata = NodeNetwork::get_metadata();
489
490 metadata["type"] = std::string(Reflect::enum_to_string(m_type));
491 metadata["fundamental"] = std::to_string(m_fundamental) + " Hz";
492 metadata["delay_length"] = std::to_string(m_delay_length_integer)
493 + " + " + std::to_string(m_delay_length_fraction) + " samples";
494 metadata["loss_factor"] = std::to_string(get_loss_factor());
495 metadata["pickup_position"] = std::to_string(get_pickup_position());
496
497 metadata["exciter_type"] = std::string(Reflect::enum_to_string(m_exciter_type));
498
499 return metadata;
500}
501
502std::optional<double> WaveguideNetwork::get_node_output(size_t index) const
503{
504 if (index < m_segments.size() && !m_last_audio_buffer.empty()) {
505 return m_last_output;
506 }
507 return std::nullopt;
508}
509
510std::optional<std::span<const double>> WaveguideNetwork::get_node_audio_buffer(size_t index) const
511{
512 if (index != 0 || m_last_audio_buffer.empty())
513 return std::nullopt;
514
515 return std::span<const double>(m_last_audio_buffer);
516}
517
518} // namespace MayaFlux::Nodes::Network
void push(const T &value)
Push new value to front of history.
History buffer for difference equations and recursive relations.
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.
Kinesis::Stochastic::Stochastic m_random_generator
void set_loop_filter_closed(const std::shared_ptr< Filters::Filter > &filter)
Set filter for the closed-end termination (mouthpiece/nut)
void set_loop_filter_open(const std::shared_ptr< Filters::Filter > &filter)
Set filter for the open-end termination (bell/bridge)
void strike(double position=0.5, double strength=1.0)
Strike the string/tube with an impulse.
std::unordered_map< std::string, std::string > get_metadata() const override
Get network metadata for debugging/visualization.
WaveguideType
Physical structure being modeled.
@ TUBE
Cylindrical bore (future: clarinet, flute)
void process_batch(unsigned int num_samples) override
Process the network for the given number of samples.
void pluck(double position=0.5, double strength=1.0)
Pluck the string at a normalized position.
void set_loop_filter(const std::shared_ptr< Filters::Filter > &filter)
Replace the loop filter.
double observe_sample(const WaveguideSegment &seg) const
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.
void set_loss_factor(double loss)
Set per-sample energy loss factor.
void set_fundamental(double freq)
Set fundamental frequency.
void unmap_parameter(const std::string &param_name) override
Remove parameter mapping.
void reset() override
Reset network to initial state.
double get_pickup_position() const
Get current pickup position.
void set_exciter_sample(const std::vector< double > &sample)
Set custom excitation waveform.
std::shared_ptr< Filters::Filter > m_exciter_filter
void process_bidirectional(WaveguideSegment &seg, unsigned int num_samples)
void process_unidirectional(WaveguideSegment &seg, unsigned int num_samples)
WaveguideNetwork(WaveguideType type, double fundamental_freq, double sample_rate=48000.0)
Create waveguide network with specified type and frequency.
double read_with_interpolation(const Memory::HistoryBuffer< double > &delay, size_t integer_part, double fraction) const
Read from delay line with linear fractional interpolation.
@ CONTINUOUS
External node as continuous exciter (bowing)
@ NOISE_BURST
Short white noise burst (default for pluck)
@ SAMPLE
User-provided excitation waveform.
std::optional< double > get_node_output(size_t index) const override
Get output of specific internal node (for ONE_TO_ONE mapping)
@ PRESSURE
Output is physical pressure at pickup (p_plus + p_minus)
double get_loss_factor() const
Get current loss factor.
void set_exciter_duration(double seconds)
Set noise burst duration for exciter.
void apply_broadcast_parameter(const std::string &param, double value)
void set_pickup_position(double position)
Set pickup position along the string.
void initialize() override
Called once before first process_batch()
void apply_one_to_one_parameter(const std::string &param, const std::shared_ptr< NodeNetwork > &source)
std::vector< WaveguideSegment > m_segments
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)
@ RING
Circular: last node connects to first.
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.
constexpr std::string_view enum_to_string(EnumType value) noexcept
Universal enum to string converter using magic_enum (original case)
Memory::HistoryBuffer< double > p_minus
Backward-traveling wave rail (BIDIRECTIONAL only)
std::shared_ptr< Filters::Filter > loop_filter_open
BIDIRECTIONAL: open-end filter (bell/bridge)
double reflection_open
Reflection coefficient at open end (pressure antinode)
std::shared_ptr< Filters::Filter > loop_filter_closed
BIDIRECTIONAL: closed-end filter (mouthpiece/nut)
std::shared_ptr< Filters::Filter > loop_filter
UNIDIRECTIONAL: single termination filter.
Memory::HistoryBuffer< double > p_plus
Forward-traveling wave rail.
double reflection_closed
Reflection coefficient at closed end (pressure node)
1D delay-line segment supporting both uni- and bidirectional propagation