WebAudio / web-audio-api

The Web Audio API v1.0, developed by the W3C Audio WG
https://webaudio.github.io/web-audio-api/
Other
1.05k stars 167 forks source link

AudioWorklet examples (4): Messaging #779

Closed hoch closed 8 years ago

hoch commented 8 years ago

IDL

// This is main thread interface. (main thread => audio thread)
partial interface AudioWorkletNode : AudioNode {
    void postMessage(any message, optional sequence<Transferable> transfer);
    attribute EventHandler onmessage;
}

// This is audio thread interface. (audio thread => main thread)
partial interface AudioWorkletProcessor {
    void postMessage(any message, optional sequence<Transferable> transfer);
    attribute EventHandler onmessage;
}

This method posts a message from the main thread to AudioWorkletNode triggering onmessage event for the target node. message is processed by structured cloning algorithm.

Examples

We can use the same messaging pattern with Worker. AudioWorklet class definition can utilize onmessage event handler and postMessage() method to set up the messaging path.

/* AudioWorklet script */
registerAudioWorkletProcessor('Foo', class extends AudioWorkletProcessor {

  constructor (options) { ... },

  process (inputs, outputs, parameters) { ... },

  onmessage (event) {
    // Do something with data from main thread.
    var dataForMainThread = { ... };

    // Send data to the main thread.
    this.postMessage(dataForMainThread);
  }
});

Likewise, the postMessage() method and the onmessage handler on an AudioWorkletNode can be used to match the counterpart from the audio thread.

/* Main thread */
var myFooNode = new AudioWorkletNode(context, 'Foo');

// Send data to the audio thread.
myFooNode.postMessage({
  value: 0.5,
  message: 'hello'
});

// Respond to postMessage() from the audio thread.
myFooNode.onmessage = function (event) {
   // Do something with data from AudioWorkletNode.
};
mdjp commented 8 years ago

F2F: Agreed. Need to define IDL for event on processing side.

hoch commented 8 years ago

I addressed the issue by splitting the definition into two sides: AudioWorkletNode and AudioWorkletProcessor.

mdjp commented 8 years ago

Closing as discussed on call 14 July 2016