shithub: treason

Download patch

ref: f8f4444cde638c762dc175508f19dd1b5cb6f7fc
parent: 0a0e38c97f48a3dc902b71eaf31905a63a832232
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Sep 15 10:18:17 EDT 2020

sync audio start to the first frame

--- a/main.c
+++ b/main.c
@@ -15,6 +15,7 @@
 static Image *curim;
 static uvlong lastframe;
 static int audiofd = -1;
+static Channel *audiosync;
 static Channel *audiofinished;
 
 static void
@@ -25,10 +26,15 @@
 	int audiofd;
 
 	if((audiofd = open("/dev/audio", OWRITE)) < 0){
-		fprint(2, "runaudio: %r");
+		fprint(2, "runaudio: %r\n");
+		recvp(audiosync);
+		chanclose(audiosync);
 	}else{
-		for(s = x; Sread(s, &f) == 0;)
+		for(s = x; Sread(s, &f) == 0;){
+			recvp(audiosync);
+			chanclose(audiosync);
 			write(audiofd, f.buf, f.sz);
+		}
 	}
 
 	sendp(audiofinished, nil);
@@ -79,6 +85,7 @@
 	draw(screen, r, curim, nil, ZP);
 	flushimage(display, 1);
 	unlockdisplay(display);
+	sendp(audiosync, nil);
 
 	delay = nanosec() - x;
 	lastframe += f->dt;
@@ -190,15 +197,17 @@
 		a[Cplayerdone].c = d->finished;
 		lastframe = 0;
 
+		if(saudio != nil){
+			audiosync = chancreate(sizeof(void*), 1);
+			audiofinished = chancreate(sizeof(void*), 0);
+			proccreate(audioproc, saudio, 4096);
+		}
+
 		for(done = 0; !done && !end;){
 			res = alt(a);
 			switch(res){
 			case Cframe:
 				drawframe(frame);
-				if(saudio != nil && audiofinished == nil){
-					audiofinished = chancreate(sizeof(void*), 0);
-					proccreate(audioproc, saudio, 4096);
-				}
 				break;
 
 			case Cplayerdone: