xbintsc 0.5.32 → 0.5.36

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 (173) hide show
  1. package/README.md +10 -7
  2. package/README.zh-CN.md +10 -8
  3. package/dist/scripts/check-version.d.ts +27 -0
  4. package/dist/scripts/check-version.js +71 -0
  5. package/dist/scripts/check-version.js.map +1 -0
  6. package/dist/scripts/package-platform.d.ts +58 -0
  7. package/dist/scripts/package-platform.js +142 -0
  8. package/dist/scripts/package-platform.js.map +1 -0
  9. package/dist/scripts/verify-platform-package.d.ts +34 -0
  10. package/dist/scripts/verify-platform-package.js +161 -0
  11. package/dist/scripts/verify-platform-package.js.map +1 -0
  12. package/dist/src/cli/help.d.ts +1 -1
  13. package/dist/src/cli/main.js +3 -1
  14. package/dist/src/cli/main.js.map +1 -1
  15. package/dist/src/driver/runtime-lib.d.ts +29 -4
  16. package/dist/src/driver/runtime-lib.js +68 -10
  17. package/dist/src/driver/runtime-lib.js.map +1 -1
  18. package/dist/src/driver/version.d.ts +1 -1
  19. package/dist/src/driver/version.js +1 -1
  20. package/dist/src/extensions/gui/index.js +6 -5
  21. package/dist/src/extensions/gui/index.js.map +1 -1
  22. package/dist/src/extensions/media/index.js +4 -3
  23. package/dist/src/extensions/media/index.js.map +1 -1
  24. package/dist/src/parser/context.d.ts +41 -0
  25. package/dist/src/parser/context.js +89 -4
  26. package/dist/src/parser/context.js.map +1 -1
  27. package/dist/src/parser/literals.js +6 -12
  28. package/dist/src/parser/literals.js.map +1 -1
  29. package/dist/tests/driver/platform-package.test.d.ts +1 -0
  30. package/dist/tests/driver/platform-package.test.js +49 -0
  31. package/dist/tests/driver/platform-package.test.js.map +1 -0
  32. package/dist/tests/driver/runtime-lib.test.js +51 -4
  33. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  34. package/dist/tests/e2e/gui-audio.test.d.ts +14 -0
  35. package/dist/tests/e2e/gui-audio.test.js +287 -0
  36. package/dist/tests/e2e/gui-audio.test.js.map +1 -0
  37. package/dist/tests/e2e/gui-helpers.d.ts +7 -0
  38. package/dist/tests/e2e/gui-helpers.js +1 -0
  39. package/dist/tests/e2e/gui-helpers.js.map +1 -1
  40. package/dist/tests/e2e/gui-media.test.js +78 -0
  41. package/dist/tests/e2e/gui-media.test.js.map +1 -1
  42. package/dist/tests/e2e/gui-style.test.js +55 -0
  43. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-webgl.test.js +258 -0
  45. package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-webgl2-3d.test.d.ts +4 -0
  47. package/dist/tests/e2e/gui-webgl2-3d.test.js +264 -0
  48. package/dist/tests/e2e/gui-webgl2-3d.test.js.map +1 -0
  49. package/dist/tests/e2e/gui-webgl2-blit.test.d.ts +10 -0
  50. package/dist/tests/e2e/gui-webgl2-blit.test.js +262 -0
  51. package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -0
  52. package/dist/tests/e2e/gui-webgl2-formats.test.d.ts +6 -0
  53. package/dist/tests/e2e/gui-webgl2-formats.test.js +395 -0
  54. package/dist/tests/e2e/gui-webgl2-formats.test.js.map +1 -0
  55. package/dist/tests/e2e/gui-webgl2-mrt.test.d.ts +4 -0
  56. package/dist/tests/e2e/gui-webgl2-mrt.test.js +105 -0
  57. package/dist/tests/e2e/gui-webgl2-mrt.test.js.map +1 -0
  58. package/dist/tests/e2e/gui-webgl2-ubo.test.d.ts +8 -0
  59. package/dist/tests/e2e/gui-webgl2-ubo.test.js +112 -0
  60. package/dist/tests/e2e/gui-webgl2-ubo.test.js.map +1 -0
  61. package/dist/tests/e2e/gui-webgl2.test.js +45 -1
  62. package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
  63. package/dist/tests/extensions/gui-index.test.js +1 -0
  64. package/dist/tests/extensions/gui-index.test.js.map +1 -1
  65. package/dist/tests/extensions/media.test.js +1 -0
  66. package/dist/tests/extensions/media.test.js.map +1 -1
  67. package/dist/tests/parser/expressions.test.js +30 -0
  68. package/dist/tests/parser/expressions.test.js.map +1 -1
  69. package/dist/tests/platform-tarball.test.d.ts +1 -0
  70. package/dist/tests/platform-tarball.test.js +111 -0
  71. package/dist/tests/platform-tarball.test.js.map +1 -0
  72. package/dist/tests/version.test.js +6 -0
  73. package/dist/tests/version.test.js.map +1 -1
  74. package/doc/ai/contributing.md +11 -0
  75. package/doc/ai/extensions.md +7 -4
  76. package/doc/ai/troubleshooting.md +7 -5
  77. package/doc/ai/zh-CN/contributing.md +9 -0
  78. package/doc/ai/zh-CN/extensions.md +4 -2
  79. package/doc/ai/zh-CN/troubleshooting.md +5 -3
  80. package/doc/gui/api.md +61 -13
  81. package/doc/gui/build.md +11 -4
  82. package/doc/gui/dom.md +6 -3
  83. package/doc/gui/media.md +4 -0
  84. package/doc/gui/webgl.md +105 -46
  85. package/doc/gui.md +7 -3
  86. package/doc/npm-package.md +55 -27
  87. package/doc/requirements.md +6 -2
  88. package/doc/zh-CN/gui/api.md +51 -10
  89. package/doc/zh-CN/gui/build.md +7 -2
  90. package/doc/zh-CN/gui/dom.md +3 -1
  91. package/doc/zh-CN/gui/media.md +3 -0
  92. package/doc/zh-CN/gui/webgl.md +22 -16
  93. package/doc/zh-CN/gui.md +3 -2
  94. package/doc/zh-CN/npm-package.md +44 -23
  95. package/doc/zh-CN/requirements.md +4 -1
  96. package/package.json +17 -1
  97. package/runtime/ext_gui/audio.cpp +365 -153
  98. package/runtime/ext_gui/audio_buffer.cpp +166 -0
  99. package/runtime/ext_gui/audio_buffer.h +37 -0
  100. package/runtime/ext_gui/audio_decode.cpp +219 -0
  101. package/runtime/ext_gui/audio_decode.h +39 -0
  102. package/runtime/ext_gui/audio_device.cpp +87 -0
  103. package/runtime/ext_gui/audio_effects.cpp +163 -0
  104. package/runtime/ext_gui/audio_effects.h +25 -0
  105. package/runtime/ext_gui/audio_internal.h +30 -0
  106. package/runtime/ext_gui/audio_mixer.cpp +494 -0
  107. package/runtime/ext_gui/audio_mixer.h +238 -0
  108. package/runtime/ext_gui/audio_param.cpp +104 -0
  109. package/runtime/ext_gui/audio_param_object.cpp +155 -0
  110. package/runtime/ext_gui/browser.cpp +11 -1
  111. package/runtime/ext_gui/css_selector.cpp +21 -3
  112. package/runtime/ext_gui/dom_api_internal.h +16 -0
  113. package/runtime/ext_gui/dom_api_webgl.cpp +7 -0
  114. package/runtime/ext_gui/dom_api_webgl2.cpp +151 -36
  115. package/runtime/ext_gui/dom_api_webgl2_draw.cpp +3 -3
  116. package/runtime/ext_gui/dom_api_webgl_buffer.cpp +6 -2
  117. package/runtime/ext_gui/dom_api_webgl_compressed.cpp +167 -0
  118. package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +160 -0
  119. package/runtime/ext_gui/dom_api_webgl_shader.cpp +4 -4
  120. package/runtime/ext_gui/dom_api_webgl_source.cpp +161 -0
  121. package/runtime/ext_gui/dom_api_webgl_state.cpp +169 -23
  122. package/runtime/ext_gui/dom_api_webgl_texture.cpp +276 -236
  123. package/runtime/ext_gui/dom_api_webgl_ubo.cpp +81 -0
  124. package/runtime/ext_gui/gui.cpp +44 -9
  125. package/runtime/ext_gui/gui_engine.h +9 -4
  126. package/runtime/ext_gui/image_bitmap.cpp +28 -0
  127. package/runtime/ext_gui/image_bitmap.h +26 -0
  128. package/runtime/ext_gui/image_bitmap_browser.cpp +136 -0
  129. package/runtime/ext_gui/media.cpp +32 -0
  130. package/runtime/ext_gui/media.h +13 -0
  131. package/runtime/ext_gui/media_api.h +23 -0
  132. package/runtime/ext_gui/webgl.h +152 -18
  133. package/runtime/ext_gui/webgl_blit.cpp +95 -0
  134. package/runtime/ext_gui/webgl_buffer.cpp +178 -263
  135. package/runtime/ext_gui/webgl_buffer_objects.cpp +270 -0
  136. package/runtime/ext_gui/webgl_clear.cpp +142 -0
  137. package/runtime/ext_gui/webgl_compressed.cpp +180 -0
  138. package/runtime/ext_gui/webgl_context.cpp +241 -156
  139. package/runtime/ext_gui/webgl_draw.cpp +68 -3
  140. package/runtime/ext_gui/webgl_draw_buffers.cpp +75 -0
  141. package/runtime/ext_gui/webgl_glsl.h +40 -0
  142. package/runtime/ext_gui/webgl_glsl_compile.cpp +6 -0
  143. package/runtime/ext_gui/webgl_glsl_rewrite.cpp +18 -0
  144. package/runtime/ext_gui/webgl_glsl_scan.cpp +20 -11
  145. package/runtime/ext_gui/webgl_glsl_scan_block.cpp +126 -0
  146. package/runtime/ext_gui/webgl_glsl_tokens.h +28 -0
  147. package/runtime/ext_gui/webgl_pipeline.cpp +71 -19
  148. package/runtime/ext_gui/webgl_program_blocks.cpp +58 -0
  149. package/runtime/ext_gui/webgl_program_internal.h +6 -0
  150. package/runtime/ext_gui/webgl_shader.cpp +13 -1
  151. package/runtime/ext_gui/webgl_state.cpp +228 -0
  152. package/runtime/ext_gui/webgl_texture.cpp +144 -17
  153. package/runtime/ext_gui/webgl_texture_3d.cpp +215 -0
  154. package/runtime/ext_gui/webgl_uniform_block.h +31 -0
  155. package/runtime/ext_gui/webgl_webgl2.cpp +58 -5
  156. package/runtime/ext_media/decode_audio.cpp +308 -0
  157. package/runtime/ext_media/media_bridge.c +2 -0
  158. package/runtime/rt.h +17 -0
  159. package/runtime/xt_array_buffer.c +30 -0
  160. package/runtime/xt_typed_array/elements.inc +13 -0
  161. package/scripts/build-media.ts +14 -6
  162. package/scripts/build-runtime.ts +21 -16
  163. package/scripts/check-version.ts +39 -9
  164. package/scripts/package-platform.ts +183 -0
  165. package/scripts/set-platform-version.ts +62 -0
  166. package/scripts/verify-platform-package.ts +182 -0
  167. package/src/cli/main.ts +5 -1
  168. package/src/driver/runtime-lib.ts +73 -9
  169. package/src/driver/version.ts +1 -1
  170. package/src/extensions/gui/index.ts +6 -5
  171. package/src/extensions/media/index.ts +4 -3
  172. package/src/parser/context.ts +99 -4
  173. package/src/parser/literals.ts +6 -12
@@ -0,0 +1,238 @@
1
+ /*
2
+ * xbintsc GUI engine — the Web Audio mixer.
3
+ *
4
+ * `audio.cpp` owns the JavaScript-facing objects (AudioContext, AudioNode,
5
+ * AudioParam, AudioBuffer); this unit owns the graph those objects describe and
6
+ * turns it into interleaved 48 kHz stereo float PCM. It knows nothing about
7
+ * `xt_value`, the DOM or the garbage collector, which is what lets the SDL
8
+ * audio callback render on its own thread without touching a runtime value.
9
+ *
10
+ * Rendering follows the Web Audio model: 128-frame render quanta, multiple
11
+ * inputs summed at every node, and an AudioParam timeline evaluated per sample.
12
+ * A node's quantum is cached for the quantum serial, so a node feeding two
13
+ * destinations (fan-out) is computed once and a cycle renders silence instead
14
+ * of recursing forever.
15
+ *
16
+ * Threading: the graph is mutated from the page thread (create/connect/start)
17
+ * and read from the SDL audio thread (render). Every public method takes the
18
+ * same mutex, so a mutation never interleaves with a render. No callback ever
19
+ * touches an `xt_value`.
20
+ */
21
+ #ifndef XT_GUI_AUDIO_MIXER_H
22
+ #define XT_GUI_AUDIO_MIXER_H
23
+
24
+ #include <cstdint>
25
+ #include <memory>
26
+ #include <mutex>
27
+ #include <unordered_map>
28
+ #include <vector>
29
+
30
+ struct SDL_AudioStream;
31
+
32
+ namespace xtgui {
33
+
34
+ /** Mix rate of every context (SDL converts to the device rate). */
35
+ constexpr int kAudioMixRate = 48000;
36
+ constexpr int kAudioMixChannels = 2;
37
+ /** Web Audio's render quantum, in frames. */
38
+ constexpr int kAudioQuantum = 128;
39
+
40
+ enum AudioNodeKind {
41
+ kAudioDestination,
42
+ kAudioGain,
43
+ kAudioOscillator,
44
+ kAudioBufferSource,
45
+ kAudioBiquad,
46
+ kAudioCompressor,
47
+ kAudioStereoPanner,
48
+ kAudioConstantSource,
49
+ kAudioDelay,
50
+ };
51
+
52
+ enum AudioOscType { kAudioOscSine, kAudioOscSquare, kAudioOscSawtooth, kAudioOscTriangle };
53
+
54
+ enum AudioBiquadType {
55
+ kAudioBiquadLowpass,
56
+ kAudioBiquadHighpass,
57
+ kAudioBiquadBandpass,
58
+ kAudioBiquadLowshelf,
59
+ kAudioBiquadHighshelf,
60
+ kAudioBiquadPeaking,
61
+ kAudioBiquadNotch,
62
+ kAudioBiquadAllpass,
63
+ };
64
+
65
+ /** One decoded/created AudioBuffer's samples, in planar float.
66
+ *
67
+ * A buffer source takes a reference to one of these when it starts. Decoding
68
+ * (or an AudioBuffer handed to `start`) copies the samples once, so playback
69
+ * never reads a runtime value and a buffer can be collected while it plays. */
70
+ struct AudioClip {
71
+ std::vector<std::vector<float>> channels;
72
+ int sample_rate = kAudioMixRate;
73
+ uint32_t length = 0;
74
+
75
+ double duration() const {
76
+ return sample_rate > 0 ? static_cast<double>(length) / sample_rate : 0.0;
77
+ }
78
+ };
79
+
80
+ /** One AudioParam: a base value plus the scheduled automation events.
81
+ *
82
+ * Events are evaluated on demand at a sample time, which keeps scheduling and
83
+ * rendering independent: a page may schedule seconds ahead, and a source may
84
+ * start with a nonzero `when`. */
85
+ class AudioParamState {
86
+ public:
87
+ void reset(double value);
88
+ /** The `value` setter: takes effect at `time` and overrides older events. */
89
+ void set_value(double value, double time);
90
+ void set_value_at_time(double value, double time);
91
+ void linear_ramp(double value, double time);
92
+ void exponential_ramp(double value, double time);
93
+ void set_target(double value, double time, double time_constant);
94
+ void cancel_scheduled(double time);
95
+ void cancel_and_hold(double time);
96
+ double value_at(double time) const;
97
+
98
+ private:
99
+ enum EventKind { kEventSet, kEventLinear, kEventExponential, kEventTarget };
100
+ struct Event {
101
+ double time;
102
+ EventKind kind;
103
+ double value;
104
+ double time_constant;
105
+ };
106
+
107
+ double evaluate(size_t index, double time) const;
108
+ void insert(Event event);
109
+
110
+ double base_ = 0.0;
111
+ std::vector<Event> events_;
112
+ };
113
+
114
+ /** One graph node. Only the fields a node's kind uses are meaningful. */
115
+ struct AudioNode {
116
+ uint64_t id = 0;
117
+ AudioNodeKind kind = kAudioGain;
118
+ std::vector<uint64_t> inputs;
119
+ AudioParamState params[5];
120
+ int param_count = 0;
121
+
122
+ /* Per-quantum cache: 2 * kAudioQuantum interleaved floats. */
123
+ std::vector<float> quantum;
124
+ uint64_t serial = 0;
125
+ bool rendering = false;
126
+
127
+ /* Oscillator. */
128
+ AudioOscType osc_type = kAudioOscSine;
129
+ double phase = 0.0;
130
+
131
+ /* Sources (oscillator and buffer). */
132
+ bool started = false;
133
+ bool began = false;
134
+ bool ended = false;
135
+ double start_time = 0.0;
136
+ double stop_time = -1.0;
137
+ std::shared_ptr<AudioClip> clip;
138
+ double clip_offset = 0.0;
139
+ double clip_duration = 0.0;
140
+ double clip_pos = 0.0;
141
+ bool loop = false;
142
+ double loop_start = 0.0;
143
+ double loop_end = 0.0;
144
+
145
+ /* Biquad filter state, one set per channel. */
146
+ AudioBiquadType biquad_type = kAudioBiquadLowpass;
147
+ double x1[2] = {0.0, 0.0};
148
+ double x2[2] = {0.0, 0.0};
149
+ double y1[2] = {0.0, 0.0};
150
+ double y2[2] = {0.0, 0.0};
151
+
152
+ /* Compressor envelope, in dB of gain reduction. */
153
+ double compressor_env = 0.0;
154
+
155
+ /* Delay line, one ring per channel. `delay_size` is 0 until the JS factory
156
+ * has supplied `maxDelayTime`. */
157
+ std::vector<float> delay[2];
158
+ size_t delay_size = 0;
159
+ size_t delay_write = 0;
160
+ double delay_max = 1.0;
161
+ };
162
+
163
+ /** One AudioContext's graph, its device stream and its clock. */
164
+ class AudioMixer {
165
+ public:
166
+ explicit AudioMixer(bool offline);
167
+ ~AudioMixer();
168
+
169
+ AudioMixer(const AudioMixer &) = delete;
170
+ AudioMixer &operator=(const AudioMixer &) = delete;
171
+
172
+ bool offline() const { return offline_; }
173
+
174
+ /* -- graph ------------------------------------------------------------- */
175
+ uint64_t create_node(AudioNodeKind kind);
176
+ void connect(uint64_t source, uint64_t target);
177
+ void disconnect(uint64_t source, uint64_t target);
178
+ void disconnect_all(uint64_t source);
179
+ void set_osc_type(uint64_t node, AudioOscType type);
180
+ void set_biquad_type(uint64_t node, AudioBiquadType type);
181
+ /** Allocate a delay line long enough for `seconds` (constructor argument of
182
+ * `createDelay`). */
183
+ void set_delay_max(uint64_t node, double seconds);
184
+ void set_buffer(uint64_t node, std::shared_ptr<AudioClip> clip, bool loop, double loop_start,
185
+ double loop_end);
186
+ void start_source(uint64_t node, double when, double offset, double duration);
187
+ void stop_source(uint64_t node, double when);
188
+
189
+ /* -- AudioParam -------------------------------------------------------- */
190
+ double param_value(uint64_t node, int index);
191
+ void param_set(uint64_t node, int index, double value);
192
+ void param_set_at(uint64_t node, int index, double value, double time);
193
+ void param_linear_ramp(uint64_t node, int index, double value, double time);
194
+ void param_exponential_ramp(uint64_t node, int index, double value, double time);
195
+ void param_set_target(uint64_t node, int index, double value, double time, double time_constant);
196
+ void param_cancel(uint64_t node, int index, double time, bool hold);
197
+
198
+ /* -- clock and device -------------------------------------------------- */
199
+ double current_time();
200
+ /** Best effort: a headless host has no device and playback stays silent. */
201
+ bool start_device();
202
+ void suspend_device();
203
+ void resume_device();
204
+ void close_device();
205
+ bool has_device();
206
+ /** Drop the graph; called by `AudioContext.close()`. */
207
+ void shutdown();
208
+
209
+ /** Render `frames` stereo frames (interleaved) from the graph. Safe to call
210
+ * from the audio callback and from `render_offline`. */
211
+ void render(float *out, int frames);
212
+ /** Synchronous render for the offline diagnostic hook: no device involved. */
213
+ void render_offline(float *out, int frames);
214
+
215
+ private:
216
+ AudioNode *node_locked(uint64_t id);
217
+ const float *pull_locked(AudioNode *node);
218
+ void render_quantum_locked();
219
+ void render_locked(float *out, int frames);
220
+
221
+ std::mutex mutex_;
222
+ std::unordered_map<uint64_t, std::shared_ptr<AudioNode>> nodes_;
223
+ uint64_t next_node_id_ = 1;
224
+ uint64_t destination_id_ = 0;
225
+ uint64_t serial_ = 0;
226
+ uint64_t frames_rendered_ = 0;
227
+ std::vector<float> pending_;
228
+ size_t pending_offset_ = 0;
229
+
230
+ const bool offline_;
231
+ bool running_ = true;
232
+ SDL_AudioStream *stream_ = nullptr;
233
+ bool device_tried_ = false;
234
+ };
235
+
236
+ } // namespace xtgui
237
+
238
+ #endif /* XT_GUI_AUDIO_MIXER_H */
@@ -0,0 +1,104 @@
1
+ /*
2
+ * xbintsc GUI engine — AudioParam automation curves.
3
+ *
4
+ * A param is a base value plus a time-ordered event list. Every accessor on the
5
+ * page's AudioParam (value, setValueAtTime, the ramps, setTargetAtTime,
6
+ * cancel*) appends an event; the mixer evaluates the list per sample so the
7
+ * curve keeps moving even when no JS calls happen between frames.
8
+ *
9
+ * The timeline belongs to the mixer and is only reached with the mixer's lock
10
+ * held, so it needs no lock of its own.
11
+ */
12
+
13
+ #include "audio_mixer.h"
14
+
15
+ #include <cmath>
16
+
17
+ namespace xtgui {
18
+
19
+ void AudioParamState::reset(double value) {
20
+ base_ = value;
21
+ events_.clear();
22
+ }
23
+
24
+ void AudioParamState::insert(Event event) {
25
+ for (size_t i = 0; i < events_.size(); i++) {
26
+ if (std::fabs(events_[i].time - event.time) < 1e-12) {
27
+ events_[i] = event;
28
+ return;
29
+ }
30
+ if (events_[i].time > event.time) {
31
+ events_.insert(events_.begin() + static_cast<std::ptrdiff_t>(i), event);
32
+ return;
33
+ }
34
+ }
35
+ events_.push_back(event);
36
+ }
37
+
38
+ void AudioParamState::set_value(double value, double time) {
39
+ if (events_.empty()) base_ = value;
40
+ insert({time, kEventSet, value, 0.0});
41
+ }
42
+
43
+ void AudioParamState::set_value_at_time(double value, double time) {
44
+ insert({time, kEventSet, value, 0.0});
45
+ }
46
+
47
+ void AudioParamState::linear_ramp(double value, double time) {
48
+ insert({time, kEventLinear, value, 0.0});
49
+ }
50
+
51
+ void AudioParamState::exponential_ramp(double value, double time) {
52
+ insert({time, kEventExponential, value, 0.0});
53
+ }
54
+
55
+ void AudioParamState::set_target(double value, double time, double time_constant) {
56
+ insert({time, kEventTarget, value, time_constant});
57
+ }
58
+
59
+ void AudioParamState::cancel_scheduled(double time) {
60
+ while (!events_.empty() && events_.back().time >= time) events_.pop_back();
61
+ }
62
+
63
+ void AudioParamState::cancel_and_hold(double time) {
64
+ double value = value_at(time);
65
+ cancel_scheduled(time);
66
+ insert({time, kEventSet, value, 0.0});
67
+ }
68
+
69
+ double AudioParamState::evaluate(size_t index, double time) const {
70
+ const Event &event = events_[index];
71
+ if (event.kind == kEventSet) return event.value;
72
+ if (event.kind == kEventTarget) {
73
+ double start = index > 0 ? evaluate(index - 1, events_[index - 1].time) : base_;
74
+ if (event.time_constant <= 0.0) return event.value;
75
+ double decay = std::exp(-(time - event.time) / event.time_constant);
76
+ return event.value + (start - event.value) * decay;
77
+ }
78
+ /* A ramp that has already ended holds its target value. */
79
+ return event.value;
80
+ }
81
+
82
+ double AudioParamState::value_at(double time) const {
83
+ if (events_.empty() || time < events_.front().time) return base_;
84
+ size_t index = 0;
85
+ while (index + 1 < events_.size() && events_[index + 1].time <= time) index++;
86
+ if (index + 1 < events_.size()) {
87
+ const Event &next = events_[index + 1];
88
+ if ((next.kind == kEventLinear || next.kind == kEventExponential) && time < next.time) {
89
+ double start_time = events_[index].time;
90
+ double start_value = evaluate(index, start_time);
91
+ double span = next.time - start_time;
92
+ double fraction = span > 0.0 ? (time - start_time) / span : 1.0;
93
+ if (next.kind == kEventLinear) {
94
+ return start_value + (next.value - start_value) * fraction;
95
+ }
96
+ double from = std::fabs(start_value) < 1e-9 ? 1e-6 : start_value;
97
+ double to = std::fabs(next.value) < 1e-9 ? 1e-6 : next.value;
98
+ return from * std::pow(to / from, fraction);
99
+ }
100
+ }
101
+ return evaluate(index, time);
102
+ }
103
+
104
+ } // namespace xtgui
@@ -0,0 +1,155 @@
1
+ /*
2
+ * xbintsc GUI engine — the JavaScript AudioParam object.
3
+ *
4
+ * The automation timeline itself lives in the mixer (`audio_param.cpp`); this
5
+ * unit is the object a page holds: a live `value` accessor plus the scheduling
6
+ * methods, each forwarding to the native node slot it names. Splitting it out of
7
+ * `audio.cpp` keeps both translation units within the file-size budget.
8
+ */
9
+
10
+ #include "audio_internal.h"
11
+
12
+ #include <cstdint>
13
+ #include <memory>
14
+
15
+ namespace {
16
+
17
+ using namespace xtgui;
18
+
19
+ double number_prop(xt_value object, const char *name) {
20
+ xt_value value = xt_object_get_cstr(object, name);
21
+ return XT_IS_NUMBER(value) ? xt_to_number(value) : 0.0;
22
+ }
23
+
24
+ xt_value fn_of(void *fn) { return xt_closure_new(fn, 0, NULL); }
25
+
26
+ struct ParamRef {
27
+ std::shared_ptr<AudioMixer> mixer;
28
+ uint64_t node = 0;
29
+ int index = 0;
30
+ };
31
+
32
+ bool param_ref(xt_value object, ParamRef *out) {
33
+ out->mixer = audio_mixer_by_id(audio_context_id(object));
34
+ out->node = static_cast<uint64_t>(number_prop(object, "__xt_audio_node"));
35
+ out->index = static_cast<int>(number_prop(object, "__xt_audio_param"));
36
+ return out->mixer != nullptr && out->node != 0;
37
+ }
38
+
39
+ xt_value param_value_get(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
40
+ (void)env;
41
+ (void)argc;
42
+ (void)argv;
43
+ ParamRef ref;
44
+ if (!param_ref(self, &ref)) return xt_number(0);
45
+ return xt_number(ref.mixer->param_value(ref.node, ref.index));
46
+ }
47
+
48
+ xt_value param_value_set(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
49
+ (void)env;
50
+ xt_value value = xt_arg(argc, argv, 0);
51
+ if (!XT_IS_NUMBER(value)) value = xt_number(xt_to_number(value));
52
+ ParamRef ref;
53
+ if (param_ref(self, &ref)) {
54
+ ref.mixer->param_set(ref.node, ref.index, xt_to_number(value));
55
+ }
56
+ return value;
57
+ }
58
+
59
+ xt_value param_set_value_at_time(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
60
+ (void)env;
61
+ ParamRef ref;
62
+ if (param_ref(self, &ref)) {
63
+ double value = xt_to_number(xt_arg(argc, argv, 0));
64
+ double time = argc > 1 ? xt_to_number(argv[1]) : ref.mixer->current_time();
65
+ ref.mixer->param_set_at(ref.node, ref.index, value, time);
66
+ }
67
+ return self;
68
+ }
69
+
70
+ xt_value param_linear_ramp(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
71
+ (void)env;
72
+ ParamRef ref;
73
+ if (param_ref(self, &ref)) {
74
+ double value = xt_to_number(xt_arg(argc, argv, 0));
75
+ double time = argc > 1 ? xt_to_number(argv[1]) : ref.mixer->current_time();
76
+ ref.mixer->param_linear_ramp(ref.node, ref.index, value, time);
77
+ }
78
+ return self;
79
+ }
80
+
81
+ xt_value param_exponential_ramp(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
82
+ (void)env;
83
+ ParamRef ref;
84
+ if (param_ref(self, &ref)) {
85
+ double value = xt_to_number(xt_arg(argc, argv, 0));
86
+ double time = argc > 1 ? xt_to_number(argv[1]) : ref.mixer->current_time();
87
+ ref.mixer->param_exponential_ramp(ref.node, ref.index, value, time);
88
+ }
89
+ return self;
90
+ }
91
+
92
+ xt_value param_set_target(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
93
+ (void)env;
94
+ ParamRef ref;
95
+ if (param_ref(self, &ref)) {
96
+ double value = xt_to_number(xt_arg(argc, argv, 0));
97
+ double time = argc > 1 ? xt_to_number(argv[1]) : ref.mixer->current_time();
98
+ double constant = argc > 2 ? xt_to_number(argv[2]) : 0.0;
99
+ ref.mixer->param_set_target(ref.node, ref.index, value, time, constant);
100
+ }
101
+ return self;
102
+ }
103
+
104
+ xt_value param_cancel(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
105
+ (void)env;
106
+ ParamRef ref;
107
+ if (param_ref(self, &ref)) {
108
+ double time = argc > 0 ? xt_to_number(argv[0]) : 0.0;
109
+ ref.mixer->param_cancel(ref.node, ref.index, time, false);
110
+ }
111
+ return self;
112
+ }
113
+
114
+ xt_value param_cancel_and_hold(xt_value self, xt_value env, int32_t argc, xt_value *argv) {
115
+ (void)env;
116
+ ParamRef ref;
117
+ if (param_ref(self, &ref)) {
118
+ double time = argc > 0 ? xt_to_number(argv[0]) : 0.0;
119
+ ref.mixer->param_cancel(ref.node, ref.index, time, true);
120
+ }
121
+ return self;
122
+ }
123
+
124
+ void set_prop(xt_value target, const char *name, xt_value value) {
125
+ xt_object_set(target, xt_string_from_cstr(name), value);
126
+ }
127
+
128
+ void set_method(xt_value target, const char *name, void *fn) { set_prop(target, name, fn_of(fn)); }
129
+
130
+ void set_number(xt_value target, const char *name, double value) {
131
+ set_prop(target, name, xt_number(value));
132
+ }
133
+
134
+ } // namespace
135
+
136
+ namespace xtgui {
137
+
138
+ xt_value audio_param_build(uint64_t context, uint64_t node, int index) {
139
+ xt_value param = xt_object_new();
140
+ set_number(param, "__xt_audio_ctx", static_cast<double>(context));
141
+ set_number(param, "__xt_audio_node", static_cast<double>(node));
142
+ set_number(param, "__xt_audio_param", index);
143
+ xt_object_define_getter(param, xt_string_from_cstr("value"), fn_of((void *)param_value_get));
144
+ xt_object_define_setter(param, xt_string_from_cstr("value"), fn_of((void *)param_value_set));
145
+ set_method(param, "setValueAtTime", (void *)param_set_value_at_time);
146
+ set_method(param, "linearRampToValueAtTime", (void *)param_linear_ramp);
147
+ set_method(param, "exponentialRampToValueAtTime", (void *)param_exponential_ramp);
148
+ set_method(param, "setTargetAtTime", (void *)param_set_target);
149
+ set_method(param, "cancelScheduledValues", (void *)param_cancel);
150
+ set_method(param, "cancelAndHoldAtTime", (void *)param_cancel_and_hold);
151
+ set_number(param, "defaultValue", 0.0);
152
+ return param;
153
+ }
154
+
155
+ } // namespace xtgui
@@ -357,6 +357,16 @@ xt_value type_proto(const std::string &name) {
357
357
  if (name == "WebGLQuery") return domapi::webgl_object_proto("query");
358
358
  if (name == "WebGLSync") return domapi::webgl_object_proto("sync");
359
359
  if (name == "WebGLTransformFeedback") return domapi::webgl_object_proto("transformfeedback");
360
+ if (name == "ImageBitmap") {
361
+ /* A bitmap is a plain immutable object, not a DOM node: a node proto would
362
+ * expose the element width/height accessors and answer 0. */
363
+ static xt_value bitmap_proto = XT_UNDEFINED;
364
+ if (!XT_IS_OBJECT(bitmap_proto)) {
365
+ bitmap_proto = xt_object_new();
366
+ xt_gc_add_root(&bitmap_proto);
367
+ }
368
+ return bitmap_proto;
369
+ }
360
370
  return domapi::node_proto();
361
371
  }
362
372
 
@@ -528,7 +538,7 @@ xt_value xt_browser_type(int32_t argc, xt_value *argv) {
528
538
  if (argc < 1) return XT_UNDEFINED;
529
539
  std::string name = cstr(xt_arg(argc, argv, 0));
530
540
  if (name == "XRWebGLLayer" || name == "XRWebGLBinding" || name == "OffscreenCanvas" ||
531
- name == "ImageBitmap" || name == "ImageData" || name == "Response" ||
541
+ name == "ImageData" || name == "Response" ||
532
542
  name == "ReadableStream" || name == "Request" || name == "Headers" ||
533
543
  name == "DOMParser") {
534
544
  return XT_UNDEFINED;
@@ -16,11 +16,29 @@ class SelectorParser {
16
16
  std::vector<ComplexSelector> selectors;
17
17
  size_t start = 0;
18
18
  int depth = 0;
19
+ char quote = 0;
20
+ /* Split at top-level commas. A selector list may be formatted across any
21
+ * number of lines: newlines are ordinary CSS whitespace, and `parseComplex`
22
+ * turns a whitespace run into a descendant combinator. Quoted attribute
23
+ * values are skipped so a `,`/`(`/`[` inside them cannot unbalance the
24
+ * split (an attribute value may itself span lines). */
19
25
  for (size_t i = 0; i <= text_.size(); i++) {
20
26
  char c = (i < text_.size()) ? text_[i] : ',';
21
- if (c == '(' || c == '[') depth++;
22
- else if (c == ')' || c == ']') depth--;
23
- else if (c == ',' && depth == 0) {
27
+ if (quote != 0) {
28
+ if (c == '\\' && i + 1 < text_.size()) {
29
+ i++;
30
+ continue;
31
+ }
32
+ if (c == quote) quote = 0;
33
+ continue;
34
+ }
35
+ if (c == '"' || c == '\'') {
36
+ quote = c;
37
+ } else if (c == '(' || c == '[') {
38
+ depth++;
39
+ } else if (c == ')' || c == ']') {
40
+ if (depth > 0) depth--;
41
+ } else if (c == ',' && depth == 0) {
24
42
  std::string part = text_.substr(start, i - start);
25
43
  ComplexSelector selector;
26
44
  if (parseComplex(part, &selector) && !selector.compounds.empty()) {
@@ -77,6 +77,9 @@ uint64_t canvas_gradient_id(xt_value handle, const xtgui::Canvas2D *canvas);
77
77
 
78
78
  /** The engine WebGL context behind a context handle, or null. */
79
79
  xtgui::WebGLContext *webgl_from_context(xt_value self);
80
+ /** Like `webgl_from_context`, but null while the context is lost: every GL
81
+ * entry point except `getError`/`isContextLost` is a no-op in that state. */
82
+ xtgui::WebGLContext *webgl_live_context(xt_value self);
80
83
  /** The numeric context id stored on a context handle (0 when absent). */
81
84
  uint64_t webgl_context_id(xt_value self);
82
85
  /** Create the JavaScript handle for a canvas's WebGL context. */
@@ -95,9 +98,19 @@ std::vector<double> webgl_numbers(xt_value value);
95
98
  std::vector<uint8_t> webgl_bytes(xt_value value);
96
99
  /** Store `count` bytes into a typed array / array handle. */
97
100
  void webgl_write_bytes(xt_value target, const uint8_t *bytes, size_t count);
101
+ /** Convert a source pixel array (RGBA/RGB/LUMINANCE/…) to RGBA8. */
102
+ void webgl_convert_pixels(const uint8_t *source, int count, int format, bool premultiply,
103
+ std::vector<uint8_t> *out);
104
+ /** Decode a `texImage2D` source (`<img>`, `<canvas>`, `<video>`, `ImageData`,
105
+ * `ImageBitmap`) into top-down RGBA8. */
106
+ bool webgl_read_source(xt_value source, int *width, int *height,
107
+ std::vector<uint8_t> *rgba, bool premultiply);
98
108
  /** Read the `UNPACK_FLIP_Y_WEBGL` / `UNPACK_PREMULTIPLY_ALPHA_WEBGL` flags set
99
109
  * through `pixelStorei`. */
100
110
  void webgl_unpack_flags(xt_value context, bool *flip_y, bool *premultiply);
111
+ /** Extension object for a compressed-texture family (null when unsupported,
112
+ * undefined when the name is not one of those extensions). */
113
+ xt_value webgl_compressed_extension(const std::string &name, xtgui::WebGLContext *context);
101
114
 
102
115
  /** How a pending handle names the node it will bind to. */
103
116
  enum PendingKind { pending_by_id = 1, pending_by_selector = 2 };
@@ -426,6 +439,8 @@ METHOD(webgl_tex_parameterf);
426
439
  METHOD(webgl_get_tex_parameter);
427
440
  METHOD(webgl_tex_image_2d);
428
441
  METHOD(webgl_tex_sub_image_2d);
442
+ METHOD(webgl_compressed_tex_image_2d);
443
+ METHOD(webgl_compressed_tex_sub_image_2d);
429
444
  METHOD(webgl_generate_mipmap);
430
445
  METHOD(webgl_create_framebuffer);
431
446
  METHOD(webgl_delete_framebuffer);
@@ -495,6 +510,7 @@ METHOD(webgl2_get_uniform_block_index);
495
510
  METHOD(webgl2_get_active_uniform_block_parameter);
496
511
  METHOD(webgl2_get_active_uniform_block_name);
497
512
  METHOD(webgl2_uniform_block_binding);
513
+ METHOD(webgl2_drawing_buffer_storage);
498
514
  METHOD(webgl2_tex_storage_2d);
499
515
  METHOD(webgl2_tex_storage_3d);
500
516
  METHOD(webgl2_tex_image_3d);
@@ -159,6 +159,8 @@ const MethodEntry kMethods[] = {
159
159
  {"getTexParameter", (void *)webgl_get_tex_parameter},
160
160
  {"texImage2D", (void *)webgl_tex_image_2d},
161
161
  {"texSubImage2D", (void *)webgl_tex_sub_image_2d},
162
+ {"compressedTexImage2D", (void *)webgl_compressed_tex_image_2d},
163
+ {"compressedTexSubImage2D", (void *)webgl_compressed_tex_sub_image_2d},
162
164
  {"generateMipmap", (void *)webgl_generate_mipmap},
163
165
  {"createFramebuffer", (void *)webgl_create_framebuffer},
164
166
  {"deleteFramebuffer", (void *)webgl_delete_framebuffer},
@@ -238,6 +240,11 @@ xtgui::WebGLContext *webgl_from_context(xt_value self) {
238
240
  return id != 0 ? xtgui::xt_webgl_by_id(id) : nullptr;
239
241
  }
240
242
 
243
+ xtgui::WebGLContext *webgl_live_context(xt_value self) {
244
+ xtgui::WebGLContext *context = webgl_from_context(self);
245
+ return context != nullptr && !context->lost() ? context : nullptr;
246
+ }
247
+
241
248
  xt_value webgl_context_handle(xt_value element, xtgui::WebGLContext *context) {
242
249
  xt_value cached = xt_object_get_cstr(element, "__xt_gui_gl");
243
250
  if (XT_IS_OBJECT(cached)) return cached;