/**
 * Description: This is a SoundFont2 parser.
 *
 * @package     midi-Synth
 * @category    module js
 * @author      Mrtenz <https://github.com/Mrtenz/soundfont2>
 * @copyright   soundfont2
 * @license     GPL-2.0-or-later
 */

let ControllerType

;(function(ControllerType) {
  ControllerType[(ControllerType["Linear"] = 0)] = "Linear"
  ControllerType[(ControllerType["Concave"] = 1)] = "Concave"
  ControllerType[(ControllerType["Convex"] = 2)] = "Convex"
  ControllerType[(ControllerType["Switch"] = 3)] = "Switch"
})(ControllerType || (ControllerType = {}))

let ControllerPolarity

;(function(ControllerPolarity) {
  ControllerPolarity[(ControllerPolarity["Unipolar"] = 0)] = "Unipolar"
  ControllerPolarity[(ControllerPolarity["Bipolar"] = 1)] = "Bipolar"
})(ControllerPolarity || (ControllerPolarity = {}))

let ControllerDirection

;(function(ControllerDirection) {
  ControllerDirection[(ControllerDirection["Increasing"] = 0)] = "Increasing"
  ControllerDirection[(ControllerDirection["Decreasing"] = 1)] = "Decreasing"
})(ControllerDirection || (ControllerDirection = {}))

let ControllerPalette

;(function(ControllerPalette) {
  ControllerPalette[(ControllerPalette["GeneralController"] = 0)] =
    "GeneralController"
  ControllerPalette[(ControllerPalette["MidiController"] = 1)] =
    "MidiController"
})(ControllerPalette || (ControllerPalette = {}))

let Controller

;(function(Controller) {
  Controller[(Controller["NoController"] = 0)] = "NoController"
  Controller[(Controller["NoteOnVelocity"] = 2)] = "NoteOnVelocity"
  Controller[(Controller["NoteOnKeyNumber"] = 3)] = "NoteOnKeyNumber"
  Controller[(Controller["PolyPressure"] = 10)] = "PolyPressure"
  Controller[(Controller["ChannelPressure"] = 13)] = "ChannelPressure"
  Controller[(Controller["PitchWheel"] = 14)] = "PitchWheel"
  Controller[(Controller["PitchWheelSensitivity"] = 16)] =
    "PitchWheelSensitivity"
  Controller[(Controller["Link"] = 127)] = "Link"
})(Controller || (Controller = {}))

let TransformType

;(function(TransformType) {
  TransformType[(TransformType["Linear"] = 0)] = "Linear"
  TransformType[(TransformType["Absolute"] = 2)] = "Absolute"
})(TransformType || (TransformType = {}))

/**
 * The default modulators at instrument level. Implementing these is up to the consumer of this
 * library. To override these modulators, other modulators have to be defined explicitly.
 */
const DEFAULT_INSTRUMENT_MODULATORS = [
  // MIDI note-on velocity to initial attenuation
  {
    id: GeneratorType.InitialAttenuation,
    source: {
      type: ControllerType.Concave,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Decreasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoteOnVelocity
    },
    value: 960,
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI note-on velocity to filter cutoff
  {
    id: GeneratorType.InitialFilterFc,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Decreasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoteOnVelocity
    },
    value: -2400, // cents
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI channel pressure to vibrato LFO pitch depth
  {
    id: GeneratorType.VibLFOToPitch,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.ChannelPressure
    },
    value: 50, // cents / max excursion
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 1 to vibrato LFO pitch depth
  {
    id: GeneratorType.VibLFOToPitch,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.MidiController,
      index: 1
    },
    value: 50,
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 7 to initial attenuation
  {
    id: GeneratorType.InitialAttenuation,
    source: {
      type: ControllerType.Concave,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Decreasing,
      palette: ControllerPalette.MidiController,
      index: 7
    },
    value: 960,
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 10 to pan position
  {
    id: GeneratorType.InitialAttenuation,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Bipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.MidiController,
      index: 10
    },
    value: 1000, // tenths of a percent
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 11 to initial attenuation
  {
    id: GeneratorType.InitialAttenuation,
    source: {
      type: ControllerType.Concave,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Decreasing,
      palette: ControllerPalette.MidiController,
      index: 11
    },
    value: 960,
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 91 to reverb effects send
  {
    id: GeneratorType.ReverbEffectsSend,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.MidiController,
      index: 91
    },
    value: 200, // tenths of a percent
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI continuous controller 93 to chorus effects send
  {
    id: GeneratorType.ChorusEffectsSend,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.MidiController,
      index: 93
    },
    value: 200, // tenths of a percent
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.NoController
    },
    transform: TransformType.Linear
  },

  // MIDI pitch wheel to initial pitch controlled by MIDI pitch wheel sensitivity
  {
    id: GeneratorType.CoarseTune,
    source: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Bipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.PitchWheel
    },
    value: 12700, // cents
    valueSource: {
      type: ControllerType.Linear,
      polarity: ControllerPolarity.Unipolar,
      direction: ControllerDirection.Increasing,
      palette: ControllerPalette.GeneralController,
      index: Controller.PitchWheelSensitivity
    },
    transform: TransformType.Linear
  }
]
