shithub: npe

Download patch

ref: 35e2b5b8391c23f01d41a181bc9d6d9dddd5425f
parent: 61a93fc303fe60819e7a0f9e264c0b592bcb8e93
author: Jacob Moody <moody@posixcafe.org>
date: Sun Feb 12 03:21:38 EST 2023

buffer proc for mixer

greases the wheel for midi to keep up at the cost
of some latency. For background music type stuff
this really doesn't matter.

--- a/libnpe_sdl2/mixer.c
+++ b/libnpe_sdl2/mixer.c
@@ -8,9 +8,7 @@
 static int forkerpid = -1;
 static int audiopid = -1;
 
-void audioexec(void *);
-void audioforker(void *);
-void audioproc(void *);
+void musicproc(void *);
 
 static int musicvol = 128;
 static int musicpaused = 0;
@@ -139,10 +137,16 @@
 	doneinit = 1;
 	memset(channels, 0, sizeof channels);
 	rfork(RFNAMEG);
-	if(rfork(RFPROC|RFFDG) == 0){
+	if(fork() == 0){
 		execl("/bin/audio/mixfs", "mixfs", nil);
 		sysfatal("exec: %r\n");
 	}
+	waitpid();
+	pipe(musicpipe);
+	if(fork() == 0){
+		musicproc(nil);
+		sysfatal("exec musicproc: %r");
+	}
 	return flags;
 }
 
@@ -207,7 +211,10 @@
 	while(loops == -1 || n-- > 0){
 		switch(rfork(RFPROC|RFFDG)){
 		case 0:
-			execl("/bin/games/midi", "midi", music->loc, nil);
+			dup(musicpipe[1], 1);
+			close(musicpipe[1]);
+			close(musicpipe[0]);
+			execl("/bin/games/midi", "midi", "-c", music->loc, nil);
 			sysfatal("exec: %r");
 			break;
 		default:
@@ -254,4 +261,25 @@
 	if(freesrc)
 		SDL_RWclose(src);
 	return m;
+}
+
+void
+musicproc(void *)
+{
+	int fd;
+	static char buf[8192];
+	int n;
+
+	fd = open("/dev/audio", OWRITE);
+	if(fd < 0)
+		sysfatal("musicproc: %r");
+
+	close(musicpipe[1]);
+	threadsetname("musicproc");
+	for(;;){
+		n = read(musicpipe[0], buf, sizeof buf);
+		if(n < 0)
+			threadexits(nil);
+		write(fd, buf, n);
+	}
 }