elemaudio / elementary

Elementary is a JavaScript library for digital audio signal processing.
https://www.elementary.audio/
MIT License
331 stars 29 forks source link

Id field on renderer initialisation #12

Closed cristianvogel closed 1 year ago

cristianvogel commented 1 year ago

I am developing a larger web site with Elementary basically in replacement of the global Audio object and WebAudio generic.

In this context, I am finding it useful to instantiate multiple WebAudioRenderer ( alias WebRenderer ? )

What would be useful to make sure everything is routed and targeted correctly, is some introspection.

I am proposing an optional id field in the constructor, that at least gets returned at the load() event but also maybe from a getter once loaded.

    this.port.postMessage(['load', {
      id,
      sampleRate,
      blockSize: 128,
      numInputChannels,
      numOutputChannels,
    }]);
  }

https://github.com/elemaudio/elementary/blob/05d8acd2b2c49228d1f8717da67015d5ceffeec2/js/packages/web-renderer/raw/WorkletProcessor.js#LL85C1-L91C4

nick-thompson commented 1 year ago

To make sure I understand this correctly, let me ask: because each WebRenderer instance is its own EventEmitter, don't you get events scoped to the correct instance by default?

let core1 = new WebRenderer();
let core2 = new WebRenderer();

// Initialization and stuff omitted for brevity

core1.on(<anything>, (e) => {
  // You know this is coming from core1
});

core2.on(<anything>, (e) => {
  // You know this is coming from core2
});

For me that seems to provide everything we would need to distinguish which events come from which instance.

cristianvogel commented 1 year ago

Is this an acceptable way of adding such a custom property?


Object.defineProperty(renderer, 'id', { value: id, writable: false });
nick-thompson commented 1 year ago

Sure, if you like! JavaScript lets you do all kinds of stuff; you could just renderer.id = id; too.

cristianvogel commented 1 year ago

Yeah, I'm loving it. Its my favourite language after SmallTalk ;)

nick-thompson commented 1 year ago

Right on :). Good to close this issue then?