shithub: riscv

Download patch

ref: 323a084780a3c0caf210e219b14725b4700175cc
parent: 3a10ba9fd216ed2f9d190e1f6473542717af2379
author: qwx <[email protected]>
date: Sun Jan 15 17:38:41 EST 2023

games/dmid: simplify and reuse input code for streaming

--- a/sys/src/games/dmid.c
+++ b/sys/src/games/dmid.c
@@ -80,6 +80,7 @@
 	vlong Δ;
 	vlong t;
 	int ev;
+	int ended;
 };
 Trk *tr;
 
@@ -153,7 +154,7 @@
 		Bread(ib, &v, 1);
 		return v;
 	}
-	if(x->p >= x->e)
+	if(x->p >= x->e || x->ended)
 		sysfatal("track overflow");
 	v = *x->p++;
 	dprint("%02ux", v);
@@ -389,8 +390,8 @@
 	}
 }
 
-void
-ev(Trk *x)
+int
+ev(Trk *x, vlong t)
 {
 	int e, n, m;
 	Chan *c;
@@ -397,19 +398,21 @@
 
 	dprint(" [%zd] ", x - tr);
 	e = get8(x);
-	if(!stream && (e & 0x80) == 0){
-		x->p--;
-		e = x->ev;
-		if((e & 0x80) == 0)
-			sysfatal("invalid event");
-	}else
-		x->ev = e;
+	if(x != nil){
+		if((e & 0x80) == 0){
+			x->p--;
+			e = x->ev;
+			if((e & 0x80) == 0)
+				sysfatal("invalid event");
+		}else
+			x->ev = e;
+	}
 	c = chan + (e & 15);
 	dprint("(%02ux) ", e);
 	n = get8(x);
 	switch(e >> 4){
 	case 0x8: noteoff(c, n, get8(x)); break;
-	case 0x9: noteon(c, n, get8(x), x->t); break;
+	case 0x9: noteon(c, n, get8(x), t); break;
 	case 0xb:
 		m = get8(x);
 		switch(n){
@@ -433,7 +436,7 @@
 		}
 		m = get8(x);
 		switch(n){
-		case 0x2f: x->p = x->e; break;
+		case 0x2f: dprint("it\'s over.\n"); return -1;
 		case 0x51: tempo = get16(x) << 8; tempo |= get8(x); break;
 		default: skip(x, m);
 		}
@@ -443,6 +446,7 @@
 	default: sysfatal("invalid event %#ux\n", e >> 4);
 	}
 	dprint("\n");
+	return 0;
 }
 
 void
@@ -546,10 +550,9 @@
 	int n, end, debug;
 	char *i;
 	double f;
-	uchar u[4];
 	Chan *c;
 	Opl *o;
-	Trk xs, *x;
+	Trk *x;
 
 	i = "/mnt/wad/genmidi";
 	debug = 0;
@@ -581,12 +584,9 @@
 		if(proccreate(tproc, nil, mainstacksize) < 0)
 			sysfatal("proccreate: %r");
 		for(;;){
-			if((n = Bread(ib, u, sizeof u)) != sizeof u)
+			getvar(nil);
+			if(ev(nil, 0) < 0)
 				break;
-			xs.p = u;
-			xs.e = u + n;
-			getvar(&xs);
-			ev(&xs);
 		}
 		threadexitsall(n < 0 ? "read: %r" : nil);
 	}
@@ -593,15 +593,16 @@
 	for(end=0; !end;){
 		end = 1;
 		for(x=tr; x<tr+ntrk; x++){
-			if(x->p >= x->e)
+			if(x->ended)
 				continue;
 			end = 0;
 			x->Δ--;
 			x->t += tc(1);
 			while(x->Δ <= 0){
-				ev(x);
-				if(x->p >= x->e)
+				if(x->ended = ev(x, x->t)){
+					x->p = x->e;
 					break;
+				}
 				x->Δ = getvar(x);
 			}
 		}