shithub: neindaw

Download patch

ref: 053d3b16498c601e2af3023f2011e9fa6bf84843
parent: 663bc67e12efaccd4109068b05f8e6d671b7bd23
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Aug 25 20:00:02 EDT 2021

free voices that faded out below "silence" threshold (chosen to be 0 when converted to s16)

--- a/fs.c
+++ b/fs.c
@@ -9,6 +9,7 @@
 
 #define MAX(a,b) ((a)>=(b)?(a):(b))
 #define MIN(a,b) ((a)<=(b)?(a):(b))
+#define Silence 0.00003f
 
 enum {
 	Maxobjs = 64,
@@ -150,7 +151,7 @@
 static int
 auxreaddsp(Aux *a, float *b, int total)
 {
-	int i, j, n, cross;
+	int i, j, n, cross, nzeromax;
 	Voice *v;
 	float *m;
 	State *s;
@@ -173,6 +174,8 @@
 
 	v = s->voice;
 	cross = -1;
+	a->numvoices = 0;
+	nzeromax = a->rate / 5; /* 1/5th of a second, assuming it's mono */
 	for (i = 0, n = total; i < nelem(s->voice) && n > 0; i++, v++) {
 		if (v->dsp == nil)
 			continue;
@@ -194,6 +197,7 @@
 			}
 			v->state = Vplaying;
 			v->samples = 0;
+			v->nzero = 0;
 			/* slippery floor */
 		case Vplaying:
 			if (n < 1)
@@ -206,7 +210,18 @@
 				b[j] += m[j];
 				if (s->crossvoice && cross < 0 && j > 0 && CROSS(m[j-1], m[j]))
 					cross = j;
+				if (m[j] >= -Silence && m[j] <= Silence)
+					v->nzero++;
+				else
+					v->nzero = 0;
 			}
+
+			if (v->nzero >= nzeromax) { /* been quite for a while? shut it */
+				cross = 0;
+				shutup(s, v);
+			} else {
+				a->numvoices++;
+			}
 		}
 	}
 
@@ -247,7 +262,7 @@
 		break;
 	case Xdspctl:
 		o = auxtype2obj(&a->type);
-		sprint(b, "numin\t%d\nnumout\t%d\trate\t%d\n", o->numin, o->numout, o->rate);
+		sprint(b, "numin\t%d\nnumout\t%d\nrate\t%d\nnumvoices\t%d\n", o->numin, o->numout, o->rate, o->numvoices);
 		readstr(r, b);
 		respond(r, nil);
 		break;
--- a/fs.h
+++ b/fs.h
@@ -38,6 +38,7 @@
 	int numin;
 	int numout;
 	int rate;
+	int numvoices;
 
 	struct UI *ui;
 	State *state;
@@ -69,6 +70,7 @@
 	uvlong samples;
 	Auxdsp *dsp;
 	int state;
+	int nzero;
 };
 
 struct State {