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.
- package/README.md +10 -7
- package/README.zh-CN.md +10 -8
- package/dist/scripts/check-version.d.ts +27 -0
- package/dist/scripts/check-version.js +71 -0
- package/dist/scripts/check-version.js.map +1 -0
- package/dist/scripts/package-platform.d.ts +58 -0
- package/dist/scripts/package-platform.js +142 -0
- package/dist/scripts/package-platform.js.map +1 -0
- package/dist/scripts/verify-platform-package.d.ts +34 -0
- package/dist/scripts/verify-platform-package.js +161 -0
- package/dist/scripts/verify-platform-package.js.map +1 -0
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +3 -1
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/driver/runtime-lib.d.ts +29 -4
- package/dist/src/driver/runtime-lib.js +68 -10
- package/dist/src/driver/runtime-lib.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/index.js +6 -5
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/media/index.js +4 -3
- package/dist/src/extensions/media/index.js.map +1 -1
- package/dist/src/parser/context.d.ts +41 -0
- package/dist/src/parser/context.js +89 -4
- package/dist/src/parser/context.js.map +1 -1
- package/dist/src/parser/literals.js +6 -12
- package/dist/src/parser/literals.js.map +1 -1
- package/dist/tests/driver/platform-package.test.d.ts +1 -0
- package/dist/tests/driver/platform-package.test.js +49 -0
- package/dist/tests/driver/platform-package.test.js.map +1 -0
- package/dist/tests/driver/runtime-lib.test.js +51 -4
- package/dist/tests/driver/runtime-lib.test.js.map +1 -1
- package/dist/tests/e2e/gui-audio.test.d.ts +14 -0
- package/dist/tests/e2e/gui-audio.test.js +287 -0
- package/dist/tests/e2e/gui-audio.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +7 -0
- package/dist/tests/e2e/gui-helpers.js +1 -0
- package/dist/tests/e2e/gui-helpers.js.map +1 -1
- package/dist/tests/e2e/gui-media.test.js +78 -0
- package/dist/tests/e2e/gui-media.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +55 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl.test.js +258 -0
- package/dist/tests/e2e/gui-webgl.test.js.map +1 -1
- package/dist/tests/e2e/gui-webgl2-3d.test.d.ts +4 -0
- package/dist/tests/e2e/gui-webgl2-3d.test.js +264 -0
- package/dist/tests/e2e/gui-webgl2-3d.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.d.ts +10 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.js +262 -0
- package/dist/tests/e2e/gui-webgl2-blit.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.d.ts +6 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.js +395 -0
- package/dist/tests/e2e/gui-webgl2-formats.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.d.ts +4 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.js +105 -0
- package/dist/tests/e2e/gui-webgl2-mrt.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.d.ts +8 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.js +112 -0
- package/dist/tests/e2e/gui-webgl2-ubo.test.js.map +1 -0
- package/dist/tests/e2e/gui-webgl2.test.js +45 -1
- package/dist/tests/e2e/gui-webgl2.test.js.map +1 -1
- package/dist/tests/extensions/gui-index.test.js +1 -0
- package/dist/tests/extensions/gui-index.test.js.map +1 -1
- package/dist/tests/extensions/media.test.js +1 -0
- package/dist/tests/extensions/media.test.js.map +1 -1
- package/dist/tests/parser/expressions.test.js +30 -0
- package/dist/tests/parser/expressions.test.js.map +1 -1
- package/dist/tests/platform-tarball.test.d.ts +1 -0
- package/dist/tests/platform-tarball.test.js +111 -0
- package/dist/tests/platform-tarball.test.js.map +1 -0
- package/dist/tests/version.test.js +6 -0
- package/dist/tests/version.test.js.map +1 -1
- package/doc/ai/contributing.md +11 -0
- package/doc/ai/extensions.md +7 -4
- package/doc/ai/troubleshooting.md +7 -5
- package/doc/ai/zh-CN/contributing.md +9 -0
- package/doc/ai/zh-CN/extensions.md +4 -2
- package/doc/ai/zh-CN/troubleshooting.md +5 -3
- package/doc/gui/api.md +61 -13
- package/doc/gui/build.md +11 -4
- package/doc/gui/dom.md +6 -3
- package/doc/gui/media.md +4 -0
- package/doc/gui/webgl.md +105 -46
- package/doc/gui.md +7 -3
- package/doc/npm-package.md +55 -27
- package/doc/requirements.md +6 -2
- package/doc/zh-CN/gui/api.md +51 -10
- package/doc/zh-CN/gui/build.md +7 -2
- package/doc/zh-CN/gui/dom.md +3 -1
- package/doc/zh-CN/gui/media.md +3 -0
- package/doc/zh-CN/gui/webgl.md +22 -16
- package/doc/zh-CN/gui.md +3 -2
- package/doc/zh-CN/npm-package.md +44 -23
- package/doc/zh-CN/requirements.md +4 -1
- package/package.json +17 -1
- package/runtime/ext_gui/audio.cpp +365 -153
- package/runtime/ext_gui/audio_buffer.cpp +166 -0
- package/runtime/ext_gui/audio_buffer.h +37 -0
- package/runtime/ext_gui/audio_decode.cpp +219 -0
- package/runtime/ext_gui/audio_decode.h +39 -0
- package/runtime/ext_gui/audio_device.cpp +87 -0
- package/runtime/ext_gui/audio_effects.cpp +163 -0
- package/runtime/ext_gui/audio_effects.h +25 -0
- package/runtime/ext_gui/audio_internal.h +30 -0
- package/runtime/ext_gui/audio_mixer.cpp +494 -0
- package/runtime/ext_gui/audio_mixer.h +238 -0
- package/runtime/ext_gui/audio_param.cpp +104 -0
- package/runtime/ext_gui/audio_param_object.cpp +155 -0
- package/runtime/ext_gui/browser.cpp +11 -1
- package/runtime/ext_gui/css_selector.cpp +21 -3
- package/runtime/ext_gui/dom_api_internal.h +16 -0
- package/runtime/ext_gui/dom_api_webgl.cpp +7 -0
- package/runtime/ext_gui/dom_api_webgl2.cpp +151 -36
- package/runtime/ext_gui/dom_api_webgl2_draw.cpp +3 -3
- package/runtime/ext_gui/dom_api_webgl_buffer.cpp +6 -2
- package/runtime/ext_gui/dom_api_webgl_compressed.cpp +167 -0
- package/runtime/ext_gui/dom_api_webgl_framebuffer.cpp +160 -0
- package/runtime/ext_gui/dom_api_webgl_shader.cpp +4 -4
- package/runtime/ext_gui/dom_api_webgl_source.cpp +161 -0
- package/runtime/ext_gui/dom_api_webgl_state.cpp +169 -23
- package/runtime/ext_gui/dom_api_webgl_texture.cpp +276 -236
- package/runtime/ext_gui/dom_api_webgl_ubo.cpp +81 -0
- package/runtime/ext_gui/gui.cpp +44 -9
- package/runtime/ext_gui/gui_engine.h +9 -4
- package/runtime/ext_gui/image_bitmap.cpp +28 -0
- package/runtime/ext_gui/image_bitmap.h +26 -0
- package/runtime/ext_gui/image_bitmap_browser.cpp +136 -0
- package/runtime/ext_gui/media.cpp +32 -0
- package/runtime/ext_gui/media.h +13 -0
- package/runtime/ext_gui/media_api.h +23 -0
- package/runtime/ext_gui/webgl.h +152 -18
- package/runtime/ext_gui/webgl_blit.cpp +95 -0
- package/runtime/ext_gui/webgl_buffer.cpp +178 -263
- package/runtime/ext_gui/webgl_buffer_objects.cpp +270 -0
- package/runtime/ext_gui/webgl_clear.cpp +142 -0
- package/runtime/ext_gui/webgl_compressed.cpp +180 -0
- package/runtime/ext_gui/webgl_context.cpp +241 -156
- package/runtime/ext_gui/webgl_draw.cpp +68 -3
- package/runtime/ext_gui/webgl_draw_buffers.cpp +75 -0
- package/runtime/ext_gui/webgl_glsl.h +40 -0
- package/runtime/ext_gui/webgl_glsl_compile.cpp +6 -0
- package/runtime/ext_gui/webgl_glsl_rewrite.cpp +18 -0
- package/runtime/ext_gui/webgl_glsl_scan.cpp +20 -11
- package/runtime/ext_gui/webgl_glsl_scan_block.cpp +126 -0
- package/runtime/ext_gui/webgl_glsl_tokens.h +28 -0
- package/runtime/ext_gui/webgl_pipeline.cpp +71 -19
- package/runtime/ext_gui/webgl_program_blocks.cpp +58 -0
- package/runtime/ext_gui/webgl_program_internal.h +6 -0
- package/runtime/ext_gui/webgl_shader.cpp +13 -1
- package/runtime/ext_gui/webgl_state.cpp +228 -0
- package/runtime/ext_gui/webgl_texture.cpp +144 -17
- package/runtime/ext_gui/webgl_texture_3d.cpp +215 -0
- package/runtime/ext_gui/webgl_uniform_block.h +31 -0
- package/runtime/ext_gui/webgl_webgl2.cpp +58 -5
- package/runtime/ext_media/decode_audio.cpp +308 -0
- package/runtime/ext_media/media_bridge.c +2 -0
- package/runtime/rt.h +17 -0
- package/runtime/xt_array_buffer.c +30 -0
- package/runtime/xt_typed_array/elements.inc +13 -0
- package/scripts/build-media.ts +14 -6
- package/scripts/build-runtime.ts +21 -16
- package/scripts/check-version.ts +39 -9
- package/scripts/package-platform.ts +183 -0
- package/scripts/set-platform-version.ts +62 -0
- package/scripts/verify-platform-package.ts +182 -0
- package/src/cli/main.ts +5 -1
- package/src/driver/runtime-lib.ts +73 -9
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/index.ts +6 -5
- package/src/extensions/media/index.ts +4 -3
- package/src/parser/context.ts +99 -4
- package/src/parser/literals.ts +6 -12
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — 3D and 2D-array texture storage and uploads.
|
|
3
|
+
*
|
|
4
|
+
* SDL_GPU treats a 3D texture and a 2D-array as layered textures. Uploads are
|
|
5
|
+
* performed one slice at a time because SDL's multi-slice transfer stride is
|
|
6
|
+
* not usable for small 3D textures on the Metal backend.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include "webgl.h"
|
|
10
|
+
|
|
11
|
+
#include <cstring>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
#include "gui_internal.h"
|
|
15
|
+
|
|
16
|
+
namespace xtgui {
|
|
17
|
+
namespace {
|
|
18
|
+
|
|
19
|
+
int texel_bytes(SDL_GPUTextureFormat format) {
|
|
20
|
+
if (format == SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT) return 8;
|
|
21
|
+
if (format == SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT) return 16;
|
|
22
|
+
return 4;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Upload a whole 3D/2D-array volume. Each slice is uploaded separately:
|
|
26
|
+
* SDL's multi-slice transfer stride is not usable for a 2-layer 3D texture on
|
|
27
|
+
* the Metal backend. */
|
|
28
|
+
bool upload_volume(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUTexture *gpu,
|
|
29
|
+
int width, int height, int depth, const uint8_t *pixels,
|
|
30
|
+
int bytes_per_texel, bool is_3d) {
|
|
31
|
+
const uint32_t slice_bytes = (uint32_t)(width * height * bytes_per_texel);
|
|
32
|
+
const uint32_t bytes = slice_bytes * (uint32_t)depth;
|
|
33
|
+
if (bytes == 0) return true;
|
|
34
|
+
std::vector<uint8_t> zeros;
|
|
35
|
+
if (pixels == nullptr) {
|
|
36
|
+
zeros.assign(bytes, 0);
|
|
37
|
+
pixels = zeros.data();
|
|
38
|
+
}
|
|
39
|
+
SDL_GPUTransferBufferCreateInfo info;
|
|
40
|
+
memset(&info, 0, sizeof(info));
|
|
41
|
+
info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
|
42
|
+
info.size = bytes;
|
|
43
|
+
SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(device, &info);
|
|
44
|
+
if (transfer == nullptr) return false;
|
|
45
|
+
void *mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
|
|
46
|
+
if (mapped == nullptr) {
|
|
47
|
+
SDL_ReleaseGPUTransferBuffer(device, transfer);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
memcpy(mapped, pixels, bytes);
|
|
51
|
+
SDL_UnmapGPUTransferBuffer(device, transfer);
|
|
52
|
+
SDL_GPUCopyPass *copy = SDL_BeginGPUCopyPass(cmd);
|
|
53
|
+
for (int slice = 0; slice < depth; slice++) {
|
|
54
|
+
SDL_GPUTextureTransferInfo source;
|
|
55
|
+
memset(&source, 0, sizeof(source));
|
|
56
|
+
source.transfer_buffer = transfer;
|
|
57
|
+
source.offset = (Uint32)slice * slice_bytes;
|
|
58
|
+
source.pixels_per_row = (Uint32)width;
|
|
59
|
+
source.rows_per_layer = (Uint32)height;
|
|
60
|
+
SDL_GPUTextureRegion region;
|
|
61
|
+
memset(®ion, 0, sizeof(region));
|
|
62
|
+
region.texture = gpu;
|
|
63
|
+
region.layer = is_3d ? 0 : (Uint32)slice;
|
|
64
|
+
region.z = is_3d ? (Uint32)slice : 0;
|
|
65
|
+
region.w = (Uint32)width;
|
|
66
|
+
region.h = (Uint32)height;
|
|
67
|
+
region.d = 1;
|
|
68
|
+
SDL_UploadToGPUTexture(copy, &source, ®ion, false);
|
|
69
|
+
}
|
|
70
|
+
SDL_EndGPUCopyPass(copy);
|
|
71
|
+
SDL_ReleaseGPUTransferBuffer(device, transfer);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Upload a sub-volume of a 3D/2D-array texture, one slice at a time. */
|
|
76
|
+
bool upload_sub_volume(SDL_GPUDevice *device, SDL_GPUCommandBuffer *cmd, SDL_GPUTexture *gpu,
|
|
77
|
+
int x, int y, int z, int width, int height, int depth,
|
|
78
|
+
const uint8_t *pixels, int bytes_per_texel, bool is_3d) {
|
|
79
|
+
const uint32_t slice_bytes = (uint32_t)(width * height * bytes_per_texel);
|
|
80
|
+
const uint32_t bytes = slice_bytes * (uint32_t)depth;
|
|
81
|
+
if (bytes == 0 || pixels == nullptr) return false;
|
|
82
|
+
SDL_GPUTransferBufferCreateInfo info;
|
|
83
|
+
memset(&info, 0, sizeof(info));
|
|
84
|
+
info.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
|
|
85
|
+
info.size = bytes;
|
|
86
|
+
SDL_GPUTransferBuffer *transfer = SDL_CreateGPUTransferBuffer(device, &info);
|
|
87
|
+
if (transfer == nullptr) return false;
|
|
88
|
+
void *mapped = SDL_MapGPUTransferBuffer(device, transfer, false);
|
|
89
|
+
if (mapped == nullptr) {
|
|
90
|
+
SDL_ReleaseGPUTransferBuffer(device, transfer);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
memcpy(mapped, pixels, bytes);
|
|
94
|
+
SDL_UnmapGPUTransferBuffer(device, transfer);
|
|
95
|
+
SDL_GPUCopyPass *copy = SDL_BeginGPUCopyPass(cmd);
|
|
96
|
+
for (int slice = 0; slice < depth; slice++) {
|
|
97
|
+
SDL_GPUTextureTransferInfo source;
|
|
98
|
+
memset(&source, 0, sizeof(source));
|
|
99
|
+
source.transfer_buffer = transfer;
|
|
100
|
+
source.offset = (Uint32)slice * slice_bytes;
|
|
101
|
+
source.pixels_per_row = (Uint32)width;
|
|
102
|
+
source.rows_per_layer = (Uint32)height;
|
|
103
|
+
SDL_GPUTextureRegion region;
|
|
104
|
+
memset(®ion, 0, sizeof(region));
|
|
105
|
+
region.texture = gpu;
|
|
106
|
+
region.layer = is_3d ? 0 : (Uint32)(z + slice);
|
|
107
|
+
region.z = is_3d ? (Uint32)(z + slice) : 0;
|
|
108
|
+
region.x = (Uint32)x;
|
|
109
|
+
region.y = (Uint32)y;
|
|
110
|
+
region.w = (Uint32)width;
|
|
111
|
+
region.h = (Uint32)height;
|
|
112
|
+
region.d = 1;
|
|
113
|
+
SDL_UploadToGPUTexture(copy, &source, ®ion, false);
|
|
114
|
+
}
|
|
115
|
+
SDL_EndGPUCopyPass(copy);
|
|
116
|
+
SDL_ReleaseGPUTransferBuffer(device, transfer);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
} // namespace
|
|
121
|
+
|
|
122
|
+
bool WebGLContext::tex_storage_3d(uint32_t id, int levels, int width, int height, int depth,
|
|
123
|
+
SDL_GPUTextureFormat format, int target) {
|
|
124
|
+
(void)levels;
|
|
125
|
+
WebGLTexture *record = texture(id);
|
|
126
|
+
if (record == nullptr || width <= 0 || height <= 0 || depth <= 0) {
|
|
127
|
+
set_error(GL_INVALID_VALUE);
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
end_pass();
|
|
131
|
+
ensure_command();
|
|
132
|
+
if (record->gpu != nullptr) {
|
|
133
|
+
submit();
|
|
134
|
+
SDL_WaitForGPUIdle(guidev::g_device);
|
|
135
|
+
SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
|
|
136
|
+
record->gpu = nullptr;
|
|
137
|
+
record->usage = 0;
|
|
138
|
+
}
|
|
139
|
+
record->width = width;
|
|
140
|
+
record->height = height;
|
|
141
|
+
record->layers = depth;
|
|
142
|
+
record->format = format;
|
|
143
|
+
record->depth = false;
|
|
144
|
+
record->compressed = false;
|
|
145
|
+
record->target = target == GL_TEXTURE_3D ? GL_TEXTURE_3D : GL_TEXTURE_2D_ARRAY;
|
|
146
|
+
record->has_mipmaps = false;
|
|
147
|
+
if (record->sampler != nullptr && guidev::g_device != nullptr) {
|
|
148
|
+
SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
|
|
149
|
+
record->sampler = nullptr;
|
|
150
|
+
}
|
|
151
|
+
record->defined = true;
|
|
152
|
+
return ensure_texture_gpu(record, false) != nullptr;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
bool WebGLContext::tex_image_3d(uint32_t id, int width, int height, int depth,
|
|
156
|
+
SDL_GPUTextureFormat format, const uint8_t *pixels,
|
|
157
|
+
int target) {
|
|
158
|
+
WebGLTexture *record = texture(id);
|
|
159
|
+
if (record == nullptr || width <= 0 || height <= 0 || depth <= 0) {
|
|
160
|
+
set_error(GL_INVALID_VALUE);
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
end_pass();
|
|
164
|
+
ensure_command();
|
|
165
|
+
if (cmd_ == nullptr) return false;
|
|
166
|
+
const int resolved_target = target == GL_TEXTURE_3D ? GL_TEXTURE_3D : GL_TEXTURE_2D_ARRAY;
|
|
167
|
+
if (record->gpu != nullptr &&
|
|
168
|
+
(record->width != width || record->height != height || record->layers != depth ||
|
|
169
|
+
record->format != format || record->target != resolved_target)) {
|
|
170
|
+
SDL_WaitForGPUIdle(guidev::g_device);
|
|
171
|
+
SDL_ReleaseGPUTexture(guidev::g_device, record->gpu);
|
|
172
|
+
record->gpu = nullptr;
|
|
173
|
+
record->usage = 0;
|
|
174
|
+
}
|
|
175
|
+
record->width = width;
|
|
176
|
+
record->height = height;
|
|
177
|
+
record->layers = depth;
|
|
178
|
+
record->format = format;
|
|
179
|
+
record->depth = false;
|
|
180
|
+
record->compressed = false;
|
|
181
|
+
record->target = resolved_target;
|
|
182
|
+
record->has_mipmaps = false;
|
|
183
|
+
if (record->sampler != nullptr) {
|
|
184
|
+
SDL_ReleaseGPUSampler(guidev::g_device, record->sampler);
|
|
185
|
+
record->sampler = nullptr;
|
|
186
|
+
}
|
|
187
|
+
if (ensure_texture_gpu(record, false) == nullptr) return false;
|
|
188
|
+
record->defined = true;
|
|
189
|
+
return upload_volume(guidev::g_device, cmd_, record->gpu, width, height, depth, pixels,
|
|
190
|
+
texel_bytes(format), resolved_target == GL_TEXTURE_3D);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
bool WebGLContext::tex_sub_image_3d(uint32_t id, int x, int y, int z, int width, int height,
|
|
194
|
+
int depth, const uint8_t *pixels) {
|
|
195
|
+
WebGLTexture *record = texture(id);
|
|
196
|
+
if (record == nullptr || !record->defined || record->gpu == nullptr ||
|
|
197
|
+
(record->target != GL_TEXTURE_3D && record->target != GL_TEXTURE_2D_ARRAY)) {
|
|
198
|
+
set_error(GL_INVALID_OPERATION);
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
if (x < 0 || y < 0 || z < 0 || width <= 0 || height <= 0 || depth <= 0 ||
|
|
202
|
+
x + width > record->width || y + height > record->height ||
|
|
203
|
+
z + depth > record->layers) {
|
|
204
|
+
set_error(GL_INVALID_VALUE);
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
end_pass();
|
|
208
|
+
ensure_command();
|
|
209
|
+
if (cmd_ == nullptr) return false;
|
|
210
|
+
return upload_sub_volume(guidev::g_device, cmd_, record->gpu, x, y, z, width, height, depth,
|
|
211
|
+
pixels, texel_bytes(record->format),
|
|
212
|
+
record->target == GL_TEXTURE_3D);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — uniform-block bookkeeping shared by programs and draws.
|
|
3
|
+
*/
|
|
4
|
+
#ifndef XT_GUI_WEBGL_UNIFORM_BLOCK_H
|
|
5
|
+
#define XT_GUI_WEBGL_UNIFORM_BLOCK_H
|
|
6
|
+
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <string>
|
|
9
|
+
|
|
10
|
+
namespace xtgui {
|
|
11
|
+
|
|
12
|
+
/** A user uniform block merged into the engine's single std140 block. */
|
|
13
|
+
struct WebGLUniformBlock {
|
|
14
|
+
std::string name;
|
|
15
|
+
std::string instance;
|
|
16
|
+
int binding = 0;
|
|
17
|
+
int base_offset = 0;
|
|
18
|
+
int data_size = 0;
|
|
19
|
+
int member_count = 0;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** The buffer bound to one indexed uniform-buffer binding point. */
|
|
23
|
+
struct WebGLUniformBinding {
|
|
24
|
+
uint32_t buffer = 0;
|
|
25
|
+
uint32_t offset = 0;
|
|
26
|
+
uint32_t size = 0;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
} // namespace xtgui
|
|
30
|
+
|
|
31
|
+
#endif /* XT_GUI_WEBGL_UNIFORM_BLOCK_H */
|
|
@@ -125,14 +125,67 @@ bool WebGLContext::is_sync(uint32_t id) const {
|
|
|
125
125
|
return syncs_.find(id) != syncs_.end();
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
uint32_t WebGLContext::uniform_block_index(uint32_t
|
|
129
|
-
|
|
130
|
-
(
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
uint32_t WebGLContext::uniform_block_index(uint32_t program_id,
|
|
129
|
+
const std::string &name) const {
|
|
130
|
+
auto found = programs_.find(program_id);
|
|
131
|
+
if (found == programs_.end()) return 0xFFFFFFFFu;
|
|
132
|
+
const std::vector<WebGLUniformBlock> &blocks = found->second->blocks;
|
|
133
|
+
for (size_t i = 0; i < blocks.size(); i++) {
|
|
134
|
+
if (blocks[i].name == name) return (uint32_t)i;
|
|
135
|
+
}
|
|
133
136
|
return 0xFFFFFFFFu;
|
|
134
137
|
}
|
|
135
138
|
|
|
139
|
+
WebGLUniformBlock *WebGLContext::uniform_block(uint32_t program_id, uint32_t index) {
|
|
140
|
+
auto found = programs_.find(program_id);
|
|
141
|
+
if (found == programs_.end() || index >= found->second->blocks.size()) return nullptr;
|
|
142
|
+
return &found->second->blocks[index];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
int WebGLContext::uniform_block_parameter(uint32_t program_id, uint32_t index,
|
|
146
|
+
int pname) const {
|
|
147
|
+
auto found = programs_.find(program_id);
|
|
148
|
+
if (found == programs_.end() || index >= found->second->blocks.size()) return 0;
|
|
149
|
+
const WebGLUniformBlock &block = found->second->blocks[index];
|
|
150
|
+
switch (pname) {
|
|
151
|
+
case GL_UNIFORM_BLOCK_BINDING:
|
|
152
|
+
return block.binding;
|
|
153
|
+
case GL_UNIFORM_BLOCK_DATA_SIZE:
|
|
154
|
+
return block.data_size;
|
|
155
|
+
case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
|
|
156
|
+
return block.member_count;
|
|
157
|
+
case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
|
|
158
|
+
case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
|
|
159
|
+
return 1;
|
|
160
|
+
default:
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
std::string WebGLContext::uniform_block_name(uint32_t program_id, uint32_t index) const {
|
|
166
|
+
auto found = programs_.find(program_id);
|
|
167
|
+
if (found == programs_.end() || index >= found->second->blocks.size()) return std::string();
|
|
168
|
+
return found->second->blocks[index].name;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
void WebGLContext::uniform_block_binding(uint32_t program_id, uint32_t index, int binding) {
|
|
172
|
+
WebGLUniformBlock *block = uniform_block(program_id, index);
|
|
173
|
+
if (block != nullptr) block->binding = binding;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
void WebGLContext::bind_buffer_base(int target, int index, uint32_t buffer_id,
|
|
177
|
+
uint32_t offset, uint32_t size) {
|
|
178
|
+
if (target != 0x8A11 || index < 0 || index >= 16) return; // GL_UNIFORM_BUFFER
|
|
179
|
+
uniform_bindings_[index].buffer = buffer_id;
|
|
180
|
+
uniform_bindings_[index].offset = offset;
|
|
181
|
+
uniform_bindings_[index].size = size;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
uint32_t WebGLContext::uniform_buffer_binding(int index) const {
|
|
185
|
+
if (index < 0 || index >= 16) return 0;
|
|
186
|
+
return uniform_bindings_[index].buffer;
|
|
187
|
+
}
|
|
188
|
+
|
|
136
189
|
/* -- sampler objects ----------------------------------------------------- */
|
|
137
190
|
|
|
138
191
|
WebGLSampler *WebGLContext::sampler(uint32_t id) {
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc media extension — in-memory audio decoding.
|
|
3
|
+
*
|
|
4
|
+
* `AudioContext.decodeAudioData` hands over bytes, not a path, so the file
|
|
5
|
+
* oriented `MediaPlayer` cannot be reused. This unit opens the bytes through a
|
|
6
|
+
* custom `AVIOContext`, decodes the best audio stream to interleaved float PCM
|
|
7
|
+
* at the stream's own sample rate, and leaves resampling to the mixer (which
|
|
8
|
+
* already resamples an AudioBuffer whose rate differs from 48 kHz).
|
|
9
|
+
*
|
|
10
|
+
* The ABI is C (`media_api.h`): `gui.a` stays FFmpeg-free and reaches this
|
|
11
|
+
* object through the table the media bridge installs. Both archives are built
|
|
12
|
+
* against the same static CRT, so the caller may `free()` the buffer through
|
|
13
|
+
* `xt_media_backend_free_audio`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#include <cerrno>
|
|
17
|
+
#include <cstdio>
|
|
18
|
+
#include <cstdlib>
|
|
19
|
+
#include <cstring>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
extern "C" {
|
|
23
|
+
#include <libavcodec/avcodec.h>
|
|
24
|
+
#include <libavformat/avformat.h>
|
|
25
|
+
#include <libavutil/channel_layout.h>
|
|
26
|
+
#include <libswresample/swresample.h>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#include "../ext_gui/media_api.h"
|
|
30
|
+
|
|
31
|
+
namespace {
|
|
32
|
+
|
|
33
|
+
struct MemoryReader {
|
|
34
|
+
const unsigned char *data;
|
|
35
|
+
size_t size;
|
|
36
|
+
size_t position;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
int read_packet(void *opaque, uint8_t *buffer, int size) {
|
|
40
|
+
auto *reader = static_cast<MemoryReader *>(opaque);
|
|
41
|
+
if (reader->position >= reader->size) return AVERROR_EOF;
|
|
42
|
+
size_t available = reader->size - reader->position;
|
|
43
|
+
int take = size < static_cast<int>(available) ? size : static_cast<int>(available);
|
|
44
|
+
if (take <= 0) return AVERROR_EOF;
|
|
45
|
+
std::memcpy(buffer, reader->data + reader->position, static_cast<size_t>(take));
|
|
46
|
+
reader->position += static_cast<size_t>(take);
|
|
47
|
+
return take;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
int64_t seek_stream(void *opaque, int64_t offset, int whence) {
|
|
51
|
+
auto *reader = static_cast<MemoryReader *>(opaque);
|
|
52
|
+
if (whence == AVSEEK_SIZE) return static_cast<int64_t>(reader->size);
|
|
53
|
+
int64_t base;
|
|
54
|
+
if (whence == SEEK_SET) {
|
|
55
|
+
base = 0;
|
|
56
|
+
} else if (whence == SEEK_CUR) {
|
|
57
|
+
base = static_cast<int64_t>(reader->position);
|
|
58
|
+
} else if (whence == SEEK_END) {
|
|
59
|
+
base = static_cast<int64_t>(reader->size);
|
|
60
|
+
} else {
|
|
61
|
+
return AVERROR(EINVAL);
|
|
62
|
+
}
|
|
63
|
+
int64_t target = base + offset;
|
|
64
|
+
if (target < 0 || target > static_cast<int64_t>(reader->size)) return AVERROR(EINVAL);
|
|
65
|
+
reader->position = static_cast<size_t>(target);
|
|
66
|
+
return target;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
void set_error(char *error, size_t size, const char *message) {
|
|
70
|
+
if (error != nullptr && size > 0) std::snprintf(error, size, "%s", message);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void set_ffmpeg_error(char *error, size_t size, const char *prefix, int code) {
|
|
74
|
+
char detail[AV_ERROR_MAX_STRING_SIZE] = {0};
|
|
75
|
+
av_strerror(code, detail, sizeof(detail));
|
|
76
|
+
if (error != nullptr && size > 0) {
|
|
77
|
+
std::snprintf(error, size, "%s: %s", prefix, detail);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The decoder pipeline for one in-memory file. Destructor releases everything,
|
|
82
|
+
* which keeps the early-return paths in `decode` readable. */
|
|
83
|
+
struct Decoder {
|
|
84
|
+
MemoryReader reader{nullptr, 0, 0};
|
|
85
|
+
AVIOContext *io = nullptr;
|
|
86
|
+
AVFormatContext *format = nullptr;
|
|
87
|
+
AVCodecContext *codec = nullptr;
|
|
88
|
+
AVPacket *packet = nullptr;
|
|
89
|
+
AVFrame *frame = nullptr;
|
|
90
|
+
SwrContext *resampler = nullptr;
|
|
91
|
+
AVChannelLayout out_layout{};
|
|
92
|
+
int out_channels = 0;
|
|
93
|
+
int sample_rate = 0;
|
|
94
|
+
int stream_index = -1;
|
|
95
|
+
std::vector<float> samples;
|
|
96
|
+
bool failed = false;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
void close_decoder(Decoder *decoder) {
|
|
100
|
+
swr_free(&decoder->resampler);
|
|
101
|
+
av_channel_layout_uninit(&decoder->out_layout);
|
|
102
|
+
if (decoder->frame != nullptr) av_frame_free(&decoder->frame);
|
|
103
|
+
if (decoder->packet != nullptr) av_packet_free(&decoder->packet);
|
|
104
|
+
if (decoder->codec != nullptr) avcodec_free_context(&decoder->codec);
|
|
105
|
+
if (decoder->format != nullptr) avformat_close_input(&decoder->format);
|
|
106
|
+
if (decoder->io != nullptr) {
|
|
107
|
+
av_freep(&decoder->io->buffer);
|
|
108
|
+
avio_context_free(&decoder->io);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Lazily configure the resampler from the first decoded frame: some files only
|
|
113
|
+
* reveal their channel layout once decoding starts. */
|
|
114
|
+
bool prepare_resampler(Decoder *decoder, const AVFrame *frame, char *error, size_t error_size) {
|
|
115
|
+
if (decoder->resampler != nullptr) return true;
|
|
116
|
+
AVChannelLayout in_layout = frame->ch_layout;
|
|
117
|
+
if (in_layout.nb_channels <= 0) in_layout = decoder->codec->ch_layout;
|
|
118
|
+
int channels = in_layout.nb_channels > 0 ? in_layout.nb_channels : 1;
|
|
119
|
+
int rate = frame->sample_rate > 0 ? frame->sample_rate : decoder->codec->sample_rate;
|
|
120
|
+
if (rate <= 0) {
|
|
121
|
+
set_error(error, error_size, "audio stream has no usable sample rate");
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
decoder->out_channels = channels > 2 ? 2 : channels;
|
|
125
|
+
decoder->sample_rate = rate;
|
|
126
|
+
av_channel_layout_default(&decoder->out_layout, decoder->out_channels);
|
|
127
|
+
int result = swr_alloc_set_opts2(&decoder->resampler, &decoder->out_layout, AV_SAMPLE_FMT_FLT,
|
|
128
|
+
decoder->sample_rate, &in_layout,
|
|
129
|
+
static_cast<AVSampleFormat>(frame->format), rate, 0, nullptr);
|
|
130
|
+
if (result < 0 || decoder->resampler == nullptr ||
|
|
131
|
+
swr_init(decoder->resampler) < 0) {
|
|
132
|
+
set_error(error, error_size, "cannot set up audio resampling");
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Convert one decoded frame and append it to `decoder->samples`. */
|
|
139
|
+
bool append_frame(Decoder *decoder, const AVFrame *frame, char *error, size_t error_size) {
|
|
140
|
+
if (!prepare_resampler(decoder, frame, error, error_size)) return false;
|
|
141
|
+
int64_t delay = swr_get_delay(decoder->resampler, frame->sample_rate);
|
|
142
|
+
int capacity = static_cast<int>(av_rescale_rnd(delay + frame->nb_samples, decoder->sample_rate,
|
|
143
|
+
frame->sample_rate, AV_ROUND_UP));
|
|
144
|
+
if (capacity <= 0) return true;
|
|
145
|
+
std::vector<float> converted(static_cast<size_t>(capacity) * decoder->out_channels);
|
|
146
|
+
uint8_t *out_data[1] = {reinterpret_cast<uint8_t *>(converted.data())};
|
|
147
|
+
int got = swr_convert(decoder->resampler, out_data, capacity,
|
|
148
|
+
const_cast<const uint8_t **>(frame->extended_data), frame->nb_samples);
|
|
149
|
+
if (got < 0) {
|
|
150
|
+
set_ffmpeg_error(error, error_size, "cannot resample audio", got);
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
size_t count = static_cast<size_t>(got) * decoder->out_channels;
|
|
154
|
+
decoder->samples.insert(decoder->samples.end(), converted.begin(),
|
|
155
|
+
converted.begin() + static_cast<std::ptrdiff_t>(count));
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Drain the decoder after the last packet. */
|
|
160
|
+
bool flush_decoder(Decoder *decoder, char *error, size_t error_size) {
|
|
161
|
+
int result = avcodec_send_packet(decoder->codec, nullptr);
|
|
162
|
+
if (result < 0 && result != AVERROR_EOF) {
|
|
163
|
+
set_ffmpeg_error(error, error_size, "cannot flush audio decoder", result);
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
while (true) {
|
|
167
|
+
result = avcodec_receive_frame(decoder->codec, decoder->frame);
|
|
168
|
+
if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) return true;
|
|
169
|
+
if (result < 0) {
|
|
170
|
+
set_ffmpeg_error(error, error_size, "cannot read audio frame", result);
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (!append_frame(decoder, decoder->frame, error, error_size)) return false;
|
|
174
|
+
av_frame_unref(decoder->frame);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
bool decode_packets(Decoder *decoder, char *error, size_t error_size) {
|
|
179
|
+
while (true) {
|
|
180
|
+
int result = av_read_frame(decoder->format, decoder->packet);
|
|
181
|
+
if (result == AVERROR_EOF) break;
|
|
182
|
+
if (result < 0) {
|
|
183
|
+
set_ffmpeg_error(error, error_size, "cannot read audio packet", result);
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
if (decoder->packet->stream_index != decoder->stream_index) {
|
|
187
|
+
av_packet_unref(decoder->packet);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
int sent = avcodec_send_packet(decoder->codec, decoder->packet);
|
|
191
|
+
av_packet_unref(decoder->packet);
|
|
192
|
+
if (sent < 0 && sent != AVERROR(EAGAIN)) {
|
|
193
|
+
set_ffmpeg_error(error, error_size, "cannot decode audio packet", sent);
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
while (true) {
|
|
197
|
+
result = avcodec_receive_frame(decoder->codec, decoder->frame);
|
|
198
|
+
if (result == AVERROR(EAGAIN) || result == AVERROR_EOF) break;
|
|
199
|
+
if (result < 0) {
|
|
200
|
+
set_ffmpeg_error(error, error_size, "cannot decode audio frame", result);
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
if (!append_frame(decoder, decoder->frame, error, error_size)) return false;
|
|
204
|
+
av_frame_unref(decoder->frame);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return flush_decoder(decoder, error, error_size);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
bool open_decoder(Decoder *decoder, int *stream_index, char *error, size_t error_size) {
|
|
211
|
+
decoder->io = avio_alloc_context(
|
|
212
|
+
static_cast<unsigned char *>(av_malloc(32768)), 32768, 0, &decoder->reader, read_packet,
|
|
213
|
+
nullptr, seek_stream);
|
|
214
|
+
if (decoder->io == nullptr) {
|
|
215
|
+
set_error(error, error_size, "cannot allocate the audio reader");
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
decoder->format = avformat_alloc_context();
|
|
219
|
+
if (decoder->format == nullptr) {
|
|
220
|
+
set_error(error, error_size, "cannot allocate the audio container");
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
decoder->format->pb = decoder->io;
|
|
224
|
+
decoder->format->flags |= AVFMT_FLAG_CUSTOM_IO;
|
|
225
|
+
int result = avformat_open_input(&decoder->format, nullptr, nullptr, nullptr);
|
|
226
|
+
if (result < 0) {
|
|
227
|
+
set_ffmpeg_error(error, error_size, "cannot open audio data", result);
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
result = avformat_find_stream_info(decoder->format, nullptr);
|
|
231
|
+
if (result < 0) {
|
|
232
|
+
set_ffmpeg_error(error, error_size, "cannot read audio metadata", result);
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
*stream_index = av_find_best_stream(decoder->format, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
|
|
236
|
+
if (*stream_index < 0) {
|
|
237
|
+
set_error(error, error_size, "audio data has no audio stream");
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
decoder->stream_index = *stream_index;
|
|
241
|
+
AVCodecParameters *parameters = decoder->format->streams[*stream_index]->codecpar;
|
|
242
|
+
const AVCodec *codec = avcodec_find_decoder(parameters->codec_id);
|
|
243
|
+
if (codec == nullptr) {
|
|
244
|
+
set_error(error, error_size, "no decoder for this audio codec");
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
decoder->codec = avcodec_alloc_context3(codec);
|
|
248
|
+
if (decoder->codec == nullptr) {
|
|
249
|
+
set_error(error, error_size, "cannot allocate the audio decoder");
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
result = avcodec_parameters_to_context(decoder->codec, parameters);
|
|
253
|
+
if (result < 0) {
|
|
254
|
+
set_ffmpeg_error(error, error_size, "cannot configure the audio decoder", result);
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
result = avcodec_open2(decoder->codec, codec, nullptr);
|
|
258
|
+
if (result < 0) {
|
|
259
|
+
set_ffmpeg_error(error, error_size, "cannot start the audio decoder", result);
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
decoder->packet = av_packet_alloc();
|
|
263
|
+
decoder->frame = av_frame_alloc();
|
|
264
|
+
if (decoder->packet == nullptr || decoder->frame == nullptr) {
|
|
265
|
+
set_error(error, error_size, "cannot allocate decoder buffers");
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
} // namespace
|
|
272
|
+
|
|
273
|
+
extern "C" int xt_media_backend_decode_audio(const unsigned char *data, size_t size,
|
|
274
|
+
XtMediaAudioBuffer *out, char *error,
|
|
275
|
+
size_t error_size) {
|
|
276
|
+
if (data == nullptr || size == 0 || out == nullptr) {
|
|
277
|
+
set_error(error, error_size, "empty audio data");
|
|
278
|
+
return 0;
|
|
279
|
+
}
|
|
280
|
+
std::memset(out, 0, sizeof(*out));
|
|
281
|
+
Decoder decoder;
|
|
282
|
+
decoder.reader = {data, size, 0};
|
|
283
|
+
int stream_index = -1;
|
|
284
|
+
bool ok = open_decoder(&decoder, &stream_index, error, error_size) &&
|
|
285
|
+
decode_packets(&decoder, error, error_size);
|
|
286
|
+
if (!ok || decoder.out_channels <= 0 || decoder.samples.empty()) {
|
|
287
|
+
if (ok) set_error(error, error_size, "audio data has no samples");
|
|
288
|
+
close_decoder(&decoder);
|
|
289
|
+
return 0;
|
|
290
|
+
}
|
|
291
|
+
int frames = static_cast<int>(decoder.samples.size() / decoder.out_channels);
|
|
292
|
+
size_t bytes = decoder.samples.size() * sizeof(float);
|
|
293
|
+
auto *samples = static_cast<float *>(std::malloc(bytes));
|
|
294
|
+
if (samples == nullptr) {
|
|
295
|
+
set_error(error, error_size, "cannot allocate the decoded audio buffer");
|
|
296
|
+
close_decoder(&decoder);
|
|
297
|
+
return 0;
|
|
298
|
+
}
|
|
299
|
+
std::memcpy(samples, decoder.samples.data(), bytes);
|
|
300
|
+
out->channels = decoder.out_channels;
|
|
301
|
+
out->sample_rate = decoder.sample_rate;
|
|
302
|
+
out->frames = frames;
|
|
303
|
+
out->samples = samples;
|
|
304
|
+
close_decoder(&decoder);
|
|
305
|
+
return 1;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
extern "C" void xt_media_backend_free_audio(void *samples) { std::free(samples); }
|
package/runtime/rt.h
CHANGED
|
@@ -250,6 +250,23 @@ xt_value xt_string_static(xt_value name, int32_t argc, xt_value *argv);
|
|
|
250
250
|
xt_value xt_typed_array_ctor_by_name(xt_value name, int32_t argc, xt_value *argv);
|
|
251
251
|
xt_value xt_typed_array_static_by_name(xt_value name, xt_value method, int32_t argc, xt_value *argv);
|
|
252
252
|
xt_value xt_typed_array_prototype(xt_value name);
|
|
253
|
+
/** Bulk access to the bytes an `ArrayBuffer` (or a typed array's backing
|
|
254
|
+
* store) holds, for decoders and encoders that cannot afford one property read
|
|
255
|
+
* per byte. `offset` and `count` are clamped to the buffer and the number of
|
|
256
|
+
* bytes actually transferred is returned. Extensions pass the backing buffer
|
|
257
|
+
* plus a view's `byteOffset`/`byteLength`; see `runtime/ext_gui/audio.cpp`. */
|
|
258
|
+
uint32_t xt_array_buffer_copy_bytes_out(xt_value buffer, uint32_t offset, uint8_t *dst,
|
|
259
|
+
uint32_t count);
|
|
260
|
+
uint32_t xt_array_buffer_copy_bytes_in(xt_value buffer, uint32_t offset, const uint8_t *src,
|
|
261
|
+
uint32_t count);
|
|
262
|
+
/** Allocate a zero-filled `ArrayBuffer` of `byte_length` bytes (the native
|
|
263
|
+
* counterpart of `new ArrayBuffer(n)`). */
|
|
264
|
+
xt_value xt_array_buffer_alloc(uint32_t byte_length);
|
|
265
|
+
/** Typed-array view internals for extensions: whether `value` is a view, the
|
|
266
|
+
* `ArrayBuffer` it views, and the byte offset of the view inside it. */
|
|
267
|
+
int xt_is_typed_array(xt_value value);
|
|
268
|
+
xt_value xt_typed_array_backing_buffer(xt_value value);
|
|
269
|
+
uint32_t xt_typed_array_view_offset(xt_value value);
|
|
253
270
|
/** Error-family construction and prototype lookup by constructor name. */
|
|
254
271
|
xt_value xt_error_ctor_by_name(xt_value name, int32_t argc, xt_value *argv);
|
|
255
272
|
xt_value xt_error_prototype(xt_value name);
|