standardized-audio-context: Unable to find native AudioParam on connection

I’m trying to integrate standardized-audio-context into Tone.js, but i’m getting an issue when i try to use it in a simple Oscillator: Uncaught Error: A value with the given key could not be found.

Here’s how i reproduced it: https://codesandbox.io/s/sharp-currying-k9v9y

For context, the oscillator uses two ConstantSourceNodes connected to the frequency and detune of the OscillatorNode. it seems to cause an issue when the constant source is connected to the AudioParam. It is unable to resolve the native AudioParam in the WeakMap. Either the AudioParam has been already garbage collected, which seems unlikely, or possibly it was not added.

Thanks for your help!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (11 by maintainers)

Most upvoted comments

After writing the message above yesterday I realized that it is unfortunately not that easy. Up to now we have the following behavior which I think is super confusing.

import { AudioBuffer as StdAudioBuffer } from 'standardized-audio-context';

const nativeAudioBuffer = new AudioBuffer({ length: 1, sampleRate: 44100 });

// This seems reasonable to me:
console.log(nativeAudioBuffer instanceof AudioBuffer); // true
console.log(nativeAudioBuffer instanceof StdAudioBuffer); // false

const stdAudioBuffer = new StdAudioBuffer({ length: 1, sampleRate: 44100 });

// But I think this is confusing:
console.log(stdAudioBuffer instanceof AudioBuffer); // true
console.log(stdAudioBuffer instanceof StdAudioBuffer); // false

I changed that now that an AudioBuffer created with standardized-audio-context also returns true when doing an instanceof check with the constructor exported by standardized-audio-context.

Only the result of the last console.log() from the example above will change when using v21.2.1 (yet to be released) onwards.

// ...
console.log(stdAudioBuffer instanceof AudioBuffer); // true
console.log(stdAudioBuffer instanceof StdAudioBuffer); // true

I hope this makes sense to you as well.

Yes an instanceof check will work.

The AudioBuffer implementation is very dirty because by definition an AudioBuffer is the only thing which can be shared across different (Offline)AudioContexts. The constructor in particular is doing something which should normally never be done: https://github.com/chrisguttandin/standardized-audio-context/blob/master/src/factories/audio-buffer-constructor.ts#L89 😃

I did all this to make the following work:

import { AudioBuffer } from 'standardized-audio-context';

const nativeAudioContext = new AudioContext();
const nativeAudioBufferSourceNode = new AudioBufferSourceNode(
    nativeAudioContext,
    { buffer: new AudioBuffer({ length: 1, sampleRate: 44100 }) }
);

thanks for looking into that. that’s helpful! i think i know how to fix it. will try something tmrw and get back to you

I took another look at your example today and this time I have another assumption which can of course be false again. 😃

A few days ago I fixed a bug which was caused by duplicate connections. Calling a.connect(b) more than once wasn’t handled correctly. I haven’t published the fix yet to npm as I was hoping to get some more breaking changes done before the next major release. But this is done now and I will publish the new version now. Hopefully this will fix your problem as well.