Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SharedArrayBuffer for acc/gyr if available #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions src/FaustAudioWorkletCommunicator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

/**
* Layout:
*
*
* invert-isAndroid (uint8)
* new-acc-data-available (uint8)
* new-gyr-data-available (uint8)
* empty (uint8)
*
* acc.x, acc.y, acc.z (f32)
*
* gyr.alpha, gyr.beta, gyr.gamma (f32)
*/
export class FaustAudioWorkletCommunicator {
protected readonly port: MessagePort;
protected readonly supportSharedArrayBuffer: boolean;
protected readonly byteLength: number;
protected uin8Invert: Uint8ClampedArray;
protected uin8NewAccData: Uint8ClampedArray;
protected uin8NewGyrData: Uint8ClampedArray;
protected f32Acc: Float32Array;
protected f32Gyr: Float32Array;
constructor(port: MessagePort) {
this.port = port;
this.supportSharedArrayBuffer = !!globalThis.SharedArrayBuffer;
this.byteLength
= 4 * Uint8Array.BYTES_PER_ELEMENT
+ 3 * Float32Array.BYTES_PER_ELEMENT
+ 3 * Float32Array.BYTES_PER_ELEMENT;
}
initializeBuffer(ab: SharedArrayBuffer | ArrayBuffer) {
let ptr = 0;
this.uin8Invert = new Uint8ClampedArray(ab, ptr, 1);
ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
this.uin8NewAccData = new Uint8ClampedArray(ab, ptr, 1);
ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
this.uin8NewGyrData = new Uint8ClampedArray(ab, ptr, 1);
ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;
ptr += Uint8ClampedArray.BYTES_PER_ELEMENT;; // empty
this.f32Acc = new Float32Array(ab, ptr, 3);
ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
this.f32Gyr = new Float32Array(ab, ptr, 3);
ptr += 3 * Float32Array.BYTES_PER_ELEMENT;
}
setNewAccDataAvailable(value: boolean) {
if (!this.uin8NewAccData) return;
this.uin8NewAccData[0] = +value;
}
getNewAccDataAvailable() {
return !!this.uin8NewAccData?.[0];
}
setNewGyrDataAvailable(value: boolean) {
if (!this.uin8NewGyrData) return;
this.uin8NewGyrData[0] = +value;
}
getNewGyrDataAvailable() {
return !!this.uin8NewGyrData?.[0];
}
setAcc({ x, y, z }: { x: number, y: number, z: number }, invert = false) {
if (!this.supportSharedArrayBuffer) {
const e = { type: "acc", data: { x, y, z }, invert };
this.port.postMessage(e);
}
if (!this.uin8NewAccData) return;
this.uin8Invert[0] = +invert;
this.f32Acc[0] = x;
this.f32Acc[1] = y;
this.f32Acc[2] = z;
this.uin8NewAccData[0] = 1;
}
getAcc() {
if (!this.uin8NewAccData) return;
const invert = !!this.uin8Invert[0];
const [x, y, z] = this.f32Acc;
return { x, y, z, invert };
}
setGyr({ alpha, beta, gamma }: { alpha: number, beta: number, gamma: number }) {
if (!this.supportSharedArrayBuffer) {
const e = { type: "gyr", data: { alpha, beta, gamma } };
this.port.postMessage(e);
}
if (!this.uin8NewGyrData) return;
this.f32Gyr[0] = alpha;
this.f32Gyr[1] = beta;
this.f32Gyr[2] = gamma;
this.uin8NewGyrData[0] = 1;
}
getGyr() {
if (!this.uin8NewGyrData) return;
const [alpha, beta, gamma] = this.f32Gyr;
return { alpha, beta, gamma };
}
}

export class FaustAudioWorkletNodeCommunicator extends FaustAudioWorkletCommunicator {
constructor(port: MessagePort) {
super(port);
if (this.supportSharedArrayBuffer) {
const sab = new SharedArrayBuffer(this.byteLength);
this.initializeBuffer(sab);
this.port.postMessage({ type: "initSab", sab });
} else {
const ab = new ArrayBuffer(this.byteLength);
this.initializeBuffer(ab);
}
}
}

export class FaustAudioWorkletProcessorCommunicator extends FaustAudioWorkletCommunicator {
constructor(port: MessagePort) {
super(port);

if (this.supportSharedArrayBuffer) {
this.port.addEventListener("message", (event) => {
const { data } = event;
if (data.type === "initSab") {
this.initializeBuffer(data.sab);
}
});
} else {
const ab = new ArrayBuffer(this.byteLength);
this.initializeBuffer(ab);
this.port.addEventListener("message", (event) => {
const msg = event.data;

switch (msg.type) {
// Sensors messages
case "acc": {
this.setAcc(msg.data, msg.invert);
break;
}
case "gyr": {
this.setGyr(msg.data);
break;
}
default:
break;
}
});
}
}
}
29 changes: 18 additions & 11 deletions src/FaustAudioWorkletNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OutputParamHandler, ComputeHandler, PlotHandler, UIHandler, MetadataHandler, FaustBaseWebAudioDsp, IFaustMonoWebAudioDsp, IFaustPolyWebAudioDsp } from "./FaustWebAudioDsp";
import type { FaustAudioWorkletNodeOptions } from "./FaustAudioWorkletProcessor";
import type { LooseFaustDspFactory, FaustDspMeta, FaustUIInputItem, FaustUIItem } from "./types";
import { FaustAudioWorkletNodeCommunicator } from "./FaustAudioWorkletCommunicator";

/**
* Base class for Monophonic and Polyphonic AudioWorkletNode
Expand All @@ -15,6 +16,7 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global
protected fPlotHandler: PlotHandler | null;
protected fUICallback: UIHandler;
protected fDescriptor: FaustUIInputItem[];
protected communicator: FaustAudioWorkletNodeCommunicator;
#hasAccInput = false;
#hasGyrInput = false;

Expand Down Expand Up @@ -60,16 +62,21 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global

FaustBaseWebAudioDsp.parseUI(this.fJSONDsp.ui, this.fUICallback);

this.communicator = new FaustAudioWorkletNodeCommunicator(this.port);

// Patch it with additional functions
this.port.onmessage = (e: MessageEvent) => {
if (e.data.type === "param" && this.fOutputHandler) {
this.fOutputHandler(e.data.path, e.data.value);
} else if (e.data.type === "plot" && this.fPlotHandler) {
this.fPlotHandler(e.data.value, e.data.index, e.data.events);
}
};
this.port.addEventListener("message", this.handleMessageAux);
this.port.start();
}

protected handleMessageAux = (e: MessageEvent) => {
if (e.data.type === "param" && this.fOutputHandler) {
this.fOutputHandler(e.data.path, e.data.value);
} else if (e.data.type === "plot" && this.fPlotHandler) {
this.fPlotHandler(e.data.value, e.data.index, e.data.events);
}
};

// Public API

// Accelerometer and gyroscope handlers
Expand Down Expand Up @@ -210,15 +217,15 @@ export class FaustAudioWorkletNode<Poly extends boolean = false> extends (global
get hasAccInput() { return this.#hasAccInput; }
propagateAcc(accelerationIncludingGravity: NonNullable<DeviceMotionEvent["accelerationIncludingGravity"]>, invert: boolean = false) {
if (!accelerationIncludingGravity) return;
const e = { type: "acc", data: accelerationIncludingGravity, invert: invert };
this.port.postMessage(e);
const { x, y, z } = accelerationIncludingGravity;
this.communicator.setAcc({ x: x!, y: y!, z: z! }, invert);
}

get hasGyrInput() { return this.#hasGyrInput; }
propagateGyr(event: Pick<DeviceOrientationEvent, "alpha" | "beta" | "gamma">) {
if (!event) return;
const e = { type: "gyr", data: event };
this.port.postMessage(e);
const { alpha, beta, gamma } = event;
this.communicator.setGyr({ alpha: alpha!, beta: beta!, gamma: gamma! });
}

setParamValue(path: string, value: number) {
Expand Down
40 changes: 36 additions & 4 deletions src/FaustAudioWorkletProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FaustAudioWorkletProcessorCommunicator } from "./FaustAudioWorkletCommunicator";
import type FaustWasmInstantiator from "./FaustWasmInstantiator";
import type { FaustBaseWebAudioDsp, FaustWebAudioDspVoice, FaustMonoWebAudioDsp, FaustPolyWebAudioDsp } from "./FaustWebAudioDsp";
import type { AudioParamDescriptor, AudioWorkletGlobalScope, LooseFaustDspFactory, FaustDspMeta, FaustUIItem } from "./types";
Expand All @@ -19,6 +20,7 @@ export interface FaustAudioWorkletProcessorDependencies<Poly extends boolean = f
FaustPolyWebAudioDsp: Poly extends true ? typeof FaustPolyWebAudioDsp : undefined;
FaustWebAudioDspVoice: Poly extends true ? typeof FaustWebAudioDspVoice : undefined;
FaustWasmInstantiator: typeof FaustWasmInstantiator;
FaustAudioWorkletProcessorCommunicator: typeof FaustAudioWorkletProcessorCommunicator;
}
export interface FaustAudioWorkletNodeOptions<Poly extends boolean = false> extends AudioWorkletNodeOptions {
processorOptions: Poly extends true ? FaustPolyAudioWorkletProcessorOptions : FaustMonoAudioWorkletProcessorOptions;
Expand Down Expand Up @@ -52,7 +54,8 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie

const {
FaustBaseWebAudioDsp,
FaustWasmInstantiator
FaustWasmInstantiator,
FaustAudioWorkletProcessorCommunicator
} = dependencies;

const {
Expand All @@ -79,20 +82,23 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie
/**
* Base class for Monophonic and Polyphonic AudioWorkletProcessor
*/
class FaustAudioWorkletProcessor<Poly extends boolean = false> extends AudioWorkletProcessor {
abstract class FaustAudioWorkletProcessor<Poly extends boolean = false> extends AudioWorkletProcessor {

// Use ! syntax when the field is not defined in the constructor
protected fDSPCode!: Poly extends true ? FaustPolyWebAudioDsp : FaustMonoWebAudioDsp;

protected paramValuesCache: Record<string, number> = {};

protected wamInfo?: { moduleId: string; instanceId: string };
protected communicator: FaustAudioWorkletProcessorCommunicator;

constructor(options: FaustAudioWorkletNodeOptions<Poly>) {
super(options);

// Setup port message handling
this.port.onmessage = (e: MessageEvent) => this.handleMessageAux(e);
// this.port.addEventListener("message", this.handleMessageAux);
// this.port.start();
this.communicator = new FaustAudioWorkletProcessorCommunicator(this.port);

const { parameterDescriptors } = (this.constructor as typeof AudioWorkletProcessor);
parameterDescriptors.forEach((pd) => {
Expand Down Expand Up @@ -140,6 +146,21 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie
this.paramValuesCache[path] = paramValue;
}
}
if (this.communicator.getNewAccDataAvailable()) {
const acc = this.communicator.getAcc();
if (acc) {
this.communicator.setNewAccDataAvailable(false);
const { invert, ...data } = acc;
this.propagateAcc(data, invert);
}
}
if (this.communicator.getNewGyrDataAvailable()) {
const gyr = this.communicator.getGyr();
if (gyr) {
this.communicator.setNewGyrDataAvailable(false);
this.propagateGyr(gyr);
}
}

return this.fDSPCode.compute(inputs[0], outputs[0]);
}
Expand All @@ -149,6 +170,7 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie

switch (msg.type) {
// Sensors messages
/*
case "acc": {
this.propagateAcc(msg.data, msg.invert);
break;
Expand All @@ -157,6 +179,7 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie
this.propagateGyr(msg.data);
break;
}
*/
// Generic MIDI message
case "midi": {
this.midiMessage(msg.data);
Expand Down Expand Up @@ -248,11 +271,19 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie
// Create Monophonic DSP
this.fDSPCode = new FaustMonoWebAudioDsp(instance, sampleRate, sampleSize, 128, factory.soundfiles);

// Setup port message handling
this.port.addEventListener("message", this.handleMessageAux);
this.port.start();

// Setup output handler
this.fDSPCode.setOutputParamHandler((path, value) => this.port.postMessage({ path, value, type: "param" }));

this.fDSPCode.start();
}

protected handleMessageAux = (e: MessageEvent) => { // use arrow function for binding
super.handleMessageAux(e);
}
}

/**
Expand All @@ -273,7 +304,8 @@ const getFaustAudioWorkletProcessor = <Poly extends boolean = false>(dependencie
this.fDSPCode = new FaustPolyWebAudioDsp(instance, sampleRate, sampleSize, 128, soundfiles);

// Setup port message handling
this.port.onmessage = (e: MessageEvent) => this.handleMessageAux(e);
this.port.addEventListener("message", this.handleMessageAux);
this.port.start();

// Setup output handler
this.fDSPCode.setOutputParamHandler((path, value) => this.port.postMessage({ path, value, type: "param" }));
Expand Down
Loading