uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit 8855b960571591ef0ba8899b376207937b55aa5b
parent 4622a8a061265fa767351ab65462cc6bda9c4f68
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date:   Sat, 17 Jul 2021 10:13:21 +0100

Started pausing the audio device when it's not in use

Diffstat:
Msrc/devices/apu.c | 7++++---
Msrc/devices/apu.h | 2+-
Msrc/uxnemu.c | 8+++++---
3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/devices/apu.c b/src/devices/apu.c @@ -37,11 +37,11 @@ envelope(Apu *c, Uint32 age) return 0x0000; } -void +int apu_render(Apu *c, Sint16 *sample, Sint16 *end) { Sint32 s; - if(!c->advance || !c->period) return; + if(!c->advance || !c->period) return 0; while(sample < end) { c->count += c->advance; c->i += c->count / c->period; @@ -49,7 +49,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end) if(c->i >= c->len) { if(!c->repeat) { c->advance = 0; - return; + return 1; } 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; } + return 1; } void diff --git a/src/devices/apu.h b/src/devices/apu.h @@ -24,6 +24,6 @@ typedef struct { Uint8 pitch, repeat; } Apu; -void apu_render(Apu *c, Sint16 *sample, Sint16 *end); +int apu_render(Apu *c, Sint16 *sample, Sint16 *end); void apu_start(Apu *c, Uint16 adsr, Uint8 pitch); Uint8 apu_get_vu(Apu *c); diff --git a/src/uxnemu.c b/src/uxnemu.c @@ -51,11 +51,13 @@ error(char *msg, const char *err) static void audio_callback(void *u, Uint8 *stream, int len) { - int i; + int i, running = 0; Sint16 *samples = (Sint16 *)stream; SDL_memset(stream, 0, len); for(i = 0; i < POLYPHONY; ++i) - apu_render(&apu[i], samples, samples + len / 2); + running += apu_render(&apu[i], samples, samples + len / 2); + if(!running) + SDL_PauseAudioDevice(audio_id, 1); (void)u; } @@ -162,7 +164,6 @@ init(void) audio_id = SDL_OpenAudioDevice(NULL, 0, &as, NULL, 0); if(!audio_id) return error("Audio", SDL_GetError()); - SDL_PauseAudioDevice(audio_id, 0); return 1; } @@ -300,6 +301,7 @@ audio_talk(Device *d, Uint8 b0, Uint8 w) c->repeat = !(d->dat[0xf] & 0x80); apu_start(c, mempeek16(d->dat, 0x8), d->dat[0xf] & 0x7f); SDL_UnlockAudioDevice(audio_id); + SDL_PauseAudioDevice(audio_id, 0); } }