Skip to content

Commit

Permalink
Remove callback from cosmoaudio API
Browse files Browse the repository at this point in the history
Using callbacks is still problematic with cosmo_dlopen() due to the need
to restore the TLS register. So using callbacks is even more strict than
using signal handlers. We are better off introducing a cosmoaudio_poll()
function. It makes the API more UNIX-like. How bad could the latency be?
  • Loading branch information
jart committed Sep 9, 2024
1 parent d99f066 commit d50d954
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 157 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ COSMOPOLITAN_OBJECTS = \

COSMOPOLITAN_H_PKGS = \
APE \
DSP_AUDIO \
LIBC \
LIBC_CALLS \
LIBC_ELF \
Expand Down
35 changes: 35 additions & 0 deletions dsp/audio/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ static struct {
typeof(cosmoaudio_open) *open;
typeof(cosmoaudio_close) *close;
typeof(cosmoaudio_write) *write;
typeof(cosmoaudio_flush) *flush;
typeof(cosmoaudio_read) *read;
typeof(cosmoaudio_poll) *poll;
} g_audio;

static const char *cosmoaudio_tmp_dir(void) {
Expand Down Expand Up @@ -232,7 +234,9 @@ static void cosmoaudio_setup(void) {
g_audio.open = cosmo_dlsym(handle, "cosmoaudio_open");
g_audio.close = cosmo_dlsym(handle, "cosmoaudio_close");
g_audio.write = cosmo_dlsym(handle, "cosmoaudio_write");
g_audio.flush = cosmo_dlsym(handle, "cosmoaudio_flush");
g_audio.read = cosmo_dlsym(handle, "cosmoaudio_read");
g_audio.poll = cosmo_dlsym(handle, "cosmoaudio_poll");
}

static void cosmoaudio_init(void) {
Expand Down Expand Up @@ -295,3 +299,34 @@ COSMOAUDIO_ABI int cosmoaudio_read(struct CosmoAudio *ca, float *data,
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
return status;
}

COSMOAUDIO_ABI int cosmoaudio_flush(struct CosmoAudio *ca) {
int status;
char sbuf[32];
if (g_audio.flush)
status = g_audio.flush(ca);
else
status = COSMOAUDIO_ELINK;
DATATRACE("cosmoaudio_flush(%p) → %s", ca,
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
return status;
}

COSMOAUDIO_ABI int cosmoaudio_poll(struct CosmoAudio *ca,
int *in_out_readFrames,
int *in_out_writeFrames) {
int status;
char sbuf[32];
char fbuf[2][20];
if (g_audio.poll)
status = g_audio.poll(ca, in_out_readFrames, in_out_writeFrames);
else
status = COSMOAUDIO_ELINK;
DATATRACE("cosmoaudio_poll(%p, %s, %s) → %s", ca,
cosmoaudio_describe_poll_frames(fbuf[0], sizeof(fbuf[0]),
in_out_readFrames),
cosmoaudio_describe_poll_frames(fbuf[1], sizeof(fbuf[1]),
in_out_writeFrames),
cosmoaudio_describe_status(sbuf, sizeof(sbuf), status));
return status;
}
1 change: 1 addition & 0 deletions dsp/audio/cosmoaudio/Makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# nmake /f Makefile.msvc check
# nmake /f Makefile.msvc MODE=debug check
#
# Note: MSVC 2019 makes the DLL 64kb smaller than MSVC 2022.

# Compiler and linker
CC=cl
Expand Down
Loading

0 comments on commit d50d954

Please sign in to comment.