shithub: neindaw

ref: 03bbdd2116025795128eed80b55bfbb2fcdb71c2
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 = 32,
};

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);
}

static int
auxreaddsp(Aux *a, float *b, int n)
{
	State *s;
	float *m, level;
	int i, j, x;

	s = a->state;
	n = fs->dsp.read(s->voice[0], b, n);
	for (i = 0; i < n && b[i] == 0.0f; i++);
	s->silent = i != n;

	/* 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;
	}

	for (i = 1; i < s->nvoice; i++) {
		if ((x = fs->dsp.read(s->voice[i], m, n)) != n) /* well that's weird... why wouldn't it work? */
			fprint(2, "voice %d returned %d samples instead of %d\n", i, x, n);

		level = 0.0f;
		for (x--; x >= 0; x--) { /* FIXME this could be faster */
			level = MAX(level, m[x]);
			b[x] = b[x]+m[x] - b[x]*m[x];
		}
		if (level <= 0.0f) { /* this one is silent, delete it */
			fs->dsp.free(s->voice[i]);
			s->voice[i] = 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 n;
}

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;
	/* autovoice means every write needs to use (possibly) a new instance */
	if (a->ui->autovoice && !s->silent && 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 */
	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]));
}