@woosh/meep-engine 3.74.0 → 3.75.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/CHANGELOG.md +20 -19
  2. package/package.json +1 -1
  3. package/src/core/geom/2d/hilbert/v2_hilbert_encode.d.ts +11 -0
  4. package/src/core/geom/2d/hilbert/v2_hilbert_encode.d.ts.map +1 -0
  5. package/src/core/geom/2d/hilbert/v2_hilbert_encode.js +46 -0
  6. package/src/engine/graphics/render/frame_graph/FrameGraph.d.ts +6 -0
  7. package/src/engine/graphics/render/frame_graph/FrameGraph.d.ts.map +1 -1
  8. package/src/engine/graphics/render/frame_graph/FrameGraph.js +9 -0
  9. package/src/engine/graphics3/GPUAcousticSystem.d.ts +29 -32
  10. package/src/engine/graphics3/GPUAcousticSystem.d.ts.map +1 -1
  11. package/src/engine/graphics3/GPUAcousticSystem.js +87 -162
  12. package/src/engine/physics/fluid/ecs/FluidObstacleSystem.d.ts +4 -4
  13. package/src/engine/sound/ecs/audio/AudioEmitterSystem.d.ts.map +1 -1
  14. package/src/engine/sound/ecs/audio/AudioEmitterSystem.js +35 -17
  15. package/src/engine/sound/ecs/audio/LiveEmitterSet.d.ts.map +1 -1
  16. package/src/engine/sound/ecs/audio/LiveEmitterSet.js +2 -3
  17. package/src/engine/sound/simulation/AcousticSimulator.d.ts +7 -0
  18. package/src/engine/sound/simulation/AcousticSimulator.d.ts.map +1 -1
  19. package/src/engine/sound/simulation/AcousticSimulator.js +27 -0
  20. package/src/engine/sound/simulation/README.md +121 -107
  21. package/src/engine/sound/sopra/README.md +8 -3
  22. package/src/engine/sound/sopra/definition/effect/AcousticReverbEffect.d.ts +2 -2
  23. package/src/engine/sound/sopra/definition/effect/AcousticReverbEffect.d.ts.map +1 -1
  24. package/src/engine/sound/sopra/definition/effect/AcousticReverbEffect.js +4 -3
  25. package/src/engine/sound/sopra/runtime/AcousticReverbConvolution.d.ts +118 -0
  26. package/src/engine/sound/sopra/runtime/AcousticReverbConvolution.d.ts.map +1 -0
  27. package/src/engine/sound/sopra/runtime/AcousticReverbConvolution.js +472 -0
  28. package/src/engine/sound/sopra/runtime/AcousticReverbLines.d.ts +166 -0
  29. package/src/engine/sound/sopra/runtime/AcousticReverbLines.d.ts.map +1 -0
  30. package/src/engine/sound/sopra/runtime/AcousticReverbLines.js +447 -0
  31. package/src/engine/sound/sopra/runtime/AcousticReverbNetwork.d.ts +69 -113
  32. package/src/engine/sound/sopra/runtime/AcousticReverbNetwork.d.ts.map +1 -1
  33. package/src/engine/sound/sopra/runtime/AcousticReverbNetwork.js +143 -491
  34. package/src/engine/sound/sopra/runtime/AcousticSpeakers.d.ts +102 -0
  35. package/src/engine/sound/sopra/runtime/AcousticSpeakers.d.ts.map +1 -0
  36. package/src/engine/sound/sopra/runtime/AcousticSpeakers.js +232 -0
  37. package/src/engine/sound/sopra/runtime/BusGraph.d.ts +17 -3
  38. package/src/engine/sound/sopra/runtime/BusGraph.d.ts.map +1 -1
  39. package/src/engine/sound/sopra/runtime/BusGraph.js +55 -5
  40. package/src/engine/sound/sopra/runtime/EventInstance.d.ts +8 -6
  41. package/src/engine/sound/sopra/runtime/EventInstance.d.ts.map +1 -1
  42. package/src/engine/sound/sopra/runtime/EventInstance.js +113 -105
  43. package/src/engine/sound/sopra/runtime/acoustic_reverb_decay.d.ts +13 -0
  44. package/src/engine/sound/sopra/runtime/acoustic_reverb_decay.d.ts.map +1 -0
  45. package/src/engine/sound/sopra/runtime/acoustic_reverb_decay.js +15 -0
  46. package/src/generation/grid/generation/discrete/GridTaskConnectRooms.d.ts.map +1 -1
  47. package/src/generation/grid/generation/discrete/GridTaskConnectRooms.js +4 -6
  48. package/src/shade/playground/skinned_blas_refit/diff_traced_against_rasterized.d.ts.map +1 -1
  49. package/src/shade/playground/skinned_blas_refit/diff_traced_against_rasterized.js +4 -5
  50. package/src/shade/playground/skinned_blas_refit/main.d.ts.map +1 -1
  51. package/src/shade/playground/skinned_blas_refit/main.js +6 -12
  52. package/src/shade/playground/skinned_mesh_soup/main.d.ts.map +1 -1
  53. package/src/shade/playground/skinned_mesh_soup/main.js +1 -2
  54. package/src/shade/playground/vgeo_scene/README.md +12 -2
  55. package/src/shade/playground/vgeo_scene/main.js +1 -1
  56. package/src/shade/playground/volumetrics_froxel/README.md +191 -191
  57. package/src/shade/playground/volumetrics_froxel/measure.d.ts +3 -3
  58. package/src/shade/playground/volumetrics_froxel/measure.d.ts.map +1 -1
  59. package/src/shade/playground/volumetrics_froxel/measure.js +11 -13
  60. package/src/shade/playground/vsm/main.d.ts.map +1 -1
  61. package/src/shade/playground/vsm/main.js +5 -6
  62. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  63. package/src/shade/renderer/Renderer.js +31 -8
  64. package/src/shade/renderer/acoustics/ACOUSTIC_LISTENER_RESOURCES.d.ts +28 -0
  65. package/src/shade/renderer/acoustics/ACOUSTIC_LISTENER_RESOURCES.d.ts.map +1 -0
  66. package/src/shade/renderer/acoustics/ACOUSTIC_LISTENER_RESOURCES.js +42 -0
  67. package/src/shade/renderer/acoustics/ACOUSTIC_PROBE_DATABASE_SPEC.d.ts +58 -0
  68. package/src/shade/renderer/acoustics/ACOUSTIC_PROBE_DATABASE_SPEC.d.ts.map +1 -0
  69. package/src/shade/renderer/acoustics/ACOUSTIC_PROBE_DATABASE_SPEC.js +108 -0
  70. package/src/shade/renderer/acoustics/GPUAcousticLattice.d.ts +1 -1
  71. package/src/shade/renderer/acoustics/GPUAcousticProbes.d.ts +161 -0
  72. package/src/shade/renderer/acoustics/GPUAcousticProbes.d.ts.map +1 -0
  73. package/src/shade/renderer/acoustics/GPUAcousticProbes.js +760 -0
  74. package/src/shade/renderer/acoustics/GPUAcoustics.d.ts +1 -1
  75. package/src/shade/renderer/acoustics/acoustic_listener_order.d.ts +12 -0
  76. package/src/shade/renderer/acoustics/acoustic_listener_order.d.ts.map +1 -0
  77. package/src/shade/renderer/acoustics/acoustic_listener_order.js +56 -0
  78. package/src/shade/renderer/acoustics/chunk_acoustic_direct.d.ts.map +1 -1
  79. package/src/shade/renderer/acoustics/chunk_acoustic_direct.js +5 -0
  80. package/src/shade/renderer/acoustics/chunk_acoustic_direct_cone.d.ts +57 -0
  81. package/src/shade/renderer/acoustics/chunk_acoustic_direct_cone.d.ts.map +1 -0
  82. package/src/shade/renderer/acoustics/chunk_acoustic_direct_cone.js +314 -0
  83. package/src/shade/renderer/acoustics/chunk_acoustic_finite.d.ts +15 -0
  84. package/src/shade/renderer/acoustics/chunk_acoustic_finite.d.ts.map +1 -0
  85. package/src/shade/renderer/acoustics/chunk_acoustic_finite.js +21 -0
  86. package/src/shade/renderer/acoustics/chunk_acoustic_gather.js +1 -1
  87. package/src/shade/renderer/acoustics/chunk_acoustic_lean.d.ts +27 -0
  88. package/src/shade/renderer/acoustics/chunk_acoustic_lean.d.ts.map +1 -0
  89. package/src/shade/renderer/acoustics/chunk_acoustic_lean.js +42 -0
  90. package/src/shade/renderer/acoustics/chunk_acoustic_listener.d.ts +80 -0
  91. package/src/shade/renderer/acoustics/chunk_acoustic_listener.d.ts.map +1 -0
  92. package/src/shade/renderer/acoustics/chunk_acoustic_listener.js +274 -0
  93. package/src/shade/renderer/acoustics/chunk_acoustic_partials.d.ts +2 -2
  94. package/src/shade/renderer/acoustics/chunk_acoustic_partials.js +2 -2
  95. package/src/shade/renderer/acoustics/chunk_acoustic_phonon.d.ts +7 -0
  96. package/src/shade/renderer/acoustics/chunk_acoustic_phonon.d.ts.map +1 -1
  97. package/src/shade/renderer/acoustics/chunk_acoustic_phonon.js +24 -1
  98. package/src/shade/renderer/acoustics/chunk_acoustic_resample.js +1 -1
  99. package/src/shade/renderer/acoustics/chunk_acoustic_reservoir.js +1 -1
  100. package/src/shade/renderer/acoustics/chunk_acoustic_through.d.ts +43 -0
  101. package/src/shade/renderer/acoustics/chunk_acoustic_through.d.ts.map +1 -0
  102. package/src/shade/renderer/acoustics/chunk_acoustic_through.js +153 -0
  103. package/src/shade/renderer/acoustics/chunk_acoustic_trace.d.ts +3 -1
  104. package/src/shade/renderer/acoustics/chunk_acoustic_trace.d.ts.map +1 -1
  105. package/src/shade/renderer/acoustics/chunk_acoustic_trace.js +10 -1
  106. package/src/shade/renderer/acoustics/shader_acoustic_combine.d.ts +7 -3
  107. package/src/shade/renderer/acoustics/shader_acoustic_combine.d.ts.map +1 -1
  108. package/src/shade/renderer/acoustics/shader_acoustic_combine.js +22 -6
  109. package/src/shade/renderer/acoustics/shader_acoustic_coupling.d.ts +14 -0
  110. package/src/shade/renderer/acoustics/shader_acoustic_coupling.d.ts.map +1 -0
  111. package/src/shade/renderer/acoustics/shader_acoustic_coupling.js +102 -0
  112. package/src/shade/renderer/acoustics/shader_acoustic_listener.d.ts +12 -0
  113. package/src/shade/renderer/acoustics/shader_acoustic_listener.d.ts.map +1 -0
  114. package/src/shade/renderer/acoustics/shader_acoustic_listener.js +80 -0
  115. package/src/shade/renderer/acoustics/shader_acoustic_receive.d.ts.map +1 -1
  116. package/src/shade/renderer/acoustics/shader_acoustic_receive.js +27 -6
  117. package/src/shade/renderer/acoustics/shader_acoustic_room.d.ts +11 -0
  118. package/src/shade/renderer/acoustics/shader_acoustic_room.d.ts.map +1 -0
  119. package/src/shade/renderer/acoustics/shader_acoustic_room.js +111 -0
  120. package/src/shade/renderer/acoustics/shader_acoustic_source.d.ts +18 -0
  121. package/src/shade/renderer/acoustics/shader_acoustic_source.d.ts.map +1 -0
  122. package/src/shade/renderer/acoustics/shader_acoustic_source.js +130 -0
  123. package/src/shade/renderer/fluid/GPUFluidCascade.d.ts +3 -3
  124. package/src/shade/renderer/fluid/GPUFluidCascadeStack.d.ts +1 -1
  125. package/src/shade/renderer/fluid/forcing/GPUFluidForcing.d.ts +7 -7
  126. package/src/shade/renderer/fluid/pressure/GPUFluidPressureSolve.d.ts +1 -1
  127. package/src/shade/renderer/fluid/query/GPUFluidQueries.d.ts +1 -1
  128. package/src/shade/renderer/geometry/virtual/build/level/VGeoFrontier.d.ts +2 -2
  129. package/src/shade/renderer/geometry/virtual/build/level/VGeoFrontier.js +2 -2
  130. package/src/shade/renderer/geometry/virtual/build/level/VGeoLevelOptions.d.ts +5 -2
  131. package/src/shade/renderer/geometry/virtual/build/level/VGeoLevelOptions.d.ts.map +1 -1
  132. package/src/shade/renderer/geometry/virtual/build/level/VGeoLevelOptions.js +5 -2
  133. package/src/shade/renderer/geometry/virtual/build/level/vgeo_build_group_step.d.ts +9 -2
  134. package/src/shade/renderer/geometry/virtual/build/level/vgeo_build_group_step.d.ts.map +1 -1
  135. package/src/shade/renderer/geometry/virtual/build/level/vgeo_build_group_step.js +48 -12
  136. package/src/shade/renderer/geometry/virtual/format/read/VGeoContainerReader.d.ts +0 -22
  137. package/src/shade/renderer/geometry/virtual/format/read/VGeoContainerReader.d.ts.map +1 -1
  138. package/src/shade/renderer/geometry/virtual/format/read/VGeoContainerReader.js +0 -42
  139. package/src/shade/renderer/geometry/virtual/format/read/VGeoReadOptions.d.ts +2 -18
  140. package/src/shade/renderer/geometry/virtual/format/read/VGeoReadOptions.d.ts.map +1 -1
  141. package/src/shade/renderer/geometry/virtual/format/read/VGeoReadOptions.js +2 -19
  142. package/src/shade/renderer/geometry/virtual/format/read/vgeo_page_worker_serve.d.ts.map +1 -1
  143. package/src/shade/renderer/geometry/virtual/format/read/vgeo_page_worker_serve.js +0 -1
  144. package/src/shade/renderer/geometry/virtual/runtime/GPUVirtualGeometryRuntime.d.ts +5 -4
  145. package/src/shade/renderer/geometry/virtual/runtime/GPUVirtualGeometryRuntime.d.ts.map +1 -1
  146. package/src/shade/renderer/geometry/virtual/runtime/GPUVirtualGeometryRuntime.js +3 -1
  147. package/src/shade/renderer/geometry/virtual/runtime/VGeoFeedbackLoop.d.ts.map +1 -1
  148. package/src/shade/renderer/geometry/virtual/runtime/VGeoFeedbackLoop.js +3 -1
  149. package/src/shade/renderer/geometry/virtual/runtime/VGeoPageFetcher.d.ts.map +1 -1
  150. package/src/shade/renderer/geometry/virtual/runtime/VGeoPageFetcher.js +0 -1
  151. package/src/shade/renderer/geometry/virtual/runtime/VGeoViewCut.d.ts +60 -14
  152. package/src/shade/renderer/geometry/virtual/runtime/VGeoViewCut.d.ts.map +1 -1
  153. package/src/shade/renderer/geometry/virtual/runtime/VGeoViewCut.js +75 -39
  154. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts +0 -9
  155. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts.map +1 -1
  156. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.js +45 -17
  157. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts +7 -3
  158. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.d.ts.map +1 -1
  159. package/src/shade/renderer/global_illumination/probe_clipmap/brick/chunk_brick_clipmap_trace.js +43 -7
  160. package/src/shade/renderer/loader/gltf/tiny-gltf.d.ts +1 -1
  161. package/src/shade/renderer/water/query/GPUWaterQueries.d.ts +1 -1
@@ -0,0 +1,472 @@
1
+ import { seededRandom } from "../../../../core/math/random/seededRandom.js";
2
+ import { LOW_MID_HZ, MID_HIGH_HZ } from "../util/reverb_band_tails.js";
3
+ import { acoustic_ramp, ACOUSTIC_GAIN_DEADBAND } from "./acoustic_writes.js";
4
+ import { RT60_PER_CENTRE_TIME } from "./acoustic_reverb_decay.js";
5
+
6
+ /**
7
+ * Centre times a new impulse response is fed the live signal for before it is heard: `ln(1 / (1 − 10^(−d/10)))` for
8
+ * `d` the write rule's half decibel. A convolver's state is emptied when it takes a response, and an exponential tail
9
+ * builds to `1 − e^(−t/τ)` of its steady energy in `t`; after this long it holds all but half a decibel of it, so the
10
+ * switch to it is not heard as a dip. Derived.
11
+ * @type {number}
12
+ */
13
+ export const ACOUSTIC_CONVOLUTION_WARMUP = Math.log(1 / (1 - 1 / (ACOUSTIC_GAIN_DEADBAND * ACOUSTIC_GAIN_DEADBAND)));
14
+
15
+ /**
16
+ * Seed of the impulse responses' noise: the same response for the same decay, run to run.
17
+ * @type {number}
18
+ */
19
+ const NOISE_SEED = 1;
20
+
21
+ /** No slot. */
22
+ const NONE = -1;
23
+
24
+ /**
25
+ * One of the tank's two convolver pairs: its send, its stereo and its mono convolver, their output gains, and the
26
+ * buffers it last took (grown, never shrunk).
27
+ */
28
+ class ConvolutionSlot {
29
+ /** @type {GainNode} */
30
+ send;
31
+
32
+ /** @type {ConvolverNode} */
33
+ stereo;
34
+
35
+ /** @type {ConvolverNode} */
36
+ mono;
37
+
38
+ /** What its stereo convolver is heard through: 1 for the pair heard. @type {GainNode} */
39
+ heard;
40
+
41
+ /** What its mono convolver is heard through, alike. @type {GainNode} */
42
+ heard_mono;
43
+
44
+ /** @type {AudioBuffer|null} */
45
+ stereo_buffer = null;
46
+
47
+ /** @type {AudioBuffer|null} */
48
+ mono_buffer = null;
49
+
50
+ /** The centre times its response decays by, per band; NaN before it took one. */
51
+ centre = new Float64Array([NaN, NaN, NaN]);
52
+ }
53
+
54
+ /** Channels a response has: the pair's two and the mono sum's one, each its own noise. */
55
+ const CHANNELS = 3;
56
+
57
+ /**
58
+ * A Butterworth second-order section's coefficients, `[b0, b1, b2, a1, a2]` over `a0` (the Audio EQ Cookbook's, Q
59
+ * `1/√2`): two in a row are a fourth-order Linkwitz–Riley filter, whose low and high outputs at one edge sum flat.
60
+ *
61
+ * @param {boolean} high
62
+ * @param {number} frequency Hz
63
+ * @param {number} sample_rate
64
+ * @returns {Float64Array}
65
+ */
66
+ function butterworth(high, frequency, sample_rate) {
67
+ const w0 = 2 * Math.PI * frequency / sample_rate;
68
+ const cos = Math.cos(w0);
69
+ const alpha = Math.sin(w0) / Math.SQRT2;
70
+ const a0 = 1 + alpha;
71
+ const b0 = (high ? 1 + cos : 1 - cos) / 2;
72
+
73
+ return new Float64Array([b0 / a0, (high ? -2 : 2) * b0 / a0, b0 / a0, -2 * cos / a0, (1 - alpha) / a0]);
74
+ }
75
+
76
+ /**
77
+ * Run a section over `input` into `output` (which may be `input`), from rest.
78
+ *
79
+ * @param {Float64Array} c
80
+ * @param {Float32Array} input
81
+ * @param {Float32Array} output
82
+ */
83
+ function run_section(c, input, output) {
84
+ let x1 = 0, x2 = 0, y1 = 0, y2 = 0;
85
+
86
+ for (let i = 0; i < input.length; i++) {
87
+ const x = input[i];
88
+ const y = c[0] * x + c[1] * x1 + c[2] * x2 - c[3] * y1 - c[4] * y2;
89
+
90
+ x2 = x1;
91
+ x1 = x;
92
+ y2 = y1;
93
+ y1 = y;
94
+ output[i] = y;
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Fill three bands with one noise split at {@link LOW_MID_HZ} and {@link MID_HIGH_HZ} by fourth-order Linkwitz–Riley
100
+ * crossovers: each band passes at unity and falls 24 dB an octave past its edges, so a band of the response holds its
101
+ * own decay where a one-pole split let the next band's into it (the high band of a dark room read 23 to 31% long).
102
+ *
103
+ * @param {Float32Array} low
104
+ * @param {Float32Array} mid
105
+ * @param {Float32Array} high
106
+ * @param {number} sample_rate
107
+ * @param {function(): number} random
108
+ */
109
+ function split_noise(low, mid, high, sample_rate, random) {
110
+ const length = low.length;
111
+
112
+ for (let i = 0; i < length; i++) {
113
+ high[i] = random() * 2 - 1;
114
+ }
115
+
116
+ const low_lp = butterworth(false, LOW_MID_HZ, sample_rate);
117
+ const low_hp = butterworth(true, LOW_MID_HZ, sample_rate);
118
+ const high_lp = butterworth(false, MID_HIGH_HZ, sample_rate);
119
+ const high_hp = butterworth(true, MID_HIGH_HZ, sample_rate);
120
+
121
+ // low: the noise below the first edge
122
+ run_section(low_lp, high, low);
123
+ run_section(low_lp, low, low);
124
+
125
+ // the rest above it, split again at the second edge
126
+ run_section(low_hp, high, high);
127
+ run_section(low_hp, high, high);
128
+
129
+ run_section(high_lp, high, mid);
130
+ run_section(high_lp, mid, mid);
131
+
132
+ run_section(high_hp, high, high);
133
+ run_section(high_hp, high, high);
134
+ }
135
+
136
+ /**
137
+ * Fill `out` with one channel of a response: the channel's three noise bands, each under its own exponential,
138
+ * `e^(−t/2τ)` in amplitude for centre time `τ`, scaled so that a band-limited input of energy `E` comes out with energy
139
+ * `E·share`. A band passes its own frequencies at unity, the noise has variance `1/3`, and `Σ e^(−n/τs)` is
140
+ * `1/(1 − e^(−1/τs))`, so the scale is `√(3(1 − e^(−1/τs))·share)`.
141
+ *
142
+ * @param {Float32Array} out
143
+ * @param {Float32Array} low
144
+ * @param {Float32Array} mid
145
+ * @param {Float32Array} high
146
+ * @param {ArrayLike<number>} centre centre times per band, seconds
147
+ * @param {number} share energy each band carries out
148
+ * @param {number} sample_rate
149
+ */
150
+ function write_response(out, low, mid, high, centre, share, sample_rate) {
151
+ const k0 = Math.exp(-1 / (centre[0] * sample_rate));
152
+ const k1 = Math.exp(-1 / (centre[1] * sample_rate));
153
+ const k2 = Math.exp(-1 / (centre[2] * sample_rate));
154
+
155
+ let e0 = Math.sqrt(3 * (1 - k0) * share);
156
+ let e1 = Math.sqrt(3 * (1 - k1) * share);
157
+ let e2 = Math.sqrt(3 * (1 - k2) * share);
158
+
159
+ const d0 = Math.sqrt(k0);
160
+ const d1 = Math.sqrt(k1);
161
+ const d2 = Math.sqrt(k2);
162
+
163
+ const length = out.length;
164
+
165
+ for (let i = 0; i < length; i++) {
166
+ out[i] = low[i] * e0 + mid[i] * e1 + high[i] * e2;
167
+
168
+ e0 *= d0;
169
+ e1 *= d1;
170
+ e2 *= d2;
171
+ }
172
+ }
173
+
174
+ /**
175
+ * # A tank of the acoustic reverb: convolution with a decaying noise
176
+ *
177
+ * What `AcousticReverbEffect` builds. Each band's tail is white noise under an exponential of the band's centre time,
178
+ * the three bands split at the edges every band split in the engine has ({@link LOW_MID_HZ}, {@link MID_HIGH_HZ}),
179
+ * by fourth-order Linkwitz–Riley crossovers (`split_noise`): a Gaussian
180
+ * tail from the first sample, so an impulse comes out as a smooth decay, with no echoes a line long and nothing that
181
+ * circulates (a loop of Web Audio nodes is not played exactly as wired: `simulation/PATH_TRACED_RESEARCH.md`).
182
+ *
183
+ * **Outputs.** {@link stereo}, the diffuse pair, two independent noises at half the energy each; {@link mono}, the
184
+ * directional share, a third. All three are uncorrelated, so the diffuse and directional shares' energies add.
185
+ *
186
+ * **Level.** A band of a send of energy `E` comes out with energy `E` over the pair, and `E` from the mono sum
187
+ * (`write_response`), whatever the decay: the send enters as it is ({@link send_gain} is 1).
188
+ *
189
+ * **A new decay.** A convolver empties its state when it takes a response, so a response swapped in under the signal
190
+ * would start from silence, and a tail cut there would be heard. The tank has two convolver pairs, each always fed:
191
+ * the one heard, and one a new response is put in. The new one is fed the live signal, silent, for
192
+ * {@link ACOUSTIC_CONVOLUTION_WARMUP} of its longest band's centre time, by when its tail has built to within half a
193
+ * decibel of steady; then the two cross over within one update interval. A decay asked for while one is warming waits
194
+ * for it, and is put in next. The first decay is heard at once.
195
+ *
196
+ * A response is at least as long as its longest band takes to fall 60 dB. A pair's buffers are made at its first decay
197
+ * and made again only when a longer one is needed; a shorter decay fills the buffer it has, falling on past 60 dB.
198
+ */
199
+ export class AcousticReverbConvolution {
200
+ /**
201
+ * Where the send enters: it feeds both pairs.
202
+ * @type {GainNode}
203
+ */
204
+ input;
205
+
206
+ /**
207
+ * The diffuse pair.
208
+ * @type {GainNode}
209
+ */
210
+ stereo;
211
+
212
+ /**
213
+ * The directional sum.
214
+ * @type {GainNode}
215
+ */
216
+ mono;
217
+
218
+ /**
219
+ * The shortest centre time the tank plays, seconds: a period of the lowest band edge, the shortest tail that can
220
+ * carry its low band.
221
+ * @type {number}
222
+ */
223
+ shortest = 1 / LOW_MID_HZ;
224
+
225
+ /**
226
+ * The two convolver pairs. For inspection.
227
+ * @type {ConvolutionSlot[]}
228
+ */
229
+ slots = [new ConvolutionSlot(), new ConvolutionSlot()];
230
+
231
+ /** The slot heard. */
232
+ #heard = 0;
233
+
234
+ /** The slot warming, {@link NONE} for none, and the context time it is warm by. */
235
+ #warming = NONE;
236
+ #warm_at = 0;
237
+
238
+ /** The decay asked for while one was warming, per band; NaN for none. */
239
+ #pending = new Float64Array([NaN, NaN, NaN]);
240
+
241
+ /** @type {BaseAudioContext} */
242
+ #ctx;
243
+
244
+ /**
245
+ * Each channel's three noise bands, `[channel][band]`, made at the first decay and made again only when a longer
246
+ * response is needed: a decay is only their envelopes.
247
+ * @type {Float32Array[][]}
248
+ */
249
+ #noise = [];
250
+
251
+ /**
252
+ * @param {BaseAudioContext} ctx
253
+ */
254
+ constructor(ctx) {
255
+ this.#ctx = ctx;
256
+
257
+ this.input = ctx.createGain();
258
+ this.stereo = ctx.createGain();
259
+ this.mono = ctx.createGain();
260
+
261
+ for (let k = 0; k < 2; k++) {
262
+ const slot = this.slots[k];
263
+
264
+ slot.send = ctx.createGain();
265
+ slot.stereo = ctx.createConvolver();
266
+ slot.mono = ctx.createConvolver();
267
+ slot.heard = ctx.createGain();
268
+
269
+ // the response carries its own level
270
+ slot.stereo.normalize = false;
271
+ slot.mono.normalize = false;
272
+
273
+ // silent until the first decay
274
+ slot.send.gain.setValueAtTime(0, 0);
275
+ slot.heard.gain.setValueAtTime(k === 0 ? 1 : 0, 0);
276
+
277
+ this.input.connect(slot.send);
278
+ slot.send.connect(slot.stereo);
279
+ slot.send.connect(slot.mono);
280
+ slot.stereo.connect(slot.heard);
281
+ slot.heard.connect(this.stereo);
282
+ }
283
+
284
+ // the mono sums are heard through the same gains, each its own pair's; split off ahead of them
285
+ for (let k = 0; k < 2; k++) {
286
+ const slot = this.slots[k];
287
+ const heard_mono = ctx.createGain();
288
+
289
+ heard_mono.gain.setValueAtTime(k === 0 ? 1 : 0, 0);
290
+ slot.mono.connect(heard_mono);
291
+ heard_mono.connect(this.mono);
292
+
293
+ slot.heard_mono = heard_mono;
294
+ }
295
+ }
296
+
297
+ /**
298
+ * The amplitude a band's send enters at: as it is, since the response carries the level.
299
+ *
300
+ * @param {number} centre_time seconds
301
+ * @returns {number}
302
+ */
303
+ send_gain(centre_time) {
304
+ return 1;
305
+ }
306
+
307
+ /**
308
+ * Ask for a decay: heard at once the first time, else warmed in the other pair and crossed over to when warm.
309
+ *
310
+ * @param {ArrayLike<number>} centre centre times, seconds, per band
311
+ * @param {ArrayLike<number>} sent the centre times last asked for; unread
312
+ * @param {number} now context time
313
+ * @param {number} interval seconds a crossover takes; 0 is the first decay, heard at once
314
+ * @param {boolean} resting unread: the tank's gains are its own
315
+ */
316
+ set_decay(centre, sent, now, interval, resting) {
317
+ if (interval === 0) {
318
+ const slot = this.slots[this.#heard];
319
+
320
+ this.#load(slot, centre);
321
+
322
+ slot.send.gain.setValueAtTime(1, now);
323
+
324
+ return;
325
+ }
326
+
327
+ if (this.#warming !== NONE) {
328
+ this.#pending[0] = centre[0];
329
+ this.#pending[1] = centre[1];
330
+ this.#pending[2] = centre[2];
331
+
332
+ return;
333
+ }
334
+
335
+ this.#warm(centre, now);
336
+ }
337
+
338
+ /**
339
+ * Each frame: a warm pair crosses over, and a decay that waited is put in.
340
+ *
341
+ * @param {number} now context time
342
+ * @param {number} interval seconds a crossover takes
343
+ */
344
+ update(now, interval) {
345
+ const warming = this.#warming;
346
+
347
+ if (warming === NONE || now < this.#warm_at) {
348
+ return;
349
+ }
350
+
351
+ const heard = this.slots[this.#heard];
352
+ const next = this.slots[warming];
353
+
354
+ acoustic_ramp(heard.heard.gain, 0, now, interval, 1);
355
+ acoustic_ramp(heard.heard_mono.gain, 0, now, interval, 1);
356
+ acoustic_ramp(next.heard.gain, 1, now, interval, 0);
357
+ acoustic_ramp(next.heard_mono.gain, 1, now, interval, 0);
358
+
359
+ // the pair no longer heard stops taking the signal once it is out of hearing
360
+ acoustic_ramp(heard.send.gain, 0, now, interval, 1);
361
+
362
+ this.#heard = warming;
363
+ this.#warming = NONE;
364
+
365
+ const pending = this.#pending;
366
+
367
+ if (pending[0] === pending[0]) {
368
+ this.#warm(pending, now + interval);
369
+
370
+ pending.fill(NaN);
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Put a decay in the pair not heard and start feeding it.
376
+ *
377
+ * @param {ArrayLike<number>} centre
378
+ * @param {number} now
379
+ */
380
+ #warm(centre, now) {
381
+ const index = 1 - this.#heard;
382
+ const slot = this.slots[index];
383
+
384
+ this.#load(slot, centre);
385
+
386
+ // fed from now, silent: its state starts empty
387
+ slot.send.gain.setValueAtTime(1, now);
388
+
389
+ this.#warming = index;
390
+ this.#warm_at = now + ACOUSTIC_CONVOLUTION_WARMUP * Math.max(centre[0], centre[1], centre[2]);
391
+ }
392
+
393
+ /**
394
+ * Write a decay's responses into a pair's buffers and hand them to its convolvers.
395
+ *
396
+ * @param {ConvolutionSlot} slot
397
+ * @param {ArrayLike<number>} centre
398
+ */
399
+ #load(slot, centre) {
400
+ const ctx = this.#ctx;
401
+ const sample_rate = ctx.sampleRate;
402
+ const longest = Math.max(centre[0], centre[1], centre[2]);
403
+ const length = Math.max(1, Math.ceil(RT60_PER_CENTRE_TIME * longest * sample_rate));
404
+
405
+ // grown, never shrunk: a longer buffer holds the response on past its 60 dB, where it is all but silent
406
+ if (slot.stereo_buffer === null || slot.stereo_buffer.length < length) {
407
+ slot.stereo_buffer = ctx.createBuffer(2, length, sample_rate);
408
+ slot.mono_buffer = ctx.createBuffer(1, length, sample_rate);
409
+ }
410
+
411
+ const noise = this.#noise_of(slot.stereo_buffer.length, sample_rate);
412
+
413
+ // the pair carries half the energy a side; the mono sum all of it
414
+ write_response(slot.stereo_buffer.getChannelData(0), noise[0][0], noise[0][1], noise[0][2], centre, 0.5, sample_rate);
415
+ write_response(slot.stereo_buffer.getChannelData(1), noise[1][0], noise[1][1], noise[1][2], centre, 0.5, sample_rate);
416
+ write_response(slot.mono_buffer.getChannelData(0), noise[2][0], noise[2][1], noise[2][2], centre, 1, sample_rate);
417
+
418
+ slot.stereo.buffer = slot.stereo_buffer;
419
+ slot.mono.buffer = slot.mono_buffer;
420
+
421
+ slot.centre[0] = centre[0];
422
+ slot.centre[1] = centre[1];
423
+ slot.centre[2] = centre[2];
424
+ }
425
+
426
+ /**
427
+ * The channels' noise bands, at least `length` long: made again from the seed when longer ones are needed, so the
428
+ * noise is the same for the same length, run to run.
429
+ *
430
+ * @param {number} length
431
+ * @param {number} sample_rate
432
+ * @returns {Float32Array[][]}
433
+ */
434
+ #noise_of(length, sample_rate) {
435
+ const noise = this.#noise;
436
+
437
+ if (noise.length === CHANNELS && noise[0][0].length >= length) {
438
+ return noise;
439
+ }
440
+
441
+ const random = seededRandom(NOISE_SEED);
442
+
443
+ noise.length = 0;
444
+
445
+ for (let channel = 0; channel < CHANNELS; channel++) {
446
+ const bands = [new Float32Array(length), new Float32Array(length), new Float32Array(length)];
447
+
448
+ split_noise(bands[0], bands[1], bands[2], sample_rate, random);
449
+ noise.push(bands);
450
+ }
451
+
452
+ return noise;
453
+ }
454
+
455
+ /**
456
+ * Unwire the tank.
457
+ */
458
+ disconnect() {
459
+ this.input.disconnect();
460
+
461
+ for (const slot of this.slots) {
462
+ slot.send.disconnect();
463
+ slot.stereo.disconnect();
464
+ slot.mono.disconnect();
465
+ slot.heard.disconnect();
466
+ slot.heard_mono.disconnect();
467
+ }
468
+
469
+ this.stereo.disconnect();
470
+ this.mono.disconnect();
471
+ }
472
+ }
@@ -0,0 +1,166 @@
1
+ /**
2
+ * The energy of the tail an impulse of unit amplitude into every line (±1) leaves, at centre time `T_s`, seconds:
3
+ * what an input gain of `1 / sqrt` of it turns into a tail of the energy sent.
4
+ *
5
+ * Every sample written to a line is read out once, then filtered and mixed back. Follow a unit of energy: read from
6
+ * line `i` (one unit out), it keeps `e_i = e^(−D_i / T_s)` of its energy through the line's filters (Jot's rule,
7
+ * squared), and the Householder matrix sends the share `A_ji²` of that into line `j`, which is `(1 − 2/N)²` back
8
+ * into its own line and `4/N²` into each other. What it reads out in all, from line `i`, is then
9
+ * `v_i = 1 + e_i Σ_j A_ji² v_j`, which with `α = 1 − 4/N` and `β = 4/N²` is
10
+ * `v_i = (1 + β e_i S) / (1 − α e_i)`, `S = Σ v_i`, and summing,
11
+ *
12
+ * S = Σ 1/(1 − α e_i) / (1 − β Σ e_i/(1 − α e_i))
13
+ *
14
+ * in expectation over the lines' phases, which a band of many modes averages over. Not `T_s` times a constant: a
15
+ * dead room loses most of a sample's energy in one pass, and a sample keeps to its own line half the time, so the
16
+ * read-out rate is not the lines' mean. Derived; pinned against the network rendered sample by sample.
17
+ *
18
+ * @param {number} centre_time seconds, above 0
19
+ * @returns {number}
20
+ */
21
+ export function acoustic_reverb_tail_energy(centre_time: number): number;
22
+ /**
23
+ * Delay lines of the network, `N`.
24
+ * @type {number}
25
+ */
26
+ export const ACOUSTIC_REVERB_LINES: number;
27
+ /**
28
+ * The sign each line is heard with: the even lines go left and the odd right, and within each side they alternate,
29
+ * `+ + − − + + − −`, so that each side's signs sum to zero. The Householder matrix puts `−2/N` of every line's sum
30
+ * back into every line at once; at low frequencies, where the lines' delays differ by a fraction of a period, that
31
+ * common part stays in phase across the lines, and summed with one sign it added up coherently — 1.1 to 1.8 dB more
32
+ * tail than sent in the low band, measured. With the signs it cancels.
33
+ * @type {number[]}
34
+ */
35
+ export const ACOUSTIC_REVERB_OUTPUT_SIGNS: number[];
36
+ /**
37
+ * The sign each line is summed with into the directional share: the first half straight, the second inverted. Within
38
+ * each side the signs it is heard with there ({@link ACOUSTIC_REVERB_OUTPUT_SIGNS}) and these are orthogonal, `+ − + −`
39
+ * against `+ + − −`, so the directional sum shares no signal with either side, and its energy and the diffuse pair's
40
+ * add whatever the lean. They sum to zero too, so the Householder matrix's common part cancels in it as in the
41
+ * sides.
42
+ * @type {number[]}
43
+ */
44
+ export const ACOUSTIC_REVERB_DIRECTIONAL_SIGNS: number[];
45
+ /**
46
+ * Frames a Web Audio cycle adds to the delay in it, at the default render quantum: Chromium renders a `DelayNode` in a
47
+ * cycle a render quantum late, so a line set to `d` comes round in `d + 128` frames (a context with another quantum,
48
+ * `renderQuantumSize`, adds that). A line's node is set that much short, never below one quantum, so that it comes
49
+ * round in its delay. An engine that adds none would come round 2.7 ms early, and decay 4 to 11% fast. The lines are
50
+ * chosen for quanta well under the shortest of them (1201 frames at 48 kHz); a context rendering in quanta near that
51
+ * long cannot hold their lengths apart.
52
+ *
53
+ * Chromium adds the quantum only on the loop edges it reaches out of its processing order, so the edges through the
54
+ * Householder summing gain do not all see it, and the network Chrome plays is not exactly the one modelled here
55
+ * (`simulation/PATH_TRACED_RESEARCH.md`).
56
+ * @type {number}
57
+ */
58
+ export const ACOUSTIC_REVERB_CYCLE_FRAMES: number;
59
+ /**
60
+ * Each line's delay, seconds.
61
+ * @type {Float64Array}
62
+ */
63
+ export const ACOUSTIC_REVERB_DELAYS: Float64Array;
64
+ /**
65
+ * # A tank of the acoustic reverb: one feedback delay network
66
+ *
67
+ * GPU acoustics design, "The reverb: one feedback delay network" (F1). Not what `AcousticReverbEffect` builds now
68
+ * (that is `AcousticReverbConvolution`); kept, with its specs, for the path-traced acoustics it was made for
69
+ * (`simulation/PATH_TRACED_RESEARCH.md`): it decays as an `AudioParam` moves, with no impulse response to make, but its
70
+ * echo density is low (a diffuse tail only after half a second) and Chromium's uneven loop edges keep it from being
71
+ * exactly the network modelled here.
72
+ *
73
+ * **The network.** {@link ACOUSTIC_REVERB_LINES} `DelayNode`s ({@link ACOUSTIC_REVERB_DELAYS}, each set short by what the
74
+ * cycle adds, {@link ACOUSTIC_REVERB_CYCLE_FRAMES}). Each line's output passes a lowshelf at 250 Hz, a highshelf at
75
+ * 2,500 Hz and a gain, and returns to every line through a Householder matrix, `I − (2/N)·11ᵀ`: to its own line whole,
76
+ * and to all of them through one summing gain of `−2/N`. The matrix is orthogonal, so the loop loses only what the
77
+ * lines' filters take. Fed from {@link input}, to the even lines as it is and to the odd ones inverted.
78
+ *
79
+ * **Decay (Jot's rule).** A line `d` seconds long is attenuated by `10^(−3d / RT60_b)` a pass in band `b`, so that
80
+ * every path through the network falls by 60 dB in `RT60_b` whatever lines it took. Jot and Chaigne's attenuation rule,
81
+ * here derived as stated rather than read from their paper. The three bands' per-pass gains are realised by the
82
+ * shelves and the gain together, solved at each band's centre ({@link ACOUSTIC_REVERB_BAND_CENTRES}) from the shelves'
83
+ * response there, which at a few dB scales with their gain. Shelves, not a crossover, because a crossover's bands
84
+ * overshoot near its edges and a gain above one in a feedback loop runs away; a shelf's response lies between its two
85
+ * gains. The solution is held passive: no shelf lifts a band above the gain the others leave it.
86
+ *
87
+ * **Level.** A send of energy `E` is scaled by one over the root of {@link acoustic_reverb_tail_energy} at its band's
88
+ * `T_s` ({@link send_gain}), so it leaves a tail of energy `E` at any decay time.
89
+ *
90
+ * **Outputs.** The lines go to a stereo pair, the even lines left and the odd right, each with its sign
91
+ * ({@link ACOUSTIC_REVERB_OUTPUT_SIGNS}), and are summed with other signs, orthogonal to both sides'
92
+ * ({@link ACOUSTIC_REVERB_DIRECTIONAL_SIGNS}), into {@link mono}, the directional share.
93
+ */
94
+ export class AcousticReverbLines {
95
+ /**
96
+ * @param {BaseAudioContext} ctx
97
+ */
98
+ constructor(ctx: BaseAudioContext);
99
+ /**
100
+ * Where the scaled send enters: it feeds the even lines, and its inversion the odd.
101
+ * @type {GainNode}
102
+ */
103
+ input: GainNode;
104
+ /**
105
+ * The diffuse pair.
106
+ * @type {ChannelMergerNode}
107
+ */
108
+ stereo: ChannelMergerNode;
109
+ /**
110
+ * The directional sum: every line with its sign of {@link ACOUSTIC_REVERB_DIRECTIONAL_SIGNS}.
111
+ * @type {GainNode}
112
+ */
113
+ mono: GainNode;
114
+ /**
115
+ * The shortest centre time the network can play, seconds: its first echo, as soon as it can answer.
116
+ * @type {number}
117
+ */
118
+ shortest: number;
119
+ /**
120
+ * The lines, and each line's lowshelf, highshelf and gain, by line: for inspection; {@link set_decay} moves them.
121
+ * @type {DelayNode[]}
122
+ */
123
+ delays: DelayNode[];
124
+ /** @type {BiquadFilterNode[]} */
125
+ lowShelves: BiquadFilterNode[];
126
+ /** @type {BiquadFilterNode[]} */
127
+ highShelves: BiquadFilterNode[];
128
+ /** @type {GainNode[]} */
129
+ lineGains: GainNode[];
130
+ /**
131
+ * The Householder matrix's summing gain, `−2/N`. For inspection.
132
+ * @type {GainNode}
133
+ */
134
+ householder: GainNode;
135
+ /**
136
+ * The amplitude a band's send enters at for a tail of the energy sent: one over the root of
137
+ * {@link acoustic_reverb_tail_energy}.
138
+ *
139
+ * @param {number} centre_time seconds
140
+ * @returns {number}
141
+ */
142
+ send_gain(centre_time: number): number;
143
+ /**
144
+ * Set the decay: each line's gain and shelves, at once where `interval` is 0, else as a ramp over it.
145
+ *
146
+ * @param {ArrayLike<number>} centre centre times, seconds, per band
147
+ * @param {ArrayLike<number>} sent the centre times last set, per band; read where `resting`
148
+ * @param {number} now context time
149
+ * @param {number} interval seconds the write ramps over; 0 sets it at once
150
+ * @param {boolean} resting whether the last write has arrived, so the params rest at what `sent` set
151
+ */
152
+ set_decay(centre: ArrayLike<number>, sent: ArrayLike<number>, now: number, interval: number, resting: boolean): void;
153
+ /**
154
+ * Each frame: nothing, since the lines' params carry the decay as it is set.
155
+ *
156
+ * @param {number} now context time
157
+ * @param {number} interval seconds
158
+ */
159
+ update(now: number, interval: number): void;
160
+ /**
161
+ * Unwire the tank.
162
+ */
163
+ disconnect(): void;
164
+ #private;
165
+ }
166
+ //# sourceMappingURL=AcousticReverbLines.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AcousticReverbLines.d.ts","sourceRoot":"","sources":["../../../../../../src/engine/sound/sopra/runtime/AcousticReverbLines.js"],"names":[],"mappings":"AA2DA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,yDAHW,MAAM,GACJ,MAAM,CAmBlB;AApFD;;;GAGG;AACH,oCAFU,MAAM,CAE6C;AAE7D;;;;;;;GAOG;AACH,2CAFU,MAAM,EAAE,CAE0H;AAE5I;;;;;;;GAOG;AACH,gDAFU,MAAM,EAAE,CAEmI;AAErJ;;;;;;;;;;;;GAYG;AACH,2CAFU,MAAM,CAEgC;AAEhD;;;GAGG;AACH,qCAFU,YAAY,CAEgF;AAoGtG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH;IA6DI;;OAEG;IACH,iBAFW,gBAAgB,EAmG1B;IAhKD;;;OAGG;IACH,OAFU,QAAQ,CAEZ;IAEN;;;OAGG;IACH,QAFU,iBAAiB,CAEpB;IAEP;;;OAGG;IACH,MAFU,QAAQ,CAEb;IAEL;;;OAGG;IACH,UAFU,MAAM,CAEqB;IAErC;;;OAGG;IACH,QAFU,SAAS,EAAE,CAET;IAEZ,iCAAiC;IACjC,YADW,gBAAgB,EAAE,CACb;IAEhB,iCAAiC;IACjC,aADW,gBAAgB,EAAE,CACZ;IAEjB,yBAAyB;IACzB,WADW,QAAQ,EAAE,CACN;IAEf;;;OAGG;IACH,aAFU,QAAQ,CAEN;IA2IZ;;;;;;OAMG;IACH,uBAHW,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;;;;OAQG;IACH,kBANW,UAAU,MAAM,CAAC,QACjB,UAAU,MAAM,CAAC,OACjB,MAAM,YACN,MAAM,WACN,OAAO,QA0BjB;IAED;;;;;OAKG;IACH,YAHW,MAAM,YACN,MAAM,QAGhB;IAED;;OAEG;IACH,mBAiBC;;CACJ"}