shithub: pplay

Download patch

ref: 4979c45ce6223b28b1129fc79f3f007f6c3bb766
parent: 3af2b21e657b6013e7ed116831b591661c682813
author: qwx <qwx@sciops.net>
date: Mon Dec 11 06:46:12 EST 2023

properly load multiple tracks

assumed equal length, etc. for now

--- a/chunk.c
+++ b/chunk.c
@@ -224,7 +224,7 @@
 	d.cur = d.from = dp->from < d.totalsz ? dp->from : 0;
 	d.to = d.totalsz;
 	d.off = -1;
-	d.t = dp->t;
+	d.trk = dp->trk;
 	return d;
 }
 
@@ -484,7 +484,7 @@
 	Chunk *c;
 
 	if(d->cur >= d->totalsz){
-		werrstr("out of bounds");
+		werrstr("out of bounds: %zd >= %zd", d->cur, d->totalsz);
 		*have = 0;
 		return nil;
 	}
--- a/cmd.c
+++ b/cmd.c
@@ -333,13 +333,10 @@
 	if((fd = path != nil ? open(path, OREAD) : 0) < 0)
 		sysfatal("open: %r");
 	tracks = erealloc(tracks, (ntracks+1) * sizeof *tracks, ntracks * sizeof *tracks);
-	t = tracks + ntracks++;
+	t = tracks + ntracks;
+	t->trk = ntracks++;
 	if(loadfile(fd, t) == nil)
 		sysfatal("initcmd: %r");
 	close(fd);
-	// FIXME
-	t->Dot.t = t;
-	if(current != nil)
-		return;
 	current = &t->Dot;
 }
--- a/dat.h
+++ b/dat.h
@@ -2,10 +2,11 @@
 typedef struct Dot Dot;
 typedef struct Buf Buf;
 typedef struct Track Track;
-typedef struct Seg Seg;
 typedef struct Punkt Punkt;
 typedef struct Rekt Rekt;
 
+typedef intptr ssize;
+
 struct Punkt{
 	int x;
 	int y;
@@ -24,11 +25,6 @@
 	Chunksz = Sampsz * Rate,
 };
 #pragma incomplete Buf
-struct Seg{
-	Track *t;
-	usize from;
-	usize to;
-};
 struct Chunk{
 	Buf *b;
 	usize boff;
@@ -36,8 +32,11 @@
 	Chunk *left;
 	Chunk *right;
 };
+// FIXME: some stuff goes in track
 struct Dot{
-	Seg;
+	ssize trk;
+	usize from;
+	usize to;
 	usize cur;
 	usize off;
 	usize totalsz;
--- a/draw.c
+++ b/draw.c
@@ -237,21 +237,23 @@
 	usize k;
 	uchar *p, *e;
 	s16int s, *l, *r, *le;
+	Track *t;
 	vlong N;
 
+	t = tracks + d.trk;	// FIXME: just pass track?
 	N = (d.to - d.from) / (T * sampwidth);
-	if(d.t->len < 2*N){	/* min, max */
-		d.t->graph[0] = erealloc(d.t->graph[0],
-			2*N * sizeof *d.t->graph[0],
-			d.t->len * sizeof *d.t->graph[0]);
-		d.t->graph[1] = erealloc(d.t->graph[1],
-			2*N * sizeof *d.t->graph[1],
-			d.t->len * sizeof *d.t->graph[1]);
+	if(t->len < 2*N){	/* min, max */
+		t->graph[0] = erealloc(t->graph[0],
+			2*N * sizeof *t->graph[0],
+			t->len * sizeof *t->graph[0]);
+		t->graph[1] = erealloc(t->graph[1],
+			2*N * sizeof *t->graph[1],
+			t->len * sizeof *t->graph[1]);
 	}
-	d.t->len = 2*N;
-	l = d.t->graph[0];
-	r = d.t->graph[1];
-	le = l + d.t->len;
+	t->len = 2*N;
+	l = t->graph[0];
+	r = t->graph[1];
+	le = l + t->len;
 	while(l < le){
 		n = T * sampwidth;
 		lmin = lmax = rmin = rmax = 0;
@@ -310,6 +312,26 @@
 }
 
 void
+setcurrent(Point o)
+{
+	int dy;
+	Track *t;
+	Rectangle r;
+
+	dy = screen->r.max.y / ntracks;
+	r = Rpt(screen->r.min, Pt(screen->r.max.x, dy));
+	for(t=tracks; t<tracks+ntracks; t++){
+		if(ptinrect(o, r)){
+			current = &t->Dot;
+			return;
+		}
+		r.min.y += dy;
+		r.max.y += dy;
+	}
+	sysfatal("setcurrent: phase error");
+}
+
+void
 resizetracks(void)
 {
 	int dy;
@@ -349,6 +371,7 @@
 redraw(int all)
 {
 	usize span;
+	Track *t;
 	Dot d;
 
 	lockdisplay(display);
@@ -365,12 +388,13 @@
 	unlockdisplay(display);
 	if(paused)
 		refresh(Drawall);
-	/* FIXME: this overloading is stupid; just fork for each? have multiple
-	 * workers to begin with à la page? */
-	d = *current;
-	d.from = d.cur = views;
-	d.to = viewe;
-	nbsend(upyours, &d);
+	// FIXME: one worker per file?
+	for(t=tracks; t<tracks+ntracks; t++){
+		d = t->Dot;
+		d.from = d.cur = views;
+		d.to = viewe;
+		nbsend(upyours, &d);
+	}
 }
 
 void
--- a/fns.h
+++ b/fns.h
@@ -25,6 +25,7 @@
 void	redraw(int);
 void	initdrw(int);
 void	advance(usize);
+void	setcurrent(Punkt);
 Chunk*	p2c(usize, usize*, Dot*);
 int	setpos(usize);
 uchar*	getslice(Dot*, usize, usize*);
--- a/pplay.c
+++ b/pplay.c
@@ -55,6 +55,7 @@
 			fprint(2, "alt: %r\n");
 			break;
 		}
+		// FIXME: multitrack mixing
 		for(bp=buf, m=sizeof buf; bp<buf+sizeof buf; bp+=n, m-=n){
 			if((b = getslice(current, m, &n)) == nil || n <= 0){
 				fprint(2, "aproc: %r\n");
@@ -156,6 +157,9 @@
 		case 1:
 			if(eqpt(mo.xy, ZP))
 				mo = mc->Mouse;
+			// FIXME: multitrack drawing first
+			//if(mc->buttons != 0)
+			//	setcurrent(mc->xy);
 			switch(mc->buttons){
 			case 1: setjump(view2ss(mc->xy.x - screen->r.min.x)); break;
 			case 2: setloop(view2ss(mc->xy.x - screen->r.min.x)); break;