noteOn method

void noteOn({
  1. required int channel,
  2. required int key,
  3. required int velocity,
})

Implementation

void noteOn({required int channel, required int key, required int velocity}) {
  if (velocity == 0) {
    noteOff(channel: channel, key: key);
    return;
  }

  if (!(0 <= channel && channel < channels.length)) {
    return;
  }

  var channelInfo = channels[channel];

  var presetId = (channelInfo.bankNumber << 16) | channelInfo.patchNumber;

  Preset? preset = _presetLookup[presetId];

  // Try fallback to the GM sound set.
  // Normally, the given patch number + the bank number 0 will work.
  // For drums (bank number >= 128), it seems to be better to select the standard set (128:0).
  var gmPresetId = channelInfo.bankNumber < 128 ? channelInfo.patchNumber : (128 << 16);
  preset ??= _presetLookup[gmPresetId];

  // No corresponding preset was found. Use the default.
  preset ??= _defaultPreset;

  for (PresetRegion presetRegion in preset.regions) {
    if (presetRegion.contains(key, velocity)) {
      for (InstrumentRegion instrumentRegion in presetRegion.instrument.regions) {
        if (instrumentRegion.contains(key, velocity)) {
          var regionPair = RegionPair(preset: presetRegion, instrument: instrumentRegion);

          var voice = _voices.requestNew(instrumentRegion, channel);

          if (voice != null) {
            voice.start(regionPair, channel, key, velocity);
          }
        }
      }
    }
  }
}