shithub: qk1

Download patch

ref: 8178edf7dadc13301de98099b987eefffd3adca6
parent: 8e5f152d104f69afa38b12e80439d378a369c71d
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Nov 8 21:13:28 EST 2023

snd: don't queue more audio than needed

--- a/fns.h
+++ b/fns.h
@@ -59,6 +59,7 @@
 char *lerr(void);
 int	sys_mkdir(char *path);
 
+long sndqueued(void);
 void sndstop(void);
 void sndwrite(uchar *buf, long sz);
 void sndclose(void);
--- a/snd.c
+++ b/snd.c
@@ -492,6 +492,27 @@
 
 	if(!ainit)
 		return;
+
+	if(sndt == 0)
+		sndt = nanosec() - Te9 / Fpsmax;
+	nsamp = (nanosec() - sndt) / (Te9 / Srate);
+	if(!cls.timedemo)
+		nsamp = (nsamp + 15) & ~15;
+	nsamp -= sndqueued()/Sblk - Srate/Fpsmax;
+	if(nsamp < 1)
+		return;
+	if(nsamp > Ssamp)
+		nsamp = Ssamp;
+	ns = nsamp * Sblk;
+	mixbuf = mixbufs[mixbufi];
+	samplesfx();
+	sampt += nsamp;
+	if(ns != 0){
+		sndwrite(mixbuf, ns);
+		mixbufi = (mixbufi + 1) % nelem(mixbufs);
+	}
+	sndt = nanosec();
+
 	VectorCopy(origin, listener_origin);
 	VectorCopy(forward, listener_forward);
 	VectorCopy(right, listener_right);
@@ -524,22 +545,6 @@
 			}
 		}
 	}
-	if(sndt == 0)
-		sndt = nanosec() - Te9 / Fpsmax;
-	nsamp = (nanosec() - sndt) / (Te9 / Srate);
-	if(!cls.timedemo)
-		nsamp = (nsamp + 15) & ~15;
-	if(nsamp > Ssamp)
-		nsamp = Ssamp;
-	ns = nsamp * Sblk;
-	mixbuf = mixbufs[mixbufi];
-	samplesfx();
-	sampt += nsamp;
-	if(ns != 0){
-		sndwrite(mixbuf, ns);
-		mixbufi = (mixbufi + 1) % nelem(mixbufs);
-	}
-	sndt = nanosec();
 }
 
 void
--- a/snd_plan9.c
+++ b/snd_plan9.c
@@ -6,6 +6,17 @@
 static uchar *mixbuf;
 static Channel *ach;
 
+long
+sndqueued(void)
+{
+	Dir *d;
+	long n;
+
+	n = (d = dirfstat(afd)) != nil ? d->length : 0;
+	free(d);
+	return n;
+}
+
 static void
 auproc(void *p)
 {
--- a/unix/snd_sdl.c
+++ b/unix/snd_sdl.c
@@ -3,6 +3,12 @@
 
 static SDL_AudioDeviceID adev;
 
+long
+sndqueued(void)
+{
+	return SDL_GetQueuedAudioSize(adev);
+}
+
 void
 sndstop(void)
 {