Synthesizer.load constructor

Synthesizer.load(
  1. SoundFont soundFont,
  2. SynthesizerSettings settings
)

Implementation

factory Synthesizer.load(SoundFont soundFont, SynthesizerSettings settings) {
  Map<int, Preset> presetLookup = {};

  Preset? defaultPreset;
  int? minPresetId;
  for (Preset preset in soundFont.presets) {
    // The preset ID is Int32, where the upper 16 bits represent the bank number
    // and the lower 16 bits represent the patch number.
    // This ID is used to search for presets by the combination of bank number
    // and patch number.
    int presetId = (preset.bankNumber << 16) | preset.patchNumber;

    presetLookup[presetId] = preset;

    // The preset with the minimum ID number will be default.
    // If the SoundFont is GM compatible, the piano will be chosen.
    if (minPresetId == null || presetId < minPresetId) {
      defaultPreset = preset;
      minPresetId = presetId;
    }
  }

  bool rc = settings.enableReverbAndChorus;

  Chorus? chorus = rc == false
      ? null
      : Chorus.create(sampleRate: settings.sampleRate, delay: 0.002, depth: 0.0019, frequency: 0.4);

  Synthesizer synth = Synthesizer(
    soundFont: soundFont,
    sampleRate: settings.sampleRate,
    blockSize: settings.blockSize,
    maximumPolyphony: settings.maximumPolyphony,
    enableReverbAndChorus: settings.enableReverbAndChorus,
    minimumVoiceDuration: settings.sampleRate ~/ 500,
    presetLookup: presetLookup,
    defaultPreset: defaultPreset!,
    blockLeft: List<double>.filled(settings.blockSize, 0),
    blockRight: List<double>.filled(settings.blockSize, 0),
    inverseBlockSize: 1.0 / settings.blockSize,
    blockRead: settings.blockSize,
    reverb: !rc ? null : Reverb.withSampleRate(settings.sampleRate),
    reverbInput: !rc ? null : List<double>.filled(settings.blockSize, 0),
    reverbOutputLeft: !rc ? null : List<double>.filled(settings.blockSize, 0),
    reverbOutputRight: !rc ? null : List<double>.filled(settings.blockSize, 0),
    chorus: chorus,
    chorusInputLeft: !rc ? null : List<double>.filled(settings.blockSize, 0),
    chorusInputRight: !rc ? null : List<double>.filled(settings.blockSize, 0),
    chorusOutputLeft: !rc ? null : List<double>.filled(settings.blockSize, 0),
    chorusOutputRight: !rc ? null : List<double>.filled(settings.blockSize, 0),
    masterVolume: 0.5,
  );

  // Channels & Voices must be set *after* the synth is constructed,
  // since they take the synthesizer ref in their constructor

  List<Channel> channels = [];

  for (int i = 0; i < channelCount; i++) {
    channels.add(Channel.create(synth, i == percussionChannel));
  }

  synth.channels = channels;
  synth._voices = VoiceCollection.create(synth, settings.maximumPolyphony);

  return synth;
}