# (B) Pitch processor, envelopes

# for TB, LD, OR, AR

# INPUTS:
#  I1: [ROOT]
#  I2: [SCALE]
#  I3: [POS] Ribbon Position
#  I4: [PRESS] Ribbon pressure
#  I5: [TB V/O]
#  I6: [LD V/O]
#  I7: [OR V/O]
#  I8: [AR V/O]

# OUTPUTS:
#  O1: [TB V/O]
#  O2: [LD V/O]
#  O3: [OR V/O]
#  O4: [AR V/O]
#  O5: [TB Env]
#  O6: [LD Env]
#  O7: [OR Env]
#  O8: [AR Env]

# GATES ON MODULE 1:
#  G1.1: [CLOCK]
#  G1.2: [RESET]
#  G1.6: [LD Out]
#  G1.7: [OR Out]
#  G1.8: [AR Out]

# GATES ON MODULE 2:
#  G2.1: [TB Gate]
#  G2.2: [LD Gate]
#  G2.3: [OR Gate]
#  G2.4: [AR Gate]
#  G2.5: [TB Acc] Input
#  G2.6: [LD Acc] Input
#  G2.7: [OR Acc] Input
#  G2.8: [AR Acc] Input

# CONTROLLER 1:
#  E1.1: [TB shift/cal] Pitch calibration for TB
#  E1.2: [LD shift/cal] Pitch calibration for LD
#  E1.3: [OR shift/cal] Pitch calibration for OR
#  E1.4: [AR shift/cal] Pitch calibration for AR

# CONTROLLER 2:
#  P2.1: [Vibrato]
#  P2.2: [Tremolo]
#  P2.3: [Bend 2] Pitch bend 2 semitones up/down
#  P2.4: [Bend 3] Pitch bend 3 semitones up/down

# CONTROLLER 3:
#  P3.1: [Vib speed]
#  P3.2: [Vib delay]
#  B3.1: [ROOT]
#  B3.2: [3RD]
#  B3.3: [5TH]
#  B3.4: [7TH]
#  B3.5: [9TH]
#  B3.6: [11TH]
#  B3.7: [13TH]
#  B3.8: [ALT 7]

# CONTROLLER 4:
#  P4.1: [Rib Slew] Slew limiter for ribbon
#  P4.2: [Ribbon pressure] Calibration of ribbon pressure input
#  B4.1: [ALT 9]
#  B4.2: [1 Bar] Loop length 1 bar (16 clock ticks)
#  B4.3: [ALT 3]
#  B4.4: [2 Bars] Loop length 2 bars
#  B4.5: [ALT 4]
#  B4.6: [4 Bars] Loop length 4 bars
#  B4.7: [ALT 6/13]
#  B4.8: [OVDub] Loop Overdup

# CONTROLLER 5:
#  S5.1: [Tuning] 0 = Off, 1-7 = tuning + octave switch
#  S5.2: [Ribbon octav]
#  S5.3: [8/16/32]
#  S5.4: [TB Rib] 0:off, 1:prio=seq, 2:prio=ribbon
#  S5.5: [Triplets]
#  S5.6: [LD Rib]
#  S5.7: [Std/Byp] Normal / Steady / Bypass
#  S5.8: [OR Rib]
#  S5.9: [Loop] Loop on/mute/off
#  S5.10: [AR Rib]

# CONTROLLER 6:
#  P6.1: [TB Envmix]
#  P6.2: [LD Envmix]
#  P6.3: [OR Envmix]
#  P6.4: [AR Envmix]
#  B6.1: [Octave] Selects octave switches on encoders
#  B6.2: [Switch] Swicht sequencer source per track

# CONTROLLER 7:
#  P7.1: [TB Attack]
#  P7.2: [Decay/Releas]
#  P7.3: [Sustain]
#  P7.4: [Level] Calibration of output level
#  P7.5: [Track]
#  P7.6: [Shape]
#  P7.7: [Accents]
#  P7.8: [Level ?]
#  P7.9: [Del Vib] Delayed vibrato
#  P7.10: [Speed ?]

# CONTROLLER 8:
#  P8.1: [LD Attack]
#  P8.2: [Decay/Releas]
#  P8.3: [Sustain]
#  P8.4: [Level] Calibration of output level
#  P8.5: [Track] Evenlope pitch tracking
#  P8.6: [Shape]
#  P8.7: [Accents]
#  P8.8: [Level ?]
#  P8.9: [Del Vib] Delayed vibrato
#  P8.10: [Speed ?] Calibration of accent level

# CONTROLLER 9:
#  P9.1: [OR Attack]
#  P9.2: [Decay/Releas]
#  P9.3: [Sustain]
#  P9.4: [Level] Calibration of output level
#  P9.5: [Track]
#  P9.6: [Shape]
#  P9.7: [Accents]
#  P9.8: [Level ?]
#  P9.9: [Del Vib]
#  P9.10: [Speed ?]

# CONTROLLER 10:
#  P10.1: [AR Attack]
#  P10.2: [Decay/Releas]
#  P10.3: [Sustain]
#  P10.4: [Level] Calibration of output level
#  P10.5: [Track] Delayed vibrato
#  P10.6: [Shape] Calibration of accent level
#  P10.7: [Accents]
#  P10.8: [Level ?]
#  P10.9: [Del Vib] Evenlope pitch tracking
#  P10.10: [Speed ?]

[e4]
[m4]
[p2b8]
[p2b8]
[s10]
[p4b2]
[p10]
[p10]
[p10]
[p10]

# -------------------------------------------------
# Inputs
# -------------------------------------------------

# Clock
[copy]
    input = G1.1
    output = _CLOCK

# Reset
[copy]
    input = G1.2
    output = _RESET

[quantizer]
    input = I5
    output = _PITCH_SEQUENCER_1

[quantizer]
    input = I6
    output = _PITCH_SEQUENCER_2

[quantizer]
    input = I7
    output = _PITCH_SEQUENCER_3

[quantizer]
    input = I8
    output = _PITCH_SEQUENCER_4

# -------------------------------------------------
# Modes
# -------------------------------------------------

[gatetool]
    inputtrigger = _CLOCK
    outputgate = _CLOCK_RUNNING
    gatelength = 2

[copy]
    input = _CLOCK_RUNNING * -1 + 1
    output = _CLOCK_STOPPED

# Create a clock that is running with maximum speed: two DROID cycles per oscillation.
# [copy]
#   input = _CYCLE_CLOCK * -1 + 1
#   output = _CYCLE_CLOCK

[copy]
    input = S5.1 * 1V - 2V # When the switch is set to 1 or more, the tuning octave ranges from 0V to 6V, so we need to substract 1V
    output = _TUNING_PITCH

[ifequal]
    input1 = S5.1
    input2 = 0
    ifequal = 0
    else = 1
    output = _TUNING_MODE

[copy]
    input = _TUNING_MODE * -1 + 1
    output = _NO_TUNING_MODE

# [mixer]
#   input1 = B1.1
#   input2 = B1.2
#   input3 = B1.3
#   input4 = B1.4
#   output = _OCTAVE_MODE_BUTTON

[ifequal]
    input1 = _OCTAVE_MODE + _SWITCH_MODE
    input2 = 0
    ifequal = 1
    output = _DIATONIC_MODE

[buttongroup]
    button1 = B6.1
    button2 = B6.2
    led1 = L6.1
    led2 = L6.2
    minactive = 0
    maxactive = 1
    buttonoutput1 = _OCTAVE_MODE
    buttonoutput2 = _SWITCH_MODE

# [button]
#   button = B6.1 + _OCTAVE_MODE_BUTTON
#   led = L6.1
#   output = _OCTAVE_SWITCHES
#   inverted = _NO_OCTAVE_SWITCHES

[copy]
    input = _NO_TUNING_MODE * _DIATONIC_MODE
    output = _ENCODER_MODE_DIATONIC_TRANS

[copy]
    input = _NO_TUNING_MODE * _OCTAVE_MODE
    output = _ENCODER_MODE_OCTAVES

[copy]
    input = _NO_TUNING_MODE * _SWITCH_MODE
    output = _ENCODER_MODE_SWITCHES

# -------------------------------------------------
# TB Controls
# -------------------------------------------------

[encoder]
    encoder = E1.1
    select = _ENCODER_MODE_OCTAVES
    clear = _TB_OCT_CLEARHERE
    startvalue = 2
    discrete = 5
    outputscale = 1V
    outputoffset = -2V
    color = 1.2
    button = _TB_OCT_CLEARHERE
    output = _TB_OCTAVE

[encoder]
    encoder = E1.1
    select = _ENCODER_MODE_DIATONIC_TRANS
    discrete = 15
    outputoffset = -7
    clear = _TB_DT_CLEARHERE
    color = 1.2
    button = _TB_DT_CLEARHERE
    startvalue = 7
    output = _TB_DIATONIC_TRANSPOSITION

[encoder]
    encoder = E1.1
    select = _ENCODER_MODE_SWITCHES
    discrete = 4
    outputoffset = 0
    clear = _TB_SW_CLEARHERE
    color = _TB_SWITCH_COLOR
    button = _TB_SW_CLEARHERE
    startvalue = 0
    output = _TB_SOURCE_SWITCH

[encoder]
    encoder = E1.1
    select = _TUNING_MODE
    movedup = _TB_CAL_UP
    moveddown = _TB_CAL_DOWN
    override = _TB_CAL_CORRECTION * 2
    button = _TB_CAL_CLEARHERE
    mode = 2
    color = 0.4
    negativecolor = 0.8

# LD Env mix
[copy]
    input = P6.1
    output = _TB_ENVELOPE_MIX

# LD ribbon mode
[copy]
    input = S5.4
    output = _TB_RIBBON_MODE

[copy]
    input = P7.1
    output = _TB_ATTACK_POT

[copy]
    input = P7.2
    output = _TB_RELEASE_POT

[copy]
    input = P7.3
    output = _TB_SUSTAIN_POT

[copy]
    input = P7.4
    output = _TB_LEVEL_POT

[copy]
    input = P7.5
    output = _TB_TRACKING_POT

[copy]
    input = P7.6
    output = _TB_SHAPE_POT

[copy]
    input = P7.7
    output = _TB_ACCENT_POT

[copy]
    input = P7.8
    output = _TB_RANDOM_LEVEL_POT

# LD delayed vibrato pot
[copy]
    input = P7.9
    output = _TB_DEL_VIB_POT

[copy]
    input = P7.10
    output = _TB_RANDOM_SPEED_POT

# -------------------------------------------------
# LD Controls
# -------------------------------------------------

[encoder]
    encoder = E1.2
    select = _ENCODER_MODE_OCTAVES
    clear = _LD_OCT_CLEARHERE
    startvalue = 2
    discrete = 5
    outputscale = 1V
    outputoffset = -2V
    color = 0.4
    button = _LD_OCT_CLEARHERE
    output = _LD_OCTAVE

[encoder]
    encoder = E1.2
    select = _ENCODER_MODE_DIATONIC_TRANS
    discrete = 15
    outputoffset = -7
    clear = _LD_DT_CLEARHERE
    color = 0.4
    button = _LD_DT_CLEARHERE
    startvalue = 7
    output = _LD_DIATONIC_TRANSPOSITION

[encoder]
    encoder = E1.2
    select = _ENCODER_MODE_SWITCHES
    discrete = 4
    outputoffset = 0
    clear = _LD_SW_CLEARHERE
    color = _LD_SWITCH_COLOR
    button = _LD_SW_CLEARHERE
    startvalue = 1
    output = _LD_SOURCE_SWITCH

[encoder]
    encoder = E1.2
    select = _TUNING_MODE
    movedup = _LD_CAL_UP
    moveddown = _LD_CAL_DOWN
    override = _LD_CAL_CORRECTION * 2
    button = _LD_CAL_CLEARHERE
    mode = 2
    color = 0.4
    negativecolor = 0.8

# LD Env mix
[copy]
    input = P6.2
    output = _LD_ENVELOPE_MIX

# LD ribbon mode
[copy]
    input = S5.6
    output = _LD_RIBBON_MODE

[copy]
    input = P8.1
    output = _LD_ATTACK_POT

[copy]
    input = P8.2
    output = _LD_RELEASE_POT

[copy]
    input = P8.3
    output = _LD_SUSTAIN_POT

[copy]
    input = P8.4
    output = _LD_LEVEL_POT

[copy]
    input = P8.5
    output = _LD_TRACKING_POT

[copy]
    input = P8.6
    output = _LD_SHAPE_POT

[copy]
    input = P8.7
    output = _LD_ACCENT_POT

[copy]
    input = P8.8
    output = _LD_RANDOM_LEVEL_POT

# LD delayed vibrato pot
[copy]
    input = P8.9
    output = _LD_DEL_VIB_POT

[copy]
    input = P8.10
    output = _LD_RANDOM_SPEED_POT

# -------------------------------------------------
# OR Controls
# -------------------------------------------------

[encoder]
    encoder = E1.3
    select = _ENCODER_MODE_OCTAVES
    clear = _OR_OCT_CLEARHERE
    startvalue = 2
    discrete = 5
    outputscale = 1V
    outputoffset = -2V
    color = 0.6
    button = _OR_OCT_CLEARHERE
    output = _OR_OCTAVE

[encoder]
    encoder = E1.3
    select = _ENCODER_MODE_DIATONIC_TRANS
    discrete = 15
    outputoffset = -7
    clear = _OR_DT_CLEARHERE
    color = 0.6
    button = _OR_DT_CLEARHERE
    startvalue = 7
    output = _OR_DIATONIC_TRANSPOSITION

[encoder]
    encoder = E1.3
    select = _ENCODER_MODE_SWITCHES
    discrete = 4
    outputoffset = 0
    clear = _OR_SW_CLEARHERE
    color = _OR_SWITCH_COLOR
    button = _OR_SW_CLEARHERE
    startvalue = 2
    output = _OR_SOURCE_SWITCH

[encoder]
    encoder = E1.3
    select = _TUNING_MODE
    movedup = _OR_CAL_UP
    moveddown = _OR_CAL_DOWN
    override = _OR_CAL_CORRECTION * 2
    button = _OR_CAL_CLEARHERE
    mode = 2
    color = 0.4
    negativecolor = 0.8

# LD Env mix
[copy]
    input = P6.3
    output = _OR_ENVELOPE_MIX

# LD ribbon mode
[copy]
    input = S5.8
    output = _OR_RIBBON_MODE

[copy]
    input = P9.1
    output = _OR_ATTACK_POT

[copy]
    input = P9.2
    output = _OR_RELEASE_POT

[copy]
    input = P9.3
    output = _OR_SUSTAIN_POT

[copy]
    input = P9.4
    output = _OR_LEVEL_POT

[copy]
    input = P9.5
    output = _OR_TRACKING_POT

[copy]
    input = P9.6
    output = _OR_SHAPE_POT

[copy]
    input = P9.7
    output = _OR_ACCENT_POT

[copy]
    input = P9.8
    output = _OR_RANDOM_LEVEL_POT

[copy]
    input = P9.9
    output = _OR_DEL_VIB_POT

[copy]
    input = P9.10
    output = _OR_RANDOM_SPEED_POT

# -------------------------------------------------
# AR Controls
# -------------------------------------------------

[encoder]
    encoder = E1.4
    select = _ENCODER_MODE_OCTAVES
    clear = _AR_OCT_CLEARHERE
    startvalue = 2
    discrete = 5
    outputscale = 1V
    outputoffset = -2V
    color = 0.8
    button = _AR_OCT_CLEARHERE
    output = _AR_OCTAVE

[encoder]
    encoder = E1.4
    select = _ENCODER_MODE_DIATONIC_TRANS
    discrete = 15
    outputoffset = -7
    clear = _AR_DT_CLEARHERE
    color = 0.8
    button = _AR_DT_CLEARHERE
    startvalue = 7
    output = _AR_DIATONIC_TRANSPOSITION

[encoder]
    encoder = E1.4
    select = _ENCODER_MODE_SWITCHES
    discrete = 4
    outputoffset = 0
    clear = _AR_SW_CLEARHERE
    color = _AR_SWITCH_COLOR
    button = _AR_SW_CLEARHERE
    startvalue = 3
    output = _AR_SOURCE_SWITCH

[encoder]
    encoder = E1.4
    select = _TUNING_MODE
    movedup = _AR_CAL_UP
    moveddown = _AR_CAL_DOWN
    override = _AR_CAL_CORRECTION * 2
    button = _AR_CAL_CLEARHERE
    mode = 2
    color = 0.4
    negativecolor = 0.8

# LD Env mix
[copy]
    input = P6.4
    output = _AR_ENVELOPE_MIX

# LD ribbon mode
[copy]
    input = S5.10
    output = _AR_RIBBON_MODE

[copy]
    input = P10.1
    output = _AR_ATTACK_POT

[copy]
    input = P10.2
    output = _AR_RELEASE_POT

[copy]
    input = P10.3
    output = _AR_SUSTAIN_POT

[copy]
    input = P10.4
    output = _AR_LEVEL_POT

[copy]
    input = P10.5
    output = _AR_TRACKING_POT

[copy]
    input = P10.6
    output = _AR_SHAPE_POT

[copy]
    input = P10.7
    output = _AR_ACCENT_POT

[copy]
    input = P10.8
    output = _AR_RANDOM_LEVEL_POT

[copy]
    input = P10.9
    output = _AR_DEL_VIB_POT

[copy]
    input = P10.10
    output = _AR_RANDOM_SPEED_POT

# -------------------------------------------------
# Vibrato / Tremolo
# -------------------------------------------------

[pot]
    pot = P3.1
    slope = 2
    output = _VIBRATO_SPEED_POT

[copy]
    input = P3.2
    output = _VIBRATO_DELAY

[lfo]
    hz = _VIBRATO_SPEED_POT * 100
    triangle = _VIBRATO_LFO
    bipolar = 1

# -------------------------------------------------
# Ribbon pressure + gate
# -------------------------------------------------

[copy]
    input = P4.2 * 1 + 0.3
    output = _RIBBON_PRESSURE_CALIBRATION

[math]
    input1 = I4 * _RIBBON_PRESSURE_CALIBRATION
    input2 = 0
    maximum = _RIBBON_PRESSURE_RAW

# Create a gate signal from the ribbon presure by comparing it with a fixed value
[compare]
    input = _RIBBON_PRESSURE_RAW
    compare = 0.02 # Set the threshold for detecting the gate here
    ifgreater = 1
    output = _RIBBON_GATE_BEFORE_LOOPER

# If the pressure is below the threshold, we force it to be 0, since without an active ribbon
# gate the pitch of the ribbon controller is undefined.
[copy]
    input = _RIBBON_PRESSURE_RAW * _RIBBON_GATE_BEFORE_LOOPER
    output = _RIBBON_PRESSURE

[copy]
    input = _RIBBON_GATE_BEFORE_LOOPER
    output = L2.4

[copy]
    input = _RIBBON_GATE * -1 + 1
    output = _RIBBON_GATE_CLOSED

[copy]
    input = _RIBBON_GATE * _CLOCK
    output = _RIBBON_PERM_GATE

# -------------------------------------------------
# Ribbon position / pitch
# -------------------------------------------------

# Output voltage range from DOEPFER ribbon controller:  0.013V .... 8.91V (at max position)
# (0.0013 ... 0.891). That makes the range 0.891 - 0.0013 = 0.8897. We scale this up to
# 1.0 by multiplying with 1.123974373384
[copy]
    input = I3 * 1.123974373384 - 0.0014611666853996
    output = _RIBBON_POS_NORMALIZED

[copy]
    input = _RIBBON_POS_NORMALIZED
    output = L2.3

[sample]
    input = _RIBBON_POS_NORMALIZED * 5V
    gate = _RIBBON_GATE_BEFORE_LOOPER
    output = _RIBBON_PITCH_INPUT

[copy]
    input = S5.2 * 1V - 2V
    output = _RIBBON_OFFSET

# -------------------------------------------------
# Ribbon looper
# -------------------------------------------------

# Select the length of the loop with a simple button group with three buttons.
# There are just 3 possible lengths: 1 bar, 2 bars, 4 bars. A bar is 16 clock ticks.
# Done.
[buttongroup]
    button1 = B4.2
    button2 = B4.4
    button3 = B4.6
    led1 = L4.2
    led2 = L4.4
    led3 = L4.6
    value1 = 16
    value2 = 32
    value3 = 64
    output = _LOOP_LENGTH

# Button for enabling overdub mode
[button]
    button = B4.8
    led = L4.8
    output = _RIBBON_LOOP_OVERDUB

# Derive bypass from the loop switch position
[ifequal]
    input1 = S5.9
    input2 = 1
    ifequal = 1
    else = 0
    output = _RIBBON_LOOP_BYPASS

# Derive the loop mode from the loop switch position
[ifequal]
    input1 = S5.9
    input2 = 0
    ifequal = 0
    else = 1
    output = _RIBBON_LOOP_SWITCH

# CV looper for the ribbon input (pitch) signal and the "Ribbon is hold down" signal. The ribbon pressure
# is done in a second looper.
[cvlooper]
    clock = _CLOCK
    reset = _RESET
    tapespeed = 0.2
    cvin = _RIBBON_PITCH_INPUT
    gatein = _RIBBON_GATE_BEFORE_LOOPER
    cvout = _RIBBON_PITCH_LOOPED
    gateout = _RIBBON_GATE
    loopswitch = _RIBBON_LOOP_SWITCH
    overlay = 1
    bypass = _RIBBON_LOOP_BYPASS + _CLOCK_STOPPED
    overdub = _RIBBON_LOOP_OVERDUB
    length = _LOOP_LENGTH

# CV looper for the ribbon pressure.
[cvlooper]
    clock = _CLOCK
    reset = _RESET
    tapespeed = 0.2
    cvin = _RIBBON_PRESSURE
    gatein = _RIBBON_GATE_BEFORE_LOOPER
    cvout = _RIBBON_PRESSURE_LOOPED
    loopswitch = _RIBBON_LOOP_SWITCH
    overlay = 1
    bypass = _RIBBON_LOOP_BYPASS + _CLOCK_STOPPED
    overdub = _RIBBON_LOOP_OVERDUB
    length = _LOOP_LENGTH

# -------------------------------------------------
# Ribbon quantizer
# -------------------------------------------------

[ifequal]
    input1 = S5.7
    input2 = 2
    ifequal = 1
    else = 0
    output = _UNQUANTIZED_MODE

[copy]
    input = S5.5 * 3 + S5.3
    output = _RIBBON_CLOCK_TIMING

# Clock multiplier for ribbon
[switch]
    input1 = 1
    input2 = 1
    input3 = 2
    input4 = 3
    input5 = 3
    input6 = 3
    input7 = 1
    input8 = 1
    input9 = 2
    offset = _RIBBON_CLOCK_TIMING
    output1 = _RIBBON_CLOCK_MULT

# Clock divider for ribbon
[switch]
    input1 = 2
    input2 = 1
    input3 = 1
    input4 = 4
    input5 = 2
    input6 = 1
    input7 = 6
    input8 = 3
    input9 = 3
    offset = _RIBBON_CLOCK_TIMING
    output1 = _RIBBON_CLOCK_DIV

[clocktool]
    clock = _CLOCK
    reset = _RESET
    output = _RIBBON_CLOCK_GATE
    multiply = _RIBBON_CLOCK_MULT
    divide = _RIBBON_CLOCK_DIV

[gatetool]
    inputgate = _RIBBON_CLOCK_GATE
    outputtrigger = _RIBBON_CLOCK

# [copy]
#   input = _RIBBON_CLOCK
#   output = G1.7

[buttongroup]
    button1 = B3.1
    button2 = B3.2
    button3 = B3.3
    button4 = B3.4
    button5 = B3.5
    button6 = B3.6
    button7 = B3.7
    button8 = B4.1
    button9 = B4.3
    button10 = B4.5
    button11 = B3.8
    button12 = B4.7
    led1 = L3.1
    led2 = L3.2
    led3 = L3.3
    led4 = L3.4
    led5 = L3.5
    led6 = L3.6
    led7 = L3.7
    led8 = L4.1
    led9 = L4.3
    led10 = L4.5
    led11 = L3.8
    led12 = L4.7
    buttonoutput1 = _SEL_ROOT
    buttonoutput2 = _SEL_3RD
    buttonoutput3 = _SEL_5TH
    buttonoutput4 = _SEL_7TH
    buttonoutput5 = _SEL_9TH
    buttonoutput6 = _SEL_11TH
    buttonoutput7 = _SEL_13TH
    buttonoutput8 = _SEL_FILL1
    buttonoutput9 = _SEL_FILL2
    buttonoutput10 = _SEL_FILL3
    buttonoutput11 = _SEL_FILL5
    buttonoutput12 = _SEL_FILL4
    minactive = 1
    maxactive = 12

[copy]
    input = I1 * 120
    output = _ROOT

[copy]
    input = I2 * 120
    output = _SCALE

[pot]
    pot = P4.1
    slope = 2
    output = _SLEW_POT

# -------------------------------------------------
# Ribbon pitch manual mod
# -------------------------------------------------

[motorfader]
    output = _PITCH_BEND_2_FADER
    fader = 3
    notches = 1

[pot]
    pot = _PITCH_BEND_2_FADER
    notch = 0.01
    bipolar = _PITCH_BEND_2

[motorfader]
    output = _PITCH_BEND_3_FADER
    fader = 4
    notches = 1

[pot]
    pot = _PITCH_BEND_3_FADER
    notch = 0.01
    bipolar = _PITCH_BEND_3

[motorfader]
    output = _MANUAL_VIBRATO_FADER
    fader = 1
    notches = 0

[pot]
    pot = _MANUAL_VIBRATO_FADER
    slope = 5
    outputscale = 0.05
    output = _MANUAL_VIBRATO

[mixer]
    input1 = _PITCH_BEND_2 * 0.16666666V
    input2 = _PITCH_BEND_3 * 0.25V
    output = _PITCH_BEND

# CV looper for the pitch bend faders.
[cvlooper]
    clock = _CLOCK
    reset = _RESET
    tapespeed = 0.2
    cvin = _PITCH_BEND
    gatein = _RIBBON_GATE_BEFORE_LOOPER
    cvout = _PITCH_BEND_LOOPED
    loopswitch = _RIBBON_LOOP_SWITCH
    overlay = 1
    bypass = _RIBBON_LOOP_BYPASS + _CLOCK_STOPPED
    overdub = _RIBBON_LOOP_OVERDUB
    length = _LOOP_LENGTH

[mixer]
    input1 = _PITCH_BEND_LOOPED
    input3 = _MANUAL_VIBRATO * _VIBRATO_LFO
    output = _MANUAL_PITCH_MOD

# -------------------------------------------------
# Tremolo
# -------------------------------------------------

[motorfader]
    output = _MANUAL_TREMOLO_FADER
    fader = 2
    notches = 0

[pot]
    pot = _MANUAL_TREMOLO_FADER
    slope = 1.5
    outputscale = _VIBRATO_LFO
    output = _MANUAL_TREMOLO

# -------------------------------------------------
# TB
# -------------------------------------------------

[switch]
    input1 = _PITCH_SEQUENCER_1
    input2 = _PITCH_SEQUENCER_2
    input3 = _PITCH_SEQUENCER_3
    input4 = _PITCH_SEQUENCER_4
    offset = _TB_SOURCE_SWITCH
    output1 = _TB_PITCH_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.1
    input2 = G2.2
    input3 = G2.3
    input4 = G2.4
    offset = _TB_SOURCE_SWITCH
    output1 = _TB_GATE_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.5
    input2 = G2.6
    input3 = G2.7
    input4 = G2.8
    offset = _TB_SOURCE_SWITCH
    output1 = _TB_ACCENT_IN

[switch]
    input1 = 1.2
    input2 = 0.4
    input3 = 0.6
    input4 = 0.8
    offset = _TB_SOURCE_SWITCH
    output1 = _TB_SWITCH_COLOR

[minifonion]
    bypass = _UNQUANTIZED_MODE
    degree = _SCALE
    input = _RIBBON_PITCH_LOOPED + _RIBBON_OFFSET
    root = _ROOT
    selectfill1 = _SEL_FILL1
    selectfill2 = _SEL_FILL2
    selectfill3 = _SEL_FILL3
    selectfill4 = _SEL_FILL4
    selectfill5 = _SEL_FILL5
    select1 = _SEL_ROOT
    select3 = _SEL_3RD
    select5 = _SEL_5TH
    select7 = _SEL_7TH
    select9 = _SEL_9TH
    select11 = _SEL_11TH
    select13 = _SEL_13TH
    trigger = _RIBBON_CLOCK
    selectnoteshift = _TB_DIATONIC_TRANSPOSITION
    notechange = _TB_RIBBON_AUTOGATE
    output = _TB_RIBBON_PITCH_QUANTIZED_UNSLEWED

[slew]
    input = _TB_RIBBON_PITCH_QUANTIZED_UNSLEWED
    slew = _SLEW_POT * 0.99
    exponential = _TB_RIBBON_PITCH_QUANTIZED

[ifequal]
    input1 = S5.7
    input2 = 1
    ifequal = _RIBBON_CLOCK * _RIBBON_GATE
    else = _TB_RIBBON_AUTOGATE
    output = _TB_RIBBON_TRIGGER

# State of "which if seq/ribbon" is active in mode "ribbon first"
[flipflop]
    set = _RIBBON_PERM_GATE
    reset = _TB_GATE_SEQUENCER * _RIBBON_GATE_CLOSED
    output = _TB_RIBBON_SELECTED_SW2

[copy]
    input = _TB_RIBBON_SELECTED_SW2
    output = L2.2

[logic]
    input1 = _TB_GATE_SEQUENCER
    negated = _TB_GATE_CLOSED

# State of "which if seq/ribbon" is active in mode "sequencer first"
[flipflop]
    set = _RIBBON_PERM_GATE * _TB_GATE_CLOSED
    reset = _TB_GATE_SEQUENCER
    output = _TB_RIBBON_SELECTED_SW1

[logic]
    input1 = _TB_RIBBON_SELECTED_SW1
    negated = _TB_SEQ_SELECTED_SW1

[ifequal]
    input1 = _TB_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _TB_RIBBON_PITCH
    else = _TB_PITCH_SEQUENCER
    output = _TB_PITCH_WITH_RIBBON_PREC

[ifequal]
    input1 = _TB_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _TB_RIBBON_TRIGGER
    else = _TB_GATE_SEQUENCER
    output = _TB_GATE_WITH_RIBBON_PREC

[ifequal]
    input1 = _TB_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _TB_RIBBON_ENV_OUTPUT
    else = _TB_CONTOUR
    output = _TB_ENV_WITH_RIBBON_PREC

[switch]
    input1 = _TB_RIBBON_PITCH
    input2 = _TB_PITCH_SEQUENCER
    offset = _TB_SEQ_SELECTED_SW1
    output1 = _TB_PITCH_WITH_SEQ_PREC

[switch]
    input1 = _TB_RIBBON_TRIGGER
    input2 = _TB_GATE_SEQUENCER
    offset = _TB_SEQ_SELECTED_SW1
    output1 = _TB_GATE_WITH_SEQ_PREC

[switch]
    input1 = _TB_RIBBON_ENV_OUTPUT
    input2 = _TB_CONTOUR
    offset = _TB_SEQ_SELECTED_SW1
    output1 = _TB_ENV_WITH_SEQ_PREC

# Select which pitch to use (from ribbon or from sequencer)
[switch]
    input1 = _TB_PITCH_SEQUENCER
    input2 = _TB_PITCH_WITH_SEQ_PREC
    input3 = _TB_PITCH_WITH_RIBBON_PREC
    offset = _TB_RIBBON_MODE
    output1 = _TB_PITCH_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _TB_GATE_SEQUENCER
    input2 = _TB_GATE_WITH_SEQ_PREC
    input3 = _TB_GATE_WITH_RIBBON_PREC
    offset = _TB_RIBBON_MODE
    output1 = _TB_GATE_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _TB_CONTOUR
    input2 = _TB_ENV_WITH_SEQ_PREC
    input3 = _TB_ENV_WITH_RIBBON_PREC
    offset = _TB_RIBBON_MODE
    output1 = _TB_ENV_SWITCHED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = 0.8
    output = _TB_ENV_OUTPUT
    else = _TB_ENV_SWITCHED

# Delayed vibrato
[contour]
    trigger = _TB_GATE_OUTPUT
    shape = 1
    release = _VIBRATO_DELAY * 1.3
    hold = _VIBRATO_DELAY * 1.5
    inverted = _TB_VIBRATO_LEVEL
    level = _TB_DEL_VIB_POT * 0.005

[copy]
    input = _VIBRATO_LFO * _TB_VIBRATO_LEVEL
    output = _TB_VIBRATO

[copy]
    input = _TB_PITCH_SWITCHED + _TB_VIBRATO
    output = _TB_PITCH_VIBRATED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = _TUNING_PITCH
    else = _TB_PITCH_VIBRATED + _TB_OCTAVE
    output = _TB_UNCAL_PITCH

[copy]
    input = _TB_RIBBON_PITCH_QUANTIZED + _MANUAL_PITCH_MOD
    output = _TB_RIBBON_PITCH

[calibrator]
    input = _TB_UNCAL_PITCH
    nudgeup = _TB_CAL_UP
    nudgedown = _TB_CAL_DOWN
    correction = _TB_CAL_CORRECTION
    output = _TB_PITCH_OUTPUT
    nudgeamount = 0.01
    clearhere = _TB_CAL_CLEARHERE

[copy]
    input = _TB_GATE_SWITCHED
    output = _TB_GATE_OUTPUT

# Get pitch for envelope pitch tracking
[copy]
    input = _TB_PITCH_OUTPUT * 10 - 2
    output = _TB_ENVELOPE_PITCH

[math]
    input1 = 0.5
    input2 = _TB_ENVELOPE_PITCH * _TB_TRACKING_POT
    power = _TB_TRACKING_FACTOR

[copy]
    input = 0 * _VIBRATO_LFO + 1
    output = _TB_TREMOLO_FACTOR

[random]
    clock = _TB_GATE_OUTPUT
    maximum = _TB_RANDOM_LEVEL_POT * 0.5 + 1
    minimum = _TB_RANDOM_LEVEL_POT * -0.5 + 1
    output = _TB_ENV_RANDOM_LEVEL

[ifequal]
    input1 = _TB_ACCENT_IN
    input2 = 0
    ifequal = _TB_ACCENT_POT * -0.4 + 1
    else = _TB_ACCENT_POT * 0.4 + 1
    output = _TB_ACCENT_IMPACT

[slew]
    input = _TB_ACCENT_IMPACT * _TB_ENV_RANDOM_LEVEL
    slew = 0.05
    exponential = _TB_VARIABLE_LEVEL

[random]
    output = _TB_ENV_RANDOM_SPEED
    clock = _TB_GATE_OUTPUT
    maximum = _TB_RANDOM_SPEED_POT * 0.8 + 1
    minimum = _TB_RANDOM_SPEED_POT * -0.8 + 1
    steps = 2

[copy]
    input = _TB_ENV_RANDOM_SPEED * _TB_TRACKING_FACTOR
    output = _TB_ENV_SPEED_MOD

[copy]
    input = _TB_RELEASE_POT * _TB_RELEASE_POT + 0.02
    output = _TB_RELEASE

# Envelope für VCA and VCF
[contour]
    attack = _TB_ATTACK_POT + 0.02
    decay = _TB_RELEASE * _TB_ENV_SPEED_MOD
    release = _TB_RELEASE * _TB_ENV_SPEED_MOD
    sustain = _TB_SUSTAIN_POT
    level = _TB_LEVEL_POT * _TB_VARIABLE_LEVEL
    gate = _TB_GATE_OUTPUT
    output = _TB_ENVELOPE_UNTREMOLOED
    decayshape = _TB_SHAPE_POT * -1 + 1
    releaseshape = _TB_SHAPE_POT * -1 + 1

[copy]
    input = _TB_ENVELOPE_UNTREMOLOED * _TB_TREMOLO_FACTOR
    output = _TB_CONTOUR

[pot]
    pot = _TB_ENVELOPE_MIX
    slope = 0.5
    output = _TB_ENVMIX_RIGHT

[pot]
    pot = _TB_ENVELOPE_MIX * -1 + 1
    slope = 0.5
    output = _TB_ENVMIX_LEFT

[copy]
    input = _RIBBON_PRESSURE_LOOPED * _TB_TREMOLO_FACTOR
    output = _TB_RIBBON_ENV

[mixer]
    input1 = _TB_RIBBON_ENV * _TB_ENVMIX_LEFT
    input2 = _TB_CONTOUR * _TB_ENVMIX_RIGHT
    output = _TB_ENVMIX

# [crossfader]
#   input1 = _RIBBON_PRESSURE_LOOPED * _TB_TREMOLO_FACTOR
#   input2 = _TB_CONTOUR
#   fade = _TB_ENVELOPE_MIX
#   output = _TB_ENVMIX

[mixer]
    input1 = _TB_ENVMIX
    input3 = _MANUAL_TREMOLO
    output = _TB_RIBBON_ENV_OUTPUT

# -------------------------------------------------
# LD
# -------------------------------------------------

[switch]
    input1 = _PITCH_SEQUENCER_1
    input2 = _PITCH_SEQUENCER_2
    input3 = _PITCH_SEQUENCER_3
    input4 = _PITCH_SEQUENCER_4
    offset = _LD_SOURCE_SWITCH
    output1 = _LD_PITCH_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.1
    input2 = G2.2
    input3 = G2.3
    input4 = G2.4
    offset = _LD_SOURCE_SWITCH
    output1 = _LD_GATE_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.5
    input2 = G2.6
    input3 = G2.7
    input4 = G2.8
    offset = _LD_SOURCE_SWITCH
    output1 = _LD_ACCENT_IN

[switch]
    input1 = 1.2
    input2 = 0.4
    input3 = 0.6
    input4 = 0.8
    offset = _LD_SOURCE_SWITCH
    output1 = _LD_SWITCH_COLOR

[minifonion]
    bypass = _UNQUANTIZED_MODE
    degree = _SCALE
    input = _RIBBON_PITCH_LOOPED + _RIBBON_OFFSET
    root = _ROOT
    selectfill1 = _SEL_FILL1
    selectfill2 = _SEL_FILL2
    selectfill3 = _SEL_FILL3
    selectfill4 = _SEL_FILL4
    selectfill5 = _SEL_FILL5
    select1 = _SEL_ROOT
    select3 = _SEL_3RD
    select5 = _SEL_5TH
    select7 = _SEL_7TH
    select9 = _SEL_9TH
    select11 = _SEL_11TH
    select13 = _SEL_13TH
    trigger = _RIBBON_CLOCK
    selectnoteshift = _LD_DIATONIC_TRANSPOSITION
    notechange = _LD_RIBBON_AUTOGATE
    output = _LD_RIBBON_PITCH_QUANTIZED_UNSLEWED

[slew]
    input = _LD_RIBBON_PITCH_QUANTIZED_UNSLEWED
    slew = _SLEW_POT * 0.99
    exponential = _LD_RIBBON_PITCH_QUANTIZED

[ifequal]
    input1 = S5.7
    input2 = 1
    ifequal = _RIBBON_CLOCK * _RIBBON_GATE
    else = _LD_RIBBON_AUTOGATE
    output = _LD_RIBBON_TRIGGER

# State of "which if seq/ribbon" is active in mode "ribbon first"
[flipflop]
    set = _RIBBON_PERM_GATE
    reset = _LD_GATE_SEQUENCER * _RIBBON_GATE_CLOSED
    output = _LD_RIBBON_SELECTED_SW2

[copy]
    input = _LD_RIBBON_SELECTED_SW2
    output = L2.2

[logic]
    input1 = _LD_GATE_SEQUENCER
    negated = _LD_GATE_CLOSED

# State of "which if seq/ribbon" is active in mode "sequencer first"
[flipflop]
    set = _RIBBON_PERM_GATE * _LD_GATE_CLOSED
    reset = _LD_GATE_SEQUENCER
    output = _LD_RIBBON_SELECTED_SW1

[logic]
    input1 = _LD_RIBBON_SELECTED_SW1
    negated = _LD_SEQ_SELECTED_SW1

[ifequal]
    input1 = _LD_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _LD_RIBBON_PITCH
    else = _LD_PITCH_SEQUENCER
    output = _LD_PITCH_WITH_RIBBON_PREC

[ifequal]
    input1 = _LD_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _LD_RIBBON_TRIGGER
    else = _LD_GATE_SEQUENCER
    output = _LD_GATE_WITH_RIBBON_PREC

[ifequal]
    input1 = _LD_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _LD_RIBBON_ENV_OUTPUT
    else = _LD_CONTOUR
    output = _LD_ENV_WITH_RIBBON_PREC

[switch]
    input1 = _LD_RIBBON_PITCH
    input2 = _LD_PITCH_SEQUENCER
    offset = _LD_SEQ_SELECTED_SW1
    output1 = _LD_PITCH_WITH_SEQ_PREC

[switch]
    input1 = _LD_RIBBON_TRIGGER
    input2 = _LD_GATE_SEQUENCER
    offset = _LD_SEQ_SELECTED_SW1
    output1 = _LD_GATE_WITH_SEQ_PREC

[switch]
    input1 = _LD_RIBBON_ENV_OUTPUT
    input2 = _LD_CONTOUR
    offset = _LD_SEQ_SELECTED_SW1
    output1 = _LD_ENV_WITH_SEQ_PREC

# Select which pitch to use (from ribbon or from sequencer)
[switch]
    input1 = _LD_PITCH_SEQUENCER
    input2 = _LD_PITCH_WITH_SEQ_PREC
    input3 = _LD_PITCH_WITH_RIBBON_PREC
    offset = _LD_RIBBON_MODE
    output1 = _LD_PITCH_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _LD_GATE_SEQUENCER
    input2 = _LD_GATE_WITH_SEQ_PREC
    input3 = _LD_GATE_WITH_RIBBON_PREC
    offset = _LD_RIBBON_MODE
    output1 = _LD_GATE_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _LD_CONTOUR
    input2 = _LD_ENV_WITH_SEQ_PREC
    input3 = _LD_ENV_WITH_RIBBON_PREC
    offset = _LD_RIBBON_MODE
    output1 = _LD_ENV_SWITCHED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = 0.8
    output = _LD_ENV_OUTPUT
    else = _LD_ENV_SWITCHED

# Delayed vibrato
[contour]
    trigger = _LD_GATE_OUTPUT
    shape = 1
    release = _VIBRATO_DELAY * 1.3
    hold = _VIBRATO_DELAY * 1.5
    inverted = _LD_VIBRATO_LEVEL
    level = _LD_DEL_VIB_POT * 0.005

[copy]
    input = _VIBRATO_LFO * _LD_VIBRATO_LEVEL
    output = _LD_VIBRATO

[copy]
    input = _LD_PITCH_SWITCHED + _LD_VIBRATO
    output = _LD_PITCH_VIBRATED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = _TUNING_PITCH
    else = _LD_PITCH_VIBRATED + _LD_OCTAVE
    output = _LD_UNCAL_PITCH

[copy]
    input = _LD_RIBBON_PITCH_QUANTIZED + _MANUAL_PITCH_MOD
    output = _LD_RIBBON_PITCH

[calibrator]
    input = _LD_UNCAL_PITCH
    nudgeup = _LD_CAL_UP
    nudgedown = _LD_CAL_DOWN
    correction = _LD_CAL_CORRECTION
    output = _LD_PITCH_OUTPUT
    nudgeamount = 0.01
    clearhere = _LD_CAL_CLEARHERE

[copy]
    input = _LD_GATE_SWITCHED
    output = _LD_GATE_OUTPUT

# Get pitch for envelope pitch tracking
[copy]
    input = _LD_PITCH_OUTPUT * 10 - 2
    output = _LD_ENVELOPE_PITCH

[math]
    input1 = 0.5
    input2 = _LD_ENVELOPE_PITCH * _LD_TRACKING_POT
    power = _LD_TRACKING_FACTOR

[copy]
    input = 0 * _VIBRATO_LFO + 1
    output = _LD_TREMOLO_FACTOR

[random]
    clock = _LD_GATE_OUTPUT
    maximum = _LD_RANDOM_LEVEL_POT * 0.5 + 1
    minimum = _LD_RANDOM_LEVEL_POT * -0.5 + 1
    output = _LD_ENV_RANDOM_LEVEL

[ifequal]
    input1 = _LD_ACCENT_IN
    input2 = 0
    ifequal = _LD_ACCENT_POT * -0.4 + 1
    else = _LD_ACCENT_POT * 0.4 + 1
    output = _LD_ACCENT_IMPACT

[slew]
    input = _LD_ACCENT_IMPACT * _LD_ENV_RANDOM_LEVEL
    slew = 0.05
    exponential = _LD_VARIABLE_LEVEL

[random]
    output = _LD_ENV_RANDOM_SPEED
    clock = _LD_GATE_OUTPUT
    maximum = _LD_RANDOM_SPEED_POT * 0.8 + 1
    minimum = _LD_RANDOM_SPEED_POT * -0.8 + 1
    steps = 2

[copy]
    input = _LD_ENV_RANDOM_SPEED * _LD_TRACKING_FACTOR
    output = _LD_ENV_SPEED_MOD

[copy]
    input = _LD_RELEASE_POT * _LD_RELEASE_POT + 0.02
    output = _LD_RELEASE

# Envelope für VCA and VCF
[contour]
    attack = _LD_ATTACK_POT + 0.02
    decay = _LD_RELEASE * _LD_ENV_SPEED_MOD
    release = _LD_RELEASE * _LD_ENV_SPEED_MOD
    sustain = _LD_SUSTAIN_POT
    level = _LD_LEVEL_POT * _LD_VARIABLE_LEVEL
    gate = _LD_GATE_OUTPUT
    output = _LD_ENVELOPE_UNTREMOLOED
    decayshape = _LD_SHAPE_POT * -1 + 1
    releaseshape = _LD_SHAPE_POT * -1 + 1

[copy]
    input = _LD_ENVELOPE_UNTREMOLOED * _LD_TREMOLO_FACTOR
    output = _LD_CONTOUR

[pot]
    pot = _LD_ENVELOPE_MIX
    slope = 0.5
    output = _LD_ENVMIX_RIGHT

[pot]
    pot = _LD_ENVELOPE_MIX * -1 + 1
    slope = 0.5
    output = _LD_ENVMIX_LEFT

[copy]
    input = _RIBBON_PRESSURE_LOOPED * _LD_TREMOLO_FACTOR
    output = _LD_RIBBON_ENV

[mixer]
    input1 = _LD_RIBBON_ENV * _LD_ENVMIX_LEFT
    input2 = _LD_CONTOUR * _LD_ENVMIX_RIGHT
    output = _LD_ENVMIX

# [crossfader]
#   input1 = _RIBBON_PRESSURE_LOOPED * _LD_TREMOLO_FACTOR
#   input2 = _LD_CONTOUR
#   fade = _LD_ENVELOPE_MIX
#   output = _LD_ENVMIX

[mixer]
    input1 = _LD_ENVMIX
    input3 = _MANUAL_TREMOLO
    output = _LD_RIBBON_ENV_OUTPUT

# -------------------------------------------------
# OR
# -------------------------------------------------

[switch]
    input1 = _PITCH_SEQUENCER_1
    input2 = _PITCH_SEQUENCER_2
    input3 = _PITCH_SEQUENCER_3
    input4 = _PITCH_SEQUENCER_4
    offset = _OR_SOURCE_SWITCH
    output1 = _OR_PITCH_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.1
    input2 = G2.2
    input3 = G2.3
    input4 = G2.4
    offset = _OR_SOURCE_SWITCH
    output1 = _OR_GATE_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.5
    input2 = G2.6
    input3 = G2.7
    input4 = G2.8
    offset = _OR_SOURCE_SWITCH
    output1 = _OR_ACCENT_IN

[switch]
    input1 = 1.2
    input2 = 0.4
    input3 = 0.6
    input4 = 0.8
    offset = _OR_SOURCE_SWITCH
    output1 = _OR_SWITCH_COLOR

[minifonion]
    bypass = _UNQUANTIZED_MODE
    degree = _SCALE
    input = _RIBBON_PITCH_LOOPED + _RIBBON_OFFSET
    root = _ROOT
    selectfill1 = _SEL_FILL1
    selectfill2 = _SEL_FILL2
    selectfill3 = _SEL_FILL3
    selectfill4 = _SEL_FILL4
    selectfill5 = _SEL_FILL5
    select1 = _SEL_ROOT
    select3 = _SEL_3RD
    select5 = _SEL_5TH
    select7 = _SEL_7TH
    select9 = _SEL_9TH
    select11 = _SEL_11TH
    select13 = _SEL_13TH
    trigger = _RIBBON_CLOCK
    selectnoteshift = _OR_DIATONIC_TRANSPOSITION
    notechange = _OR_RIBBON_AUTOGATE
    output = _OR_RIBBON_PITCH_QUANTIZED_UNSLEWED

[slew]
    input = _OR_RIBBON_PITCH_QUANTIZED_UNSLEWED
    slew = _SLEW_POT * 0.99
    exponential = _OR_RIBBON_PITCH_QUANTIZED

[ifequal]
    input1 = S5.7
    input2 = 1
    ifequal = _RIBBON_CLOCK * _RIBBON_GATE
    else = _OR_RIBBON_AUTOGATE
    output = _OR_RIBBON_TRIGGER

# State of "which if seq/ribbon" is active in mode "ribbon first"
[flipflop]
    set = _RIBBON_PERM_GATE
    reset = _OR_GATE_SEQUENCER * _RIBBON_GATE_CLOSED
    output = _OR_RIBBON_SELECTED_SW2

[copy]
    input = _OR_RIBBON_SELECTED_SW2
    output = L2.2

[logic]
    input1 = _OR_GATE_SEQUENCER
    negated = _OR_GATE_CLOSED

# State of "which if seq/ribbon" is active in mode "sequencer first"
[flipflop]
    set = _RIBBON_PERM_GATE * _OR_GATE_CLOSED
    reset = _OR_GATE_SEQUENCER
    output = _OR_RIBBON_SELECTED_SW1

[logic]
    input1 = _OR_RIBBON_SELECTED_SW1
    negated = _OR_SEQ_SELECTED_SW1

[ifequal]
    input1 = _OR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _OR_RIBBON_PITCH
    else = _OR_PITCH_SEQUENCER
    output = _OR_PITCH_WITH_RIBBON_PREC

[ifequal]
    input1 = _OR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _OR_RIBBON_TRIGGER
    else = _OR_GATE_SEQUENCER
    output = _OR_GATE_WITH_RIBBON_PREC

[ifequal]
    input1 = _OR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _OR_RIBBON_ENV_OUTPUT
    else = _OR_CONTOUR
    output = _OR_ENV_WITH_RIBBON_PREC

[switch]
    input1 = _OR_RIBBON_PITCH
    input2 = _OR_PITCH_SEQUENCER
    offset = _OR_SEQ_SELECTED_SW1
    output1 = _OR_PITCH_WITH_SEQ_PREC

[switch]
    input1 = _OR_RIBBON_TRIGGER
    input2 = _OR_GATE_SEQUENCER
    offset = _OR_SEQ_SELECTED_SW1
    output1 = _OR_GATE_WITH_SEQ_PREC

[switch]
    input1 = _OR_RIBBON_ENV_OUTPUT
    input2 = _OR_CONTOUR
    offset = _OR_SEQ_SELECTED_SW1
    output1 = _OR_ENV_WITH_SEQ_PREC

# Select which pitch to use (from ribbon or from sequencer)
[switch]
    input1 = _OR_PITCH_SEQUENCER
    input2 = _OR_PITCH_WITH_SEQ_PREC
    input3 = _OR_PITCH_WITH_RIBBON_PREC
    offset = _OR_RIBBON_MODE
    output1 = _OR_PITCH_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _OR_GATE_SEQUENCER
    input2 = _OR_GATE_WITH_SEQ_PREC
    input3 = _OR_GATE_WITH_RIBBON_PREC
    offset = _OR_RIBBON_MODE
    output1 = _OR_GATE_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _OR_CONTOUR
    input2 = _OR_ENV_WITH_SEQ_PREC
    input3 = _OR_ENV_WITH_RIBBON_PREC
    offset = _OR_RIBBON_MODE
    output1 = _OR_ENV_SWITCHED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = 0.8
    output = _OR_ENV_OUTPUT
    else = _OR_ENV_SWITCHED

# Delayed vibrato
[contour]
    trigger = _OR_GATE_OUTPUT
    shape = 1
    release = _VIBRATO_DELAY * 1.3
    hold = _VIBRATO_DELAY * 1.5
    inverted = _OR_VIBRATO_LEVEL
    level = _OR_DEL_VIB_POT * 0.005

[copy]
    input = _VIBRATO_LFO * _OR_VIBRATO_LEVEL
    output = _OR_VIBRATO

[copy]
    input = _OR_PITCH_SWITCHED + _OR_VIBRATO
    output = _OR_PITCH_VIBRATED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = _TUNING_PITCH
    else = _OR_PITCH_VIBRATED + _OR_OCTAVE
    output = _OR_UNCAL_PITCH

[copy]
    input = _OR_RIBBON_PITCH_QUANTIZED + _MANUAL_PITCH_MOD
    output = _OR_RIBBON_PITCH

[calibrator]
    input = _OR_UNCAL_PITCH
    nudgeup = _OR_CAL_UP
    nudgedown = _OR_CAL_DOWN
    correction = _OR_CAL_CORRECTION
    output = _OR_PITCH_OUTPUT
    nudgeamount = 0.01
    clearhere = _OR_CAL_CLEARHERE

[copy]
    input = _OR_GATE_SWITCHED
    output = _OR_GATE_OUTPUT

# Get pitch for envelope pitch tracking
[copy]
    input = _OR_PITCH_OUTPUT * 10 - 2
    output = _OR_ENVELOPE_PITCH

[math]
    input1 = 0.5
    input2 = _OR_ENVELOPE_PITCH * _OR_TRACKING_POT
    power = _OR_TRACKING_FACTOR

[copy]
    input = 0 * _VIBRATO_LFO + 1
    output = _OR_TREMOLO_FACTOR

[random]
    clock = _OR_GATE_OUTPUT
    maximum = _OR_RANDOM_LEVEL_POT * 0.5 + 1
    minimum = _OR_RANDOM_LEVEL_POT * -0.5 + 1
    output = _OR_ENV_RANDOM_LEVEL

[ifequal]
    input1 = _OR_ACCENT_IN
    input2 = 0
    ifequal = _OR_ACCENT_POT * -0.4 + 1
    else = _OR_ACCENT_POT * 0.4 + 1
    output = _OR_ACCENT_IMPACT

[slew]
    input = _OR_ACCENT_IMPACT * _OR_ENV_RANDOM_LEVEL
    slew = 0.05
    exponential = _OR_VARIABLE_LEVEL

[random]
    output = _OR_ENV_RANDOM_SPEED
    clock = _OR_GATE_OUTPUT
    maximum = _OR_RANDOM_SPEED_POT * 0.8 + 1
    minimum = _OR_RANDOM_SPEED_POT * -0.8 + 1
    steps = 2

[copy]
    input = _OR_ENV_RANDOM_SPEED * _OR_TRACKING_FACTOR
    output = _OR_ENV_SPEED_MOD

[copy]
    input = _OR_RELEASE_POT * _OR_RELEASE_POT + 0.02
    output = _OR_RELEASE

# Envelope für VCA and VCF
[contour]
    attack = _OR_ATTACK_POT + 0.02
    decay = _OR_RELEASE * _OR_ENV_SPEED_MOD
    release = _OR_RELEASE * _OR_ENV_SPEED_MOD
    sustain = _OR_SUSTAIN_POT
    level = _OR_LEVEL_POT * _OR_VARIABLE_LEVEL
    gate = _OR_GATE_OUTPUT
    output = _OR_ENVELOPE_UNTREMOLOED
    decayshape = _OR_SHAPE_POT * -1 + 1
    releaseshape = _OR_SHAPE_POT * -1 + 1

[copy]
    input = _OR_ENVELOPE_UNTREMOLOED * _OR_TREMOLO_FACTOR
    output = _OR_CONTOUR

[pot]
    pot = _OR_ENVELOPE_MIX
    slope = 0.5
    output = _OR_ENVMIX_RIGHT

[pot]
    pot = _OR_ENVELOPE_MIX * -1 + 1
    slope = 0.5
    output = _OR_ENVMIX_LEFT

[copy]
    input = _RIBBON_PRESSURE_LOOPED * _OR_TREMOLO_FACTOR
    output = _OR_RIBBON_ENV

[mixer]
    input1 = _OR_RIBBON_ENV * _OR_ENVMIX_LEFT
    input2 = _OR_CONTOUR * _OR_ENVMIX_RIGHT
    output = _OR_ENVMIX

# [crossfader]
#   input1 = _RIBBON_PRESSURE_LOOPED * _OR_TREMOLO_FACTOR
#   input2 = _OR_CONTOUR
#   fade = _OR_ENVELOPE_MIX
#   output = _OR_ENVMIX

[mixer]
    input1 = _OR_ENVMIX
    input3 = _MANUAL_TREMOLO
    output = _OR_RIBBON_ENV_OUTPUT

# -------------------------------------------------
# AR
# -------------------------------------------------

[switch]
    input1 = _PITCH_SEQUENCER_1
    input2 = _PITCH_SEQUENCER_2
    input3 = _PITCH_SEQUENCER_3
    input4 = _PITCH_SEQUENCER_4
    offset = _AR_SOURCE_SWITCH
    output1 = _AR_PITCH_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.1
    input2 = G2.2
    input3 = G2.3
    input4 = G2.4
    offset = _AR_SOURCE_SWITCH
    output1 = _AR_GATE_SEQUENCER

# Move this to the TB/LD/OR/AR sections
[switch]
    input1 = G2.5
    input2 = G2.6
    input3 = G2.7
    input4 = G2.8
    offset = _AR_SOURCE_SWITCH
    output1 = _AR_ACCENT_IN

[switch]
    input1 = 1.2
    input2 = 0.4
    input3 = 0.6
    input4 = 0.8
    offset = _AR_SOURCE_SWITCH
    output1 = _AR_SWITCH_COLOR

[minifonion]
    bypass = _UNQUANTIZED_MODE
    degree = _SCALE
    input = _RIBBON_PITCH_LOOPED + _RIBBON_OFFSET
    root = _ROOT
    selectfill1 = _SEL_FILL1
    selectfill2 = _SEL_FILL2
    selectfill3 = _SEL_FILL3
    selectfill4 = _SEL_FILL4
    selectfill5 = _SEL_FILL5
    select1 = _SEL_ROOT
    select3 = _SEL_3RD
    select5 = _SEL_5TH
    select7 = _SEL_7TH
    select9 = _SEL_9TH
    select11 = _SEL_11TH
    select13 = _SEL_13TH
    trigger = _RIBBON_CLOCK
    selectnoteshift = _AR_DIATONIC_TRANSPOSITION
    notechange = _AR_RIBBON_AUTOGATE
    output = _AR_RIBBON_PITCH_QUANTIZED_UNSLEWED

[slew]
    input = _AR_RIBBON_PITCH_QUANTIZED_UNSLEWED
    slew = _SLEW_POT * 0.99
    exponential = _AR_RIBBON_PITCH_QUANTIZED

[ifequal]
    input1 = S5.7
    input2 = 1
    ifequal = _RIBBON_CLOCK * _RIBBON_GATE
    else = _AR_RIBBON_AUTOGATE
    output = _AR_RIBBON_TRIGGER

# State of "which if seq/ribbon" is active in mode "ribbon first"
[flipflop]
    set = _RIBBON_PERM_GATE
    reset = _AR_GATE_SEQUENCER * _RIBBON_GATE_CLOSED
    output = _AR_RIBBON_SELECTED_SW2

[copy]
    input = _AR_RIBBON_SELECTED_SW2
    output = L2.2

[logic]
    input1 = _AR_GATE_SEQUENCER
    negated = _AR_GATE_CLOSED

# State of "which if seq/ribbon" is active in mode "sequencer first"
[flipflop]
    set = _RIBBON_PERM_GATE * _AR_GATE_CLOSED
    reset = _AR_GATE_SEQUENCER
    output = _AR_RIBBON_SELECTED_SW1

[logic]
    input1 = _AR_RIBBON_SELECTED_SW1
    negated = _AR_SEQ_SELECTED_SW1

[ifequal]
    input1 = _AR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _AR_RIBBON_PITCH
    else = _AR_PITCH_SEQUENCER
    output = _AR_PITCH_WITH_RIBBON_PREC

[ifequal]
    input1 = _AR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _AR_RIBBON_TRIGGER
    else = _AR_GATE_SEQUENCER
    output = _AR_GATE_WITH_RIBBON_PREC

[ifequal]
    input1 = _AR_RIBBON_SELECTED_SW2
    input2 = 1
    ifequal = _AR_RIBBON_ENV_OUTPUT
    else = _AR_CONTOUR
    output = _AR_ENV_WITH_RIBBON_PREC

[switch]
    input1 = _AR_RIBBON_PITCH
    input2 = _AR_PITCH_SEQUENCER
    offset = _AR_SEQ_SELECTED_SW1
    output1 = _AR_PITCH_WITH_SEQ_PREC

[switch]
    input1 = _AR_RIBBON_TRIGGER
    input2 = _AR_GATE_SEQUENCER
    offset = _AR_SEQ_SELECTED_SW1
    output1 = _AR_GATE_WITH_SEQ_PREC

[switch]
    input1 = _AR_RIBBON_ENV_OUTPUT
    input2 = _AR_CONTOUR
    offset = _AR_SEQ_SELECTED_SW1
    output1 = _AR_ENV_WITH_SEQ_PREC

# Select which pitch to use (from ribbon or from sequencer)
[switch]
    input1 = _AR_PITCH_SEQUENCER
    input2 = _AR_PITCH_WITH_SEQ_PREC
    input3 = _AR_PITCH_WITH_RIBBON_PREC
    offset = _AR_RIBBON_MODE
    output1 = _AR_PITCH_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _AR_GATE_SEQUENCER
    input2 = _AR_GATE_WITH_SEQ_PREC
    input3 = _AR_GATE_WITH_RIBBON_PREC
    offset = _AR_RIBBON_MODE
    output1 = _AR_GATE_SWITCHED

# Select which gate to use (from quantized ribbon or from sequencer)
[switch]
    input1 = _AR_CONTOUR
    input2 = _AR_ENV_WITH_SEQ_PREC
    input3 = _AR_ENV_WITH_RIBBON_PREC
    offset = _AR_RIBBON_MODE
    output1 = _AR_ENV_SWITCHED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = 0.8
    output = _AR_ENV_OUTPUT
    else = _AR_ENV_SWITCHED

# Delayed vibrato
[contour]
    trigger = _AR_GATE_OUTPUT
    shape = 1
    release = _VIBRATO_DELAY * 1.3
    hold = _VIBRATO_DELAY * 1.5
    inverted = _AR_VIBRATO_LEVEL
    level = _AR_DEL_VIB_POT * 0.005

[copy]
    input = _VIBRATO_LFO * _AR_VIBRATO_LEVEL
    output = _AR_VIBRATO

[copy]
    input = _AR_PITCH_SWITCHED + _AR_VIBRATO
    output = _AR_PITCH_VIBRATED

[ifequal]
    input1 = _TUNING_MODE
    input2 = 1
    ifequal = _TUNING_PITCH
    else = _AR_PITCH_VIBRATED + _AR_OCTAVE
    output = _AR_UNCAL_PITCH

[copy]
    input = _AR_RIBBON_PITCH_QUANTIZED + _MANUAL_PITCH_MOD
    output = _AR_RIBBON_PITCH

[calibrator]
    input = _AR_UNCAL_PITCH
    nudgeup = _AR_CAL_UP
    nudgedown = _AR_CAL_DOWN
    correction = _AR_CAL_CORRECTION
    output = _AR_PITCH_OUTPUT
    nudgeamount = 0.01
    clearhere = _AR_CAL_CLEARHERE

[copy]
    input = _AR_GATE_SWITCHED
    output = _AR_GATE_OUTPUT

# Get pitch for envelope pitch tracking
[copy]
    input = _AR_PITCH_OUTPUT * 10 - 2
    output = _AR_ENVELOPE_PITCH

[math]
    input1 = 0.5
    input2 = _AR_ENVELOPE_PITCH * _AR_TRACKING_POT
    power = _AR_TRACKING_FACTOR

[copy]
    input = 0 * _VIBRATO_LFO + 1
    output = _AR_TREMOLO_FACTOR

[random]
    clock = _AR_GATE_OUTPUT
    maximum = _AR_RANDOM_LEVEL_POT * 0.5 + 1
    minimum = _AR_RANDOM_LEVEL_POT * -0.5 + 1
    output = _AR_ENV_RANDOM_LEVEL

[ifequal]
    input1 = _AR_ACCENT_IN
    input2 = 0
    ifequal = _AR_ACCENT_POT * -0.4 + 1
    else = _AR_ACCENT_POT * 0.4 + 1
    output = _AR_ACCENT_IMPACT

[slew]
    input = _AR_ACCENT_IMPACT * _AR_ENV_RANDOM_LEVEL
    slew = 0.05
    exponential = _AR_VARIABLE_LEVEL

[random]
    output = _AR_ENV_RANDOM_SPEED
    clock = _AR_GATE_OUTPUT
    maximum = _AR_RANDOM_SPEED_POT * 0.8 + 1
    minimum = _AR_RANDOM_SPEED_POT * -0.8 + 1
    steps = 2

[copy]
    input = _AR_ENV_RANDOM_SPEED * _AR_TRACKING_FACTOR
    output = _AR_ENV_SPEED_MOD

[copy]
    input = _AR_RELEASE_POT * _AR_RELEASE_POT + 0.02
    output = _AR_RELEASE

# Envelope für VCA and VCF
[contour]
    attack = _AR_ATTACK_POT + 0.02
    decay = _AR_RELEASE * _AR_ENV_SPEED_MOD
    release = _AR_RELEASE * _AR_ENV_SPEED_MOD
    sustain = _AR_SUSTAIN_POT
    level = _AR_LEVEL_POT * _AR_VARIABLE_LEVEL
    gate = _AR_GATE_OUTPUT
    output = _AR_ENVELOPE_UNTREMOLOED
    decayshape = _AR_SHAPE_POT * -1 + 1
    releaseshape = _AR_SHAPE_POT * -1 + 1

[copy]
    input = _AR_ENVELOPE_UNTREMOLOED * _AR_TREMOLO_FACTOR
    output = _AR_CONTOUR

[pot]
    pot = _AR_ENVELOPE_MIX
    slope = 0.5
    output = _AR_ENVMIX_RIGHT

[pot]
    pot = _AR_ENVELOPE_MIX * -1 + 1
    slope = 0.5
    output = _AR_ENVMIX_LEFT

[copy]
    input = _RIBBON_PRESSURE_LOOPED * _AR_TREMOLO_FACTOR
    output = _AR_RIBBON_ENV

[mixer]
    input1 = _AR_RIBBON_ENV * _AR_ENVMIX_LEFT
    input2 = _AR_CONTOUR * _AR_ENVMIX_RIGHT
    output = _AR_ENVMIX

# [crossfader]
#   input1 = _RIBBON_PRESSURE_LOOPED * _AR_TREMOLO_FACTOR
#   input2 = _AR_CONTOUR
#   fade = _AR_ENVELOPE_MIX
#   output = _AR_ENVMIX

[mixer]
    input1 = _AR_ENVMIX
    input3 = _MANUAL_TREMOLO
    output = _AR_RIBBON_ENV_OUTPUT

# -------------------------------------------------
# Outputs
# -------------------------------------------------

# TB V/O out
[copy]
    input = _TB_PITCH_OUTPUT
    output = O1

# TB env out
[copy]
    input = _TB_ENV_OUTPUT
    output = O5

# LD V/O out
[copy]
    input = _LD_PITCH_OUTPUT
    output = O2

# LD env out
[copy]
    input = _LD_ENV_OUTPUT
    output = O6

# LD gate out
[copy]
    input = _LD_GATE_OUTPUT
    output = G1.6

# OR V/O out
[copy]
    input = _OR_PITCH_OUTPUT
    output = O3

# OR env out
[copy]
    input = _OR_ENV_OUTPUT
    output = O7

# OR gate out
[copy]
    input = _OR_GATE_OUTPUT
    output = G1.7

# AR V/O out
[copy]
    input = _AR_PITCH_OUTPUT
    output = O4

# AR Env out
[copy]
    input = _AR_ENV_OUTPUT
    output = O8

# AR gate out
[copy]
    input = _AR_GATE_OUTPUT
    output = G1.8

[droid]
    lpfilter1 = 0
    lpfilter2 = 0
    lpfilter3 = 0
    lpfilter4 = 0
    lpfilter5 = 0.2
    lpfilter6 = 0.2
    lpfilter7 = 0.2
    lpfilter8 = 0.2
