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

This commit is contained in:
Andrew Alderwick 2021-07-17 10:13:21 +01:00
parent 4622a8a061
commit 8855b96057
3 changed files with 10 additions and 7 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);
}
}