shithub: neindaw

ref: d92a4578490a554a34b3e81dd16129c6c96f3dcc
dir: /fs.c/

View raw version
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "common.h"
#include "ui.h"
#include "fs.h"

#define MAX(a,b) ((a)>=(b)?(a):(b))
#define MIN(a,b) ((a)<=(b)?(a):(b))

enum {
	Maxobjs = 64,
	Maxvoice = 16,
};

static Aux *objs[Maxobjs];

static Aux rootaux[] = {
	[Xctl] = {.type = Xctl},
	[Xmetadata] = {.type = Xmetadata},
	[Xclone] = {.type = Xclone},
};

extern File *uif;
extern State *uis;

static Fs *fs;

static Aux *
newobj(char *name)
{
	File *f;
	Aux *o;
	int i, mode;

	for (i = 0, o = nil; o == nil && i < nelem(objs); i++) {
		if (objs[i] == nil){
			o = objs[i] = calloc(1, sizeof(*o));
			break;
		}
	}
	if (o == nil)
		return nil;

	o->id = i;
	o->type = Xdsp;
	o->ctl = Xdspctl;
	o->data = Xdspdata;

	sprint(name, "%d", o->id);
	if ((f = createfile(fs->srv.tree->root, name, nil, DMDIR|0775, o)) == nil)
		return nil;
	closefile(createfile(f, "ctl", nil, 0664, &o->ctl));
	mode = 0;
	if (fs->dsp.read != nil)
		mode |= 0444;
	if (fs->dsp.write != nil)
		mode |= 0222;
	closefile(createfile(f, "data", nil, mode, &o->data));

	uif = f;
	o->state = uis = calloc(1, sizeof(State));
	o->state->voice = calloc(1, sizeof(Auxdsp*));
	o->state->voice[o->state->nvoice++] = fs->dsp.new(&o->numin, &o->numout);
	o->state->silent = 1;
	closefile(f);

	return o;
}

static void
freeobj(Aux *o)
{
	int i;

	if (o == nil)
		return;

	if (o->type == Xdsp) {
		objs[o->id] = nil;
		for (i = 0; i < o->state->nvoice; i++)
			fs->dsp.free(o->state->voice[i]);
		free(o->state->voice);
		free(o->state);
	}

	free(o);
}

static void *
auxtype2obj(int *type)
{
	switch (*type) {
	case Xdspctl:
	case Xuictl:
		return (uchar*)type - offsetof(Aux, ctl);
	case Xdspdata:
		return (uchar*)type - offsetof(Aux, data);
	case Xuimeta:
		return (uchar*)type - offsetof(Aux, metadata);
	default:
		sysfatal("trying to get aux out of type %d", *type);
	}

	return nil;
}

static void
fsopen(Req *r)
{
	respond(r, nil);
}

#define CROSS(a,b) (a <= 0.0f && a < b)

static int
auxreaddsp(Aux *a, float *b, int n)
{
	State *s;
	float *m, level, least;
	int i, j, cross, silent, total, quiet;

	s = a->state;
	cross = 0;
	total = n;
	if (!s->silent) {
		silent = 1;
		n = fs->dsp.read(s->voice[0], b, n);
		total = n;
		for (i = 0; i < n; i++) {
			if (b[i] != 0.0f) {
				silent = 0;
				/* not silent, don't care for crossing? leave early */
				if (!s->crossvoice)
					break;
			}
			/* find the cross point */
			if (s->crossvoice && i > 0 && cross < 1 && CROSS(b[i-1], b[i])) {
				cross = i;
				break;
			}
		}
		s->silent = silent;
	}

	/* simple case: error, no data, or just one voice */
	if (n < 1 || s->nvoice < 2)
		return n;

	/* otherwise need to mix all the voices together */
	m = s->mixer;
	if (s->mixersz < n) {
		s->mixersz = n;
		if ((m = realloc(m, n*sizeof(float))) == nil) {
			fprint(2, "no memory for mixer, giving up\n");
			return n;
		}
		s->mixer = m;
	}

	if (s->silent)
		memset(b, 0, sizeof(*b)*n);
	else if (s->crossvoice && cross < 1) /* no cross point? wait for it */
		return total;

	/* multiple voices and crossvoicing with a cross point known? shut up the main voice */
	if (cross > 0){
		s->silent = 1;
		memset(b+cross, 0, sizeof(*b)*(n-cross));
		/* start mixing into the crosspoint */
		b += cross;
		m += cross;
		n -= cross;
	}

	quiet = -1;
	least = 999.0;
	for (i = 1; i < s->nvoice && n > 1; i++) {
		if (s->crossvoice && i > 1 && cross < 1) /* no cross point? wait for it */
			break;

		n = fs->dsp.read(s->voice[i], m, n);
		level = 0.0f;
		cross = 0;
		for (j = 0; j < n; j++) { /* FIXME this could be faster */
			if (s->crossvoice && i+1 < s->nvoice && cross < 1 && j > 0) {
				/* crossvoicing AND we have at least one more on top, see if we can shut *this* one up */
				if (CROSS(m[j-1], m[j])) {
					cross = j;
					level = 0.0f; /* shut up forever */
					b += j;
					m += j;
					n -= j;
					break;
				}
			}
			level = MAX(level, m[j]);
			b[j] += m[j];
		}

		if (level <= 0.0f) { /* this one is silent, delete it */
			fs->dsp.free(s->voice[i]);
			s->voice[i] = nil;
			/* freed one, don't try to free more */
			least = -999.0f;
			quiet = -1;
		} else if (level < least) {
			quiet = i;
			least = level;
		}
	}

	/* free up a voice if there are too many */
	if (s->nvoice >= Maxvoice && quiet >= 0) {
		fs->dsp.free(s->voice[quiet]);
		s->voice[quiet] = nil;
	}

	/* relocate the voices */
	for (i = j = 1; i < s->nvoice; i++) {
		if (s->voice[i] != nil)
			s->voice[j++] = s->voice[i];
	}
	s->nvoice = j;

	return total;
}

static void
auxreset(Aux *a)
{
	int i;

	for (i = 0; i < a->state->nvoice; i++)
		fs->dsp.reset(a->state->voice[i]);
}

static void
fsread(Req *r)
{
	Aux *a, *o;
	char b[256];

	a = r->fid->file->aux;
	switch (a->type) {
	case Xctl:
		respond(r, nil);
		break;
	case Xdspctl:
		o = auxtype2obj(&a->type);
		sprint(b, "numin\t%d\nnumout\t%d\n", o->numin, o->numout);
		readstr(r, b);
		respond(r, nil);
		break;
	case Xmetadata:
		readstr(r, fs->metadata);
		respond(r, nil);
		break;
	case Xclone:
		if (r->ifcall.offset == 0) {
			if (newobj(b) != nil) {
				readstr(r, b);
			} else {
				snprint(b, sizeof(b), "no free objects: %r");
				respond(r, b);
				break;
			}
		}
		respond(r, nil);
		break;
	case Xuictl:
	case Xuimeta:
		o = auxtype2obj(&a->type);
		if (o->ui->readstr != nil)
			readstr(r, o->ui->readstr(o->ui, a->type, b, sizeof(b)));
		respond(r, nil);
		break;
	case Xdspdata:
		o = auxtype2obj(&a->type);
		r->ofcall.count = auxreaddsp(o, (float*)r->ofcall.data, r->ifcall.count/sizeof(float))*sizeof(float);
		respond(r, nil);
		break;
	default:
		respond(r, "not implemented");
		break;
	}
}

static int
auxwrite(Aux *a, int type, char *data)
{
	State *s;
	Auxdsp *clone;
	void *so, *sc;
	u8int *tmp;
	int r, sz;

	if (a->ui->writestr == nil) {
		werrstr("not implemented");
		return -1;
	}

	s = a->state;
	s->crossvoice |= a->ui->crossvoice;

	/* autovoice means every write needs to use (possibly) a new instance */
	if (a->ui->autovoice && (!s->silent || s->nvoice > 1) && fs->dsp.clone != nil && fs->dsp.state != nil) {
		/* now do the impossible */
		so = fs->dsp.state(s->voice[0], &sz);
		tmp = malloc(sz);
		memmove(tmp, so, sz); /* save the original state */
		/* write to the original and check if a new voice has to be created */
		if ((r = a->ui->writestr(a->ui, type, data)) < 0 || *a->ui->zone == 0.0f) {
			free(tmp);
			return r;
		}
		clone = fs->dsp.clone(s->voice[0]); /* clone the original */
		sc = fs->dsp.state(clone, &sz);
		memmove(sc, so, sz); /* copy the changed state to the clone */
		memmove(so, tmp, sz); /* revert the original state */
		free(tmp);
		/* now we have the original dsp intact, with a cloned dsp actually having the changed state */

		s->voice = realloc(s->voice, (s->nvoice+1)*sizeof(*s->voice));
		s->voice[s->nvoice++] = clone;
		return r;
	}

	/* in any other case, just write to the original */
	s->silent = 0;
	return a->ui->writestr(a->ui, type, data);
}

static void
fswrite(Req *r)
{
	Aux *a, *o;
	char b[256];

	if (r->ifcall.count >= sizeof(b)) {
		respond(r, "can't fit into buffer");
		return;
	}

	memmove(b, r->ifcall.data, r->ifcall.count);
	b[r->ifcall.count] = '\0';
	r->ofcall.count = r->ifcall.count;

	a = r->fid->file->aux;
	switch (a->type) {
	case Xuictl:
		o = auxtype2obj(&a->type);
		if (auxwrite(o, a->type, b) >= 0)
			respond(r, nil);
		else
			responderror(r);
		break;
	case Xdspctl: /* FIXME changing sampling rate */
		o = auxtype2obj(&a->type);
		if (strncmp(b, "reset", 5) == 0) /* FIXME ui needs to be reset as well */
			auxreset(o);
		respond(r, nil);
		break;
	case Xmetadata: /* FIXME should be possible to add new key/value */
	default:
		respond(r, "not implemented");
		break;
	}
}

static void
fsdestroyfile(File *f)
{
	Aux *a;

	if ((a = f->aux) == nil)
		return;
	switch (a->type) {
	case Xdsp:
	case Xui:
		freeobj(a);
		f->aux = nil;
		break;
	}
}

void
fsinit(void *fs_)
{
	fs = fs_;
	fs->srv.open = fsopen;
	fs->srv.read = fsread;
	fs->srv.write = fswrite;
	fs->srv.tree = alloctree(nil, nil, DMDIR|0775, fsdestroyfile);
	closefile(createfile(fs->srv.tree->root, "ctl", nil, 0666, &rootaux[Xctl]));
	closefile(createfile(fs->srv.tree->root, "metadata", nil, 0444, &rootaux[Xmetadata]));
	closefile(createfile(fs->srv.tree->root, "clone", nil, 0444, &rootaux[Xclone]));
}