| 1 | 1 | class Scale: |
| 2 | 2 | def __init__(self, tonic: str): |
| 3 | | pass |
| 3 | self.tonic = tonic.capitalize() |
| 4 | # Define the chromatic scales with sharps and flats |
| 5 | self.sharp_chromatic = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'] |
| 6 | self.flat_chromatic = ['A', 'Bb', 'B', 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab'] |
| 7 | |
| 8 | # Define which tonics use flats |
| 9 | self.flat_keys = {'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'd', 'g', 'c', 'f', 'bb', 'eb'} |
| 10 | |
| 11 | # Determine whether to use sharps or flats |
| 12 | if self.tonic in self.flat_keys or tonic in self.flat_keys: |
| 13 | self.chromatic_scale = self.flat_chromatic |
| 14 | else: |
| 15 | self.chromatic_scale = self.sharp_chromatic |
| 16 | |
| 17 | # Find the starting index for the tonic |
| 18 | try: |
| 19 | self.start_index = self.chromatic_scale.index(self.tonic) |
| 20 | except ValueError: |
| 21 | # If not found, try with the other scale |
| 22 | if self.chromatic_scale is self.sharp_chromatic: |
| 23 | self.chromatic_scale = self.flat_chromatic |
| 24 | else: |
| 25 | self.chromatic_scale = self.sharp_chromatic |
| 26 | # Edge Case: Tonic not found in either scale representation |
| 27 | self.start_index = self.chromatic_scale.index(self.tonic) |
| 4 | 28 | |
| 5 | 29 | def chromatic(self) -> list[str]: |
| 6 | | pass |
| 30 | # Edge Case: Empty scale |
| 31 | if not self.chromatic_scale: |
| 32 | return [] |
| 33 | |
| 34 | result = [] |
| 35 | for i in range(12): |
| 36 | index = (self.start_index + i) % 12 |
| 37 | result.append(self.chromatic_scale[index]) |
| 38 | return result |
| 7 | 39 | |
| 8 | 40 | def interval(self, intervals: str) -> list[str]: |
| 9 | | pass |
| 41 | # Edge Case: Empty intervals string |
| 42 | if not intervals: |
| 43 | return [self.tonic] |
| 44 | |
| 45 | result = [self.tonic] |
| 46 | current_index = self.start_index |
| 47 | |
| 48 | # Define step sizes |
| 49 | step_sizes = { |
| 50 | 'm': 1, # minor second (half step) |
| 51 | 'M': 2, # major second (whole step) |
| 52 | 'A': 3 # augmented second (three semitones) |
| 53 | } |
| 54 | |
| 55 | for interval in intervals: |
| 56 | # Edge Case: Invalid interval character |
| 57 | if interval not in step_sizes: |
| 58 | raise ValueError(f"Invalid interval: {interval}") |
| 59 | |
| 60 | current_index = (current_index + step_sizes[interval]) % 12 |
| 61 | result.append(self.chromatic_scale[current_index]) |
| 62 | |
| 63 | return result |
| 64 | # Handled Edge Cases: Empty intervals string, Invalid interval character |