[p2b8]
[p10]
[p2b8]

# Algorithmic trigger and melody sequencer
# LIBRARY: number=8, version=1.0

# INPUTS
# I1: [CLK] Clock
# I2: [V/O] optional: external pitch
# I3: [ROT] optional: root note
# I4: [SCA] optional: scale (degree)
# I5:
# I6:
# I7:
# I8:
#
# OUTPUTS
# O1: [TRG] trigger
# O2: [TG2] trigger delayed by 2 clocks
# O3: [TG5] trigger delayed by 5 clocks
# O4:
# O5: [PI1] pitch note 1
# O6: [PI2] pitch note 2
# O7: [PI3] pitch note 3
# O8:

# CONTROLLER 1
# P1.1: [LEN] Pattern length (1 ... 8)
# P1.2: [SEP] Chord separation (-7 ... +7)
# B1.1: [S1] Sequencer step 1
# B1.2: [S2] Sequencer step 2
# B1.3: [S3] Sequencer step 3
# B1.4: [S4] Sequencer step 4
# B1.5: [S5] Sequencer step 5
# B1.6: [S6] Sequencer step 6
# B1.7: [S7] Sequencer step 7
# B1.8: [S8] Sequencer step 8

# CONTROLLER 2
# P2.1: [Var] Melody variation
# P2.2: [Mel] Melody number (1 ... 20)
# P2.3: [M1] Melody pitch 1
# P2.4: [M2] Melody pitch 2
# P2.5: [M3] Melody pitch 3
# P2.6: [M4] Melody pitch 4
# P2.7: [M5] Melody pitch 5
# P2.8: [M6] Melody pitch 6
# P2.9: [M7] Melody pitch 7
# P2.10:[M8] Melody pitch 8

# CONTROLLER 3
# P3.1: [Ran] Pitch range
# P3.2: [Low] Lowest pitch
# B3.1: [1]   select root
# B3.2: [3]   select 3rd
# B3.3: [5]   select 5th
# B3.4: [7]   select 7th
# B3.5: [9]   select 9th
# B3.6: [11]  select 11th
# B3.7: [13]  select 13th
# B3.8: Trigger delay

[copy]
    input = 0 # C
    output = N3

[copy]
    input = 7 / 120 # natural minor
    output = N4

[copy]
    input = P1.1 * 7 + 1
    output = _LENGTH



[algoquencer]
    clock = I1
    length = _LENGTH
    pitch1 = P2.3   * _PITCH_RANGE + _PITCH_BASE
    pitch2 = P2.4   * _PITCH_RANGE + _PITCH_BASE
    pitch3 = P2.5   * _PITCH_RANGE + _PITCH_BASE
    pitch4 = P2.6   * _PITCH_RANGE + _PITCH_BASE
    pitch5 = P2.7   * _PITCH_RANGE + _PITCH_BASE
    pitch6 = P2.8   * _PITCH_RANGE + _PITCH_BASE
    pitch7 = P2.9   * _PITCH_RANGE + _PITCH_BASE
    pitch8 = P2.10  * _PITCH_RANGE + _PITCH_BASE
    pitchlow = _PITCH_BASE
    pitchhigh = _PITCH_BASE + _PITCH_RANGE
    variation = P2.1
    dejavu = 1
    pattern = P2.2 * 19
    pitch = N2
    trigger = _QUANTIZE

# Step sequencer
[algoquencer]
    clock = I1
    length = _LENGTH
    button1 = B1.1
    button2 = B1.2
    button3 = B1.3
    button4 = B1.4
    button5 = B1.5
    button6 = B1.6
    button7 = B1.7
    button8 = B1.8
    led1 = L1.1
    led2 = L1.2
    led3 = L1.3
    led4 = L1.4
    led5 = L1.5
    led6 = L1.6
    led7 = L1.7
    led8 = L1.8
    trigger = O1


[togglebutton]
    button = B3.8
    led = L3.8
    offvalue = 0
    onvalue = 1
    output = _DELAY

[triggerdelay]
    input = O1
    output = O2
    clock = I1
    delay = _DELAY * 2

[triggerdelay]
    input = O1
    output = O3
    clock = I1
    delay = _DELAY * 5

# Melody sequencer
[copy]
    input = P3.2 * 3V
    output = _PITCH_BASE

[copy]
    input = P3.1 * 3V
    output = _PITCH_RANGE


[togglebutton]
    button = B3.1
    led = L3.1

[togglebutton]
    button = B3.2
    led = L3.2

[togglebutton]
    button = B3.3
    led = L3.3

[togglebutton]
    button = B3.4
    led = L3.4

[togglebutton]
    button = B3.5
    led = L3.5

[togglebutton]
    button = B3.6
    led = L3.6

[togglebutton]
    button = B3.7
    led = L3.7

[minifonion]
    input = I2
    trigger = _QUANTIZE
    root = I3 * 120
    degree = I4 * 120
    select1 = L3.1
    select3 = L3.2
    select5 = L3.3
    select7 = L3.4
    select9 = L3.5
    select11 = L3.6
    select13 = L3.7
    output = _PITCH1

[copy]
    input = P1.2 * 14 - 7
    output = _NOTE_SHIFT


[minifonion]
    input = _PITCH1
    trigger = _QUANTIZE
    root = I3 * 120
    degree = I4 * 120
    selectnoteshift = _NOTE_SHIFT
    select1 = L3.1
    select3 = L3.2
    select5 = L3.3
    select7 = L3.4
    select9 = L3.5
    select11 = L3.6
    select13 = L3.7
    output = _PITCH2

[minifonion]
    input = _PITCH1
    trigger = _QUANTIZE
    root = I3 * 120
    degree = I4 * 120
    selectnoteshift = _NOTE_SHIFT * 2
    select1 = L3.1
    select3 = L3.2
    select5 = L3.3
    select7 = L3.4
    select9 = L3.5
    select11 = L3.6
    select13 = L3.7
    output = _PITCH3

# Fold all pitches into a musical range
[fold]
    input = _PITCH1
    output = O5
    minimum = 0V
    maximum = 6V

[fold]
    input = _PITCH2
    output = O6
    minimum = 0V
    maximum = 6V

[fold]
    input = _PITCH3
    output = O7
    minimum = 0V
    maximum = 6V
