xbintsc 0.7.7 → 0.7.9

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 (160) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/cli/help.js +1 -1
  3. package/dist/src/codegen/generator/calls/invocation.js +6 -4
  4. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  5. package/dist/src/codegen/generator/context.d.ts +4 -3
  6. package/dist/src/codegen/generator/context.js +6 -5
  7. package/dist/src/codegen/generator/context.js.map +1 -1
  8. package/dist/src/codegen/generator/expressions/assignment.js +3 -3
  9. package/dist/src/driver/config.d.ts +1 -1
  10. package/dist/src/driver/options.d.ts +1 -1
  11. package/dist/src/driver/version.d.ts +1 -1
  12. package/dist/src/driver/version.js +1 -1
  13. package/dist/src/extensions/gui/features.d.ts +14 -6
  14. package/dist/src/extensions/gui/features.js +34 -6
  15. package/dist/src/extensions/gui/features.js.map +1 -1
  16. package/dist/src/extensions/gui/index.js +3 -3
  17. package/dist/tests/codegen/llvm.test.js +1 -1
  18. package/dist/tests/e2e/gui-audio.test.js +24 -0
  19. package/dist/tests/e2e/gui-audio.test.js.map +1 -1
  20. package/dist/tests/e2e/gui-draw-perf.test.js +9 -0
  21. package/dist/tests/e2e/gui-draw-perf.test.js.map +1 -1
  22. package/dist/tests/e2e/gui-font-face.test.d.ts +3 -1
  23. package/dist/tests/e2e/gui-font-face.test.js +99 -7
  24. package/dist/tests/e2e/gui-font-face.test.js.map +1 -1
  25. package/dist/tests/e2e/gui-frame-pacing.test.d.ts +15 -0
  26. package/dist/tests/e2e/gui-frame-pacing.test.js +68 -0
  27. package/dist/tests/e2e/gui-frame-pacing.test.js.map +1 -0
  28. package/dist/tests/e2e/gui-initial-url.test.d.ts +10 -0
  29. package/dist/tests/e2e/gui-initial-url.test.js +49 -0
  30. package/dist/tests/e2e/gui-initial-url.test.js.map +1 -0
  31. package/dist/tests/e2e/gui-layout.test.js +85 -0
  32. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  33. package/dist/tests/e2e/gui-webgl-depth-init.test.d.ts +13 -0
  34. package/dist/tests/e2e/gui-webgl-depth-init.test.js +144 -0
  35. package/dist/tests/e2e/gui-webgl-depth-init.test.js.map +1 -0
  36. package/dist/tests/e2e/gui-webgl-depth-range.test.d.ts +18 -0
  37. package/dist/tests/e2e/gui-webgl-depth-range.test.js +140 -0
  38. package/dist/tests/e2e/gui-webgl-depth-range.test.js.map +1 -0
  39. package/dist/tests/e2e/gui-webgl.test.js +49 -0
  40. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  41. package/dist/tests/e2e/gui-webgpu-attachment.test.d.ts +19 -0
  42. package/dist/tests/e2e/gui-webgpu-attachment.test.js +258 -0
  43. package/dist/tests/e2e/gui-webgpu-attachment.test.js.map +1 -0
  44. package/dist/tests/e2e/method-call-cache.test.js +27 -0
  45. package/dist/tests/e2e/method-call-cache.test.js.map +1 -1
  46. package/dist/tests/extensions/gui-features.test.js +27 -2
  47. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  48. package/doc/ai/extensions.md +10 -4
  49. package/doc/ai/zh-CN/extensions.md +8 -3
  50. package/doc/gui/animation.md +1 -1
  51. package/doc/gui/api.md +35 -2
  52. package/doc/gui/build.md +34 -15
  53. package/doc/gui/dom.md +3 -2
  54. package/doc/gui/known-issues.md +1 -1
  55. package/doc/gui/layout.md +9 -3
  56. package/doc/gui/rendering.md +45 -8
  57. package/doc/gui/runtime.md +13 -0
  58. package/doc/gui/webgl.md +29 -3
  59. package/doc/gui/webgpu.md +20 -0
  60. package/doc/gui.md +9 -4
  61. package/doc/implemented/codegen.md +4 -3
  62. package/doc/zh-CN/gui/animation.md +1 -1
  63. package/doc/zh-CN/gui/api.md +25 -2
  64. package/doc/zh-CN/gui/build.md +20 -7
  65. package/doc/zh-CN/gui/dom.md +3 -2
  66. package/doc/zh-CN/gui/known-issues.md +1 -1
  67. package/doc/zh-CN/gui/layout.md +7 -4
  68. package/doc/zh-CN/gui/rendering.md +33 -8
  69. package/doc/zh-CN/gui/runtime.md +9 -0
  70. package/doc/zh-CN/gui/webgl.md +7 -4
  71. package/doc/zh-CN/gui/webgpu.md +18 -0
  72. package/doc/zh-CN/gui.md +10 -5
  73. package/doc/zh-CN/implemented/codegen.md +4 -3
  74. package/package.json +7 -7
  75. package/runtime/ext_gui/audio_graph_commands.cpp +127 -0
  76. package/runtime/ext_gui/audio_mixer.cpp +84 -163
  77. package/runtime/ext_gui/audio_mixer.h +59 -3
  78. package/runtime/ext_gui/audio_mixer_extra.cpp +8 -0
  79. package/runtime/ext_gui/audio_param_apply.cpp +160 -0
  80. package/runtime/ext_gui/browser.cpp +6 -19
  81. package/runtime/ext_gui/browser_location.cpp +78 -0
  82. package/runtime/ext_gui/css.cpp +1 -0
  83. package/runtime/ext_gui/css.h +4 -0
  84. package/runtime/ext_gui/css_sheet.cpp +4 -2
  85. package/runtime/ext_gui/document_animation.cpp +7 -0
  86. package/runtime/ext_gui/dom_api_computed_style.cpp +3 -2
  87. package/runtime/ext_gui/dom_api_webgl.cpp +39 -2
  88. package/runtime/ext_gui/dom_api_webgl_extensions.cpp +134 -0
  89. package/runtime/ext_gui/dom_api_webgl_internal.h +4 -0
  90. package/runtime/ext_gui/dom_api_webgl_shader.cpp +28 -4
  91. package/runtime/ext_gui/dom_api_webgl_state.cpp +6 -103
  92. package/runtime/ext_gui/gui.cpp +100 -1
  93. package/runtime/ext_gui/gui_events.cpp +8 -2
  94. package/runtime/ext_gui/layout_flex.cpp +5 -2
  95. package/runtime/ext_gui/layout_flow.cpp +24 -1
  96. package/runtime/ext_gui/layout_flow_internal.h +11 -2
  97. package/runtime/ext_gui/media_query.cpp +3 -0
  98. package/runtime/ext_gui/perf.cpp +18 -6
  99. package/runtime/ext_gui/perf.h +37 -0
  100. package/runtime/ext_gui/style.cpp +28 -10
  101. package/runtime/ext_gui/style.h +10 -1
  102. package/runtime/ext_gui/style_length.cpp +4 -0
  103. package/runtime/ext_gui/style_print.cpp +7 -1
  104. package/runtime/ext_gui/style_values.cpp +6 -5
  105. package/runtime/ext_gui/text.cpp +4 -0
  106. package/runtime/ext_gui/text_font_bytes.cpp +117 -0
  107. package/runtime/ext_gui/text_font_bytes.h +65 -0
  108. package/runtime/ext_gui/text_font_db.cpp +5 -0
  109. package/runtime/ext_gui/text_font_sfnt.cpp +71 -24
  110. package/runtime/ext_gui/text_font_sfnt.h +5 -2
  111. package/runtime/ext_gui/text_hb.cpp +34 -5
  112. package/runtime/ext_gui/text_stb.cpp +7 -30
  113. package/runtime/ext_gui/text_webfont.cpp +217 -0
  114. package/runtime/ext_gui/warnings.cpp +26 -0
  115. package/runtime/ext_gui/warnings.h +8 -0
  116. package/runtime/ext_gui/webfont_bridge.c +23 -0
  117. package/runtime/ext_gui/webgl.h +36 -39
  118. package/runtime/ext_gui/webgl_blit_color.cpp +5 -0
  119. package/runtime/ext_gui/webgl_buffer_objects.cpp +19 -5
  120. package/runtime/ext_gui/webgl_context.cpp +2 -0
  121. package/runtime/ext_gui/webgl_draw.cpp +72 -12
  122. package/runtime/ext_gui/webgl_draw_setup.cpp +8 -2
  123. package/runtime/ext_gui/webgl_draw_state.h +13 -2
  124. package/runtime/ext_gui/webgl_framebuffer.cpp +8 -0
  125. package/runtime/ext_gui/webgl_objects.h +19 -0
  126. package/runtime/ext_gui/webgl_program.h +64 -0
  127. package/runtime/ext_gui/webgl_state.cpp +7 -0
  128. package/runtime/ext_gui/webgl_targets.cpp +31 -7
  129. package/runtime/ext_gui/webgl_uniform.cpp +78 -0
  130. package/runtime/ext_gui/webgpu.h +10 -0
  131. package/runtime/ext_gui/webgpu_buffer.cpp +3 -1
  132. package/runtime/ext_gui/webgpu_canvas.cpp +1 -0
  133. package/runtime/ext_gui/webgpu_command.cpp +51 -11
  134. package/runtime/ext_gui/webgpu_convert.cpp +7 -2
  135. package/runtime/ext_gui/webgpu_pass.cpp +12 -0
  136. package/runtime/ext_gui/webgpu_queue.cpp +3 -0
  137. package/runtime/ext_gui/webgpu_resources.cpp +1 -0
  138. package/runtime/ext_gui/window.cpp +6 -0
  139. package/runtime/rt_object.h +6 -0
  140. package/runtime/rt_profile.h +6 -0
  141. package/runtime/xt_containers/access.inc +2 -47
  142. package/runtime/xt_containers/ic_get.inc +70 -0
  143. package/runtime/xt_containers/method_cache.inc +42 -11
  144. package/runtime/xt_containers/objects.inc +12 -1
  145. package/runtime/xt_containers/shapes.inc +78 -0
  146. package/runtime/xt_containers.c +1 -0
  147. package/runtime/xt_profile.c +3 -0
  148. package/scripts/build-gui.ts +43 -49
  149. package/scripts/gui-engine-build.ts +58 -0
  150. package/scripts/gui-vendor-source.ts +53 -0
  151. package/scripts/gui-webfont-build.ts +138 -0
  152. package/src/cli/help.ts +1 -1
  153. package/src/codegen/generator/calls/invocation.ts +6 -4
  154. package/src/codegen/generator/context.ts +6 -6
  155. package/src/codegen/generator/expressions/assignment.ts +3 -3
  156. package/src/driver/config.ts +1 -1
  157. package/src/driver/options.ts +1 -1
  158. package/src/driver/version.ts +1 -1
  159. package/src/extensions/gui/features.ts +34 -6
  160. package/src/extensions/gui/index.ts +3 -3
@@ -0,0 +1,127 @@
1
+ /*
2
+ * xbintsc GUI engine — queued graph mutations.
3
+ *
4
+ * `connect`, `disconnect`, the source start/stop pair and the per-node setup
5
+ * calls mutate the graph from the page's thread. Taking the mixer's render
6
+ * mutex here made a sound effect's setup wait for the current quantum (up to a
7
+ * few milliseconds with a convolver send), which dominated the main thread's
8
+ * audio time on a scene that spawns effects while it renders. They queue a
9
+ * command instead; the audio thread applies it at the next quantum boundary,
10
+ * in the same FIFO as the AudioParam updates, with the mutex already held.
11
+ */
12
+
13
+ #include "audio_mixer.h"
14
+
15
+ #include <algorithm>
16
+ #include <utility>
17
+
18
+ namespace xtgui {
19
+
20
+ namespace {
21
+
22
+ double clamp_delay_seconds(double value, double low, double high) {
23
+ if (!(value > low)) return low;
24
+ if (value > high) return high;
25
+ return value;
26
+ }
27
+
28
+ } // namespace
29
+
30
+ void AudioMixer::connect(uint64_t source, uint64_t target) {
31
+ post_command([this, source, target]() {
32
+ AudioNode *from = node_locked(source);
33
+ AudioNode *to = node_locked(target);
34
+ if (from == nullptr || to == nullptr) return;
35
+ for (uint64_t id : to->inputs) {
36
+ if (id == source) return;
37
+ }
38
+ to->inputs.push_back(source);
39
+ });
40
+ }
41
+
42
+ void AudioMixer::disconnect(uint64_t source, uint64_t target) {
43
+ post_command([this, source, target]() {
44
+ AudioNode *to = node_locked(target);
45
+ if (to == nullptr) return;
46
+ to->inputs.erase(std::remove(to->inputs.begin(), to->inputs.end(), source), to->inputs.end());
47
+ });
48
+ }
49
+
50
+ void AudioMixer::disconnect_all(uint64_t source) {
51
+ post_command([this, source]() {
52
+ for (auto &entry : nodes_) {
53
+ std::vector<uint64_t> &inputs = entry.second->inputs;
54
+ inputs.erase(std::remove(inputs.begin(), inputs.end(), source), inputs.end());
55
+ }
56
+ });
57
+ }
58
+
59
+ void AudioMixer::set_osc_type(uint64_t node, AudioOscType type) {
60
+ post_command([this, node, type]() {
61
+ AudioNode *target = node_locked(node);
62
+ if (target != nullptr) target->osc_type = type;
63
+ });
64
+ }
65
+
66
+ void AudioMixer::set_biquad_type(uint64_t node, AudioBiquadType type) {
67
+ post_command([this, node, type]() {
68
+ AudioNode *target = node_locked(node);
69
+ if (target != nullptr) target->biquad_type = type;
70
+ });
71
+ }
72
+
73
+ void AudioMixer::set_delay_max(uint64_t node, double seconds) {
74
+ post_command([this, node, seconds]() {
75
+ AudioNode *target = node_locked(node);
76
+ if (target == nullptr) return;
77
+ const double clamped = clamp_delay_seconds(seconds, 0.001, 60.0);
78
+ target->delay_max = clamped;
79
+ target->delay_size = static_cast<size_t>(clamped * kAudioMixRate) + 2;
80
+ target->delay_write = 0;
81
+ for (int channel = 0; channel < kAudioMixChannels; channel++) {
82
+ target->delay[channel].assign(target->delay_size, 0.0f);
83
+ }
84
+ });
85
+ }
86
+
87
+ void AudioMixer::set_buffer(uint64_t node, std::shared_ptr<AudioClip> clip, bool loop,
88
+ double loop_start, double loop_end) {
89
+ post_command([this, node, clip = std::move(clip), loop, loop_start, loop_end]() {
90
+ AudioNode *target = node_locked(node);
91
+ if (target == nullptr) return;
92
+ target->clip = clip;
93
+ target->loop = loop;
94
+ target->loop_start = loop_start;
95
+ target->loop_end = loop_end;
96
+ });
97
+ }
98
+
99
+ void AudioMixer::start_source(uint64_t node, double when, double offset, double duration) {
100
+ post_command([this, node, when, offset, duration]() {
101
+ AudioNode *target = node_locked(node);
102
+ if (target == nullptr) return;
103
+ const double now = static_cast<double>(frames_rendered_.load(std::memory_order_relaxed)) /
104
+ kAudioMixRate;
105
+ target->started = true;
106
+ target->began = false;
107
+ target->ended = false;
108
+ target->start_time = when > now ? when : now;
109
+ target->stop_time = -1.0;
110
+ target->clip_offset = offset > 0.0 ? offset : 0.0;
111
+ target->clip_duration = duration;
112
+ target->clip_pos = target->clip_offset * (target->clip ? target->clip->sample_rate
113
+ : kAudioMixRate);
114
+ });
115
+ }
116
+
117
+ void AudioMixer::stop_source(uint64_t node, double when) {
118
+ post_command([this, node, when]() {
119
+ AudioNode *target = node_locked(node);
120
+ if (target == nullptr) return;
121
+ const double now = static_cast<double>(frames_rendered_.load(std::memory_order_relaxed)) /
122
+ kAudioMixRate;
123
+ target->stop_time = when > now ? when : now;
124
+ });
125
+ }
126
+
127
+ } // namespace xtgui
@@ -14,8 +14,12 @@
14
14
 
15
15
  #include <algorithm>
16
16
  #include <cmath>
17
+ #include <cstdio>
18
+ #include <cstdlib>
17
19
  #include <cstring>
18
20
 
21
+ #include <SDL3/SDL.h>
22
+
19
23
  namespace xtgui {
20
24
 
21
25
  namespace {
@@ -54,6 +58,11 @@ double waveform(AudioOscType type, double phase) {
54
58
 
55
59
  AudioMixer::AudioMixer(bool offline) : offline_(offline) {
56
60
  pending_.reserve(kSamples);
61
+ const char *silent = std::getenv("XT_GUI_AUDIO_SILENT");
62
+ silent_ = silent != NULL && silent[0] != '\0' && silent[0] != '0';
63
+ const char *profile = std::getenv("XT_AUDIO_PROFILE");
64
+ profile_ = profile != NULL && profile[0] != '\0' && profile[0] != '0';
65
+ start_ns_ = SDL_GetTicksNS();
57
66
  }
58
67
 
59
68
  AudioMixer::~AudioMixer() { close_device(); }
@@ -64,9 +73,8 @@ AudioNode *AudioMixer::node_locked(uint64_t id) {
64
73
  }
65
74
 
66
75
  uint64_t AudioMixer::create_node(AudioNodeKind kind) {
67
- std::lock_guard<std::mutex> lock(mutex_);
68
76
  auto node = std::make_shared<AudioNode>();
69
- node->id = next_node_id_++;
77
+ node->id = next_node_id_.fetch_add(1, std::memory_order_relaxed);
70
78
  node->kind = kind;
71
79
  node->quantum.assign(kSamples, 0.0f);
72
80
  switch (kind) {
@@ -145,174 +153,56 @@ uint64_t AudioMixer::create_node(AudioNodeKind kind) {
145
153
  node->param_count = 0;
146
154
  break;
147
155
  }
148
- nodes_[node->id] = node;
149
- if (kind == kAudioDestination) destination_id_ = node->id;
150
- if (kind == kAudioListener) listener_id_ = node->id;
151
- return node->id;
152
- }
153
-
154
-
155
- void AudioMixer::connect(uint64_t source, uint64_t target) {
156
- std::lock_guard<std::mutex> lock(mutex_);
157
- AudioNode *from = node_locked(source);
158
- AudioNode *to = node_locked(target);
159
- if (from == nullptr || to == nullptr) return;
160
- for (uint64_t id : to->inputs) {
161
- if (id == source) return;
162
- }
163
- to->inputs.push_back(source);
164
- }
165
-
166
- void AudioMixer::disconnect(uint64_t source, uint64_t target) {
167
- std::lock_guard<std::mutex> lock(mutex_);
168
- AudioNode *to = node_locked(target);
169
- if (to == nullptr) return;
170
- to->inputs.erase(std::remove(to->inputs.begin(), to->inputs.end(), source), to->inputs.end());
171
- }
172
-
173
- void AudioMixer::disconnect_all(uint64_t source) {
174
- std::lock_guard<std::mutex> lock(mutex_);
175
- for (auto &entry : nodes_) {
176
- std::vector<uint64_t> &inputs = entry.second->inputs;
177
- inputs.erase(std::remove(inputs.begin(), inputs.end(), source), inputs.end());
178
- }
179
- }
180
-
181
- void AudioMixer::set_osc_type(uint64_t node, AudioOscType type) {
182
- std::lock_guard<std::mutex> lock(mutex_);
183
- AudioNode *target = node_locked(node);
184
- if (target != nullptr) target->osc_type = type;
185
- }
186
-
187
- void AudioMixer::set_biquad_type(uint64_t node, AudioBiquadType type) {
188
- std::lock_guard<std::mutex> lock(mutex_);
189
- AudioNode *target = node_locked(node);
190
- if (target != nullptr) target->biquad_type = type;
191
- }
192
-
193
- void AudioMixer::set_delay_max(uint64_t node, double seconds) {
194
- std::lock_guard<std::mutex> lock(mutex_);
195
- AudioNode *target = node_locked(node);
196
- if (target == nullptr) return;
197
- seconds = clamp_value(seconds, 0.001, 60.0);
198
- target->delay_max = seconds;
199
- target->delay_size = static_cast<size_t>(seconds * kAudioMixRate) + 2;
200
- target->delay_write = 0;
201
- for (int channel = 0; channel < kAudioMixChannels; channel++) {
202
- target->delay[channel].assign(target->delay_size, 0.0f);
203
- }
204
- }
205
-
206
- void AudioMixer::set_buffer(uint64_t node, std::shared_ptr<AudioClip> clip, bool loop,
207
- double loop_start, double loop_end) {
208
- std::lock_guard<std::mutex> lock(mutex_);
209
- AudioNode *target = node_locked(node);
210
- if (target == nullptr) return;
211
- target->clip = std::move(clip);
212
- target->loop = loop;
213
- target->loop_start = loop_start;
214
- target->loop_end = loop_end;
215
- }
216
-
217
- void AudioMixer::start_source(uint64_t node, double when, double offset, double duration) {
218
- std::lock_guard<std::mutex> lock(mutex_);
219
- AudioNode *target = node_locked(node);
220
- if (target == nullptr) return;
221
- double now = static_cast<double>(frames_rendered_) / kAudioMixRate;
222
- target->started = true;
223
- target->began = false;
224
- target->ended = false;
225
- target->start_time = when > now ? when : now;
226
- target->stop_time = -1.0;
227
- target->clip_offset = offset > 0.0 ? offset : 0.0;
228
- target->clip_duration = duration;
229
- target->clip_pos = target->clip_offset * (target->clip ? target->clip->sample_rate
230
- : kAudioMixRate);
231
- }
232
-
233
- void AudioMixer::stop_source(uint64_t node, double when) {
234
- std::lock_guard<std::mutex> lock(mutex_);
235
- AudioNode *target = node_locked(node);
236
- if (target == nullptr) return;
237
- double now = static_cast<double>(frames_rendered_) / kAudioMixRate;
238
- target->stop_time = when > now ? when : now;
239
- }
240
-
241
- /* -- AudioMixer: parameters ----------------------------------------------- */
242
-
243
- double AudioMixer::param_value(uint64_t node, int index) {
244
- std::lock_guard<std::mutex> lock(mutex_);
245
- AudioNode *target = node_locked(node);
246
- if (target == nullptr || index < 0 || index >= target->param_count) return 0.0;
247
- double now = static_cast<double>(frames_rendered_) / kAudioMixRate;
248
- return target->params[index].value_at(now);
249
- }
250
-
251
- void AudioMixer::param_set(uint64_t node, int index, double value) {
252
- std::lock_guard<std::mutex> lock(mutex_);
253
- AudioNode *target = node_locked(node);
254
- if (target == nullptr || index < 0 || index >= target->param_count) return;
255
- double now = static_cast<double>(frames_rendered_) / kAudioMixRate;
256
- target->params[index].set_value(value, now);
257
- }
258
-
259
- void AudioMixer::param_set_at(uint64_t node, int index, double value, double time) {
260
- std::lock_guard<std::mutex> lock(mutex_);
261
- AudioNode *target = node_locked(node);
262
- if (target != nullptr && index >= 0 && index < target->param_count) {
263
- target->params[index].set_value_at_time(value, time);
264
- }
265
- }
266
-
267
- void AudioMixer::param_linear_ramp(uint64_t node, int index, double value, double time) {
268
- std::lock_guard<std::mutex> lock(mutex_);
269
- AudioNode *target = node_locked(node);
270
- if (target != nullptr && index >= 0 && index < target->param_count) {
271
- target->params[index].linear_ramp(value, time);
272
- }
273
- }
274
-
275
- void AudioMixer::param_exponential_ramp(uint64_t node, int index, double value, double time) {
276
- std::lock_guard<std::mutex> lock(mutex_);
277
- AudioNode *target = node_locked(node);
278
- if (target != nullptr && index >= 0 && index < target->param_count) {
279
- target->params[index].exponential_ramp(value, time);
280
- }
281
- }
282
-
283
- void AudioMixer::param_set_target(uint64_t node, int index, double value, double time,
284
- double time_constant) {
285
- std::lock_guard<std::mutex> lock(mutex_);
286
- AudioNode *target = node_locked(node);
287
- if (target != nullptr && index >= 0 && index < target->param_count) {
288
- target->params[index].set_target(value, time, time_constant);
156
+ {
157
+ std::lock_guard<std::mutex> pending_lock(pending_mutex_);
158
+ pending_nodes_[node->id] = node;
289
159
  }
160
+ post_command([this, node]() {
161
+ const uint64_t id = node->id;
162
+ nodes_[id] = node;
163
+ if (node->kind == kAudioDestination) destination_id_ = id;
164
+ if (node->kind == kAudioListener) listener_id_ = id;
165
+ std::lock_guard<std::mutex> pending_lock(pending_mutex_);
166
+ pending_nodes_.erase(id);
167
+ });
168
+ return node->id;
290
169
  }
291
170
 
292
- void AudioMixer::param_cancel(uint64_t node, int index, double time, bool hold) {
293
- std::lock_guard<std::mutex> lock(mutex_);
294
- AudioNode *target = node_locked(node);
295
- if (target != nullptr && index >= 0 && index < target->param_count) {
296
- if (hold) {
297
- target->params[index].cancel_and_hold(time);
298
- } else {
299
- target->params[index].cancel_scheduled(time);
300
- }
301
- }
302
- }
171
+ /* Graph mutations (`connect`, `start`, the per-node setup calls) live in
172
+ * `audio_graph_commands.cpp`; they queue a command instead of taking the
173
+ * render mutex. */
303
174
 
304
175
  /* -- AudioMixer: clock ---------------------------------------------------- */
305
176
 
306
- double AudioMixer::current_time() {
307
- std::lock_guard<std::mutex> lock(mutex_);
308
- return static_cast<double>(frames_rendered_) / kAudioMixRate;
177
+ double AudioMixer::current_time() const {
178
+ /* Lock-free: the audio thread holds `mutex_` while it renders a quantum, and
179
+ * a page schedules sounds from `currentTime` on every frame. Waiting for that
180
+ * lock here was measurable main-thread time with a convolver send running. */
181
+ return static_cast<double>(frames_rendered_.load(std::memory_order_relaxed)) / kAudioMixRate;
309
182
  }
310
183
 
311
184
  void AudioMixer::shutdown() {
312
185
  close_device();
313
186
  std::lock_guard<std::mutex> lock(mutex_);
187
+ drain_commands_locked();
314
188
  nodes_.clear();
315
189
  destination_id_ = 0;
190
+ {
191
+ std::lock_guard<std::mutex> pending_lock(pending_mutex_);
192
+ pending_nodes_.clear();
193
+ }
194
+ if (profile_) {
195
+ const double wall_ms = static_cast<double>(SDL_GetTicksNS() - start_ns_) / 1000000.0;
196
+ const double wait_ms = static_cast<double>(lock_wait_ns_.load()) / 1000000.0;
197
+ const double render_ms = static_cast<double>(render_ns_.load()) / 1000000.0;
198
+ const double max_ms = static_cast<double>(max_render_ns_.load()) / 1000000.0;
199
+ std::fprintf(stderr,
200
+ "xt_audio: wall=%.1fms lockWait=%.1fms render=%.1fms (%.1f%%) "
201
+ "quanta=%llu maxQuantum=%.3fms silent=%d\n",
202
+ wall_ms, wait_ms, render_ms,
203
+ wall_ms > 0.0 ? render_ms * 100.0 / wall_ms : 0.0,
204
+ (unsigned long long)render_quanta_.load(), max_ms, silent_ ? 1 : 0);
205
+ }
316
206
  }
317
207
 
318
208
  /* -- AudioMixer: render --------------------------------------------------- */
@@ -335,8 +225,33 @@ void AudioMixer::render_locked(float *out, int frames) {
335
225
  }
336
226
 
337
227
  void AudioMixer::render(float *out, int frames) {
338
- std::lock_guard<std::mutex> lock(mutex_);
339
- render_locked(out, frames);
228
+ /* SDL may ask for many quanta in one callback (its stream buffer is tens of
229
+ * milliseconds). Rendering the whole callback under one lock made a page's
230
+ * audio-parameter update wait for all of it — with a convolver send that can
231
+ * be several milliseconds — showing up as main-thread frame time. Hold the
232
+ * graph lock for one quantum at a time instead; the mixer stays real-time and
233
+ * a setter never waits for more than a quantum. */
234
+ int done = 0;
235
+ while (done < frames) {
236
+ int chunk = frames - done;
237
+ if (chunk > kAudioQuantum) chunk = kAudioQuantum;
238
+ const uint64_t wait_start = profile_ ? SDL_GetTicksNS() : 0;
239
+ std::unique_lock<std::mutex> lock(mutex_);
240
+ const uint64_t locked_at = profile_ ? SDL_GetTicksNS() : 0;
241
+ render_locked(out + static_cast<size_t>(done) * kAudioMixChannels, chunk);
242
+ if (profile_) {
243
+ const uint64_t end = SDL_GetTicksNS();
244
+ lock_wait_ns_.fetch_add(locked_at - wait_start, std::memory_order_relaxed);
245
+ render_ns_.fetch_add(end - locked_at, std::memory_order_relaxed);
246
+ render_quanta_.fetch_add(1, std::memory_order_relaxed);
247
+ uint64_t longest = max_render_ns_.load(std::memory_order_relaxed);
248
+ const uint64_t took = end - locked_at;
249
+ while (took > longest &&
250
+ !max_render_ns_.compare_exchange_weak(longest, took, std::memory_order_relaxed)) {
251
+ }
252
+ }
253
+ done += chunk;
254
+ }
340
255
  }
341
256
 
342
257
  void AudioMixer::render_offline(float *out, int frames) {
@@ -346,13 +261,19 @@ void AudioMixer::render_offline(float *out, int frames) {
346
261
  }
347
262
 
348
263
  void AudioMixer::render_quantum_locked() {
264
+ /* Apply whatever the page queued since the previous quantum. The callbacks
265
+ * assume `mutex_` is held; they never block the main thread because the
266
+ * setters only touch the command queue. */
267
+ drain_commands_locked();
349
268
  serial_++;
350
269
  pending_.assign(kSamples, 0.0f);
351
270
  pending_offset_ = 0;
352
- AudioNode *destination = node_locked(destination_id_);
353
- if (destination != nullptr) {
354
- const float *mixed = pull_locked(destination);
355
- if (mixed != nullptr) std::memcpy(pending_.data(), mixed, sizeof(float) * kSamples);
271
+ if (!silent_) {
272
+ AudioNode *destination = node_locked(destination_id_);
273
+ if (destination != nullptr) {
274
+ const float *mixed = pull_locked(destination);
275
+ if (mixed != nullptr) std::memcpy(pending_.data(), mixed, sizeof(float) * kSamples);
276
+ }
356
277
  }
357
278
  frames_rendered_ += kAudioQuantum;
358
279
  }
@@ -21,7 +21,9 @@
21
21
  #ifndef XT_GUI_AUDIO_MIXER_H
22
22
  #define XT_GUI_AUDIO_MIXER_H
23
23
 
24
+ #include <atomic>
24
25
  #include <cstdint>
26
+ #include <functional>
25
27
  #include <memory>
26
28
  #include <mutex>
27
29
  #include <unordered_map>
@@ -283,7 +285,7 @@ class AudioMixer {
283
285
  void param_cancel(uint64_t node, int index, double time, bool hold);
284
286
 
285
287
  /* -- clock and device -------------------------------------------------- */
286
- double current_time();
288
+ double current_time() const;
287
289
  /** Best effort: a headless host has no device and playback stays silent. */
288
290
  bool start_device();
289
291
  void suspend_device();
@@ -304,14 +306,56 @@ class AudioMixer {
304
306
  const float *pull_locked(AudioNode *node);
305
307
  void render_quantum_locked();
306
308
  void render_locked(float *out, int frames);
309
+ /** Queue a graph/parameter mutation for the audio thread; the command runs
310
+ * with `mutex_` held at the next quantum boundary. Parameter scheduling goes
311
+ * through here so a page's per-frame Web Audio calls never wait for a
312
+ * convolver block to finish (the mixer holds `mutex_` for its whole DSP
313
+ * pass). */
314
+ void post_command(std::function<void()> command);
315
+ /** Apply queued mutations now (for a getter that must observe them). */
316
+ void drain_commands_locked();
317
+ /** Apply a parameter mutation straight to a node the audio thread has not
318
+ * inserted yet, under the pending table's lock, and return true. Return false
319
+ * when the node is already in the graph so the caller queues the mutation in
320
+ * the FIFO instead.
321
+ *
322
+ * A pending node's parameters are not rendered yet, so touching them here is
323
+ * safe: the queued insertion erases the entry from `pending_nodes_` under this
324
+ * same lock, so a direct write either completes before the audio thread can
325
+ * render the node or is not attempted at all. Without this, a
326
+ * `gain.value = 0.5` right after `createGain()` was only *queued* while
327
+ * `param_value` answered from the pending node's untouched default — a getter
328
+ * read `1` back until something drained the FIFO. On a host with no audio
329
+ * device (a headless CI runner, or a program that builds its graph before the
330
+ * first `start()`) nothing drains it, so the write was never observable. */
331
+ template <typename Mutate>
332
+ bool mutate_pending_param(uint64_t node, int index, Mutate mutate) {
333
+ std::lock_guard<std::mutex> lock(pending_mutex_);
334
+ auto found = pending_nodes_.find(node);
335
+ if (found == pending_nodes_.end()) return false;
336
+ AudioNode &target = *found->second;
337
+ if (index >= 0 && index < target.param_count) mutate(target.params[index]);
338
+ return true;
339
+ }
307
340
 
308
341
  std::mutex mutex_;
342
+ std::mutex command_mutex_;
343
+ std::vector<std::function<void()>> commands_;
309
344
  std::unordered_map<uint64_t, std::shared_ptr<AudioNode>> nodes_;
310
- uint64_t next_node_id_ = 1;
345
+ /** Nodes created by the page but not yet inserted by the audio thread. The
346
+ * constructor runs on the main thread and `param_value` reads a pending
347
+ * node's defaults there, so creating a node never waits for the render lock;
348
+ * insertion is queued like every other graph mutation. */
349
+ std::unordered_map<uint64_t, std::shared_ptr<AudioNode>> pending_nodes_;
350
+ std::mutex pending_mutex_;
351
+ std::atomic<uint64_t> next_node_id_{1};
311
352
  uint64_t destination_id_ = 0;
312
353
  uint64_t listener_id_ = 0;
313
354
  uint64_t serial_ = 0;
314
- uint64_t frames_rendered_ = 0;
355
+ /** Read by the page (`AudioContext.currentTime`, `AudioParam` scheduling)
356
+ * while the audio thread advances it; atomic so a read never waits for a
357
+ * quantum render. Updated under `mutex_`, which still orders graph state. */
358
+ std::atomic<uint64_t> frames_rendered_{0};
315
359
  std::vector<float> pending_;
316
360
  size_t pending_offset_ = 0;
317
361
 
@@ -319,6 +363,18 @@ class AudioMixer {
319
363
  bool running_ = true;
320
364
  SDL_AudioStream *stream_ = nullptr;
321
365
  bool device_tried_ = false;
366
+
367
+ /* Audio-path diagnostics. `XT_GUI_AUDIO_SILENT=1` renders silence while
368
+ * keeping the clock and the locking, which separates the mixer's DSP cost
369
+ * from the page's main-thread Web Audio calls. `XT_AUDIO_PROFILE=1`
370
+ * accumulates the render/lock times and dumps them at shutdown. */
371
+ bool silent_ = false;
372
+ bool profile_ = false;
373
+ uint64_t start_ns_ = 0;
374
+ std::atomic<uint64_t> lock_wait_ns_{0};
375
+ std::atomic<uint64_t> render_ns_{0};
376
+ std::atomic<uint64_t> render_quanta_{0};
377
+ std::atomic<uint64_t> max_render_ns_{0};
322
378
  };
323
379
 
324
380
  } // namespace xtgui
@@ -11,6 +11,7 @@ namespace xtgui {
11
11
 
12
12
  void AudioMixer::set_shaper_curve(uint64_t node, const std::vector<float> &curve, int oversample) {
13
13
  std::lock_guard<std::mutex> lock(mutex_);
14
+ drain_commands_locked();
14
15
  AudioNode *target = node_locked(node);
15
16
  if (target == nullptr) return;
16
17
  /* A one-entry curve is a DC curve on the platform; keep the same length
@@ -21,6 +22,7 @@ void AudioMixer::set_shaper_curve(uint64_t node, const std::vector<float> &curve
21
22
 
22
23
  void AudioMixer::set_shaper_oversample(uint64_t node, int oversample) {
23
24
  std::lock_guard<std::mutex> lock(mutex_);
25
+ drain_commands_locked();
24
26
  AudioNode *target = node_locked(node);
25
27
  if (target != nullptr) target->shaper_oversample = oversample > 1 ? oversample : 1;
26
28
  }
@@ -28,6 +30,7 @@ void AudioMixer::set_shaper_oversample(uint64_t node, int oversample) {
28
30
  void AudioMixer::set_convolver_impulse(uint64_t node, std::shared_ptr<AudioClip> clip,
29
31
  bool normalize) {
30
32
  std::lock_guard<std::mutex> lock(mutex_);
33
+ drain_commands_locked();
31
34
  AudioNode *target = node_locked(node);
32
35
  if (target == nullptr) return;
33
36
  prepare_convolver(target, clip, normalize);
@@ -35,6 +38,7 @@ void AudioMixer::set_convolver_impulse(uint64_t node, std::shared_ptr<AudioClip>
35
38
 
36
39
  void AudioMixer::set_analyser_fft(uint64_t node, int fft_size) {
37
40
  std::lock_guard<std::mutex> lock(mutex_);
41
+ drain_commands_locked();
38
42
  AudioNode *target = node_locked(node);
39
43
  if (target == nullptr) return;
40
44
  if (fft_size < 32) fft_size = 32;
@@ -46,6 +50,7 @@ void AudioMixer::set_analyser_fft(uint64_t node, int fft_size) {
46
50
 
47
51
  void AudioMixer::set_analyser_smoothing(uint64_t node, float smoothing) {
48
52
  std::lock_guard<std::mutex> lock(mutex_);
53
+ drain_commands_locked();
49
54
  AudioNode *target = node_locked(node);
50
55
  if (target == nullptr) return;
51
56
  if (smoothing < 0.0f) smoothing = 0.0f;
@@ -55,6 +60,7 @@ void AudioMixer::set_analyser_smoothing(uint64_t node, float smoothing) {
55
60
 
56
61
  bool AudioMixer::analyser_time_domain(uint64_t node, int count, std::vector<float> *out) {
57
62
  std::lock_guard<std::mutex> lock(mutex_);
63
+ drain_commands_locked();
58
64
  AudioNode *target = node_locked(node);
59
65
  if (target == nullptr || out == nullptr) return false;
60
66
  const size_t size = target->analyser_ring.size();
@@ -70,6 +76,7 @@ bool AudioMixer::analyser_time_domain(uint64_t node, int count, std::vector<floa
70
76
 
71
77
  bool AudioMixer::analyser_spectrum(uint64_t node, std::vector<float> *out) {
72
78
  std::lock_guard<std::mutex> lock(mutex_);
79
+ drain_commands_locked();
73
80
  AudioNode *target = node_locked(node);
74
81
  if (target == nullptr || out == nullptr) return false;
75
82
  const size_t size = target->analyser_ring.size();
@@ -111,6 +118,7 @@ void AudioMixer::set_panner(uint64_t node, int distance_model, double ref_distan
111
118
  double max_distance, double rolloff, double cone_inner,
112
119
  double cone_outer, double cone_gain, int panning_model) {
113
120
  std::lock_guard<std::mutex> lock(mutex_);
121
+ drain_commands_locked();
114
122
  AudioNode *target = node_locked(node);
115
123
  if (target == nullptr) return;
116
124
  target->distance_model = distance_model;