12 constexpr uint8_t k_empty = 0;
13 constexpr uint8_t k_ready = 1;
14 constexpr uint8_t k_busy = 2;
15 constexpr uint8_t k_retired = 3;
28 std::shared_ptr<TerminationOperator>
mouth;
29 std::shared_ptr<TerminationOperator>
bell;
37 :
DuctNetwork(length,
std::vector<double> { radius, radius }, sample_rate)
44 setup(length,
Profile { .radii = radii, .function = {} }, sample_rate);
50 setup(length,
Profile { .radii = {}, .function = std::move(radius) }, sample_rate);
65 m_mouth = std::make_shared<ReedOperator>();
66 m_bell = std::make_shared<RadiationOperator>();
77 m_field_points.store(prepared->grid.intervals + 1, std::memory_order_relaxed);
78 m_field.reserve(prepared->grid.intervals + 1);
92 const std::shared_ptr<TerminationOperator>& mouth,
93 const std::shared_ptr<TerminationOperator>& bell)
const
95 const Acoustics::DuctSpec spec { .length = length, .sample_rate =
m_rate, .mouth_volume = mouth_volume };
97 std::expected<Acoustics::DuctGrid, Acoustics::DuctError> grid = std::unexpected(Acoustics::DuctError::INVALID_PROFILE);
98 if (!profile.
radii.empty()) {
99 grid = Acoustics::make_duct_grid(spec, profile.
radii);
101 grid = Acoustics::make_duct_grid(spec, profile.
function);
114 "DuctNetwork terminations cannot terminate the {} interval duct", grid->intervals);
118 auto prepared = std::make_shared<Prepared>();
119 prepared->spec = spec;
120 prepared->pressure.assign(grid->intervals + 1, 0.0);
121 prepared->velocity.assign(grid->intervals, 0.0);
122 prepared->grid = std::move(*grid);
123 prepared->mouth = mouth;
124 prepared->bell = bell;
126 prepared->friction = losses.
friction;
129 const auto& areas = prepared->grid.area;
130 const double mean_area = std::accumulate(areas.begin(), areas.end(), 0.0) /
static_cast<double>(areas.size());
131 std::tie(prepared->friction, prepared->viscosity) = Acoustics::viscothermal_rates(
132 std::sqrt(mean_area / std::numbers::pi), spec.sound_speed, spec.air_density);
135 if (!prepared->loss.configure(prepared->grid, prepared->friction, prepared->viscosity)) {
137 "DuctNetwork wall losses of {} and {} cannot be applied", prepared->friction, prepared->viscosity);
153 std::this_thread::yield();
155 m_field.reserve(next->grid.intervals + 1);
159 uint8_t state =
m_handoff.load(std::memory_order_acquire);
161 if (state == k_retired) {
163 m_handoff.store(k_empty, std::memory_order_release);
164 }
else if (state == k_ready) {
165 if (
m_handoff.compare_exchange_strong(state, k_busy, std::memory_order_acq_rel)) {
166 next->keep_state = next->keep_state &&
m_slot->keep_state;
168 m_handoff.store(k_ready, std::memory_order_release);
171 }
else if (state == k_busy) {
172 std::this_thread::yield();
175 m_handoff.store(k_ready, std::memory_order_release);
183 uint8_t expected = k_ready;
184 if (!
m_handoff.compare_exchange_strong(expected, k_busy, std::memory_order_acq_rel)) {
189 m_handoff.store(k_retired, std::memory_order_release);
193 Acoustics::resample_line(
m_slot->pressure,
m_active->pressure, 0.0);
194 Acoustics::resample_line(
m_slot->velocity,
m_active->velocity, 0.5);
239 next->keep_state =
true;
248 return set_profile(std::vector<double> { radius, radius });
254 Profile profile { .radii = radii, .function = {} };
260 next->keep_state =
true;
270 Profile profile { .radii = {}, .function = std::move(radius) };
276 next->keep_state =
true;
291 next->keep_state =
true;
301 const Losses losses { .automatic =
false, .friction = friction, .viscosity = viscosity };
307 next->keep_state =
true;
317 const Losses losses { .automatic =
true };
323 next->keep_state =
true;
338 next->keep_state =
true;
341 m_mouth = std::move(termination);
355 next->keep_state =
true;
358 m_bell = std::move(termination);
378 m_pickup.store(std::clamp(position, 0.0, 1.0), std::memory_order_relaxed);
401 state.
mouth->process(
static_cast<float>(num_samples));
402 state.
bell->process(
static_cast<float>(num_samples));
405 const double pickup =
m_pickup.load(std::memory_order_relaxed);
406 const Acoustics::DuctGrid& grid = state.
grid;
409 for (
unsigned int i = 0; i < num_samples; ++i) {
410 Acoustics::advance_velocity(state.
velocity, state.
pressure, grid.velocity_gain);
413 const auto [mouth_pressure, mouth_inflow] = state.
mouth->couple(state.
pressure.front(), state.
velocity.front());
414 Acoustics::advance_pressure(state.
pressure, state.
velocity, grid.pressure_gain);
415 state.
pressure.front() = mouth_pressure;
417 const auto [bell_pressure, bell_inflow] = state.
bell->couple(state.
pressure.back(), -state.
velocity.back());
418 state.
pressure.back() = bell_pressure;
420 const double outflow = -bell_inflow;
423 : Acoustics::sample_pressure(state.
pressure, pickup);
433 std::this_thread::yield();
458 const ParameterKey key { .name = mapping.param_name, .hash = mapping.param_hash };
459 const double value = mapping.broadcast_source->get_last_output();
463 }
else if (key.
matches(
"scale")) {
466 m_active->mouth->set_mapped_parameter(key, value);
467 m_active->bell->set_mapped_parameter(key, value);
474 if (
const auto pickup =
m_controls.pickup.pending()) {
477 if (
const auto output =
m_controls.output.pending()) {
481 const auto forward = [
this](std::string_view name,
Control& control) {
482 if (name ==
"scale" || name ==
"pickup" || name ==
"output") {
485 if (
const auto value = control.pending()) {
487 m_active->mouth->set_mapped_parameter(key, *value);
488 m_active->bell->set_mapped_parameter(key, *value);
493 [&](
const auto&... member) { (forward(member.name, member.get(
m_controls)), ...); },
498 const std::string& param_name,
499 const std::shared_ptr<Node>& source,
515 const std::string& param_name,
516 const std::shared_ptr<NodeNetwork>& source)
533 [&](
const auto& mapping) {
return mapping.param_name == param_name; });
543 std::this_thread::yield();
545 const std::optional<double> value = index <
m_field.size()
546 ? std::optional<double> {
m_field[index] }
563 std::this_thread::yield();
565 std::vector<double> copy =
m_field;
575 metadata[
"length"] = std::to_string(
m_length) +
" m";
576 metadata[
"intervals"] = std::to_string(
m_intervals);
577 metadata[
"courant"] = std::to_string(
m_courant);
578 metadata[
"bandwidth"] = std::to_string(
m_bandwidth) +
" Hz";
581 metadata[
"mouth"] = std::string(
m_mouth->get_type_name());
582 metadata[
"bell"] = std::string(
m_bell->get_type_name());
#define MF_WARN(comp, ctx,...)
One named control of a network or operator: empty, a value, or a mapped source.
bool set_wall_losses(double friction, double viscosity)
Set the wall losses by hand.
bool set_mouth(std::shared_ptr< TerminationOperator > termination)
Replace the termination at the mouth.
std::atomic< Output > m_output
bool set_profile(const std::vector< double > &radii)
Replace the profile with radii sampled evenly from mouth to bell.
std::shared_ptr< Prepared > build(double length, const Profile &profile, const Losses &losses, double mouth_volume, const std::shared_ptr< TerminationOperator > &mouth, const std::shared_ptr< TerminationOperator > &bell) const
bool use_viscothermal_losses()
Take the wall losses from the duct's mean radius, fitted to the viscothermal attenuation of a tube at...
void reset() override
Return the duct to rest, installed at the next block.
std::shared_ptr< TerminationOperator > m_mouth
std::shared_ptr< Prepared > m_slot
void setup(double length, Profile profile, double sample_rate)
void publish(std::shared_ptr< Prepared > next)
void unmap_parameter(const std::string ¶m_name) override
Remove parameter mapping.
DuctNetwork(double length, double radius, double sample_rate=48000.0)
A cylinder.
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.
std::shared_ptr< Prepared > m_active
std::vector< double > pressure_field() const
Copy of the pressures along the duct from the last block.
Output
What the network publishes.
@ PICKUP
Pressure inside the duct at the pickup position.
@ RADIATED
Pressure radiated at one metre, from the flow out of the bell.
std::atomic< double > m_pickup
void process_batch(unsigned int num_samples) override
Process the network for the given number of samples.
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::atomic< uint8_t > m_handoff
bool set_mouth_volume(double volume)
Add air behind the mouth end, in cubic metres.
std::shared_ptr< TerminationOperator > m_bell
void apply_pickup_position(double position)
void bind(Prepared &prepared, bool keep)
double m_applied_viscosity
std::atomic< size_t > m_field_points
std::vector< double > m_field
void initialize() override
Called once before first process_batch().
std::shared_ptr< TerminationOperator > get_bell() const
std::vector< double > m_scratch
bool set_length(double length)
Change the length in metres, keeping the profile.
DuctControls & m_controls
void update_mapped_parameters()
double m_applied_friction
void publish_block(std::span< const double > block)
std::unordered_map< std::string, std::string > get_metadata() const override
Get network metadata for debugging/visualization.
std::shared_ptr< TerminationOperator > get_mouth() const
bool set_radius(double radius)
Make the duct a cylinder of this radius in metres.
std::mutex m_producer_mutex
bool set_bell(std::shared_ptr< TerminationOperator > termination)
Replace the termination at the bell.
double m_previous_outflow
std::optional< double > get_node_output(size_t index) const override
Pressure at one point along the duct, from the last block.
std::vector< ParameterMapping > m_parameter_mappings
C & declare_controls()
Create the network's controls struct and keep it alive.
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 refresh_controls()
Read node sources and apply the shared scale.
std::shared_ptr< OperatorChain > m_operator_chain
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.
@ Configuration
Configuration and parameter updates.
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
constexpr uint64_t parameter_hash(std::string_view name) noexcept
Hash a parameter name once when installing a mapping.
constexpr ParameterKey parameter_key(std::string_view name) noexcept
@ CHAIN
Linear sequence: node[i] → node[i+1].
MappingMode
How one source maps to a network's elements.
@ ONE_TO_ONE
Source elements correspond to network elements.
@ BROADCAST
One source value applies to every network element.
@ 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).
Controls of a DuctNetwork, each a number or a mapped node.
std::shared_ptr< TerminationOperator > mouth
std::shared_ptr< TerminationOperator > bell
std::vector< double > pressure
std::vector< double > velocity
std::vector< double > radii
std::function< double(double)> function
std::shared_ptr< Node > broadcast_source
std::shared_ptr< NodeNetwork > network_source
constexpr bool matches(std::string_view candidate) const noexcept
A cached hash paired with its original name for collision-safe matching.