Sha256: 0a66b61007eb0f1ed20133e5ffd276a66234e93f70f24583f48e5fc032b65b15

Contents?: true

Size: 1.34 KB

Versions: 44

Compression:

Stored size: 1.34 KB

Contents

class Scale(object):

    ASCENDING_INTERVALS = ['m', 'M', 'A']
    CHROMATIC_SCALE = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A',
                       'A#', 'B']
    FLAT_CHROMATIC_SCALE = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab',
                            'A', 'Bb', 'B']
    FLAT_KEYS = ['F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'd', 'g', 'c', 'f', 'bb',
                 'eb']

    def __init__(self, tonic, scale_name, pattern=None):
        self.tonic = tonic.capitalize()
        self.name = self.tonic + ' ' + scale_name
        self.pattern = pattern
        self.chromatic_scale = (self.FLAT_CHROMATIC_SCALE
                                if tonic in self.FLAT_KEYS else
                                self.CHROMATIC_SCALE)
        self.pitches = self._assign_pitches()

    def _assign_pitches(self):
        if self.pattern is None:
            return self._reorder_chromatic_scale()
        last_index = 0
        pitches = []
        scale = self._reorder_chromatic_scale()
        for i, interval in enumerate(self.pattern):
            pitches.append(scale[last_index])
            last_index += self.ASCENDING_INTERVALS.index(interval) + 1
        return pitches

    def _reorder_chromatic_scale(self):
        index = self.chromatic_scale.index(self.tonic)
        return self.chromatic_scale[index:] + self.chromatic_scale[:index]

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.55 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.54 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.53 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.52 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.51 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.50 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.49 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.48 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.47 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.46 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.45 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.44 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.43 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.42 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.41 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.40 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.39 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.38 tracks/python/exercises/scale-generator/example.py
trackler-2.1.0.37 tracks/python/exercises/scale-generator/example.py