# (H) Miscellaneous

# - FM, LD+OR Modulation, Magneto clock

# INPUTS:
#  I1: [AR Env]
#  I2: [AR V/O]
#  I4: [P Clock] Permanent clock

# OUTPUTS:
#  O1: [FM V/O]
#  O2: [FM Dep]
#  O3: [FM Noi]
#  O5: [LD Mod]
#  O6: [OR Mod]

# GATES ON MODULE 1:
#  G1.1: [Clock]
#  G1.2: [Reset]
#  G1.3: [LD Gate]
#  G1.4: [OR Gate]
#  G1.5: [AR Gate]
#  G1.8: [Mag Clk] Clock for Magneto

# CONTROLLER 1:
#  E1.1: [FM Ratio]
#  E1.2: [FM Pluckines]
#  E1.3: [FM Depth]
#  E1.4: [FM Env / Cal]

# CONTROLLER 2:
#  P2.1: [Noise Level] Additional short attack noise for FM voice
#  P2.2: [LFO] Speed for LD/OR Modulation
#  B2.1: [TUNE] Tuning mode for AR
#  B2.3: [LD /2] LD Modulation offbeats
#  B2.4: [LFO] OR Modulation LFO
#  B2.5: [LD /3] LD Modulation one in three
#  B2.6: [AR /3] OR Modulation one in three
#  B2.7: [LD Ran] LD Modulation random voltages
#  B2.8: [OR Ran] OR Modulation random voltages

# CONTROLLER 3:
#  P3.1: [FM Depth?] Animation for FM Depth
#  P3.2: [FM Ratio?]
#  B3.1: [1]
#  B3.2: [3]
#  B3.3: [2]
#  B3.4: [6]
#  B3.5: [4]
#  B3.6: [9]
#  B3.7: [8]
#  B3.8: [12]

[e4]
[p2b8]
[p2b8]
[p2b8]

# -------------------------------------------------
# FM Noise
# -------------------------------------------------

[copy]
    input = 0.01
    output = _NOISE_ATTACK

[copy]
    input = 0.02
    output = _NOISE_RELEASE

[pot]
    pot = P2.1
    outputscale = 0.2
    output = _NOISE_LEVEL

[contour]
    trigger = G1.5
    attack = _NOISE_ATTACK
    release = _NOISE_RELEASE
    level = _NOISE_LEVEL
    releaseshape = 1
    output = O3

# -------------------------------------------------
# FM Tuning
# -------------------------------------------------

[button]
    button = B2.1
    led = L2.1
    output = _TUNING_MODE
    inverted = _NO_TUNING_MODE

[encoder]
    encoder = E1.1
    select = _TUNING_MODE
    discrete = 7
    outputscale = 1V
    output = _FM_OPERATOR_TUNING_PITCH

# disable encoder 2 in tuning mode
[encoder]
    encoder = E1.2
    select = _TUNING_MODE
    mode = 0

[encoder]
    encoder = E1.3
    select = _TUNING_MODE
    output = _FM_DEPTH_TUNING

[encoder]
    select = _TUNING_MODE
    encoder = E1.4
    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

# -------------------------------------------------
# FM Depth
# -------------------------------------------------

[encoder]
    select = _NO_TUNING_MODE
    encoder = E1.2
    output = _FM_ENV_SLEW

[copy]
    input = _FM_ENV_SLEW * -1 + 1
    output = _FM_PLUCKINESS

[encoder]
    encoder = E1.3
    select = _NO_TUNING_MODE
    output = _FM_DEPTH_POT

[encoder]
    select = _NO_TUNING_MODE
    encoder = E1.4
    output = _FM_ENV_POT

[copy]
    input = P3.1 * 0.3
    output = _FM_DEPTH_CORRIDOR

[random]
    clock = G1.5
    maximum = _FM_DEPTH_CORRIDOR + 1
    minimum = _FM_DEPTH_CORRIDOR * -1 + 1
    output = _FM_DEPTH_RANDOM

[slew]
    input = I1
    slewdown = _FM_ENV_SLEW * 0.3
    slewup = 0
    linear = _FM_EXTERNAL_ENV

[math]
    input1 = _FM_EXTERNAL_ENV * _FM_DEPTH_RANDOM
    input2 = _FM_PLUCKINESS * 3 + 1
    power = _FM_ENVELOPE

[crossfader]
    input1 = _FM_DEPTH_POT * 0.7
    input2 = _FM_ENVELOPE * 0.7
    fade = _FM_ENV_POT
    output = _FM_DEPTH_NO_TUNING

# [mixer]
#   input1 = _FM_DEPTH_POT
#   input2 = _FM_ENVELOPE * _FM_ENV_POT
#   output = _FM_DEPTH_NO_TUNING

[switch]
    input1 = _FM_DEPTH_NO_TUNING
    input2 = _FM_DEPTH_TUNING
    offset = _TUNING_MODE
    output1 = O2

# -------------------------------------------------
# FM Randomization
# -------------------------------------------------

[random]
    clock = G1.5
    output = _FM_RATIO_RANDOM
    minimum = -4 * P3.2
    maximum = 4 * P3.2

[copy]
    input = _FM_RATIO_RANDOM
    output = _FM_RANDOM_OFFSET

# -------------------------------------------------
# FM Operator pitch
# -------------------------------------------------

# Ratio between operator and main oscillator
[encoder]
    select = _NO_TUNING_MODE
    encoder = E1.1
    discrete = 12
    color = 0.6
    output = _FM_RATIO_SWITCH_UNCLOCKED

[sample]
    input = _FM_RATIO_SWITCH_UNCLOCKED # Tuning mode: clock
    sample = G1.5
    output = _FM_RATIO_SWITCH

# Switch for the various FM ratios
[switch]
    input1 = 0V
    input2 = 0V + 0.5849V
    input3 = 1V
    input4 = 1V + 0.5849V
    input5 = 2V
    input6 = 2V + 0.5849V
    input7 = 3V
    input8 = 3V + 0.5849V
    input9 = 4V
    input10 = 4V + 0.5849V
    input11 = 5V
    input12 = 5V + 0.5849V
#   input13 = 6V
#   input14 = 6V + 0.5849V
    offset = _FM_RATIO_SWITCH + _FM_RANDOM_OFFSET
    output1 = _OPERATOR_PITCH_OFFSET

[mixer]
    input1 = _OPERATOR_PITCH_OFFSET
    input2 = -1V
    input3 = I2
    output = _FM_OPERATOR_NORMAL_PITCH

[switch]
    input1 = _FM_OPERATOR_NORMAL_PITCH
    input2 = _FM_OPERATOR_TUNING_PITCH
    offset = _TUNING_MODE
    output1 = _FM_OPERATOR_PITCH

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

# -------------------------------------------------
# Magneto clock
# -------------------------------------------------

[buttongroup]
    button1 = B3.1
    button2 = B3.2
    button3 = B3.3
    button4 = B3.4
    button5 = B3.5
    button6 = B3.6
    button7 = B3.7
    button8 = B3.8
    value1 = 1
    value2 = 3
    value3 = 2
    value4 = 6
    value5 = 4
    value6 = 9
    value7 = 8
    value8 = 12
    led1 = L3.1
    led2 = L3.2
    led3 = L3.3
    led4 = L3.4
    led5 = L3.5
    led6 = L3.6
    led7 = L3.7
    led8 = L3.8
    output = _MAGNETO_CLOCK_DIVISION

# Use a clock tool to sense the clock speed and convert it into V/O. The idea:
# when the clock stops, the sensed pitch is frozen. That way we can output
# a steady clock to Magneto - independent of the incoming clock. This avoid
# sudden speed changes in the tape speed and audible artifacts.
[clocktool]
    clock = I4
    divide = _MAGNETO_CLOCK_DIVISION
    outputpitch = _MAG_CLOCK_PITCH

[lfo]
    rate = _MAG_CLOCK_PITCH
    square = G1.8

# -------------------------------------------------
# Modulation sequencer
# -------------------------------------------------

[sequencer]
    clock = G1.1
    reset = G1.2
    pitch1 = 0
    pitch2 = 1
    pitchoutput = _MODULATION_OFFBEATS

[sequencer]
    clock = G1.1
    reset = G1.2
    pitch1 = 0
    pitch2 = 0
    pitch3 = 1
    pitchoutput = _MODULATION_13

# -------------------------------------------------
# LD Modulation
# -------------------------------------------------

[random]
    clock = G1.3
    output = _LD_MODULATION_RANDOM

[buttongroup]
    button1 = B2.3
    button2 = B2.5
    button3 = B2.7
    minactive = 0
    value1 = _MODULATION_OFFBEATS
    value2 = _MODULATION_13
    value3 = _LD_MODULATION_RANDOM
    led1 = L2.3
    led2 = L2.5
    led3 = L2.7
    output = O5

# -------------------------------------------------
# OR Modulation
# -------------------------------------------------

[pot]
    pot = P2.2
    slope = 3
    output = _LFO_POT

[lfo]
    hz = _LFO_POT * 20
    triangle = _OR_MODULATION_LFO

[flipflop]
    toggle = G1.4
    output = _OR_MODULATION_FLIPFLOP

[copy]
    input = _OR_MODULATION_FLIPFLOP * 0.5
    output = _OR_MODULATION_EVERY_SECOND

[random]
    clock = G1.4
    output = _OR_MODULATION_RANDOM

[buttongroup]
    button1 = B2.4
    button2 = B2.6
    button3 = B2.8
    button4 = B2.2
    minactive = 0
    value1 = _OR_MODULATION_LFO
    value2 = _MODULATION_13
    value3 = _OR_MODULATION_RANDOM
    value4 = _OR_MODULATION_LFO * 0.5 + _OR_MODULATION_EVERY_SECOND
    led1 = L2.4
    led2 = L2.6
    led3 = L2.8
    output = O6
    led4 = L2.2

# -------------------------------------------------
# CV Looper
# -------------------------------------------------

[pot]
    pot = P4.1
    onchange = _MOVED

[button]
    button = B4.1
    output = _LOOP_RECORDING
    inverted = _LOOP_PLAYING

[once]
    trigger = _STOP_LOOPER_AT_START
    delay = 1

[mixer]
    input1 = _MOVED * _LOOP_PLAYING
    input2 = _STOP_LOOPER_AT_START
    output = _STOP_LOOPER

# State von Bypass
[flipflop]
    reset = _LOOP_RECORDING * -1 + 1
    set = _STOP_LOOPER
    startvalue = 1
    output = _LOOP_BYPASS

[recorder]
    cvin = P4.1
    cvout = O8
    loop = 1
    mode = _LOOP_RECORDING + 1
    bypass = _LOOP_BYPASS
    recordled = L4.1

[lfo]
    hz = 3
    square = _LOOPER_LED_LFO

[select]
    input = _LOOPER_LED_LFO
    output = L4.1

[select]
    select = _LOOP_BYPASS
    input = 0
    output = L4.1

[select]
    select = _LOOP_RECORDING
    input = 1
    output = L4.1
