Visitar URL original
TAG issue: Subclassing / Node composition · Issue #251 · WebAudio/web-audio-api · GitHub
Skip to content

TAG issue: Subclassing / Node composition #251

Closed

Description

@chrislo

The following issue was raised by the W3C TAG as part of their review of the Web Audio API

ISSUE: Subclassing

Related to a lack of constructors, but worth calling out independently, it's not currently possible to meaningfully compose node types, either through mixins or through subclassing. In JS, this sort of "is a" relationship is usually set up through the subclassing pattern:

var SubNodeType = function() {
  SuperNodeType.call(this);
};
SubNodeType.prototype = Object.create(SuperNodeType.prototype);
SubNodeType.prototype.constructor = SubNodeType;
// ...

There doesn't seem to be any way in the current design to enable this sort of composition. This is deeply unfortunate.

Activity

  1. juliangruber commented on Dec 1, 2013

    @juliangruber

    with sublassing capabilities writing custom nodes will be way easier and there won't be a fight of CustomNode(input) vs input.connect(customNode.node) etc.

  2. juliangruber commented on Dec 2, 2013

    @juliangruber

    For example, I have a multi channel analyzer node. If I were to expose a currently ideomatic webaudio api, it would be

    sourceOne.connect(analyzer.node);
    sourceTwo.connect(analyzer.node);
    analyzer.on('data', function(data) {
      console.log(data); // [[ 0, 1, ... ], [ 1, 1, ... ]]
    });

    Having unnamed parameters sucks, and especially when you disconnect one node it becomes super confusing. So the api I have now is like this:

    analyzer
      .add('one', sourceOne)
      .add('two', sourceTwo)
      .on('data', function(data) {
        console.log(data); // { one: [ 0, 1, ... ], two: [ 1, 1, ... ] }
      });

    This works but isn't ideomatic webaudio style, i.e. it's not using .connect. What I'd ideally do is this:

    sourceOne.connect(analyzer.input('one'));
    sourceTwo.connect(analyzer.input('two'));
    analyzer.on('data', function(data) {
      console.log(data); // { one: [ 0, 1, ... ], two: [ 1, 1, ... ] }
    });

    So I'd have be able to create custom audionodes.

  3. changed the title [-]Subclassing[/-] [+]Subclassing / Node composition[/+] on Oct 30, 2014
  4. added this to the milestone on Oct 30, 2014
  5. cwilso commented on Oct 30, 2014

    @cwilso
    Contributor

    This is likely a meta-issue: make sure we can compose nodes somehow.

  6. k4b7 commented on Feb 23, 2015

    @k4b7
    Contributor

    Would it be useful to a factory method for creating custom nodes? Maybe something like this:

    AudioContext.prototype.createCustomNode = function(CustomNode, ...options) {
            var node = Object.create(CustomNode.prototype);
            Object.defineProperty(node, "context", {
                enumerable: true,
                configurable: false,
                writable: false,
                value: this
            });
            CustomNode.apply(node, options);
            return node;
        }
    

    It would be nice if we could do write source.connect(customNode, [output], [input]). This would necessitate customNode to define some sort of inputs array that contains one or more AudioNode instances which source would look at when making the connection. The could be polyfilled by doing something like this:

    var connect = AudioNode.prototype.connect;
    AudioNode.prototype.connect = function(destination, output = 0, input = 0) {
       if (destination instanceof CustomNode) {
           this.connect(destination.inputs[input], output);
       } else if (destination instanceof AudioNode) {
           this.connect(destination, output, input);
       } else if (destination instanceof AudioParam) {
           this.connect(destination, output);
       } else {
           throw "not a valid destination";
       }
    };
    

    In the case of a custom node possessing multiple input nodes which have multiple inputs themselves a user would have to specify which of the inputs in the following way: node.connect(customNode.inputs[0], [output], [subInput])

  7. cwilso commented on Apr 30, 2015

    @cwilso
    Contributor

    Even having a custom "compositorNode" that could have a graph internally would be a fix for this. It really, really sucks to not be able to properly do this, since it means you can't create (e.g.) a "ChorusEffectNode" that can be both connected to and connected.

  8. hoch commented on Apr 30, 2015

    @hoch
    Member

    I would really love to see this happen. I've already gone extra miles to have something like this in my WAAX library. Having this feature on API level would be much more useful then JS polyfills and prototype override/inheritance. If this happens, I can see my plug-ins can be ported with very little effort.

    Also another useful thing to have is a collective AudioParam. Some parameters can be abstracted on top of a set of AudioParams. Redirection/collective control on them makes much more sense inside of CompositorNode.

    @chris Do you have any idea/proposal for this?

  9. 37 remaining items

  10. hoch commented on May 20, 2016

    @hoch
    Member

    @domenic Thanks for the idea. I will certainly make some improvements!

  11. hoch commented on May 20, 2016

    @hoch
    Member

    @joeberkovitz @mdjp What would be the next step here?

  12. joeberkovitz commented on May 23, 2016

    @joeberkovitz

    Seems to me we can just proceed on cleaning up the constructor definitions
    as per the other issue filed on this point, and clarify that they are in
    the global window namespace.

    I don't think we need to do anything special to address the
    composite-node-construction case for v1 although it's nice to get a better
    pictre of how this might be done.

    ...Joe

    . . . . . ...Joe

    Joe Berkovitz
    President
    Noteflight LLC

    +1 978 314 6271

    49R Day Street
    Somerville MA 02144
    USA

    "Bring music to life"
    www.noteflight.com

    On Fri, May 20, 2016 at 10:49 AM, Hongchan Choi notifications@github.com
    wrote:

    @joeberkovitz https://github.com/joeberkovitz @mdjp
    https://github.com/mdjp What would be the next step here?

    —
    You are receiving this because you were mentioned.
    Reply to this email directly or view it on GitHub
    #251 (comment)

  13. hoch commented on May 23, 2016

    @hoch
    Member

    Then perhaps we can move this issue to the ready-for-editing status.

  14. added
    Needs Edits
    on May 24, 2016
  15. joeberkovitz commented on May 24, 2016

    @joeberkovitz

    done!

  16. joeberkovitz commented on Sep 23, 2016

    @joeberkovitz

    Now we have constructors, this isn't an issue any more.

  17. added
    tag-tracker
    on Apr 21, 2020
  18. dy commented on Jul 7, 2021

    @dy

    Is there an info was the extending implemented by any browser? Chrome 91 doesn't seem to allow that.

  19. hoch commented on Jul 12, 2021

    @hoch
    Member

    Chrome 91 doesn't seem to allow that.

    Can you elaborate what you tried and what did not work?

  20. dy commented on Jul 14, 2021

    @dy

    @hoch I mean your example:

    class CustomGainNode extends GainNode {
      mute () {
        this.gain.setValueAtTime(0, 0);
      }
    }
    let ctx = new AudioContext
    let g = new CustomGainNode(ctx)
    g.mute()
    // VM207:1 Uncaught TypeError: g.mute is not a function
    //    at <anonymous>:1:3

    Can that be considered a chromium bug? Is that expected to be implemented?
    Firefox, Safari support that.
    Filed a bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1229154

  21. hoch commented on Jul 14, 2021

    @hoch
    Member

    I responded on the bug, but it seems working correctly for me. We can continue the discussion on the bug, not here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Needs Editstag-tracker

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions