def _initialize_by_pitch_name(self, pitch_string): from abjad.tools import pitchtools named_pitch_class = pitchtools.NamedPitchClass(pitch_string) octave_number = pitchtools.Octave.from_pitch_name( pitch_string).octave_number self._initialize_by_named_pitch_class_and_octave_number( named_pitch_class, octave_number)
def voice_pitch_class(self, pitch_class): r"""Voices `pitch_class` in this pitch-range. :: >>> a_pitch_range = pitchtools.PitchRange('[C4, C6]') >>> a_pitch_range.voice_pitch_class('c') (NamedPitch("c'"), NamedPitch("c''"), NamedPitch("c'''")) :: >>> a_pitch_range.voice_pitch_class('b') (NamedPitch("b'"), NamedPitch("b''")) :: >>> a_pitch_range = pitchtools.PitchRange('[C4, A4)') >>> a_pitch_range.voice_pitch_class('b') () Returns tuple of zero or more named pitches. """ from abjad.tools import pitchtools named_pitch_class = pitchtools.NamedPitchClass(pitch_class) named_pitch = pitchtools.NamedPitch( named_pitch_class, self.start_pitch.octave_number, ) result = [] while named_pitch <= self.stop_pitch: if named_pitch in self: result.append(named_pitch) named_pitch += 12 return tuple(result)
def __init__(self, tonic='c', mode='major'): from abjad.tools import pitchtools from abjad.tools import scoretools from abjad.tools import tonalanalysistools tonic = pitchtools.NamedPitchClass(tonic) mode = tonalanalysistools.Mode(mode) self._tonic = tonic self._mode = mode self._default_scope = scoretools.Staff
def _initialize_by_pitch_class_octave_number_string( self, pitch_class_octave_number_string): from abjad.tools import pitchtools groups = self._pitch_class_octave_number_regex.match( pitch_class_octave_number_string).groups() named_pitch_class = pitchtools.NamedPitchClass( pitch_class_octave_number_string) octave_number = int(groups[2]) self._initialize_by_named_pitch_class_and_octave_number( named_pitch_class, octave_number)
def _initialize_by_pitch_number_and_diatonic_pitch_class_name( self, pitch_number, diatonic_pitch_class_name): from abjad.tools import pitchtools accidental, octave_number = \ pitchtools.spell_pitch_number( pitch_number, diatonic_pitch_class_name) pitch_class_name = diatonic_pitch_class_name + \ accidental.abbreviation named_pitch_class = pitchtools.NamedPitchClass(pitch_class_name) self._initialize_by_named_pitch_class_and_octave_number( named_pitch_class, octave_number)
def named_pitch_class(self): r'''Named pitch-class of named pitch. :: >>> NamedPitch("cs''").named_pitch_class NamedPitchClass('cs') Returns named pitch-class. ''' from abjad.tools import pitchtools return pitchtools.NamedPitchClass(self)
def __init__(self, *args): from abjad.tools import pitchtools if args and \ isinstance(args[0], collections.Iterable) and \ not stringtools.is_string(args[0]) and \ len(args) == 1: args = args[0] if len(args) == 1: if isinstance(args[0], (int, float)): arg = mathtools.integer_equivalent_number_to_integer( float(args[0])) self._initialize_by_pitch_number(arg) elif isinstance(args[0], type(self)): self._initialize_by_named_pitch(*args) elif isinstance(args[0], pitchtools.NumberedPitch): self._initialize_by_pitch_number( args[0].pitch_number) elif isinstance(args[0], pitchtools.PitchClass): self._initialize_by_named_pitch_class_and_octave_number( pitchtools.NamedPitchClass(args[0]), 4) elif hasattr(args[0], 'named_pitch'): self._initialize_by_named_pitch(args[0].named_pitch) elif self.is_pitch_class_octave_number_string(args[0]): self._initialize_by_pitch_class_octave_number_string(*args) elif isinstance(args[0], str): self._initialize_by_pitch_name(*args) else: message = 'can not initialize {} from {!r}.' message = message.format(type(self).__name__, args) raise ValueError(message) elif len(args) == 2: if isinstance(args[0], str): self._initialize_by_pitch_class_name_and_octave_number(*args) elif isinstance(args[0], pitchtools.NamedPitchClass): self._initialize_by_named_pitch_class_and_octave_number(*args) elif isinstance(args[0], (int, float)): if isinstance(args[1], str): self._initialize_by_pitch_number_and_diatonic_pitch_class_name( *args) elif isinstance(args[1], pitchtools.NamedPitchClass): self._initialize_by_pitch_number_and_named_pitch_class(*args) else: raise TypeError else: message = 'can not initialize {}: {!r}.' message = message.format(type(self).__name__, args) raise ValueError(message) elif len(args) == 0: self._initialize_by_pitch_class_name_and_octave_number('c', 4) else: message = 'can not initialize {}: {!r}.' message = message.format(type(self).__name__, args) raise ValueError(message)
def named_pitch_class(self): r'''Named pitch-class corresponding to numbered pitch-class. :: >>> pitchtools.NumberedPitchClass(13).named_pitch_class NamedPitchClass('cs') Returns named pitch-class. ''' from abjad.tools import pitchtools return pitchtools.NamedPitchClass(self)
def voice_horizontally(self, initial_octave=4): r'''Voices pitch-class segment as pitch segment, with each pitch as close in distance to the previous pitch as possible. :: >>> pitch_classes = pitchtools.PitchClassSegment( ... "c b d e f g e b a c") >>> pitch_segment = pitch_classes.voice_horizontally() >>> show(pitch_segment) # doctest: +SKIP Returns pitch segment. ''' from abjad.tools import pitchtools initial_octave = pitchtools.Octave(initial_octave) pitches = [] if self: pitch_class = pitchtools.NamedPitchClass(self[0]) pitch = pitchtools.NamedPitch(pitch_class, initial_octave) pitches.append(pitch) for pitch_class in self[1:]: pitch_class = pitchtools.NamedPitchClass(pitch_class) pitch = pitchtools.NamedPitch(pitch_class, initial_octave) semitones = abs((pitch - pitches[-1]).semitones) while 6 < semitones: if pitch < pitches[-1]: pitch += 12 else: pitch -= 12 semitones = abs((pitch - pitches[-1]).semitones) pitches.append(pitch) if self.item_class is pitchtools.NamedPitchClass: item_class = pitchtools.NamedPitch else: item_class = pitchtools.NumberedPitch return pitchtools.PitchSegment( items=pitches, item_class=item_class, )
def voice_vertically(self, initial_octave=4): r'''Voices pitch-class segment as pitch segment, with each pitch always higher than the previous. :: >>> scale_degree_numbers = [1, 3, 5, 7, 9, 11, 13] >>> scale = tonalanalysistools.Scale('c', 'minor') >>> pitch_classes = pitchtools.PitchClassSegment(( ... scale.scale_degree_to_named_pitch_class(x) ... for x in scale_degree_numbers)) >>> pitch_segment = pitch_classes.voice_vertically() >>> pitch_segment PitchSegment(["c'", "ef'", "g'", "bf'", "d''", "f''", "af''"]) >>> show(pitch_segment) # doctest: +SKIP Returns pitch segment. ''' from abjad.tools import pitchtools initial_octave = pitchtools.Octave(initial_octave) pitches = [] if self: pitch_class = pitchtools.NamedPitchClass(self[0]) pitch = pitchtools.NamedPitch(pitch_class, initial_octave) pitches.append(pitch) for pitch_class in self[1:]: pitch_class = pitchtools.NamedPitchClass(pitch_class) pitch = pitchtools.NamedPitch(pitch_class, initial_octave) while pitch < pitches[-1]: pitch += 12 pitches.append(pitch) if self.item_class is pitchtools.NamedPitchClass: item_class = pitchtools.NamedPitch else: item_class = pitchtools.NumberedPitch return pitchtools.PitchSegment( items=pitches, item_class=item_class, )
def named_pitch_class_to_scale_degree(self, *args): r'''Changes named pitch-class to scale degree. Returns scale degree. ''' from abjad.tools import tonalanalysistools foreign_pitch_class = pitchtools.NamedPitchClass(*args) letter = foreign_pitch_class.diatonic_pitch_class_name for i, pc in enumerate(self): if pc.diatonic_pitch_class_name == letter: native_pitch_class = pc scale_degree_index = i scale_degree_number = scale_degree_index + 1 break native_pitch = pitchtools.NamedPitch(native_pitch_class, 4) foreign_pitch = pitchtools.NamedPitch(foreign_pitch_class, 4) accidental = foreign_pitch.accidental - native_pitch.accidental return tonalanalysistools.ScaleDegree(accidental, scale_degree_number)
def shadow_pitch_contour_reservoir(pitch_contour_reservoir): r'''Shadows pitch contour reservoir. ''' shadow_pitch_lookup = { pitchtools.NamedPitchClass('a'): -5, # add a P4 below pitchtools.NamedPitchClass('g'): -3, # add a m3 below pitchtools.NamedPitchClass('f'): -1, # add a m2 below pitchtools.NamedPitchClass('e'): -4, # add a M3 below pitchtools.NamedPitchClass('d'): -2, # add a M2 below pitchtools.NamedPitchClass('c'): -3, # add a m3 below pitchtools.NamedPitchClass('b'): -2, # add a M2 below } shadowed_reservoir = {} for instrument_name, pitch_contours in pitch_contour_reservoir.items(): # The viola does not receive any diads if instrument_name == 'Viola': shadowed_reservoir['Viola'] = pitch_contours continue shadowed_pitch_contours = [] for pitch_contour in pitch_contours[:-1]: shadowed_pitch_contour = [] for pitch in pitch_contour: pitch_class = pitch.named_pitch_class shadow_pitch = pitch + shadow_pitch_lookup[pitch_class] diad = (shadow_pitch, pitch) shadowed_pitch_contour.append(diad) shadowed_pitch_contours.append(tuple(shadowed_pitch_contour)) # treat the final contour differently: the last note does not become a diad final_shadowed_pitch_contour = [] for pitch in pitch_contours[-1][:-1]: pitch_class = pitch.named_pitch_class shadow_pitch = pitch + shadow_pitch_lookup[pitch_class] diad = (shadow_pitch, pitch) final_shadowed_pitch_contour.append(diad) final_shadowed_pitch_contour.append(pitch_contours[-1][-1]) shadowed_pitch_contours.append(tuple(final_shadowed_pitch_contour)) shadowed_reservoir[instrument_name] = tuple(shadowed_pitch_contours) return shadowed_reservoir
def __init__(self, root=None, *args): from abjad.tools import tonalanalysistools root = root or 'c' root = pitchtools.NamedPitchClass(root) chord_quality = tonalanalysistools.RootlessChordClass(*args) npcs = [] for hdi in chord_quality: mdi = pitchtools.NamedInterval(hdi) npc = root + mdi npcs.append(npc) bass = npcs[0] PitchClassSet.__init__( self, items=npcs, item_class=pitchtools.NamedPitchClass, ) self._root = root self._chord_quality = chord_quality self._bass = bass
def voice_pitch_class(self, pitch_class): r"""Voices `pitch_class`: .. container:: example Voices C three times: >>> pitch_range = abjad.PitchRange('[C4, C6]') >>> pitch_range.voice_pitch_class('c') (NamedPitch("c'"), NamedPitch("c''"), NamedPitch("c'''")) .. container:: example Voices B two times: >>> pitch_range = abjad.PitchRange('[C4, C6]') >>> pitch_range.voice_pitch_class('b') (NamedPitch("b'"), NamedPitch("b''")) .. container:: example Returns empty because B can not voice: >>> pitch_range = abjad.PitchRange('[C4, A4)') >>> pitch_range.voice_pitch_class('b') () Returns tuple of zero or more named pitches. """ from abjad.tools import pitchtools named_pitch_class = pitchtools.NamedPitchClass(pitch_class) pair = (named_pitch_class.name, self.start_pitch.octave.number) named_pitch = pitchtools.NamedPitch(pair) result = [] while named_pitch <= self.stop_pitch: if named_pitch in self: result.append(named_pitch) named_pitch += 12 return tuple(result)
def instantiate_pitch_and_interval_test_collection(): r'''Instantiate pitch and interval test collection: :: >>> for x in pitchtools.instantiate_pitch_and_interval_test_collection(): x ... NumberedInversionEquivalentIntervalClass(1) NamedInversionEquivalentIntervalClass('+M2') NumberedInterval(1) NumberedIntervalClass(1) NamedInterval('+M2') NamedIntervalClass('+M2') NamedPitch('c') NamedPitchClass('c') NumberedPitch(1) NumberedPitchClass(1) Use to test pitch and interval interface consistency. Returns list. ''' from abjad.tools import pitchtools result = [] result.append(pitchtools.NumberedInversionEquivalentIntervalClass(1)) result.append(pitchtools.NamedInversionEquivalentIntervalClass('M2')) result.append(pitchtools.NumberedInterval(1)) result.append(pitchtools.NumberedIntervalClass(1)) result.append(pitchtools.NamedInterval('M2')) result.append(pitchtools.NamedIntervalClass('M2')) result.append(pitchtools.NamedPitch('c')) result.append(pitchtools.NamedPitchClass('c')) result.append(pitchtools.NumberedPitch(1)) result.append(pitchtools.NumberedPitchClass(1)) return result
def voice_pitch_classes( self, pitch_classes, allow_open_strings=True, ): r"""Voices `pitch_classes`. .. container:: example **Example 1.** :: >>> tuning = indicatortools.Tuning(('G3', 'D4', 'A4', 'E5')) >>> voicings = tuning.voice_pitch_classes(('a',)) >>> for voicing in voicings: ... voicing ... (NamedPitch('a'), None, None, None) (NamedPitch("a'"), None, None, None) (None, NamedPitch("a'"), None, None) (None, NamedPitch("a''"), None, None) (None, None, NamedPitch("a'"), None) (None, None, NamedPitch("a''"), None) (None, None, NamedPitch("a'''"), None) (None, None, None, NamedPitch("a''")) (None, None, None, NamedPitch("a'''")) :: >>> voicings = tuning.voice_pitch_classes( ... ('a', 'd'), ... allow_open_strings=False, ... ) >>> for voicing in voicings: ... voicing ... (NamedPitch('a'), NamedPitch("d''"), None, None) (NamedPitch('a'), NamedPitch("d'''"), None, None) (NamedPitch('a'), None, NamedPitch("d''"), None) (NamedPitch('a'), None, NamedPitch("d'''"), None) (NamedPitch('a'), None, None, NamedPitch("d'''")) (NamedPitch('a'), None, None, NamedPitch("d''''")) (NamedPitch("d'"), NamedPitch("a'"), None, None) (NamedPitch("d'"), NamedPitch("a''"), None, None) (NamedPitch("d'"), None, NamedPitch("a''"), None) (NamedPitch("d'"), None, NamedPitch("a'''"), None) (NamedPitch("d'"), None, None, NamedPitch("a''")) (NamedPitch("d'"), None, None, NamedPitch("a'''")) (NamedPitch("a'"), NamedPitch("d''"), None, None) (NamedPitch("a'"), NamedPitch("d'''"), None, None) (NamedPitch("a'"), None, NamedPitch("d''"), None) (NamedPitch("a'"), None, NamedPitch("d'''"), None) (NamedPitch("a'"), None, None, NamedPitch("d'''")) (NamedPitch("a'"), None, None, NamedPitch("d''''")) (NamedPitch("d''"), NamedPitch("a'"), None, None) (NamedPitch("d''"), NamedPitch("a''"), None, None) (NamedPitch("d''"), None, NamedPitch("a''"), None) (NamedPitch("d''"), None, NamedPitch("a'''"), None) (NamedPitch("d''"), None, None, NamedPitch("a''")) (NamedPitch("d''"), None, None, NamedPitch("a'''")) (None, NamedPitch("a'"), NamedPitch("d''"), None) (None, NamedPitch("a'"), NamedPitch("d'''"), None) (None, NamedPitch("a'"), None, NamedPitch("d'''")) (None, NamedPitch("a'"), None, NamedPitch("d''''")) (None, NamedPitch("d''"), NamedPitch("a''"), None) (None, NamedPitch("d''"), NamedPitch("a'''"), None) (None, NamedPitch("d''"), None, NamedPitch("a''")) (None, NamedPitch("d''"), None, NamedPitch("a'''")) (None, NamedPitch("a''"), NamedPitch("d''"), None) (None, NamedPitch("a''"), NamedPitch("d'''"), None) (None, NamedPitch("a''"), None, NamedPitch("d'''")) (None, NamedPitch("a''"), None, NamedPitch("d''''")) (None, NamedPitch("d'''"), NamedPitch("a''"), None) (None, NamedPitch("d'''"), NamedPitch("a'''"), None) (None, NamedPitch("d'''"), None, NamedPitch("a''")) (None, NamedPitch("d'''"), None, NamedPitch("a'''")) (None, None, NamedPitch("d''"), NamedPitch("a''")) (None, None, NamedPitch("d''"), NamedPitch("a'''")) (None, None, NamedPitch("a''"), NamedPitch("d'''")) (None, None, NamedPitch("a''"), NamedPitch("d''''")) (None, None, NamedPitch("d'''"), NamedPitch("a''")) (None, None, NamedPitch("d'''"), NamedPitch("a'''")) (None, None, NamedPitch("a'''"), NamedPitch("d'''")) (None, None, NamedPitch("a'''"), NamedPitch("d''''")) Returns tuple of sequences. """ from abjad.tools import pitchtools pitch_classes = [pitchtools.NamedPitchClass(x) for x in pitch_classes] pitch_classes.extend([None] * (len(self.pitches) - len(pitch_classes))) permutations = set([ tuple(x) for x in sequencetools.yield_all_permutations_of_sequence( pitch_classes) ]) pitch_ranges = self.pitch_ranges result = [] for permutation in permutations: sequences = [] for pitch_range, pitch_class in zip(pitch_ranges, permutation): if pitch_class is None: sequences.append([None]) continue pitches = pitch_range.voice_pitch_class(pitch_class) if not allow_open_strings: pitches = [ pitch for pitch in pitches if pitch != pitch_range.start_pitch ] if not pitches: pitches = [None] sequences.append(pitches) subresult = sequencetools.yield_outer_product_of_sequences( sequences) subresult = [tuple(x) for x in subresult] result.extend(subresult) result.sort() return tuple(result)
def t_PITCHNAME(self, t): r'[a-g](ff|ss|f|s|tqf|tqs|qs|qf)?' t.value = pitchtools.NamedPitchClass(t.value) return t
def _initialize_by_string(self, expr): from abjad.tools import pitchtools named_pitch_class = pitchtools.NamedPitchClass(expr) self._initialize_by_named_pitch_class(named_pitch_class)
def make_notes(self, n=None, written_duration=None): r'''Make first `n` notes in pitch class segment. Set `n` equal to `n` or length of segment. Set `written_duration` equal to `written_duration` or ``1/8``: :: >>> pitch_class_segment = pitchtools.PitchClassSegment( ... [2, 4.5, 6, 11, 4.5, 10]) :: >>> notes = pitch_class_segment.make_notes() >>> staff = Staff(notes) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { d'8 eqs'8 fs'8 b'8 eqs'8 bf'8 } Allow nonassignable `written_duration`: :: >>> notes = pitch_class_segment.make_notes(4, Duration(5, 16)) >>> staff = Staff(notes) >>> time_signature = TimeSignature((5, 4)) >>> attach(time_signature, staff) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { \time 5/4 d'4 ~ d'16 eqs'4 ~ eqs'16 fs'4 ~ fs'16 b'4 ~ b'16 } Returns list of notes. ''' from abjad.tools import scoretools from abjad.tools import pitchtools n = n or len(self) written_duration = written_duration or durationtools.Duration(1, 8) result = scoretools.make_notes([0] * n, [written_duration]) for i, logical_tie in enumerate(iterate(result).by_logical_tie()): pitch_class = pitchtools.NamedPitchClass(self[i % len(self)]) pitch = pitchtools.NamedPitch(pitch_class, 4) for note in logical_tie: note.written_pitch = pitch return result
def _initialize_by_pitch_number(self, pitch_number): from abjad.tools import pitchtools named_pitch_class = pitchtools.NamedPitchClass(pitch_number) octave_number = pitch_number // 12 + 4 self._initialize_by_named_pitch_class_and_octave_number( named_pitch_class, octave_number)
def helper(x, y): return cmp(pitchtools.NamedPitch(pitchtools.NamedPitchClass(x), 0), pitchtools.NamedPitch(pitchtools.NamedPitchClass(y), 0))