updateSynth method

  1. @override
Future<void> updateSynth(
  1. String synthId,
  2. SynthKitSynthOptions options
)
override

Implementation

@override
Future<void> updateSynth(
  String synthId,
  SynthKitSynthOptions options,
) async {
  final synth = _requireSynth(synthId);
  synth.synth.callMethodVarArgs<JSAny?>('set'.toJS, <JSAny?>[
    _synthOptions(options),
  ]);

  _setGainValue(synth.gain, options.volume);

  if (options.filter.enabled) {
    final filter = synth.filter;
    if (filter == null) {
      final tone = _tone!;
      final filterCtor = tone.getProperty<JSFunction>('Filter'.toJS);
      final newFilter = filterCtor.callAsConstructor<JSObject>(
        options.filter.cutoffHz.toJS,
        'lowpass'.toJS,
      );
      synth.synth.callMethod<JSAny?>('disconnect'.toJS);
      synth.gain.callMethod<JSAny?>('disconnect'.toJS);
      synth.synth.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[
        newFilter,
      ]);
      newFilter.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[
        synth.gain,
      ]);
      synth.gain.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[
        _masterGain,
      ]);
      synth.filter = newFilter;
    } else {
      filter.setProperty('type'.toJS, 'lowpass'.toJS);
      final frequency = filter.getProperty<JSObject>('frequency'.toJS);
      frequency.setProperty('value'.toJS, options.filter.cutoffHz.toJS);
    }
  } else if (synth.filter != null) {
    synth.synth.callMethod<JSAny?>('disconnect'.toJS);
    synth.filter!.callMethod<JSAny?>('disconnect'.toJS);
    synth.synth.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[
      synth.gain,
    ]);
    synth.gain.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[
      _masterGain,
    ]);
    synth.filter!.callMethod<JSAny?>('dispose'.toJS);
    synth.filter = null;
  }

  synth.options = options;
}