def test_shortcuts(self): assert note("C") == Note(name="C", octave=4) assert note("Db3") == Note(name="Db", octave=3) assert note("Db") == Note(name="Db") assert note("D#5") == Note(name="Eb", octave=5) assert note("D#") == Note(name="Eb", octave=4)
def test_chord_types_and_shortcuts(self): assert Chord(root=note('C4'), typ='major') == chord(['C4', 'E4', 'G4']) assert Chord(root=note('C4'), typ='minor') == chord(['C4', 'Eb4', 'G4']) assert chord('C4 major') == chord(['C4', 'E4', 'G4']) assert chord('C major') == chord(['C4', 'E4', 'G4']) assert chord('C aug') == chord(['C4', 'E', 'Ab4']) assert chord('C dim') == chord(['C4', 'Eb4', 'Gb4'])
def chord(input): """ Shortcut: chord(['C5', 'E5', 'G5') -> Chord object Shortcut: chord('C5 dim') -> Chord object """ if type(input) == list: notes = [ note(n) for n in input ] return Chord(notes) else: tokens = input.split() assert len(tokens) == 2, "invalid chord expression: %s" % input return Chord(root=note(tokens[0]), typ=tokens[1])
def test_basics(self): r = Roman(scale=scale("C4 major")) assert r.do("1") == note("C4") assert r.do("3") == note("E4") assert r.do("4") == note("F4") assert r.do("IV") == chord("F4 major") assert r.do("iv") == chord("F4 minor") # somewhat non-standard notation but allows describing other chord types (see chord.py) assert r.do("I:power") == chord(["C4", "G4"])
def __init__(self, root=None, typ=None): """ Constructs a scale: scale = Scale(root='C4', typ='major') """ assert root is not None assert typ is not None if isinstance(root, str): root = note(root) self.root = root self.typ = typ
def do(self, sym): """ Accepts symbols like C4-major or C4,E4,G4 or note symbols like 'C4' """ # The dash is a bit of a notation hack, it's there because "C4 major" # would look like two symbols, so we need to have no whitespace # between them if '-' in sym: return chord(sym.replace("-"," ")) elif "," in sym: return chord(sym.split(",")) else: return note(sym)
def do(self, sym): """ Accepts symbols like C4-major or C4,E4,G4 or note symbols like 'C4' """ # The dash is a bit of a notation hack, it's there because "C4 major" # would look like two symbols, so we need to have no whitespace # between them if sym is None or sym == '-': # REST: return chord([]) if '-' in sym: return chord(sym.replace("-", " ")) elif "," in sym: return chord(sym.split(",")) else: return note(sym)
def test_basics(self): notes = [ note('C4'), note('E4'), note('G4')] chord1 = Chord(notes=notes) assert chord1.notes[0] == note('C4') assert chord1.notes[1] == note('E4') assert chord1.notes[2] == note('G4') assert str(chord1) == 'Chord<C4,E4,G4>' chord2 = chord(['G4','C4','E4']) assert str(chord2) == 'Chord<C4,E4,G4>' assert chord1 == chord2 assert chord(['C4','E4','G4']) != chord(['C4']) assert chord(['C4','E4','G4']) != chord(['C4', 'E4', 'G4', 'C5'])
def __init__(self, notes=None, root=None, typ=None): """ Constructs a chord, in different ways: notes = [ note('C4'), note('E4'), note('G4') ] chord = Chord(notes=notes) OR: chord = Chord(root=note('C4'), type='major') OR: chord = Chord(root='C4', type='major') """ self.notes = [] if notes and root: raise Exception("notes= and root= are mutually exclusive") if notes is None and root is None: raise Exception("specify either notes= or root=") if root and typ is None: raise Exception("typ= is required when using root=") if typ and typ not in CHORD_TYPES: raise Exception("unknown chord type: %s, expecting one of: %s" % (typ, CHORD_TYPES)) if isinstance(root, str): root = note(root) if notes is not None: for x in notes: assert type(x) == Note self.notes = notes else: self.typ = typ self.root = root self.notes = self._chordify()
def test_basics(self): scale1 = Scale(root=note('C4'), typ='major') scale2 = scale('C4 major') assert scale1 == scale2 assert str(scale1) == 'Scale<C4 major>'
def test_basics(self): l = Literal() assert l.do("C4") == note("C4") assert l.do("C4-major") == chord("C4 major") assert l.do("C4,E4,G4") == chord("C4 major")
def test_basics(self): scale1 = Scale(root=note("C4"), typ="major") scale2 = scale("C4 major") assert scale1 == scale2 assert str(scale1) == "Scale<C4 major>"
def scale(input): """ Shortcut: scale(['C major') -> Scale object """ (root, typ) = input.split() return Scale(root=note(root), typ=typ)