From 82c7251dc3ff57ae97c0ce24b72e2e2ae5f6945b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigrid=20Solveig=20Hafl=C3=ADnud=C3=B3ttir?= Date: Sat, 21 Aug 2021 22:51:11 +0000 Subject: [PATCH] mixfs: add reading (audio loopback) --- sys/src/cmd/audio/mixfs/mixfs.c | 59 +++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/sys/src/cmd/audio/mixfs/mixfs.c b/sys/src/cmd/audio/mixfs/mixfs.c index 15a082e24..de6f4a557 100644 --- a/sys/src/cmd/audio/mixfs/mixfs.c +++ b/sys/src/cmd/audio/mixfs/mixfs.c @@ -16,6 +16,7 @@ struct Stream { int used; int run; + int rd; ulong wp; QLock; Rendez; @@ -23,6 +24,7 @@ struct Stream ulong mixrp; int mixbuf[NBUF][NCHAN]; +int lbbuf[NBUF][NCHAN]; Lock mixlock; Stream streams[16]; @@ -61,6 +63,7 @@ fsopen(Req *r) s->used = 1; qunlock(s); + s->rd = (r->ifcall.mode&OWRITE) == 0; r->fid->aux = s; respond(r, nil); return; @@ -144,6 +147,7 @@ audioproc(void *) for(i=0; i> 8; @@ -154,6 +158,56 @@ audioproc(void *) } } +void +fsread(Req *r) +{ + Srv *srv; + int i, j, n, m, v; + Stream *s; + uchar *p; + + p = (uchar*)r->ofcall.data; + n = r->ifcall.count; + n &= ~(NCHAN*2 - 1); + r->ofcall.count = n; + n /= (NCHAN*2); + + srv = r->srv; + srvrelease(srv); + s = r->fid->aux; + qlock(s); + while(n > 0){ + if(s->run == 0){ + s->wp = mixrp; + s->run = 1; + } + m = NBUF-1 - (long)(s->wp - mixrp); + if(m <= 0){ + s->run = 1; + rsleep(s); + continue; + } + if(m > n) + m = n; + + lock(&mixlock); + for(i=0; iwp % NBUF][j]; + *p++ = v & 0xFF; + *p++ = v >> 8; + } + s->wp++; + } + unlock(&mixlock); + + n -= m; + } + qunlock(s); + respond(r, nil); + srvacquire(srv); +} + void fswrite(Req *r) { @@ -234,7 +288,7 @@ fsstart(Srv *) Stream *s; for(s=streams; s < streams+nelem(streams); s++){ - s->used = s->run = 0; + s->used = s->run = s->rd = 0; s->Rendez.l = &s->QLock; } proccreate(audioproc, nil, 16*1024); @@ -248,6 +302,7 @@ fsend(Srv *) Srv fs = { .open= fsopen, + .read= fsread, .write= fswrite, .stat= fsstat, .destroyfid= fsclunk, @@ -286,7 +341,7 @@ threadmain(int argc, char **argv) usage(); fs.tree = alloctree(nil, nil, DMDIR|0777, nil); - createfile(fs.tree->root, "audio", nil, 0222, nil); + createfile(fs.tree->root, "audio", nil, 0666, nil); threadpostmountsrv(&fs, srv, mtpt, MREPL); mtpt = smprint("%s/audio", mtpt);