User:Contribution/Ups & downs notation in Python: Difference between revisions

Contribution (talk | contribs)
Created page with "<syntaxhighlight lang="python"> def upsdowns(degree: int, steps_per_octave: int, steps_per_fifth: int, steps_per_degree: int = 1): sharpness = 7 * steps_per_fifth - 4 * steps_per_octave POS_ALTS, NEG_ALTS = ({(-1) ** (i + 1) * i for i in range(1, 6)}, {(-1) ** i * i for i in range(1, 6)}) naturals = [] for a in range(-5, 6): pos = (a * steps_per_fifth) % steps_per_octave oadj = -1 if (pos < steps_per_octave / 2 and a in POS_ALTS) else (1..."
 
Contribution (talk | contribs)
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
def upsdowns(degree: int, steps_per_octave: int, steps_per_fifth: int, steps_per_degree: int = 1):
def upsdowns(degree: int, steps_per_octave: int, steps_per_fifth: int, steps_per_degree: int = 1):
     sharpness = 7 * steps_per_fifth - 4 * steps_per_octave
     sharpness = 7 * steps_per_fifth - 4 * steps_per_octave
     POS_ALTS, NEG_ALTS = ({(-1) ** (i + 1) * i for i in range(1, 6)}, {(-1) ** i * i for i in range(1, 6)})
     POS_ALTS, NEG_ALTS = ({(i if i % 2 else -i) for i in range(1, 6)}, {(-i if i % 2 else i) for i in range(1, 6)})


     naturals = []
     naturals = []
     for a in range(-5, 6):
     for a in range(-5, 6):
         pos = (a * steps_per_fifth) % steps_per_octave
         pos = (a * steps_per_fifth) % steps_per_octave
         oadj = -1 if (pos < steps_per_octave / 2 and a in POS_ALTS) else (1 if (pos > steps_per_octave / 2 and a in NEG_ALTS) else 0)
         oadj = -1 if (2 * pos < steps_per_octave and a in POS_ALTS) else (1 if (2 * pos > steps_per_octave and a in NEG_ALTS) else 0)
         naturals.append([pos, [0, a, oadj]])
         naturals.append([pos, [0, a, oadj]])
     naturals.sort(key=lambda x: (x[0], -x[1][1], x[1][2]))
     naturals.sort(key=lambda x: (x[0], -x[1][1], x[1][2]))
Line 38: Line 38:
         elems = new
         elems = new


     def arrow(a):
     def arrow_str(a):
         if a == "neutral": return "~"
         if a == "neutral": return "~"
         sym, n = ("v" if a < 0 else "^"), abs(int(a))
         sym, n = ("v" if a < 0 else "^"), abs(int(a))
         return sym * n if n <= 3 else f"{sym}<sup>{n}</sup>"
         return sym * n if n <= 3 else f"{sym}<sup>{n}</sup>"


     def label(fi, a):
     def label_str(fi, a):
         if a == "neutral": letter = ""
         if a == "neutral": letter = ""
         elif abs(fi) <= 1: letter = "" if a != 0 else "P"
         elif abs(fi) <= 1: letter = "" if a != 0 else "P"
Line 50: Line 50:
         return f"{letter}{int((fi * 4) % 7 + 1)}"
         return f"{letter}{int((fi * 4) % 7 + 1)}"


     def octave_str(o: int) -> str:
     def octave_str(o):
         return "" if o == 0 else f" {'+' if o > 0 else '-'}{abs(o)}"
         return "" if o == 0 else f" {'+' if o > 0 else '-'}{abs(o)}"


Line 59: Line 59:
         return out
         return out


     return dedupe([arrow(off) + label(fi, off) + octave_str(octv) for off, fi, octv in elems])
     return dedupe([arrow_str(off) + label_str(fi, off) + octave_str(octv) for off, fi, octv in elems])
</syntaxhighlight>
</syntaxhighlight>