summaryrefslogtreecommitdiffstats
path: root/gen_pitches.py
blob: 8c153175455fabaf3ed2581444664470780ca532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from functools import partial
from string import Template

index_to_pitch = [ "a", "b", "c", "d", "e", "f", "g"]
pitch_to_index = { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6}

def pitch_transpose(pitch, delta=1):
    return index_to_pitch[(pitch_to_index[pitch[0]] + delta) % 7] + pitch[1:]

def bar_transpose(bar, delta=1):
    return " ".join(pitch_transpose(pitch, delta) for pitch in bar.split())

def compile_exercise(seed_asc, seed_desc):
    asc = "\n".join(bar_transpose(seed_asc, i) for i in xrange(7))
    desc = "\n".join(bar_transpose(seed_desc, -i) for i in xrange(7))
    right = """\\relative e {
$asc

$asc
\\bar "||"
}
\\relative e'' {
$desc

$desc
\\bar "|."
}
"""
    left = """\\relative e, {
$asc

$asc
\\bar "||"
}
\\relative e' {
$desc

$desc
\\bar "|."
}
"""
    tpl_left, tpl_right = Template(left), Template(right)
    return (tpl_left.substitute(asc=asc, desc=desc),
            tpl_right.substitute(asc=asc, desc=desc))


if __name__ == "__main__":
    import sys
    from string import Template
    seeds = sys.argv[1]
    with open(seeds) as fh:
        for line in fh:
            num, seed_asc, seed_desc = line.split("#")
            lh, rh = compile_exercise(seed_asc, seed_desc)
            with open("rh" + num + ".ily", "w") as fhr:
                fhr.write(rh)
            with open("lh" + num + ".ily", "w") as fhr:
                fhr.write(lh)