PresetRegion.fromLists constructor

PresetRegion.fromLists({
  1. required List<Generator> global,
  2. required List<Generator> local,
  3. required List<Instrument> instruments,
})

Implementation

factory PresetRegion.fromLists(
    {required List<Generator> global,
    required List<Generator> local,
    required List<Instrument> instruments})
{
  // initialize default values
  Map<GeneratorType, int> gs = {};
  for (GeneratorType gType in GeneratorType.values) {
    gs[gType] = 0;
  }
  gs[GeneratorType.keyRange] = 0x7F00;
  gs[GeneratorType.velocityRange] = 0x7F00;

  for (Generator parameter in global) {
    gs[parameter.type] = castToShort(parameter.value);
  }

  for (Generator parameter in local) {
    gs[parameter.type] = castToShort(parameter.value);
  }

  Instrument inst;

  if (instruments.isNotEmpty) {

    int? id = gs[GeneratorType.instrument];

    if (id == null || id < 0 || id >= instruments.length) {
      throw "The preset contains an invalid instrument ID '$id'.";
    }

    inst = instruments[id];

  } else {

    inst = Instrument.defaultInstrument();
  }

  return PresetRegion(instrument: inst, gs: gs);
}