createSynth method

  1. @override
Future<String> createSynth(
  1. SynthKitSynthOptions options
)
override

Implementation

@override
Future<String> createSynth(SynthKitSynthOptions options) async {
  await _ensureInitialized();
  final tone = _tone!;
  final polySynthCtor = tone.getProperty<JSFunction>('PolySynth'.toJS);
  final synth = polySynthCtor.callAsConstructor<JSObject>(
    _polySynthOptions(tone, options),
  );

  final gainCtor = tone.getProperty<JSFunction>('Gain'.toJS);
  final gain = gainCtor.callAsConstructor<JSObject>(options.volume.toJS);
  JSObject? filter;
  if (options.filter.enabled) {
    final filterCtor = tone.getProperty<JSFunction>('Filter'.toJS);
    filter = filterCtor.callAsConstructor<JSObject>(
      options.filter.cutoffHz.toJS,
      'lowpass'.toJS,
    );
    synth.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[filter]);
    filter.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[gain]);
  } else {
    synth.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[gain]);
  }
  gain.callMethodVarArgs<JSAny?>('connect'.toJS, <JSAny?>[_masterGain]);

  final synthId = 'web_synth_${_nextSynthId++}';
  _synths[synthId] = _WebSynthHandle(
    synth: synth,
    gain: gain,
    filter: filter,
    options: options,
  );
  return synthId;
}