Browse code

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

Andrew Alderwick authored on 17/07/2021 09:13:21
Showing 3 changed files
... ...
@@ -37,11 +37,11 @@ envelope(Apu *c, Uint32 age)
37 37
 	return 0x0000;
38 38
 }
39 39
 
40
-void
40
+int
41 41
 apu_render(Apu *c, Sint16 *sample, Sint16 *end)
42 42
 {
43 43
 	Sint32 s;
44
-	if(!c->advance || !c->period) return;
44
+	if(!c->advance || !c->period) return 0;
45 45
 	while(sample < end) {
46 46
 		c->count += c->advance;
47 47
 		c->i += c->count / c->period;
... ...
@@ -49,7 +49,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end)
49 49
 		if(c->i >= c->len) {
50 50
 			if(!c->repeat) {
51 51
 				c->advance = 0;
52
-				return;
52
+				return 1;
53 53
 			}
54 54
 			c->i %= c->len;
55 55
 		}
... ...
@@ -57,6 +57,7 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end)
57 57
 		*sample++ += s * c->volume[0] / 0x180;
58 58
 		*sample++ += s * c->volume[1] / 0x180;
59 59
 	}
60
+	return 1;
60 61
 }
61 62
 
62 63
 void
... ...
@@ -24,6 +24,6 @@ typedef struct {
24 24
 	Uint8 pitch, repeat;
25 25
 } Apu;
26 26
 
27
-void apu_render(Apu *c, Sint16 *sample, Sint16 *end);
27
+int apu_render(Apu *c, Sint16 *sample, Sint16 *end);
28 28
 void apu_start(Apu *c, Uint16 adsr, Uint8 pitch);
29 29
 Uint8 apu_get_vu(Apu *c);
... ...
@@ -51,11 +51,13 @@ error(char *msg, const char *err)
51 51
 static void
52 52
 audio_callback(void *u, Uint8 *stream, int len)
53 53
 {
54
-	int i;
54
+	int i, running = 0;
55 55
 	Sint16 *samples = (Sint16 *)stream;
56 56
 	SDL_memset(stream, 0, len);
57 57
 	for(i = 0; i < POLYPHONY; ++i)
58
-		apu_render(&apu[i], samples, samples + len / 2);
58
+		running += apu_render(&apu[i], samples, samples + len / 2);
59
+	if(!running)
60
+		SDL_PauseAudioDevice(audio_id, 1);
59 61
 	(void)u;
60 62
 }
61 63
 
... ...
@@ -162,7 +164,6 @@ init(void)
162 164
 	audio_id = SDL_OpenAudioDevice(NULL, 0, &as, NULL, 0);
163 165
 	if(!audio_id)
164 166
 		return error("Audio", SDL_GetError());
165
-	SDL_PauseAudioDevice(audio_id, 0);
166 167
 	return 1;
167 168
 }
168 169
 
... ...
@@ -300,6 +301,7 @@ audio_talk(Device *d, Uint8 b0, Uint8 w)
300 301
 		c->repeat = !(d->dat[0xf] & 0x80);
301 302
 		apu_start(c, mempeek16(d->dat, 0x8), d->dat[0xf] & 0x7f);
302 303
 		SDL_UnlockAudioDevice(audio_id);
304
+		SDL_PauseAudioDevice(audio_id, 0);
303 305
 	}
304 306
 }
305 307