fix web tracking optional channels and improve performance

This commit is contained in:
Stefano D'Angelo 2024-02-07 17:22:29 +01:00
parent 46d089e7a2
commit 173fa632b4
3 changed files with 71 additions and 37 deletions

View File

@ -19,6 +19,8 @@ LDFLAGS_ALL = \
-Wl,--export=processor_new \ -Wl,--export=processor_new \
-Wl,--export=processor_free \ -Wl,--export=processor_free \
-Wl,--export=processor_get_x_buf \ -Wl,--export=processor_get_x_buf \
-Wl,--export=processor_get_x \
-Wl,--export=processor_get_zero_buf \
-Wl,--export=processor_get_y_buf \ -Wl,--export=processor_get_y_buf \
-Wl,--export=processor_get_out_params \ -Wl,--export=processor_get_out_params \
-Wl,--export=processor_process \ -Wl,--export=processor_process \

View File

@ -17,6 +17,7 @@ typedef struct {
#if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0 #if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0
float x_buf[DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N * 128]; float x_buf[DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N * 128];
const float * x[DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N]; const float * x[DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N];
float zero_buf[128];
#endif #endif
#if DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N > 0 #if DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N > 0
float y_buf[DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N * 128]; float y_buf[DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N * 128];
@ -55,8 +56,7 @@ instance * processor_new(float sample_rate) {
plugin_reset(&i->p); plugin_reset(&i->p);
#if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0 #if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0
for (size_t j = 0; j < DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N; j++) memset(i->zero_buf, 0, 128 * sizeof(float));
i->x[j] = i->x_buf + 128 * j;
#endif #endif
#if DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N > 0 #if DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N > 0
for (size_t j = 0; j < DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N; j++) for (size_t j = 0; j < DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N; j++)
@ -82,8 +82,26 @@ float * processor_get_x_buf(instance * i) {
#endif #endif
} }
float * processor_get_y_buf(instance * i) { const float ** processor_get_x(instance * i) {
#if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0 #if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0
return i->x;
#else
(void)i;
return NULL;
#endif
}
float * processor_get_zero_buf(instance * i) {
#if DATA_PRODUCT_AUDIO_INPUT_CHANNELS_N > 0
return i->zero_buf;
#else
(void)i;
return NULL;
#endif
}
float * processor_get_y_buf(instance * i) {
#if DATA_PRODUCT_AUDIO_OUTPUT_CHANNELS_N > 0
return i->y_buf; return i->y_buf;
#else #else
(void)i; (void)i;

View File

@ -5,6 +5,12 @@
var buses = {{=JSON.stringify(it.product.buses, null, 2)}}; var buses = {{=JSON.stringify(it.product.buses, null, 2)}};
var parameters = {{=JSON.stringify(it.product.parameters, null, 2)}}; var parameters = {{=JSON.stringify(it.product.parameters, null, 2)}};
var busesIn = buses.filter(x => x.type == "audio" && x.direction == "input");
var busesOut = buses.filter(x => x.type == "audio" && x.direction == "output");
var nChansIn = busesIn.reduce((a, x) => a + (x.channels == "mono" ? 1 : 2), 0);
var nChansOut = busesOut.reduce((a, x) => a + (x.channels == "mono" ? 1 : 2), 0);
class Processor extends AudioWorkletProcessor { class Processor extends AudioWorkletProcessor {
constructor(options) { constructor(options) {
super(); super();
@ -17,26 +23,14 @@ class Processor extends AudioWorkletProcessor {
if (!this.instance) if (!this.instance)
throw "Could not instantiate processor module"; throw "Could not instantiate processor module";
function getBuffers(p, output) { if (nChansIn > 0) {
var ret = []; this.xBufP = this.module.processor_get_x_buf(this.instance);
for (var i = 0; i < buses.length; i++) { this.zeroBufP = this.module.processor_get_zero_buf(this.instance);
if (buses[i].type != "audio" || (output && buses[i].direction == "input") || (!output && buses[i].direction == "output")) this.xBuf = new Float32Array(this.module.memory.buffer, this.xBufP, 128 * nChansIn);
continue; this.x = new Uint32Array(this.module.memory.buffer, this.module.processor_get_x(this.instance), nChansIn);
if (buses[i].channels == "mono") {
ret.push([ new Float32Array(this.module.memory.buffer, p, 128) ]);
p += 128 * 4;
} else {
ret.push([
new Float32Array(this.module.memory.buffer, p, 128),
new Float32Array(this.module.memory.buffer, p + 128 * 4, 128)
]);
p += 2 * 128 * 4;
} }
} if (nChansOut > 0)
return ret; this.yBuf = new Float32Array(this.module.memory.buffer, this.module.processor_get_y_buf(this.instance), 128 * nChansOut);
}
this.x = getBuffers.call(this, this.module.processor_get_x_buf(this.instance), false);
this.y = getBuffers.call(this, this.module.processor_get_y_buf(this.instance), true);
this.parametersIn = []; this.parametersIn = [];
for (var i = 0; i < parameters.length; i++) for (var i = 0; i < parameters.length; i++)
@ -84,26 +78,46 @@ class Processor extends AudioWorkletProcessor {
while (i < n) { while (i < n) {
var s = Math.min(n - i, 128); var s = Math.min(n - i, 128);
for (var j = 0; j < this.x.length; j++) { var j = 0;
var input = inputs[j]; var o = 0;
if (!input.length) { for (var k = 0; k < busesIn.length; k++) {
for (var k = 0; k < this.x[j].length; k++) var bus = busesIn[k];
this.x[j][k].fill(0); var input = inputs[k];
} else {
for (var k = 0; k < this.x[j].length; k++) if (!input[0])
if (k < input.length) this.x[j] = bus.optional ? 0 : this.zeroBufP;
this.x[j][k].set(input[k].subarray(i, s)); else {
else this.xBuf.set(input[0].subarray(i, i + s), o);
this.x[j][k].fill(0); this.x[j] = this.xBufP + 4 * o;
o += 128;
}
j++;
if (bus.channels == "stereo") {
if (!input[0])
this.x[j] = this.x[j - 1];
else if (!input[1])
this.x[j] = this.zeroBufP;
else {
this.xBuf.set(input[1].subarray(i, i + s), o);
this.x[j] = this.xBufP + 4 * o;
o += 128;
}
j++;
} }
} }
this.module.processor_process(this.instance, s); this.module.processor_process(this.instance, s);
for (var j = 0; j < this.y.length; j++) { var j = 0;
var output = outputs[j]; for (var k = 0; k < outputs.length; k++) {
for (var k = 0; k < this.y[j].length; k++) var output = outputs[k];
output[k].set(this.y[j][k].subarray(i, s)); output[0].set(this.yBuf.subarray(128 * j, 128 * j + s), i);
j++;
if (output[1]) {
output[1].set(this.yBuf.subarray(128 * j, 128 * j + s), i);
j++;
}
} }
i += s; i += s;