MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TerminationOperators.cpp
Go to the documentation of this file.
2
4
5namespace Acoustics = Kinesis::Acoustics;
6
7namespace {
8
9 [[nodiscard]] bool grid_usable(const Acoustics::DuctGrid& grid) noexcept
10 {
11 return grid.intervals >= 2 && grid.pressure_gain.size() == grid.intervals + 1;
12 }
13
14 [[nodiscard]] double end_gain_of(const Acoustics::DuctGrid& grid, DuctEnd end) noexcept
15 {
16 return end == DuctEnd::MOUTH ? grid.pressure_gain.front() : grid.pressure_gain.back();
17 }
18
19 [[nodiscard]] double end_radius_of(const Acoustics::DuctGrid& grid, DuctEnd end) noexcept
20 {
21 return end == DuctEnd::MOUTH ? grid.start_radius : grid.end_radius;
22 }
23
24 [[nodiscard]] bool positive_finite(double value) noexcept
25 {
26 return std::isfinite(value) && value > 0.0;
27 }
28
29 constexpr double lip_stiffening = 0.4;
30 constexpr double lip_narrowing = 0.2;
31 constexpr double lip_damping = 1.0;
32
33 enum class Rule : uint8_t {
34 ANY,
35 NON_NEGATIVE,
36 UNIT,
37 POSITIVE
38 };
39
40 void put(std::atomic<double>& slot, double value, Rule rule) noexcept
41 {
42 if (!std::isfinite(value)) {
43 return;
44 }
45
46 switch (rule) {
47 case Rule::ANY:
48 slot.store(value, std::memory_order_relaxed);
49 break;
50 case Rule::NON_NEGATIVE:
51 slot.store(std::max(0.0, value), std::memory_order_relaxed);
52 break;
53 case Rule::UNIT:
54 slot.store(std::clamp(value, 0.0, 1.0), std::memory_order_relaxed);
55 break;
56 case Rule::POSITIVE:
57 if (value > 0.0) {
58 slot.store(value, std::memory_order_relaxed);
59 }
60 break;
61 }
62 }
63
64}
65
66//-----------------------------------------------------------------------------
67// Wall
68//-----------------------------------------------------------------------------
69
70bool WallOperator::accepts(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& /*spec*/, DuctEnd /*end*/) const
71{
72 return grid_usable(grid);
73}
74
75void WallOperator::prepare(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& /*spec*/, DuctEnd end)
76{
77 m_end_gain = end_gain_of(grid, end);
78}
79
80std::pair<double, double> WallOperator::couple(double end_pressure, double interior_flow)
81{
82 return { Acoustics::end_cell_pressure(end_pressure, m_end_gain, -interior_flow), 0.0 };
83}
84
85//-----------------------------------------------------------------------------
86// Reed
87//-----------------------------------------------------------------------------
88
89ReedOperator::ReedOperator(double width, double rest_opening, double closing_pressure)
91 , m_width(width)
92 , m_rest_opening(rest_opening)
93 , m_closing_pressure(closing_pressure)
94{
95}
96
97Acoustics::ReedChannel ReedOperator::channel() const noexcept
98{
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;
102
103 return {
104 .width = m_width.load(std::memory_order_relaxed),
105 .rest_opening = m_rest_opening.load(std::memory_order_relaxed) * narrowing,
106 .closing_pressure = m_closing_pressure.load(std::memory_order_relaxed) * stiffening,
107 .air_density = m_density
108 };
109}
110
111bool ReedOperator::accepts(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end) const
112{
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))
117 && positive_finite(m_closing_pressure.load(std::memory_order_relaxed));
118}
119
120void ReedOperator::prepare(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end)
121{
122 m_end_gain = end_gain_of(grid, end);
123 m_density = spec.air_density;
124 m_time_step = grid.time_step;
125 reset();
126 process(0.0F);
127}
128
129void ReedOperator::retarget(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end)
130{
131 m_end_gain = end_gain_of(grid, end);
132 m_density = spec.air_density;
133 m_time_step = grid.time_step;
134 process(0.0F);
135}
136
138{
139 m_pressure = 0.0;
140 m_warm_start = 0.0;
141 m_motion = { 0.0, 0.0 };
142 m_noise_state = 0.0;
143 m_last_flow = 0.0;
144 m_random = 0x9E3779B97F4A7C15ULL;
145}
146
148{
149 m_random ^= m_random >> 12;
150 m_random ^= m_random << 25;
151 m_random ^= m_random >> 27;
152 const uint64_t bits = (m_random * 0x2545F4914F6CDD1DULL) >> 11;
153 return static_cast<double>(bits) * (2.0 / 9007199254740992.0) - 1.0;
154}
155
156void ReedOperator::process(float /*frames*/)
157{
160
161 const double smoothing = m_smoothing.load(std::memory_order_relaxed);
162 m_alpha = smoothing > 0.0 ? 1.0 - std::exp(-m_time_step / smoothing) : 1.0;
163
164 const double cutoff = m_noise_cutoff.load(std::memory_order_relaxed);
165 m_noise_alpha = cutoff > 0.0 ? 1.0 - std::exp(-2.0 * std::numbers::pi * cutoff * m_time_step) : 1.0;
166 m_noise_scale = std::sqrt(3.0 * (2.0 - m_noise_alpha) / m_noise_alpha);
167 m_unique.store(
168 m_resonance.load(std::memory_order_relaxed) > 0.0
169 || Acoustics::reed_coupling_is_unique(channel(), Acoustics::flow_coupling_gain(m_end_gain)),
170 std::memory_order_relaxed);
171}
172
173std::pair<double, double> ReedOperator::couple(double end_pressure, double interior_flow)
174{
175 m_pressure += m_alpha * (m_target_pressure.load(std::memory_order_relaxed) - m_pressure);
176
177 double breath = 0.0;
178 const double noise = m_noise.load(std::memory_order_relaxed);
179 if (noise > 0.0) {
181 breath = noise * std::abs(m_last_flow) * m_noise_scale * m_noise_state;
182 }
183
184 const double resonance = m_resonance.load(std::memory_order_relaxed);
185 double pressure = 0.0;
186 double flow = 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),
192 m_reed_area.load(std::memory_order_relaxed), m_time_step,
193 m_motion, m_end_gain, m_pressure, end_pressure, interior_flow - breath);
194 pressure = dynamic_pressure;
195 flow = dynamic_flow;
196 } else {
197 const auto [static_pressure, static_flow, difference, status] = Acoustics::reed_mouth_step(
198 channel(), m_warm_start, m_end_gain, m_pressure, end_pressure, interior_flow - breath);
199 pressure = static_pressure;
200 flow = static_flow;
201 }
202
203 m_last_flow = flow;
204 return { pressure, flow + breath };
205}
206
207void ReedOperator::set_parameter(std::string_view param, double value)
208{
210 if (param == "pressure") {
211 put(m_target_pressure, value, Rule::ANY);
212 } else if (param == "smoothing") {
213 put(m_smoothing, value, Rule::NON_NEGATIVE);
214 } else if (param == "resonance") {
215 put(m_resonance, value, Rule::NON_NEGATIVE);
216 } else if (param == "reed_damping") {
217 put(m_reed_damping, value, Rule::NON_NEGATIVE);
218 } else if (param == "noise") {
219 put(m_noise, value, Rule::NON_NEGATIVE);
220 } else if (param == "noise_cutoff") {
221 put(m_noise_cutoff, value, Rule::NON_NEGATIVE);
222 } else if (param == "reed_area") {
223 put(m_reed_area, value, Rule::NON_NEGATIVE);
224 } else if (param == "lip") {
225 put(m_lip, value, Rule::UNIT);
226 } else if (param == "closing_pressure") {
227 put(m_closing_pressure, value, Rule::POSITIVE);
228 } else if (param == "opening") {
229 put(m_rest_opening, value, Rule::POSITIVE);
230 } else if (param == "width") {
231 put(m_width, value, Rule::POSITIVE);
232 }
233}
234
236{
237 const auto apply = [](Control& control, std::atomic<double>& slot, Rule rule) {
238 if (const auto value = control.pending()) {
239 put(slot, *value, rule);
240 }
241 };
242
243 apply(m_controls.pressure, m_target_pressure, Rule::ANY);
244 apply(m_controls.smoothing, m_smoothing, Rule::NON_NEGATIVE);
245 apply(m_controls.resonance, m_resonance, Rule::NON_NEGATIVE);
246 apply(m_controls.reed_damping, m_reed_damping, Rule::NON_NEGATIVE);
247 apply(m_controls.noise, m_noise, Rule::NON_NEGATIVE);
248 apply(m_controls.noise_cutoff, m_noise_cutoff, Rule::NON_NEGATIVE);
249 apply(m_controls.reed_area, m_reed_area, Rule::NON_NEGATIVE);
250 apply(m_controls.lip, m_lip, Rule::UNIT);
251 apply(m_controls.closing_pressure, m_closing_pressure, Rule::POSITIVE);
252 apply(m_controls.opening, m_rest_opening, Rule::POSITIVE);
253 apply(m_controls.width, m_width, Rule::POSITIVE);
254}
255
257{
258 controls.pressure.observe(m_target_pressure.load(std::memory_order_relaxed));
259 controls.smoothing.observe(m_smoothing.load(std::memory_order_relaxed));
260 controls.resonance.observe(m_resonance.load(std::memory_order_relaxed));
261 controls.reed_damping.observe(m_reed_damping.load(std::memory_order_relaxed));
262 controls.noise.observe(m_noise.load(std::memory_order_relaxed));
263 controls.noise_cutoff.observe(m_noise_cutoff.load(std::memory_order_relaxed));
264 controls.reed_area.observe(m_reed_area.load(std::memory_order_relaxed));
265 controls.lip.observe(m_lip.load(std::memory_order_relaxed));
266 controls.closing_pressure.observe(m_closing_pressure.load(std::memory_order_relaxed));
267 controls.opening.observe(m_rest_opening.load(std::memory_order_relaxed));
268 controls.width.observe(m_width.load(std::memory_order_relaxed));
269}
270
271std::optional<double> ReedOperator::query_state(std::string_view query) const
272{
273 if (query == "pressure") {
274 return m_target_pressure.load(std::memory_order_relaxed);
275 }
276 if (query == "closing_pressure") {
277 return m_closing_pressure.load(std::memory_order_relaxed);
278 }
279 if (query == "opening") {
280 return m_rest_opening.load(std::memory_order_relaxed);
281 }
282 if (query == "width") {
283 return m_width.load(std::memory_order_relaxed);
284 }
285 if (query == "smoothing") {
286 return m_smoothing.load(std::memory_order_relaxed);
287 }
288 if (query == "resonance") {
289 return m_resonance.load(std::memory_order_relaxed);
290 }
291 if (query == "reed_damping") {
292 return m_reed_damping.load(std::memory_order_relaxed);
293 }
294 if (query == "noise") {
295 return m_noise.load(std::memory_order_relaxed);
296 }
297 if (query == "noise_cutoff") {
298 return m_noise_cutoff.load(std::memory_order_relaxed);
299 }
300 if (query == "reed_area") {
301 return m_reed_area.load(std::memory_order_relaxed);
302 }
303 if (query == "lip") {
304 return m_lip.load(std::memory_order_relaxed);
305 }
306 if (query == "unique") {
307 return m_unique.load(std::memory_order_relaxed) ? 1.0 : 0.0;
308 }
309 return std::nullopt;
310}
311
312//-----------------------------------------------------------------------------
313// Radiation
314//-----------------------------------------------------------------------------
315
316RadiationOperator::RadiationOperator(Acoustics::RadiationModel model)
317 : m_model(model)
318{
319}
320
321std::optional<Acoustics::RadiationCoefficients> RadiationOperator::coefficients_for(
322 const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end) const
323{
324 if (!grid_usable(grid)) {
325 return std::nullopt;
326 }
327
328 const double radius = end_radius_of(grid, end);
329 return Acoustics::make_radiation_coefficients(
330 { .model = m_model,
331 .radius = radius,
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));
337}
338
339bool RadiationOperator::accepts(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end) const
340{
341 return coefficients_for(grid, spec, end).has_value();
342}
343
344void RadiationOperator::prepare(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end)
345{
346 const auto coefficients = coefficients_for(grid, spec, end);
347 m_ready = coefficients.has_value();
348 if (m_ready) {
349 m_coefficients = *coefficients;
350 }
351 m_end_gain = end_gain_of(grid, end);
352 m_state = {};
353}
354
355void RadiationOperator::retarget(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& spec, DuctEnd end)
356{
357 const auto coefficients = coefficients_for(grid, spec, end);
358 m_ready = coefficients.has_value();
359 if (m_ready) {
360 m_coefficients = *coefficients;
361 }
362 m_end_gain = end_gain_of(grid, end);
363}
364
366{
367 m_state = {};
368}
369
370std::pair<double, double> RadiationOperator::couple(double end_pressure, double interior_flow)
371{
372 if (!m_ready) {
373 return { Acoustics::end_cell_pressure(end_pressure, m_end_gain, -interior_flow), 0.0 };
374 }
375
376 const auto [pressure, outflow] = Acoustics::radiation_step(m_coefficients, m_state, end_pressure, -interior_flow);
377 return { pressure, -outflow };
378}
379
380//-----------------------------------------------------------------------------
381// Junction
382//-----------------------------------------------------------------------------
383
384JunctionOperator::JunctionOperator(FlowLaw law, double maximum_flow, double driving_pressure)
386 , m_law(std::move(law))
387 , m_maximum_flow(maximum_flow)
388 , m_driving_pressure(driving_pressure)
389{
390}
391
392bool JunctionOperator::accepts(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& /*spec*/, DuctEnd end) const
393{
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));
396}
397
398void JunctionOperator::prepare(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& /*spec*/, DuctEnd end)
399{
400 m_end_gain = end_gain_of(grid, end);
401 reset();
402}
403
404void JunctionOperator::retarget(const Acoustics::DuctGrid& grid, const Acoustics::DuctSpec& /*spec*/, DuctEnd end)
405{
406 m_end_gain = end_gain_of(grid, end);
407}
408
410{
411 m_guess = 0.0;
412 m_status.store(0, std::memory_order_relaxed);
413}
414
415std::pair<double, double> JunctionOperator::couple(double end_pressure, double interior_flow)
416{
417 if (!m_law) {
418 return { Acoustics::end_cell_pressure(end_pressure, m_end_gain, -interior_flow), 0.0 };
419 }
420
421 const double driving = m_driving_pressure.load(std::memory_order_relaxed);
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);
425
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);
429
430 m_guess = difference;
431 m_status.store(static_cast<uint8_t>(status), std::memory_order_relaxed);
432 return { Acoustics::centred_end_pressure(driving, difference, end_pressure), flow };
433}
434
435void JunctionOperator::process(float /*frames*/)
436{
439}
440
441void JunctionOperator::set_parameter(std::string_view param, double value)
442{
444 if (param == "driving_pressure") {
445 put(m_driving_pressure, value, Rule::ANY);
446 } else if (param == "maximum_flow") {
447 put(m_maximum_flow, value, Rule::POSITIVE);
448 }
449}
450
452{
453 if (const auto driving = m_controls.driving_pressure.pending()) {
454 put(m_driving_pressure, *driving, Rule::ANY);
455 }
456 if (const auto largest = m_controls.maximum_flow.pending()) {
457 put(m_maximum_flow, *largest, Rule::POSITIVE);
458 }
459}
460
462{
463 controls.driving_pressure.observe(m_driving_pressure.load(std::memory_order_relaxed));
464 controls.maximum_flow.observe(m_maximum_flow.load(std::memory_order_relaxed));
465}
466
467std::optional<double> JunctionOperator::query_state(std::string_view query) const
468{
469 if (query == "driving_pressure") {
470 return m_driving_pressure.load(std::memory_order_relaxed);
471 }
472 if (query == "maximum_flow") {
473 return m_maximum_flow.load(std::memory_order_relaxed);
474 }
475 if (query == "status") {
476 return static_cast<double>(m_status.load(std::memory_order_relaxed));
477 }
478 return std::nullopt;
479}
480
481} // namespace MayaFlux::Nodes::Network
std::optional< T > pending() noexcept
The value to apply now as type T, or nullopt when nothing is due.
Definition Control.hpp:94
void observe(T actual) noexcept
Shows the value the owner really has, unless a source is feeding it.
Definition Control.hpp:134
One named control of a network or operator: empty, a value, or a mapped source.
Definition Control.hpp:34
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
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::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.
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
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.
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)
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::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.
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.
void process(float frames) override
Process for one batch cycle.
std::pair< double, double > couple(double end_pressure, double interior_flow) override
Advance the end cell and its coupling by one sample.
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.