shithub: riscv

Download patch

ref: 82c7251dc3ff57ae97c0ce24b72e2e2ae5f6945b
parent: 8c796bf8b9c10a0845717c47af908e47dfe14482
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Sat Aug 21 18:51:11 EDT 2021

mixfs: add reading (audio loopback)

--- a/sys/src/cmd/audio/mixfs/mixfs.c
+++ b/sys/src/cmd/audio/mixfs/mixfs.c
@@ -16,6 +16,7 @@
 {
 	int	used;
 	int	run;
+	int	rd;
 	ulong	wp;
 	QLock;
 	Rendez;
@@ -23,6 +24,7 @@
 
 ulong	mixrp;
 int	mixbuf[NBUF][NCHAN];
+int	lbbuf[NBUF][NCHAN];
 Lock	mixlock;
 Stream	streams[16];
 
@@ -61,6 +63,7 @@
 			s->used = 1;
 			qunlock(s);
 
+			s->rd = (r->ifcall.mode&OWRITE) == 0;
 			r->fid->aux = s;
 			respond(r, nil);
 			return;
@@ -144,6 +147,7 @@
 		for(i=0; i<m; i++){
 			for(j=0; j<NCHAN; j++){
 				v = clip16(mixbuf[mixrp % NBUF][j]);
+				lbbuf[mixrp % NBUF][j] = v;
 				mixbuf[mixrp % NBUF][j] = 0;
 				*p++ = v & 0xFF;
 				*p++ = v >> 8;
@@ -155,6 +159,56 @@
 }
 
 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; i<m; i++){
+			for(j=0; j<NCHAN; j++){
+				v = lbbuf[s->wp % 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)
 {
 	Srv *srv;
@@ -234,7 +288,7 @@
 	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 @@
 
 Srv fs = {
 	.open=		fsopen,
+	.read=		fsread,
 	.write=		fswrite,
 	.stat=		fsstat,
 	.destroyfid=	fsclunk,
@@ -286,7 +341,7 @@
 		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);