# Claude bass and chords: an 8-bar part, 122 BPM, F minor (Fm9 | Dbmaj9 | Bbm9 | Cm7, two bars each).
# Written for monody's guide "Claude and FL Studio" from Image-Line's Piano roll scripting reference.
# Not run in FL Studio by us: read it, try it on an empty pattern, and undo if it is not what you want.
#
# Use: put this file in Documents/Image-Line/FL Studio/Settings/Piano roll scripts, open the Piano roll of an
# empty instrument channel, choose the script from the Piano roll menu's Script list, pick Bass or Chords, OK.
# Set the project tempo to 122 yourself: a Piano roll script writes notes, not the tempo.
# Pitches are MIDI note numbers (FL Studio shows 60 as C5). Times are in beats from the start of the pattern.

import flpianoroll as flp

# (MIDI note number, start beat, length in beats, MIDI velocity 0-127)
BASS = [
    (41, 0, 0.5, 110),
    (41, 0.75, 0.25, 92),
    (53, 1.5, 0.5, 100),
    (41, 2.5, 0.5, 104),
    (48, 3.25, 0.25, 90),
    (41, 4, 0.5, 110),
    (41, 4.75, 0.25, 92),
    (53, 5.5, 0.5, 100),
    (41, 6.5, 0.375, 104),
    (41, 7, 0.375, 96),
    (39, 7.5, 0.5, 100),
    (37, 8, 0.5, 110),
    (37, 8.75, 0.25, 92),
    (49, 9.5, 0.5, 100),
    (37, 10.5, 0.5, 104),
    (44, 11.25, 0.25, 90),
    (37, 12, 0.5, 110),
    (37, 12.75, 0.25, 92),
    (49, 13.5, 0.5, 100),
    (37, 14.5, 0.375, 104),
    (37, 15, 0.375, 96),
    (36, 15.5, 0.5, 100),
    (34, 16, 0.5, 110),
    (34, 16.75, 0.25, 92),
    (46, 17.5, 0.5, 100),
    (34, 18.5, 0.5, 104),
    (41, 19.25, 0.25, 90),
    (34, 20, 0.5, 110),
    (34, 20.75, 0.25, 92),
    (46, 21.5, 0.5, 100),
    (34, 22.5, 0.375, 104),
    (34, 23, 0.375, 96),
    (35, 23.5, 0.5, 100),
    (36, 24, 0.5, 110),
    (36, 24.75, 0.25, 92),
    (48, 25.5, 0.5, 100),
    (36, 26.5, 0.5, 104),
    (43, 27.25, 0.25, 90),
    (36, 28, 0.5, 110),
    (36, 28.75, 0.25, 92),
    (48, 29.5, 0.5, 100),
    (36, 30.5, 0.375, 104),
    (39, 31, 0.375, 96),
    (40, 31.5, 0.5, 100),
]

CHORDS = [
    (56, 0, 1, 96),
    (60, 0, 1, 96),
    (63, 0, 1, 96),
    (67, 0, 1, 96),
    (56, 1.5, 1, 84),
    (60, 1.5, 1, 84),
    (63, 1.5, 1, 84),
    (67, 1.5, 1, 84),
    (56, 3, 0.75, 88),
    (60, 3, 0.75, 88),
    (63, 3, 0.75, 88),
    (67, 3, 0.75, 88),
    (56, 4, 1, 96),
    (60, 4, 1, 96),
    (63, 4, 1, 96),
    (67, 4, 1, 96),
    (56, 5.5, 1, 84),
    (60, 5.5, 1, 84),
    (63, 5.5, 1, 84),
    (67, 5.5, 1, 84),
    (56, 7, 0.75, 88),
    (60, 7, 0.75, 88),
    (63, 7, 0.75, 88),
    (67, 7, 0.75, 88),
    (56, 8, 1, 96),
    (60, 8, 1, 96),
    (63, 8, 1, 96),
    (65, 8, 1, 96),
    (56, 9.5, 1, 84),
    (60, 9.5, 1, 84),
    (63, 9.5, 1, 84),
    (65, 9.5, 1, 84),
    (56, 11, 0.75, 88),
    (60, 11, 0.75, 88),
    (63, 11, 0.75, 88),
    (65, 11, 0.75, 88),
    (56, 12, 1, 96),
    (60, 12, 1, 96),
    (63, 12, 1, 96),
    (65, 12, 1, 96),
    (56, 13.5, 1, 84),
    (60, 13.5, 1, 84),
    (63, 13.5, 1, 84),
    (65, 13.5, 1, 84),
    (56, 15, 0.75, 88),
    (60, 15, 0.75, 88),
    (63, 15, 0.75, 88),
    (65, 15, 0.75, 88),
    (56, 16, 1, 96),
    (60, 16, 1, 96),
    (61, 16, 1, 96),
    (65, 16, 1, 96),
    (56, 17.5, 1, 84),
    (60, 17.5, 1, 84),
    (61, 17.5, 1, 84),
    (65, 17.5, 1, 84),
    (56, 19, 0.75, 88),
    (60, 19, 0.75, 88),
    (61, 19, 0.75, 88),
    (65, 19, 0.75, 88),
    (56, 20, 1, 96),
    (60, 20, 1, 96),
    (61, 20, 1, 96),
    (65, 20, 1, 96),
    (56, 21.5, 1, 84),
    (60, 21.5, 1, 84),
    (61, 21.5, 1, 84),
    (65, 21.5, 1, 84),
    (56, 23, 0.75, 88),
    (60, 23, 0.75, 88),
    (61, 23, 0.75, 88),
    (65, 23, 0.75, 88),
    (55, 24, 1, 96),
    (58, 24, 1, 96),
    (60, 24, 1, 96),
    (63, 24, 1, 96),
    (55, 25.5, 1, 84),
    (58, 25.5, 1, 84),
    (60, 25.5, 1, 84),
    (63, 25.5, 1, 84),
    (55, 27, 0.75, 88),
    (58, 27, 0.75, 88),
    (60, 27, 0.75, 88),
    (63, 27, 0.75, 88),
    (55, 28, 1, 96),
    (58, 28, 1, 96),
    (60, 28, 1, 96),
    (63, 28, 1, 96),
    (55, 29.5, 1, 84),
    (58, 29.5, 1, 84),
    (60, 29.5, 1, 84),
    (63, 29.5, 1, 84),
    (55, 31, 0.75, 88),
    (58, 31, 0.75, 88),
    (60, 31, 0.75, 88),
    (63, 31, 0.75, 88),
]

PARTS = ['Bass (44 notes)', 'Chords (96 notes)']


def createDialog():
    form = flp.ScriptDialog('Claude bass and chords',
                            '8 bars, 122 BPM, F minor. Adds one part to this Piano roll.')
    form.addInputCombo('Part', PARTS, 0)
    form.addInputCheckbox('Clear notes first', True)
    return form


def apply(form):
    if form.getInputValue('Clear notes first'):
        flp.score.clearNotes(True)
    part = BASS if form.getInputValue('Part') == 0 else CHORDS
    ppq = flp.score.PPQ
    for pitch, start, length, velocity in part:
        note = flp.Note()
        note.number = pitch
        note.time = int(round(start * ppq))
        note.length = int(round(length * ppq))
        note.velocity = velocity / 127.0
        flp.score.addNote(note)
