shithub: pplay

Download patch

ref: 3af2b21e657b6013e7ed116831b591661c682813
parent: 7249d5febf377411ff6c1cf47ffaaa63954ca149
author: qwx <[email protected]>
date: Sun Dec 10 19:37:13 EST 2023

changes for actually supporting multitrack

--- a/cmd.c
+++ b/cmd.c
@@ -4,8 +4,8 @@
 #include "dat.h"
 #include "fns.h"
 
-usize ndots, ntracks;
-Dot *current, *dots;
+usize ntracks;
+Dot *current;
 Track *tracks;
 
 static int epfd[2];
@@ -241,6 +241,7 @@
 void
 quit(void)
 {
+	// FIXME: no, have yield/sleep/...
 	threadsetgrp(1);
 	threadkillgrp(0);
 	threadintgrp(0);
@@ -291,8 +292,8 @@
 	return x;
 }
 
-int
-advance(Dot *d, usize n)
+static void
+advanceone(Dot *d, usize n)
 {
 	usize m;
 
@@ -304,9 +305,17 @@
 		if(d->cur == d->to)
 			d->cur = d->from;
 	}
-	return 0;
 }
 
+void
+advance(usize n)
+{
+	Track *t;
+
+	for(t=tracks; t<tracks+ntracks; t++)
+		advanceone(t, n);
+}
+
 static void
 catch(void *, char *msg)
 {
@@ -315,18 +324,22 @@
 	noted(NDFLT);
 }
 
-int
-initcmd(int fd)
+void
+addtrack(char *path)
 {
-	Dot d;
+	int fd;
+	Track *t;
 
-	tracks = emalloc(++ntracks * sizeof *tracks);
-	d.t = tracks;
-	if(loadfile(fd, &d) == nil)
+	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++;
+	if(loadfile(fd, t) == nil)
 		sysfatal("initcmd: %r");
-	dots = emalloc(++ndots * sizeof *dots);
-	dots[0] = d;
-	current = dots;
-	*current = d;
-	return 0;
+	close(fd);
+	// FIXME
+	t->Dot.t = t;
+	if(current != nil)
+		return;
+	current = &t->Dot;
 }
--- a/dat.h
+++ b/dat.h
@@ -3,7 +3,18 @@
 typedef struct Buf Buf;
 typedef struct Track Track;
 typedef struct Seg Seg;
+typedef struct Punkt Punkt;
+typedef struct Rekt Rekt;
 
+struct Punkt{
+	int x;
+	int y;
+};
+struct Rekt{
+	Punkt min;
+	Punkt max;
+};
+
 enum{
 	Rate = 44100,
 	WriteRate = 25,
@@ -13,6 +24,11 @@
 	Chunksz = Sampsz * Rate,
 };
 #pragma incomplete Buf
+struct Seg{
+	Track *t;
+	usize from;
+	usize to;
+};
 struct Chunk{
 	Buf *b;
 	usize boff;
@@ -20,15 +36,6 @@
 	Chunk *left;
 	Chunk *right;
 };
-struct Track{
-	vlong len;
-	s16int *graph[2];
-};
-struct Seg{
-	Track *t;
-	usize from;
-	usize to;
-};
 struct Dot{
 	Seg;
 	usize cur;
@@ -36,8 +43,15 @@
 	usize totalsz;
 	Chunk *norris;
 };
-extern Dot *dots, *current;
-extern usize ndots, ntracks;
+struct Track{
+	Dot;
+	Rekt;
+	// FIXME: both for samples:
+	vlong len;
+	s16int *graph[2];
+};
+extern Dot *current;
+extern usize ntracks;
 extern Track *tracks;
 
 extern QLock lsync;
--- a/draw.c
+++ b/draw.c
@@ -5,6 +5,10 @@
 #include "dat.h"
 #include "fns.h"
 
+/* urgh */
+typedef Point Punkt;
+typedef Rectangle Rekt;
+
 QLock lsync;
 int debugdraw;
 
@@ -305,6 +309,22 @@
 	}
 }
 
+void
+resizetracks(void)
+{
+	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++){
+		t->Rectangle = r;
+		r.min.y += dy;
+		r.max.y += dy;
+	}
+}
+
 static void
 resetdraw(void)
 {
@@ -322,6 +342,7 @@
 	bgscalyl = (viewr.max.y - font->height) / (stereo ? 4 : 2);
 	bgscalyr = viewr.max.y - bgscalyl;
 	bgscalf = 32767. / bgscalyl;
+	resizetracks();
 }
 
 void
--- a/fns.h
+++ b/fns.h
@@ -10,7 +10,8 @@
 Chunk*	loadfile(int, Dot*);
 void	quit(void);
 int	cmd(char*);
-int	initcmd(int);
+void	addtrack(char*);
+void	resizetracks(void);
 void	refresh(int);
 void	setzoom(int, int);
 int	zoominto(vlong, vlong);
@@ -23,7 +24,7 @@
 vlong	view2ss(int);
 void	redraw(int);
 void	initdrw(int);
-int	advance(Dot*, usize);
+void	advance(usize);
 Chunk*	p2c(usize, usize*, Dot*);
 int	setpos(usize);
 uchar*	getslice(Dot*, usize, usize*);
--- a/pplay.c
+++ b/pplay.c
@@ -62,7 +62,7 @@
 				goto again;
 			}
 			memcpy(bp, b, n);
-			advance(current, n);
+			advance(n);
 			refresh(Drawcur);
 		}
 		if(write(afd, buf, sizeof buf) != sizeof buf){
@@ -105,7 +105,6 @@
 void
 threadmain(int argc, char **argv)
 {
-	int fd;
 	char *p;
 	Mouse mo;
 	Rune r;
@@ -120,11 +119,11 @@
 	fmtinstall(L'Δ', Δfmt);
 	fmtinstall(L'χ', χfmt);
 	fmtinstall(L'τ', τfmt);
-	if((fd = *argv != nil ? open(*argv, OREAD) : 0) < 0)
-		sysfatal("open: %r");
-	if(initcmd(fd) < 0)
-		sysfatal("init: %r");
-	close(fd);
+	if(*argv != nil)
+		while(*argv != nil)
+			addtrack(*argv++);
+	else
+		addtrack(nil);
 	initdrw(notriob);
 	if((kc = initkeyboard(nil)) == nil)
 		sysfatal("initkeyboard: %r");