15 double fundamental_freq,
18 , m_fundamental(fundamental_freq)
47 const auto len = seg.p_plus.capacity();
81 auto filter = std::make_shared<Filters::FIR>(
82 std::vector<double> { 0.5, 0.5 });
96 double fraction)
const
98 double s0 = delay[integer_part];
99 double s1 = delay[integer_part + 1];
100 return s0 + fraction * (s1 - s0);
128 std::this_thread::yield();
138 thread_local std::vector<double> scratch;
139 scratch.assign(num_samples, 0.0);
155 std::this_thread::yield();
164 unsigned int num_samples, std::vector<double>& out)
170 for (
unsigned int i = 0; i < num_samples; ++i) {
188 unsigned int num_samples, std::vector<double>& out)
194 for (
unsigned int i = 0; i < num_samples; ++i) {
209 ? filt_open->process_sample(plus_end)
212 ? filt_closed->process_sample(minus_end)
228 position = std::clamp(position, 0.01, 0.99);
235 const auto pluck_sample =
static_cast<size_t>(position *
static_cast<double>(len));
237 ? std::max(strength, 1.0)
243 for (
size_t s = 0; s < len; ++s) {
245 if (s <= pluck_sample) {
246 value = effective_strength *
static_cast<double>(s)
247 /
static_cast<double>(pluck_sample);
249 value = effective_strength *
static_cast<double>(len - s)
250 /
static_cast<double>(len - pluck_sample);
252 seg.p_plus.push(value);
263 position = std::clamp(position, 0.01, 0.99);
276 const auto strike_center =
static_cast<size_t>(position *
static_cast<double>(len));
277 const size_t burst_width = std::max<size_t>(len / 10, 4);
279 ? std::max(strength, 1.0)
285 for (
size_t s = 0; s < len; ++s) {
286 const double dist = std::abs(
static_cast<double>(s)
287 -
static_cast<double>(strike_center));
288 const double window = std::exp(-(dist * dist)
289 / (2.0 *
static_cast<double>(burst_width * burst_width)));
304 ? std::numeric_limits<size_t>::max()
400 if (seg.p_plus.capacity() < required) {
409 loss = std::clamp(loss, 0.0, 1.0);
411 seg.loss_factor = loss;
429 position = std::clamp(position, 0.0, 1.0);
464 double value = mapping.broadcast_source->get_last_output();
474 if (param ==
"frequency") {
476 }
else if (param ==
"damping" || param ==
"loss") {
478 }
else if (param ==
"position") {
480 }
else if (param ==
"scale") {
487 const std::shared_ptr<NodeNetwork>& )
492 const std::string& param_name,
493 const std::shared_ptr<Node>& source,
508 const std::string& param_name,
509 const std::shared_ptr<NodeNetwork>& source_network)
525 [&](
const auto& m) {
return m.param_name == param_name; });
532std::unordered_map<std::string, std::string>
538 metadata[
"fundamental"] = std::to_string(
m_fundamental) +
" Hz";
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.
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.
size_t m_exciter_samples_remaining
Kinesis::Stochastic::Stochastic m_random_generator
std::shared_ptr< Node > m_exciter_node
void set_loop_filter_closed(const std::shared_ptr< Filters::Filter > &filter)
Set filter for the closed-end termination (mouthpiece/nut)
void set_exciter_type(ExciterType type)
Set exciter type.
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.
MeasurementMode m_measurement_mode
void process_unidirectional(WaveguideSegment &seg, unsigned int num_samples, std::vector< double > &out)
void update_mapped_parameters()
size_t m_exciter_node_buffer_pos
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.
ExciterType m_exciter_type
void create_default_loop_filter()
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 ¶m_name, const std::shared_ptr< Node > &source, MappingMode mode=MappingMode::BROADCAST) override
Map external node output to network parameter.
void initialize_exciter()
std::vector< double > m_exciter_node_buffer
double generate_exciter_sample()
double m_delay_length_fraction
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 ¶m_name) override
Remove parameter mapping.
void reset() override
Reset network to initial state.
size_t m_delay_length_integer
double get_pickup_position() const
Get current pickup position.
void set_exciter_sample(const std::vector< double > &sample)
Set custom excitation waveform.
std::vector< double > m_exciter_sample
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.
ExciterType
Excitation signal types for waveguide synthesis.
@ IMPULSE
Single-sample Dirac impulse.
@ FILTERED_NOISE
Spectrally-shaped noise burst.
@ 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 compute_delay_length()
double m_exciter_duration
void set_exciter_duration(double seconds)
Set noise burst duration for exciter.
void apply_broadcast_parameter(const std::string ¶m, double value)
void set_pickup_position(double position)
Set pickup position along the string.
size_t m_exciter_sample_position
void initialize() override
Called once before first process_batch()
void apply_one_to_one_parameter(const std::string ¶m, 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)
std::shared_ptr< Node > broadcast_source
std::shared_ptr< NodeNetwork > network_source
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)
@ UNIDIRECTIONAL
Single loop (STRING)
@ BIDIRECTIONAL
Forward + backward rails (TUBE)
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