shithub: pplay

Download patch

ref: 8574965a34feb65d5870e3fc9d24df1fcf3d0c1f
parent: 928e9db52eaf922181c5b5d7e618e66cfd9afb61
author: qwx <qwx@sciops.net>
date: Sun Apr 30 07:50:15 EDT 2023

minimize crackling on jump and chunk barriers by yielding only once enough audio is written

--- a/pplay.c
+++ b/pplay.c
@@ -20,7 +20,7 @@
 static void
 athread(void *)
 {
-	int nerr;
+	int m, nerr;
 	uchar *b;
 	usize n;
 
@@ -28,18 +28,21 @@
 	for(;;){
 		if(afd < 0 || nerr > 10)
 			return;
-		if((b = getslice(&dot, Outsz, &n)) == nil || n <= 0){
-			fprint(2, "athread: %r\n");
-			nerr++;
-			continue;
+		for(m=Outsz; m>0; m-=n){
+			if((b = getslice(&dot, Outsz, &n)) == nil || n <= 0){
+				fprint(2, "athread: %r\n");
+				nerr++;
+				goto skip;
+			}
+			if(write(afd, b, n) != n){
+				fprint(2, "athread write: %r\n");
+				threadexits("write");
+			}
 		}
-		if(write(afd, b, n) != n){
-			fprint(2, "athread write: %r (nerr %d)\n", nerr);
-			break;
-		}
 		nerr = 0;
 		advance(&dot, n);
 		update();
+skip:
 		yield();
 	}
 }