def __init__(self, anchor_pitch=DiatonicPitch.parse('A:0'), anchor_value=9, pitch_unit=1): """ Constructor, """ self.__anchor_pitch = anchor_pitch if not isinstance(self.anchor_pitch, DiatonicPitch): raise Exception('Anchor is not a DiatonicPitch') self.__anchor_value = anchor_value self.__pitch_unit = pitch_unit anchor_index = self.anchor_pitch.chromatic_distance base_value = anchor_value - anchor_index * pitch_unit self.value_to_pitch = OrderedMap() self.pitch_to_value = dict() for i in range(ChromaticScale.chromatic_start_index(), ChromaticScale.chromatic_end_index() + 1): pitch = DiatonicFoundation.map_to_diatonic_scale(i)[0] value = base_value + i * pitch_unit self.value_to_pitch.insert(value, pitch) self.pitch_to_value[pitch] = value PitchRangeInterpreter.__init__(self)
def test_map_to_diatonic_scale(self): answers = DiatonicFoundation.map_to_diatonic_scale(46) assert DiatonicPitch.parse('Cbb:4') in answers assert DiatonicPitch.parse('A#:3') in answers assert DiatonicPitch.parse('Bb:3') in answers answers = DiatonicFoundation.map_to_diatonic_scale(47) assert DiatonicPitch.parse('Cb:4') in answers assert DiatonicPitch.parse('A##:3') in answers assert DiatonicPitch.parse('B:3') in answers answers = DiatonicFoundation.map_to_diatonic_scale(48) assert DiatonicPitch.parse('C:4') in answers assert DiatonicPitch.parse('B#:3') in answers assert DiatonicPitch.parse('Dbb:4') in answers answers = DiatonicFoundation.map_to_diatonic_scale(49) assert DiatonicPitch.parse('C#:4') in answers assert DiatonicPitch.parse('B##:3') in answers assert DiatonicPitch.parse('Db:4') in answers
def eval_as_accurate_chromatic_distance(self, v): floor_value = self.value_to_pitch.floor(v) if floor_value is None: raise ChromaticRangeInterpreterException( 'Illegal chromatic pitch range paramger value {0}.'.format(v)) low_pitch = self.value_to_pitch[floor_value] index = low_pitch.chromatic_distance if index >= ChromaticScale.chromatic_end_index() or math.isclose( v, floor_value): return low_pitch.chromatic_distance high_pitch = DiatonicFoundation.map_to_diatonic_scale(index + 1)[0] return low_pitch.chromatic_distance + \ ((v - floor_value) / self.pitch_unit) * \ (high_pitch.chromatic_distance - low_pitch.chromatic_distance)
def __str__(self): return 'P-R({0}, {1})'.format( DiatonicFoundation.map_to_diatonic_scale(self.start_index)[0], DiatonicFoundation.map_to_diatonic_scale(self.end_index)[0])
def enharmonics(self): return DiatonicFoundation.map_to_diatonic_scale( self.chromatic_distance)