shithub: zuke

Download patch

ref: 4d09041fff14c843f08fcf027a7302164d78719c
parent: 6f58415a9781a09a52c40a6b1f1f3bd88cfaedc1
author: Sigrid Haflínudóttir <[email protected]>
date: Wed Dec 18 15:26:33 EST 2019

mkplist: use bio; prioritize composers and lead performers over any other id3v2-specific tags

--- a/mkplist.c
+++ b/mkplist.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 #include <tags.h>
 #include "plist.h"
 
@@ -11,10 +12,12 @@
 
 #define MAX(a, b) (a > b ? a : b)
 
-static int fd;
+static Biobuf *bf;
 static Meta *curr;
 static Meta *all;
 static int numall;
+static int firstiscomposer;
+static int keepfirstartist;
 
 static void
 freemeta(Meta *m)
@@ -49,9 +52,9 @@
 }
 
 static void
-cb(Tagctx *ctx, int t, const char *v, int offset, int size, Tagread f)
+cb(Tagctx *ctx, int t, const char *k, const char *v, int offset, int size, Tagread f)
 {
-	int i;
+	int i, iscomposer;
 
 	USED(ctx);
 
@@ -58,6 +61,19 @@
 	switch(t){
 	case Tartist:
 		if(curr->numartist < Maxartist){
+			iscomposer = strcmp(k, "TCM") == 0 || strcmp(k, "TCOM") == 0;
+			/* prefer lead performer/soloist, helps when TP2/TPE2 is the first one and is set to "VA" */
+			/* always put composer first, if available */
+			if(iscomposer || (!keepfirstartist && (strcmp(k, "TP1") == 0 || strcmp(k, "TPE1") == 0))) {
+				if(curr->numartist > 0)
+					curr->artist[curr->numartist] = curr->artist[curr->numartist-1];
+				curr->artist[0] = strdup(v);
+				curr->numartist++;
+				keepfirstartist = 1;
+				firstiscomposer = iscomposer;
+				return;
+			}
+
 			for(i = 0; i < curr->numartist; i++){
 				if(cistrcmp(curr->artist[i], v) == 0)
 					return;
@@ -96,7 +112,7 @@
 ctxread(Tagctx *ctx, void *buf, int cnt)
 {
 	USED(ctx);
-	return read(fd, buf, cnt);
+	return Bread(bf, buf, cnt);
 }
 
 static int
@@ -103,7 +119,7 @@
 ctxseek(Tagctx *ctx, int offset, int whence)
 {
 	USED(ctx);
-	return seek(fd, offset, whence);
+	return Bseek(bf, offset, whence);
 }
 
 static char buf[4096];
@@ -162,11 +178,12 @@
 
 			if((d->mode & DMDIR) == 0){
 				ctx.filename = path;
-				if((fd = open(path, OREAD)) < 0)
+				if((bf = Bopen(path, OREAD)) == nil)
 					fprint(2, "%s: %r\n", path);
 				else if((curr = newmeta()) == nil)
 					sysfatal("no memory");
 				else{
+					firstiscomposer = keepfirstartist = 0;
 					if(tagsget(&ctx) != 0)
 						fprint(2, "%s: no tags\n", path);
 					if(ctx.duration == 0)
@@ -181,7 +198,7 @@
 					curr->path = strdup(path);
 					curr->duration = ctx.duration;
 				}
-				close(fd);
+				Bterm(bf);
 			}else if(depth < Maxdepth){ /* recurse into the directory */
 				scan(dir, depth+1);
 				path = *dir;