TypeScript: AudioWorkletProcessor type definition is missing

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms: AudioWorkletProcessor

You guys have type definitions for AudioWorkletNode and such but missed the definition for AudioWorkletProcessor as per: https://webaudio.github.io/web-audio-api/#audioworkletprocessor

This is what I have for now:

interface AudioWorkletProcessor {
    readonly port: MessagePort;
    process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Map<string, Float32Array>): void;
}

declare var AudioWorkletProcessor: {
    prototype: AudioWorkletProcessor;
    new(options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
}

Cheers

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 37
  • Comments: 22 (7 by maintainers)

Most upvoted comments

I think the typescript DOM lib generator publishes this to package @types/audioworklet but that needs to be manually added to your package.json

The definition of registerProcessor was missing as well.

I’m using the following definitions:

interface AudioWorkletProcessor {
  readonly port: MessagePort;
  process(
    inputs: Float32Array[][],
    outputs: Float32Array[][],
    parameters: Record<string, Float32Array>
  ): boolean;
}

declare var AudioWorkletProcessor: {
  prototype: AudioWorkletProcessor;
  new (options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
};

declare function registerProcessor(
  name: string,
  processorCtor: (new (
    options?: AudioWorkletNodeOptions
  ) => AudioWorkletProcessor) & {
    parameterDescriptors?: AudioParamDescriptor[];
  }
);


EDIT: added the parameter descriptors to the registerProcessor() declaration.

Install @types/audioworklet

npm i @types/audioworklet -D

Add @ types/audioworklet to the tsconfig. json file to work for me.

{
  "compilerOptions": {
    "types": [
      "@types/audioworklet"
    ]
  }
}
declare function registerProcessor(
  name: string,
  processorCtor: (new (
    options?: AudioWorkletNodeOptions
  ) => AudioWorkletProcessor) & {
    parameterDescriptors?: AudioParamDescriptor[];
  }
): undefined;

Add correct return type to avoid implicit any warnings. https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletGlobalScope/registerProcessor#return_value

I think the typescript DOM lib generator publishes this to package @types/audioworklet but that needs to be manually added to your package.json

Unfortunately, @types/audioworklet only works if you aren’t using DOM at the same time because of definition conflicts.

I’ve added @types/audioworklet to my package but when I create a custom processor that extends AudioWorkletProcessor in typescript. It shows an error regarding the process function because it isn’t declared in the interface of the AudioWorkletProcessor. Upon hovering over AudioWorkletProcessor in VSCode, the following text is shown:

var AudioWorkletProcessor: {
    new (): AudioWorkletProcessor;
    prototype: AudioWorkletProcessor;
}

What’s the problem here? Why hasn’t the declarations (discussed above) been pushed to the @types/audioworklet package?

PS: Please forgive any ignorance with regards to type declarations and all from my side 😅

I get an error because the optional options constructor parameter is missing entirely from that definition. It’s present in the handwritten versions further up the thread. Surprising that was overlooked.

Working on Web Audio API library I was surprised it’s missing in TS. Most of other stuff, including other Audio Worklet related classes/interfaces are present.

@navelpluisje parameters: Record<string, Float32Array> should also work.