sdl audio
Some checks failed
Build and Deploy / package-dist (push) Has been cancelled
Build and Deploy / deploy-itch (push) Has been cancelled
Build and Deploy / deploy-gitea (push) Has been cancelled
Build and Deploy / build-linux (push) Has been cancelled
Build and Deploy / build-windows (CLANG64) (push) Has been cancelled

This commit is contained in:
2025-04-28 08:48:44 -05:00
parent 7eca07c7d1
commit f68e45f898
6 changed files with 129 additions and 9 deletions

View File

@@ -71,7 +71,9 @@ sdl3_opts.add_cmake_defines({
'SDL_SHARED': 'OFF',
'SDL_TEST': 'OFF',
'CMAKE_BUILD_TYPE': 'Release',
'SDL_THREADS': 'ON'
'SDL_THREADS': 'ON',
'SDL_PIPEWIRE': 'ON',
'SDL_PULSEAUDIO': 'ON',
})
cc = meson.get_compiler('c')

View File

@@ -387,8 +387,12 @@ draw.slice9 = function slice9(image, rect = [0,0], slice = 0, info = slice9_info
if (!image) throw Error('Need an image to render.')
if (typeof image === "string")
image = graphics.texture(image)
var mesh = geometry.slice9(image.texture, rect, util.normalizeSpacing(slice), info)
render.queue({
render.slice9(image, rect, slice, slice9_info, pipeline);
// var mesh = geometry.slice9(image.texture, rect, util.normalizeSpacing(slice), info)
// console.log(json.encode(mesh))
// render.geometry(image, mesh, pipeline)
/* render.queue({
type: 'geometry',
mesh,
image,
@@ -396,6 +400,7 @@ draw.slice9 = function slice9(image, rect = [0,0], slice = 0, info = slice9_info
first_index:0,
num_indices:mesh.num_indices
})
*/
}
draw.slice9[prosperon.DOC] = `
:param image: An image object or string path to a texture.

View File

@@ -2,6 +2,8 @@ var render = {}
var context
var util = use('util')
render.initialize = function(config)
{
var default_conf = {
@@ -97,9 +99,14 @@ render.settings = function(set)
context.draw_color(set.color)
}
render.geometry = function(mesh, pipeline)
render.geometry = function(image, mesh, pipeline)
{
context.geometry(image, mesh)
}
render.slice9 = function(image, rect, slice, info, pipeline)
{
context.slice9(image.texture, image.rect_px, util.normalizeSpacing(slice), rect);
}
render.get_image = function(rect)

View File

@@ -1087,6 +1087,11 @@ QJSCLASS(datastream,)
QJSCLASS(SDL_Window,)
QJSCLASS(SDL_Camera,)
void SDL_AudioStream_free(JSRuntime *rt, SDL_AudioStream *st) {
SDL_DestroyAudioStream(st);
}
QJSCLASS(SDL_AudioStream,)
void SDL_Texture_free(JSRuntime *rt, SDL_Texture *t){
SDL_DestroyTexture(t);
}
@@ -2907,6 +2912,7 @@ JSC_CCALL(renderer_geometry,
JS_FreeValue(js,color);
JS_FreeValue(js,uv);
JS_FreeValue(js,indices);
printf("AMDET\n");
)
JSC_CCALL(renderer_logical_size,
@@ -3093,6 +3099,15 @@ JSC_CCALL(renderer_sprite,
SDL_RenderTexture(ctx->sdl, tex, &uv, &dst);
)
JSC_CCALL(renderer_slice9,
renderer_ctx *ctx = js2renderer_ctx(js, self);
SDL_Texture *tex = js2SDL_Texture(js,argv[0]);
rect src = js2rect(js, argv[1]);
lrtb exts = js2lrtb(js, argv[2]);
rect dst = renderer_worldrect_to_screen(ctx, js2rect(js, argv[3]));
SDL_RenderTexture9Grid(ctx->sdl, tex, &src, exts.l, exts.r, exts.t, exts.b, 0.0f, &dst);
)
static const JSCFunctionListEntry js_renderer_ctx_funcs[] = {
MIST_FUNC_DEF(SDL_Renderer, draw_color, 1),
MIST_FUNC_DEF(SDL_Renderer, present, 0),
@@ -3105,6 +3120,7 @@ static const JSCFunctionListEntry js_renderer_ctx_funcs[] = {
MIST_FUNC_DEF(renderer, sprite, 1),
MIST_FUNC_DEF(renderer, load_texture, 1),
MIST_FUNC_DEF(renderer, get_image, 1),
MIST_FUNC_DEF(renderer, slice9, 4),
MIST_FUNC_DEF(renderer, scale, 1),
MIST_FUNC_DEF(renderer, logical_size,1),
@@ -3119,6 +3135,85 @@ static const JSCFunctionListEntry js_renderer_ctx_funcs[] = {
MIST_FUNC_DEF(renderer, make_sprite_mesh, 2),
};
static int format_str_to_enum(const char *f, SDL_AudioFormat *out)
{
struct { const char *s; SDL_AudioFormat f; } map[] = {
{"u8", SDL_AUDIO_U8 }, {"s16", SDL_AUDIO_S16},
{"s32", SDL_AUDIO_S32}, {"f32", SDL_AUDIO_F32}
};
for (size_t i=0;i<countof(map);++i)
if (!strcmp(f,map[i].s)) { *out = map[i].f; return 1; }
return 0;
}
static SDL_AudioSpec js2audiospec(JSContext *js, JSValue obj)
{
SDL_AudioSpec spec;
JSValue v;
v = JS_GetPropertyStr(js, obj, "format");
if (!JS_IsUndefined(v)) {
const char *s = JS_ToCString(js, v);
format_str_to_enum(s, &spec.format);
JS_FreeCString(js, s);
}
JS_FreeValue(js, v);
v = JS_GetPropertyStr(js, obj, "channels");
if (!JS_IsUndefined(v)) JS_ToUint32(js, &spec.channels, v);
JS_FreeValue(js, v);
v = JS_GetPropertyStr(js, obj, "samplerate");
if (!JS_IsUndefined(v)) JS_ToUint32(js, &spec.freq, v);
JS_FreeValue(js, v);
return spec;
}
// SDL AUDIO
JSC_CCALL(sdl_audio_drivers,
int num = SDL_GetNumAudioDrivers();
JSValue arr = JS_NewArray(js);
for (int i = 0; i < num; i++)
JS_SetPropertyUint32(js, arr, i, JS_NewString(js, SDL_GetAudioDriver(i)));
return arr;
)
JSC_CCALL(sdl_audio_devices,
int n;
SDL_AudioDeviceID *ids = SDL_GetAudioPlaybackDevices(&n);
JSValue arr = JS_NewArray(js);
for (int i = 0; i < n; i++)
JS_SetPropertyUint32(js,arr,i,JS_NewString(js, SDL_GetAudioDeviceName(ids[i])));
return arr;
)
JSC_CCALL(sdl_audio_open_stream,
char *type = JS_IsString(argv[0]) ? JS_ToCString(js, argv[0]) : NULL;
SDL_AudioDeviceID devid = !strcmp(type, "capture") ? SDL_AUDIO_DEVICE_DEFAULT_RECORDING : SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
if (type)
JS_FreeCString(js, type);
// SDL_AudioSpec want = js2audiospec(js, argv[1]);
SDL_AudioStream *st = SDL_OpenAudioDeviceStream(devid, NULL, NULL, NULL);
if (!st)
return JS_ThrowInternalError(js, "open failed: %s", SDL_GetError());
return SDL_AudioStream2js(js, st);
)
static const JSCFunctionListEntry js_sdl_audio_funcs[] = {
MIST_FUNC_DEF(sdl_audio, drivers, 0),
MIST_FUNC_DEF(sdl_audio, devices, 0),
MIST_FUNC_DEF(sdl_audio, open_stream, 2),
};
// GPU API
JSC_CCALL(gpu_claim_window,
SDL_GPUDevice *gpu = js2SDL_GPUDevice(js,self);
@@ -7107,6 +7202,7 @@ MISTUSE(util)
MISTUSE(video)
MISTUSE(camera)
MISTUSE(debug)
MISTUSE(sdl_audio)
#include "qjs_crypto.h"
#include "qjs_time.h"
@@ -7147,6 +7243,8 @@ void ffi_load(JSContext *js)
arrput(rt->module_registry, MISTLINE(wota));
arrput(rt->module_registry, MISTLINE(crypto));
arrput(rt->module_registry, MISTLINE(blob));
arrput(rt->module_registry, MISTLINE(sdl_audio));
#ifdef TRACY_ENABLE
arrput(rt->module_registry, MISTLINE(tracy));

View File

@@ -1328,7 +1328,7 @@ int main(int argc, char **argv)
}
}
SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);
SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO | SDL_INIT_AUDIO);
queue_event = SDL_RegisterEvents(1);
int cores = SDL_GetNumLogicalCPUCores();

View File

@@ -37,8 +37,7 @@ var hudcam = {
var angle = 0
var pos = [0,0,0]
var s = 0
var f = 0
function loop()
{
pos.x += 1
@@ -59,9 +58,18 @@ function loop()
draw.rectangle({x:350, y:60, width:200, height:120}, {radius:10,thickness:3})
*/
render.camera(hudcam)
draw.image("button_grey", [0,0])
draw.slice9("button_grey", {x:0,y:0,width:200,height:250}, 10)
render.present()
$_.delay(loop, 1/60)
}
var sound = use('sound')
prosperon.myguy = sound.play("test.mp3")
var ss = use('sdl_audio')
console.log(ss.drivers())
console.log(ss.devices())
var feeder = ss.open_stream("playback", {format: "f32", channels:2, freq:48000})
loop()