From 87157258b89bcba34854c95ea4fb339977b4dada Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Fri, 20 Aug 2021 22:45:39 +0100 Subject: [PATCH] Implemented Audio*/vector which runs when notes finish playing --- src/devices/apu.c | 3 ++- src/devices/apu.h | 1 + src/uxnemu.c | 13 ++++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/devices/apu.c b/src/devices/apu.c index 58b5c44..9c55f06 100644 --- a/src/devices/apu.c +++ b/src/devices/apu.c @@ -49,7 +49,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end) if(c->i >= c->len) { if(!c->repeat) { c->advance = 0; - return 1; + break; } c->i %= c->len; } @@ -57,6 +57,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end) *sample++ += s * c->volume[0] / 0x180; *sample++ += s * c->volume[1] / 0x180; } + if(!c->advance) apu_finished_handler(c); return 1; } diff --git a/src/devices/apu.h b/src/devices/apu.h index 839000b..6531c9a 100644 --- a/src/devices/apu.h +++ b/src/devices/apu.h @@ -26,3 +26,4 @@ typedef struct { int apu_render(Apu *c, Sint16 *sample, Sint16 *end); void apu_start(Apu *c, Uint16 adsr, Uint8 pitch); Uint8 apu_get_vu(Apu *c); +void apu_finished_handler(Apu *c); diff --git a/src/uxnemu.c b/src/uxnemu.c index afbe40e..549cb1f 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -32,7 +32,7 @@ static SDL_Rect gRect; static Ppu ppu; static Apu apu[POLYPHONY]; static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole; -static Uint32 stdin_event; +static Uint32 stdin_event, audio0_event; static Uint8 zoom = 1, reqdraw = 0; @@ -410,6 +410,14 @@ nil_talk(Device *d, Uint8 b0, Uint8 w) #pragma mark - Generics +void +apu_finished_handler(Apu *c) +{ + SDL_Event event; + event.type = audio0_event + (c - apu); + SDL_PushEvent(&event); +} + static int stdin_handler(void *p) { @@ -474,6 +482,8 @@ run(Uxn *u) if(event.type == stdin_event) { devconsole->dat[0x2] = event.cbutton.button; uxn_eval(u, mempeek16(devconsole->dat, 0)); + } else if(event.type >= audio0_event && event.type < audio0_event + POLYPHONY) { + uxn_eval(u, mempeek16((devaudio0 + (event.type - audio0_event))->dat, 0)); } } } @@ -504,6 +514,7 @@ main(int argc, char **argv) Uxn u; stdin_event = SDL_RegisterEvents(1); + audio0_event = SDL_RegisterEvents(POLYPHONY); SDL_CreateThread(stdin_handler, "stdin", NULL); if(argc < 2)