def testPitchEquality(self): ''' Test updating accidental display. ''' data = [ ('a', 'b', False), ('a', 'a', True), ('a#', 'a', False), ('a#', 'b-', False), ('a#', 'a-', False), ('a##', 'a#', False), ('a#4', 'a#4', True), ('a-3', 'a-4', False), ('a#3', 'a#4', False), ] for x, y, match in data: p1 = Pitch(x) p2 = Pitch(y) self.assertEqual(p1 == p2, match) # specific case of changing octave p1 = Pitch('a#') p2 = Pitch('a#') self.assertEqual(p1, p2) p1.octave = 4 p2.octave = 3 self.assertNotEqual(p1, p2) p1.octave = 4 p2.octave = 4 self.assertEqual(p1, p2)
def _transpose_pitch_in_scale_space( original_pitch: pitch.Pitch, steps: int, reference_scale: scale.ConcreteScale, ) -> pitch.Pitch: if steps == 0: return if steps > 0: direction = "ascending" else: direction = "descending" steps *= -1 new_pitch = reference_scale.next(original_pitch, direction, steps) original_pitch.step = new_pitch.step original_pitch.octave = new_pitch.octave original_pitch.accidental = new_pitch.accidental
def transposePitchFromC(self, p: pitch.Pitch, *, inPlace=False) -> Optional[pitch.Pitch]: ''' Takes a pitch in C major and transposes it so that it has the same step position in the current key signature. >>> ks = key.KeySignature(-3) >>> p1 = pitch.Pitch('B') >>> p2 = ks.transposePitchFromC(p1) >>> p2.name 'D' Original pitch is unchanged: >>> p1.name 'B' >>> ks2 = key.KeySignature(2) >>> p2 = ks2.transposePitchFromC(p1) >>> p2.name 'C#' For out of scale pitches the relationship still works; note also that original octave is preserved. >>> p3 = pitch.Pitch('G-4') >>> p4 = ks.transposePitchFromC(p3) >>> p4.nameWithOctave 'B--4' If inPlace is True then nothing is returned and the original pitch is modified. >>> p5 = pitch.Pitch('C5') >>> ks.transposePitchFromC(p5, inPlace=True) >>> p5.nameWithOctave 'E-5' New method in v6. ''' transInterval = None transTimes = 0 originalOctave = p.octave if not inPlace: p = copy.deepcopy(p) if self.sharps == 0: if inPlace: return else: return p elif self.sharps < 0: transTimes = abs(self.sharps) transInterval = interval.Interval('P4') else: transTimes = self.sharps transInterval = interval.Interval('P5') for i in range(transTimes): transInterval.transposePitch(p, inPlace=True) if originalOctave is not None: p.octave = originalOctave if not inPlace: return p