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

[Question] Using AudioWorkletNodeOptions For Multichannel outputs #1671

Closed seanwalker909 closed 6 years ago

seanwalker909 commented 6 years ago

I'm trying to use AudioWorkletNodeOptions to allow my AudioWorklet to have multiple outputs. I'm trying to make a panning algorithm that will work with an arbitrary number of speakers, similar to PanX in SuperCollider. The documentation is not very helpful, where does the dictionary need to be defined? in the .js class file? or in index.html? Can someone please provide a simple example of where to define the dictionary and how? The code in the documentation does not seem to work:

dictionary AudioWorkletNodeOptions : AudioNodeOptions {
  unsigned long numberOfInputs = 1;
  unsigned long numberOfOutputs = 1;
  sequence<unsigned long> outputChannelCount;
  record<DOMString, double> parameterData;
  object? processorOptions = null;
};
hoch commented 6 years ago

The spec is not a "developer-friendly" document. It's more for browser implementors. Have you tried something like this?

const massiveChannelCount = 24;
const panX = new AudioWorkletNode(context, 'my-processor', {
  channelCount: massiveChannelCount,
  channelCountMode: 'explicit',
  channelInterpretation: 'discrete',
});

This section might be a bit more helpful: https://webaudio.github.io/web-audio-api/#configuring-channels-with-audioworkletnodeoptions

I can see .ar and .kr take the channel count. Does it change the channel count dynamically during the audio rendering? For AudioWorklet, you can do that by omitting the channel count. If the input is n channels, the AudioWorklet will adapt to its incoming channel count.

seanwalker909 commented 6 years ago

Ooooh okay, thanks! PanX does not do that but that's okay it's not something I need. Doesn't the constructor in the AudioWorkletProcessor class also need to have an AudioWorkletNodeOptions as an argument?

constructor(AudioWorkletNodeOptions options) {
    super();
  }

If so, can you please provide an example of how to use this dictionary in the constructor and set up the worklet to have multiple channels?

hoch commented 6 years ago

When you have a guarantee that the processor always will work with massiveChannelCount above, you can use the value in the processor's constructor like this.

class PanXProcessor extends AudioWorkletProcessor {
  constructor(nodeOptions) {
    super();
    this._channelCount = nodeOptions.channelCount;
  }
}

Then you can use the _channelCount in anywhere class definition. Alternatively, you can dynamically query the channel count from inputs[0] array in AWP.process() method.

process(inputs, outputs, parameters) {
  const input = inputs[0];
  const channelCount = input.length;
}

I still think this question should be asked/answered outside of the spec discussion. If you have more follow-ups, please consider using other QA forums. The WebAudio slack channel would be better for these questions.

rtoy commented 6 years ago

Nothing to do here for the spec, right?

seanwalker909 commented 6 years ago

no, sorry for posting it here. I just didn't know where else to post it. Where are the QA forums?

hoch commented 6 years ago

We don't have a dedicated forum for the WebAudio, but I recommend the slack channel.

seanwalker909 commented 6 years ago

How can I be added to the slack channel? I have another question regarding eliminating clicks and pops when switching output channels in the panX algorithm I'm developing.

chrisguttandin commented 6 years ago

hi @seanwalker909, there is a little app which generates an invitation: https://web-audio-slackin.herokuapp.com/ I don't know the details but there seems to be no easier way to do this.

rtoy commented 6 years ago

I think for questions like that, stackoverflow might be a better place. Then others can benefit from the solution. Or if you think it might be a bug in the browser, file an issue with your browser. There's no harm in doing so; the worst that can happen is that it's working as intended.