9 [[nodiscard]]
bool grid_usable(
const Acoustics::DuctGrid& grid)
noexcept
11 return grid.intervals >= 2 && grid.pressure_gain.size() == grid.intervals + 1;
14 [[nodiscard]]
double end_gain_of(
const Acoustics::DuctGrid& grid, DuctEnd end)
noexcept
16 return end == DuctEnd::MOUTH ? grid.pressure_gain.front() : grid.pressure_gain.back();
19 [[nodiscard]]
double end_radius_of(
const Acoustics::DuctGrid& grid, DuctEnd end)
noexcept
21 return end == DuctEnd::MOUTH ? grid.start_radius : grid.end_radius;
24 [[nodiscard]]
bool positive_finite(
double value)
noexcept
26 return std::isfinite(value) && value > 0.0;
29 constexpr double lip_stiffening = 0.4;
30 constexpr double lip_narrowing = 0.2;
31 constexpr double lip_damping = 1.0;
33 enum class Rule : uint8_t {
40 void put(std::atomic<double>& slot,
double value, Rule rule)
noexcept
42 if (!std::isfinite(value)) {
48 slot.store(value, std::memory_order_relaxed);
50 case Rule::NON_NEGATIVE:
51 slot.store(std::max(0.0, value), std::memory_order_relaxed);
54 slot.store(std::clamp(value, 0.0, 1.0), std::memory_order_relaxed);
58 slot.store(value, std::memory_order_relaxed);
72 return grid_usable(grid);
82 return { Acoustics::end_cell_pressure(end_pressure,
m_end_gain, -interior_flow), 0.0 };
99 const double lip = std::clamp(
m_lip.load(std::memory_order_relaxed), 0.0, 1.0);
100 const double stiffening = 1.0 + lip_stiffening * lip;
101 const double narrowing = 1.0 - lip_narrowing * lip;
104 .width =
m_width.load(std::memory_order_relaxed),
105 .rest_opening =
m_rest_opening.load(std::memory_order_relaxed) * narrowing,
113 return grid_usable(grid) && positive_finite(spec.air_density)
114 && positive_finite(end_gain_of(grid, end)) && positive_finite(grid.time_step)
115 && positive_finite(
m_width.load(std::memory_order_relaxed))
116 && positive_finite(
m_rest_opening.load(std::memory_order_relaxed))
152 const uint64_t bits = (
m_random * 0x2545F4914F6CDD1DULL) >> 11;
153 return static_cast<double>(bits) * (2.0 / 9007199254740992.0) - 1.0;
161 const double smoothing =
m_smoothing.load(std::memory_order_relaxed);
164 const double cutoff =
m_noise_cutoff.load(std::memory_order_relaxed);
169 || Acoustics::reed_coupling_is_unique(
channel(), Acoustics::flow_coupling_gain(
m_end_gain)),
170 std::memory_order_relaxed);
178 const double noise =
m_noise.load(std::memory_order_relaxed);
184 const double resonance =
m_resonance.load(std::memory_order_relaxed);
185 double pressure = 0.0;
187 if (resonance > 0.0) {
188 const double lip = std::clamp(
m_lip.load(std::memory_order_relaxed), 0.0, 1.0);
189 const auto [dynamic_pressure, dynamic_flow, difference] = Acoustics::dynamic_reed_mouth_step(
190 channel(), resonance * (1.0 + lip_stiffening * lip),
191 m_reed_damping.load(std::memory_order_relaxed) * (1.0 + lip_damping * lip),
194 pressure = dynamic_pressure;
197 const auto [static_pressure, static_flow, difference, status] = Acoustics::reed_mouth_step(
199 pressure = static_pressure;
204 return { pressure, flow + breath };
210 if (param ==
"pressure") {
212 }
else if (param ==
"smoothing") {
214 }
else if (param ==
"resonance") {
216 }
else if (param ==
"reed_damping") {
218 }
else if (param ==
"noise") {
219 put(
m_noise, value, Rule::NON_NEGATIVE);
220 }
else if (param ==
"noise_cutoff") {
222 }
else if (param ==
"reed_area") {
224 }
else if (param ==
"lip") {
225 put(
m_lip, value, Rule::UNIT);
226 }
else if (param ==
"closing_pressure") {
228 }
else if (param ==
"opening") {
230 }
else if (param ==
"width") {
231 put(
m_width, value, Rule::POSITIVE);
237 const auto apply = [](
Control& control, std::atomic<double>& slot, Rule rule) {
238 if (
const auto value = control.
pending()) {
239 put(slot, *value, rule);
273 if (query ==
"pressure") {
276 if (query ==
"closing_pressure") {
279 if (query ==
"opening") {
282 if (query ==
"width") {
283 return m_width.load(std::memory_order_relaxed);
285 if (query ==
"smoothing") {
286 return m_smoothing.load(std::memory_order_relaxed);
288 if (query ==
"resonance") {
289 return m_resonance.load(std::memory_order_relaxed);
291 if (query ==
"reed_damping") {
294 if (query ==
"noise") {
295 return m_noise.load(std::memory_order_relaxed);
297 if (query ==
"noise_cutoff") {
300 if (query ==
"reed_area") {
301 return m_reed_area.load(std::memory_order_relaxed);
303 if (query ==
"lip") {
304 return m_lip.load(std::memory_order_relaxed);
306 if (query ==
"unique") {
307 return m_unique.load(std::memory_order_relaxed) ? 1.0 : 0.0;
322 const Acoustics::DuctGrid& grid,
const Acoustics::DuctSpec& spec,
DuctEnd end)
const
324 if (!grid_usable(grid)) {
328 const double radius = end_radius_of(grid, end);
329 return Acoustics::make_radiation_coefficients(
332 .sound_speed = spec.sound_speed,
333 .air_density = spec.air_density,
334 .time_step = grid.time_step },
335 std::numbers::pi * radius * radius,
336 end_gain_of(grid, end));
347 m_ready = coefficients.has_value();
358 m_ready = coefficients.has_value();
373 return { Acoustics::end_cell_pressure(end_pressure,
m_end_gain, -interior_flow), 0.0 };
376 const auto [pressure, outflow] = Acoustics::radiation_step(
m_coefficients,
m_state, end_pressure, -interior_flow);
377 return { pressure, -outflow };
394 return grid_usable(grid) &&
m_law && positive_finite(end_gain_of(grid, end))
395 && positive_finite(
m_maximum_flow.load(std::memory_order_relaxed));
412 m_status.store(0, std::memory_order_relaxed);
418 return { Acoustics::end_cell_pressure(end_pressure,
m_end_gain, -interior_flow), 0.0 };
422 const double largest =
m_maximum_flow.load(std::memory_order_relaxed);
423 const double gain = Acoustics::flow_coupling_gain(
m_end_gain);
424 const double target = Acoustics::flow_coupling_target(driving, end_pressure, gain, interior_flow);
426 const auto law = [
this](
double difference) -> Acoustics::FlowSample {
return m_law(difference); };
427 const auto [difference, flow, status] = Acoustics::solve_pressure_flow(
428 law, gain, target, target - gain * largest, target + gain * largest,
m_guess);
431 m_status.store(
static_cast<uint8_t
>(status), std::memory_order_relaxed);
432 return { Acoustics::centred_end_pressure(driving, difference, end_pressure), flow };
444 if (param ==
"driving_pressure") {
446 }
else if (param ==
"maximum_flow") {
453 if (
const auto driving =
m_controls.driving_pressure.pending()) {
456 if (
const auto largest =
m_controls.maximum_flow.pending()) {
469 if (query ==
"driving_pressure") {
472 if (query ==
"maximum_flow") {
475 if (query ==
"status") {
476 return static_cast<double>(
m_status.load(std::memory_order_relaxed));
std::optional< T > pending() noexcept
The value to apply now as type T, or nullopt when nothing is due.
void observe(T actual) noexcept
Shows the value the owner really has, unless a source is feeding it.
One named control of a network or operator: empty, a value, or a mapped source.
void prepare(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a grid and end, and reset the state.
std::function< Kinesis::Acoustics::FlowSample(double)> FlowLaw
std::atomic< uint8_t > m_status
void reset() override
Clear the state, keeping the parameters.
void set_parameter(std::string_view param, double value) override
Set operator parameter.
void retarget(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a new grid of the same duct while it keeps sounding.
std::atomic< double > m_maximum_flow
std::pair< double, double > couple(double end_pressure, double interior_flow) override
Advance the end cell and its coupling by one sample.
void read_controls(JunctionControls &controls) const
Fills every control with the value the junction holds now.
std::optional< double > query_state(std::string_view query) const override
Query operator internal state.
JunctionOperator(FlowLaw law, double maximum_flow, double driving_pressure=0.0)
void process(float frames) override
Process for one batch cycle.
JunctionControls & m_controls
std::atomic< double > m_driving_pressure
bool accepts(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) const override
Whether prepare would succeed for this grid and end.
C & declare_controls()
Create the operator's controls struct and keep it alive.
void refresh_controls()
Read node sources into their controls.
bool settle_named_control(std::string_view name)
Drops the pending typed value of the control called name.
Kinesis::Acoustics::RadiationCoefficients m_coefficients
Kinesis::Acoustics::RadiationModel m_model
void prepare(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a grid and end, and reset the state.
std::pair< double, double > couple(double end_pressure, double interior_flow) override
Advance the end cell and its coupling by one sample.
Kinesis::Acoustics::RadiationState m_state
void retarget(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a new grid of the same duct while it keeps sounding.
void reset() override
Clear the state, keeping the parameters.
std::optional< Kinesis::Acoustics::RadiationCoefficients > coefficients_for(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) const
bool accepts(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) const override
Whether prepare would succeed for this grid and end.
RadiationOperator(Kinesis::Acoustics::RadiationModel model=Kinesis::Acoustics::unflanged_radiation)
std::atomic< double > m_resonance
void retarget(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a new grid of the same duct while it keeps sounding.
void read_controls(ReedControls &controls) const
Fills every control with the value the reed holds now.
std::atomic< double > m_rest_opening
std::optional< double > query_state(std::string_view query) const override
Query operator internal state.
void set_parameter(std::string_view param, double value) override
Set operator parameter.
std::atomic< double > m_noise
void prepare(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a grid and end, and reset the state.
ReedOperator(double width=0.012, double rest_opening=4.0e-4, double closing_pressure=3000.0)
void reset() override
Clear the state, keeping the parameters.
std::atomic< double > m_reed_damping
double next_white() noexcept
std::array< double, 2 > m_motion
std::atomic< double > m_smoothing
void process(float frames) override
Process for one batch cycle.
std::atomic< double > m_width
std::atomic< double > m_lip
std::atomic< double > m_target_pressure
std::atomic< double > m_noise_cutoff
std::pair< double, double > couple(double end_pressure, double interior_flow) override
Advance the end cell and its coupling by one sample.
ReedControls & m_controls
std::atomic< bool > m_unique
std::atomic< double > m_closing_pressure
std::atomic< double > m_reed_area
bool accepts(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) const override
Whether prepare would succeed for this grid and end.
Kinesis::Acoustics::ReedChannel channel() const noexcept
bool accepts(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) const override
Whether prepare would succeed for this grid and end.
void prepare(const Kinesis::Acoustics::DuctGrid &grid, const Kinesis::Acoustics::DuctSpec &spec, DuctEnd end) override
Bind to a grid and end, and reset the state.
std::pair< double, double > couple(double end_pressure, double interior_flow) override
Advance the end cell and its coupling by one sample.
DuctEnd
Which end of a duct an operator terminates.
Controls of a JunctionOperator, each a number or a mapped node.
Controls of a ReedOperator, each a number or a mapped node.