MayaFlux 0.4.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
126 if (!is_enabled() || m_segments.empty()) {
127 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
128 std::this_thread::yield();
129
130 m_last_audio_buffer.assign(num_samples, 0.0);
131 m_last_output = 0.0;
132 m_audio_buffer_lock.clear(std::memory_order_release);
133 return;
134 }
135
137
138 thread_local std::vector<double> scratch;
139 scratch.assign(num_samples, 0.0);
140
141 auto& seg = m_segments[0];
142
144 process_unidirectional(seg, num_samples, scratch);
145 } else {
146 process_bidirectional(seg, num_samples, scratch);
147 }
148
149 while (m_audio_buffer_lock.test_and_set(std::memory_order_acquire))
150 std::this_thread::yield();
151
152 m_last_audio_buffer.assign(scratch.begin(), scratch.end());
155 m_audio_buffer_lock.clear(std::memory_order_release);
156}
157
159 unsigned int num_samples, std::vector<double>& out)
160{
161 for (unsigned int i = 0; i < num_samples; ++i) {
162 const double exciter = generate_exciter_sample();
163
164 const double delayed = read_with_interpolation(
166
167 const double filtered = seg.loop_filter ? seg.loop_filter->process_sample(delayed)
168 : delayed;
169
170 seg.p_plus.push(
171 exciter + filtered * seg.loss_factor * seg.reflection_closed);
172 out[i] = observe_sample(seg);
173 }
174}
175
177 unsigned int num_samples, std::vector<double>& out)
178{
179 for (unsigned int i = 0; i < num_samples; ++i) {
180 const double exciter = generate_exciter_sample();
181
182 const double plus_end = read_with_interpolation(
184
185 const double minus_end = read_with_interpolation(
187
188 auto* filt_open = seg.loop_filter_open ? seg.loop_filter_open.get()
189 : seg.loop_filter.get();
190 auto* filt_closed = seg.loop_filter_closed ? seg.loop_filter_closed.get()
191 : seg.loop_filter.get();
192
193 const double filtered_plus = filt_open ? filt_open->process_sample(plus_end)
194 : plus_end;
195 const double filtered_minus = filt_closed ? filt_closed->process_sample(minus_end)
196 : minus_end;
197
198 seg.p_minus.push(filtered_plus * seg.loss_factor * seg.reflection_open);
199 seg.p_plus.push(exciter + filtered_minus * seg.loss_factor * seg.reflection_closed);
200 out[i] = observe_sample(seg);
201 }
202}
203
204//-----------------------------------------------------------------------------
205// Excitation
206//-----------------------------------------------------------------------------
207
208void WaveguideNetwork::pluck(double position, double strength)
209{
210 position = std::clamp(position, 0.01, 0.99);
211
212 if (m_segments.empty()) {
213 return;
214 }
215
216 auto& seg = m_segments[0];
217 const size_t len = m_delay_length_integer;
218 const auto pluck_sample = static_cast<size_t>(position * static_cast<double>(len));
219
220 seg.p_plus = Memory::HistoryBuffer<double>(seg.p_plus.capacity());
221 seg.p_minus = Memory::HistoryBuffer<double>(seg.p_minus.capacity());
222
223 for (size_t s = 0; s < len; ++s) {
224 double value = 0.0;
225 if (s <= pluck_sample) {
226 value = strength * static_cast<double>(s)
227 / static_cast<double>(pluck_sample);
228 } else {
229 value = strength * static_cast<double>(len - s)
230 / static_cast<double>(len - pluck_sample);
231 }
232 seg.p_plus.push(value);
233 }
234
235 m_exciter_active = false;
237}
238
239void WaveguideNetwork::strike(double position, double strength)
240{
241 position = std::clamp(position, 0.01, 0.99);
242
244 m_exciter_duration = 0.002;
245
247
248 if (m_segments.empty()) {
249 return;
250 }
251
252 auto& seg = m_segments[0];
253 const size_t len = m_delay_length_integer;
254 const auto strike_center = static_cast<size_t>(position * static_cast<double>(len));
255 const size_t burst_width = std::max<size_t>(len / 10, 4);
256
257 seg.p_plus = Memory::HistoryBuffer<double>(seg.p_plus.capacity());
258 seg.p_minus = Memory::HistoryBuffer<double>(seg.p_minus.capacity());
259
260 for (size_t s = 0; s < len; ++s) {
261 const double dist = std::abs(static_cast<double>(s)
262 - static_cast<double>(strike_center));
263 const double window = std::exp(-(dist * dist)
264 / (2.0 * static_cast<double>(burst_width * burst_width)));
265 seg.p_plus.push(strength * m_random_generator(-1.0, 1.0) * window);
266 }
267
268 m_exciter_active = false;
270}
271
273{
274 m_exciter_duration = std::max(0.001, seconds);
275}
276
277void WaveguideNetwork::set_exciter_sample(const std::vector<double>& sample)
278{
279 m_exciter_sample = sample;
280}
281
283{
284 m_exciter_active = true;
286
287 switch (m_exciter_type) {
290 break;
291
294 m_exciter_samples_remaining = static_cast<size_t>(
295 m_exciter_duration * static_cast<double>(m_sample_rate));
296 break;
297
300 break;
301
303 m_exciter_samples_remaining = std::numeric_limits<size_t>::max();
304 break;
305 }
306}
307
309{
311 m_exciter_active = false;
312 return 0.0;
313 }
314
316 double sample = 0.0;
317
318 switch (m_exciter_type) {
320 sample = 1.0;
321 break;
322
324 sample = m_random_generator(-1.0, 1.0);
325 break;
326
328 double noise = m_random_generator(-1.0, 1.0);
329 sample = m_exciter_filter ? m_exciter_filter->process_sample(noise) : noise;
330 break;
331 }
332
336 }
337 break;
338
340 if (m_exciter_node) {
341 sample = m_exciter_node->process_sample(0.0);
342 }
343 break;
344 }
345
346 return sample;
347}
348
349//-----------------------------------------------------------------------------
350// Waveguide Control
351//-----------------------------------------------------------------------------
352
354{
355 m_fundamental = std::max(20.0, freq);
357
358 if (!m_segments.empty()) {
359 const size_t required = m_delay_length_integer + 2;
360 auto& seg = m_segments[0];
361 if (seg.p_plus.capacity() < required) {
362 seg.p_plus = Memory::HistoryBuffer<double>(required);
363 seg.p_minus = Memory::HistoryBuffer<double>(required);
364 }
365 }
366}
367
369{
370 loss = std::clamp(loss, 0.0, 1.0);
371 for (auto& seg : m_segments) {
372 seg.loss_factor = loss;
373 }
374}
375
377{
378 return m_segments.empty() ? 0.996 : m_segments[0].loss_factor;
379}
380
381void WaveguideNetwork::set_loop_filter(const std::shared_ptr<Filters::Filter>& filter)
382{
383 if (!m_segments.empty()) {
384 m_segments[0].loop_filter = filter;
385 }
386}
387
389{
390 position = std::clamp(position, 0.0, 1.0);
391 m_pickup_sample = static_cast<size_t>(position * static_cast<double>(m_delay_length_integer));
392 m_pickup_sample = std::clamp(m_pickup_sample, size_t { 0 }, m_delay_length_integer);
393}
394
396{
397 if (m_delay_length_integer == 0) {
398 return 0.5;
399 }
400 return static_cast<double>(m_pickup_sample) / static_cast<double>(m_delay_length_integer);
401}
402
403void WaveguideNetwork::set_loop_filter_open(const std::shared_ptr<Filters::Filter>& filter)
404{
405 if (!m_segments.empty()) {
406 m_segments[0].loop_filter_open = filter;
407 }
408}
409
410void WaveguideNetwork::set_loop_filter_closed(const std::shared_ptr<Filters::Filter>& filter)
411{
412 if (!m_segments.empty()) {
413 m_segments[0].loop_filter_closed = filter;
414 }
415}
416
417//-----------------------------------------------------------------------------
418// Parameter Mapping
419//-----------------------------------------------------------------------------
420
422{
423 for (const auto& mapping : m_parameter_mappings) {
424 if (mapping.mode == MappingMode::BROADCAST && mapping.broadcast_source) {
425 double value = mapping.broadcast_source->get_last_output();
426 apply_broadcast_parameter(mapping.param_name, value);
427 } else if (mapping.mode == MappingMode::ONE_TO_ONE && mapping.network_source) {
428 apply_one_to_one_parameter(mapping.param_name, mapping.network_source);
429 }
430 }
431}
432
433void WaveguideNetwork::apply_broadcast_parameter(const std::string& param, double value)
434{
435 if (param == "frequency") {
436 set_fundamental(value);
437 } else if (param == "damping" || param == "loss") {
438 set_loss_factor(value);
439 } else if (param == "position") {
440 set_pickup_position(value);
441 } else if (param == "scale") {
442 m_output_scale = std::max(0.0, value);
443 }
444}
445
447 const std::string& /*param*/,
448 const std::shared_ptr<NodeNetwork>& /*source*/)
449{
450}
451
453 const std::string& param_name,
454 const std::shared_ptr<Node>& source,
455 MappingMode mode)
456{
457 unmap_parameter(param_name);
458
459 ParameterMapping mapping;
460 mapping.param_name = param_name;
461 mapping.mode = mode;
462 mapping.broadcast_source = source;
463 mapping.network_source = nullptr;
464
465 m_parameter_mappings.push_back(std::move(mapping));
466}
467
469 const std::string& param_name,
470 const std::shared_ptr<NodeNetwork>& source_network)
471{
472 unmap_parameter(param_name);
473
474 ParameterMapping mapping;
475 mapping.param_name = param_name;
477 mapping.broadcast_source = nullptr;
478 mapping.network_source = source_network;
479
480 m_parameter_mappings.push_back(std::move(mapping));
481}
482
483void WaveguideNetwork::unmap_parameter(const std::string& param_name)
484{
485 std::erase_if(m_parameter_mappings,
486 [&](const auto& m) { return m.param_name == param_name; });
487}
488
489//-----------------------------------------------------------------------------
490// Metadata
491//-----------------------------------------------------------------------------
492
493std::unordered_map<std::string, std::string>
495{
496 auto metadata = NodeNetwork::get_metadata();
497
498 metadata["type"] = std::string(Reflect::enum_to_string(m_type));
499 metadata["fundamental"] = std::to_string(m_fundamental) + " Hz";
500 metadata["delay_length"] = std::to_string(m_delay_length_integer)
501 + " + " + std::to_string(m_delay_length_fraction) + " samples";
502 metadata["loss_factor"] = std::to_string(get_loss_factor());
503 metadata["pickup_position"] = std::to_string(get_pickup_position());
504
505 metadata["exciter_type"] = std::string(Reflect::enum_to_string(m_exciter_type));
506
507 return metadata;
508}
509
510std::optional<double> WaveguideNetwork::get_node_output(size_t index) const
511{
512 if (index < m_segments.size() && !m_last_audio_buffer.empty()) {
513 return m_last_output;
514 }
515 return std::nullopt;
516}
517
518std::optional<std::span<const double>> WaveguideNetwork::get_node_audio_buffer(size_t index) const
519{
520 if (index != 0 || m_last_audio_buffer.empty())
521 return std::nullopt;
522
523 return std::span<const double>(m_last_audio_buffer);
524}
525
526} // namespace MayaFlux::Nodes::Network
glm::vec3 position
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.
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.
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.
void process_unidirectional(WaveguideSegment &seg, unsigned int num_samples, std::vector< double > &out)
WaveguideType
Physical structure being modeled.
@ TUBE
Cylindrical bore (future: clarinet, flute)
void process_bidirectional(WaveguideSegment &seg, unsigned int num_samples, std::vector< double > &out)
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
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