Sha256: 0ad1e08e1f98c4c9d2ebc874c4f3ebc49c9f1ee00518e6f5224af2595133917f

Contents?: true

Size: 1.39 KB

Versions: 71

Compression:

Stored size: 1.39 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, intervals=None):
        self.tonic = tonic.capitalize()
        self.intervals = intervals
        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.intervals is None:
            return self._reorder_chromatic_scale()
        last_index = 0
        pitches = []
        scale = self._reorder_chromatic_scale()
        for i, interval in enumerate(self.intervals):
            pitches.append(scale[last_index])
            last_index += self.ASCENDING_INTERVALS.index(interval) + 1
        if pitches[0] != scale[last_index % len(scale)]:
            raise ValueError("Interval is broken")
        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

71 entries across 71 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.158 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.157 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.156 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.155 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.154 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.153 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.152 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.151 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.150 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.149 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.148 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.147 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.146 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.145 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.144 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.143 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.142 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.141 tracks/python/exercises/scale-generator/example.py
trackler-2.2.1.140 tracks/python/exercises/scale-generator/example.py