ref: 233b15c62e1c272d7017811b0287d590a34aba97
parent: d29b1fa9119ef89090971376b6c4b5378813efd7
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed May 13 08:40:19 EDT 2020
piper: work with stereo instances
--- a/piper/piper.c
+++ b/piper/piper.c
@@ -11,6 +11,7 @@
void *aux;
int data;
int id;
+ int numout;
};
struct Group {
@@ -46,7 +47,8 @@
{
Inst *inst;
void *aux;
- char *path, tmp[8];
+ char *path, *s, tmp[8];
+ Biobuf *b;
int i, n, f;
for (i = 0; i < g->numinst; i++) {
@@ -86,11 +88,28 @@
if ((g->inst = realloc(g->inst, sizeof(Inst)*(g->numinst+1))) == nil)
sysfatal("memory");
inst = &g->inst[g->numinst];
+ memset(inst, 0, sizeof(*inst));
inst->data = f;
inst->id = i;
inst->aux = aux;
g->numinst++;
+ path = smprint("%s/%d/ctl", g->path, i);
+ if ((b = Bopen(path, OREAD)) == nil)
+ sysfatal("%r");
+ free(path);
+ while ((s = Brdline(b, '\n')) != nil) {
+ if (strncmp(s, "numout\t", 7) == 0) {
+ s[Blinelen(b)-1] = 0;
+ inst->numout = atoi(s+7);
+ break;
+ }
+ }
+ Bterm(b);
+
+ if (inst->numout != 1 && inst->numout != 2)
+ sysfatal("%s/%d: %d channels", g->path, i, inst->numout);
+
if (i == id)
return inst;
@@ -188,29 +207,41 @@
int i, j, n;
s16int *pcm;
float *out, *x, f;
+ Inst *inst;
+ Group *g;
- pcm = malloc(2*Bufframes*sizeof(*pcm));
- out = malloc(Bufframes*sizeof(*out));
- x = malloc(Bufframes*sizeof(*x));
+ pcm = malloc(2*Bufframes*sizeof(*pcm)); /* stereo */
+ out = malloc(2*Bufframes*sizeof(*out));
+ x = malloc(2*Bufframes*sizeof(*x));
+
for (;;) {
- memset(out, 0, Bufframes*sizeof(*out));
+ memset(out, 0, 2*Bufframes*sizeof(*out));
qlock(&grouplock);
for (i = 0; i < numgroups; i++) {
- for (j = 0; j < groups[i].numinst; j++) {
- if (readn(groups[i].inst[j].data, x, Bufframes*sizeof(*x)) < 1)
+ g = &groups[i];
+ for (j = 0; j < g->numinst; j++) {
+ inst = &g->inst[j];
+ if (readn(inst->data, x, inst->numout*Bufframes*sizeof(*x)) < 1)
break;
- for (n = 0; n < Bufframes; n++)
- out[n] += x[n];
+ if (inst->numout == 1) {
+ for (n = 0; n < Bufframes; n++) {
+ out[n*2+0] += x[n];
+ out[n*2+1] += x[n];
+ }
+ } else {
+ for (n = 0; n < Bufframes; n++)
+ out[n] += x[n];
+ }
}
}
qunlock(&grouplock);
- for (n = 0; n < Bufframes; n++) {
+ for (n = 0; n < 2*Bufframes; n++) {
f = out[n] * 8192.0 * vol;
if (f > 32767.0)
f = 32767.0;
else if (f < -32767.0)
f = -32767.0;
- pcm[n*2+0] = pcm[n*2+1] = f;
+ pcm[n] = f;
}
if (write(audio, pcm, 2*Bufframes*sizeof(*pcm)) < 0)
break;