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);
149 unsigned int num_samples)
151 for (
unsigned int i = 0; i < num_samples; ++i) {
168 unsigned int num_samples)
170 for (
unsigned int i = 0; i < num_samples; ++i) {
184 const double filtered_plus = filt_open ? filt_open->process_sample(plus_end)
186 const double filtered_minus = filt_closed ? filt_closed->process_sample(minus_end)
202 position = std::clamp(position, 0.01, 0.99);
210 const auto pluck_sample =
static_cast<size_t>(position *
static_cast<double>(len));
215 for (
size_t s = 0; s < len; ++s) {
217 if (s <= pluck_sample) {
218 value = strength *
static_cast<double>(s)
219 /
static_cast<double>(pluck_sample);
221 value = strength *
static_cast<double>(len - s)
222 /
static_cast<double>(len - pluck_sample);
224 seg.p_plus.push(value);
233 position = std::clamp(position, 0.01, 0.99);
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);
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)));
353 if (seg.p_plus.capacity() < required) {
362 loss = std::clamp(loss, 0.0, 1.0);
364 seg.loss_factor = loss;
382 position = std::clamp(position, 0.0, 1.0);
417 double value = mapping.broadcast_source->get_last_output();
427 if (param ==
"frequency") {
429 }
else if (param ==
"damping" || param ==
"loss") {
431 }
else if (param ==
"position") {
433 }
else if (param ==
"scale") {
440 const std::shared_ptr<NodeNetwork>& )
445 const std::string& param_name,
446 const std::shared_ptr<Node>& source,
461 const std::string& param_name,
462 const std::shared_ptr<NodeNetwork>& source_network)
478 [&](
const auto& m) {
return m.param_name == param_name; });
485std::unordered_map<std::string, std::string>
491 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.
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.
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_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 update_mapped_parameters()
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.
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()
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
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.
@ 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