shithub: neindaw

ref: fd3f0be9dc1524bf79cb6f7cb6938aec7e934ffd
dir: /piper/piper.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <bio.h>
#include "piper.h"

typedef struct Group Group;

struct Group {
	Synth *synth;
	void **aux;
	int numaux;
};

static Group *groups;
static int numgroups;

static Synth *synths[] = {
	&ay_3_8910,
	&kick_drum,
};

static void
usage(void)
{
	print("usage: %s DIR...\n", argv0);
	threadexitsall("usage");
}

void
threadmain(int argc, char **argv)
{
	char *s;
	Synth *synth;
	Biobuf *b;
	int i, j;

	ARGBEGIN{
	default:
		usage();
	}ARGEND;

	if (argc < 1)
		usage();

	quotefmtinstall();

	/* go through all groups */
	for (i = 0; i < argc; i++) {
		/* search for specific synth handler by its name */
		s = smprint("%s/metadata", argv[i]);
		if ((b = Bopen(s, OREAD)) == nil)
			sysfatal("%r");
		free(s);
		synth = nil;
		while ((s = Brdline(b, '\n')) != nil) {
			if (strncmp(s, "name\t", 5) == 0) {
				s[Blinelen(b)-1] = 0;
				for (j = 0; j < nelem(synths) && strcmp(synths[j]->name, s+5) != 0; j++);
				if (j >= nelem(synths))
					sysfatal("unknown synth %q\n", s+5);
				if ((groups = realloc(groups, sizeof(Group)*(numgroups+1))) == nil)
					sysfatal("memory");
				synth = synths[j];
				memset(&groups[numgroups], 0, sizeof(Group));
				groups[numgroups].synth = synth;
				break;
			}
		}
		Bterm(b);
		if (synth == nil)
			sysfatal("no name set in %s/metadata", argv[i]);
	}

	threadexitsall(nil);
}