Ejemplo n.º 1
0
    def octave(self):
        r'''Octave of numbered pitch.

        ::

            >>> pitchtools.NumberedPitch(13).octave
            Octave(5)

        Returns octave.
        '''
        from abjad.tools import pitchtools
        return pitchtools.Octave(self.octave_number)
Ejemplo n.º 2
0
    def octave(self):
        r'''Octave of named pitch.

        ::

            >>> NamedPitch("cs''").octave
            Octave(5)

        Returns octave.
        '''
        from abjad.tools import pitchtools
        return pitchtools.Octave(self._octave_number)
Ejemplo n.º 3
0
    def octave(self):
        r'''Gets octave of numbered pitch.

        ..  container:: example

            >>> abjad.NumberedPitch(13).octave
            Octave(5)

        Returns octave.
        '''
        from abjad.tools import pitchtools
        number = self._number // 12 + 4
        return pitchtools.Octave(number=number)
Ejemplo n.º 4
0
    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,
        )
Ejemplo n.º 5
0
    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,
        )
Ejemplo n.º 6
0
def make_n_middle_c_centered_pitches(n):
    '''Make `n` middle-c centered pitches, where 0 < `n`:

    ::

        >>> for p in pitchtools.make_n_middle_c_centered_pitches(5): p
        NamedPitch('f')
        NamedPitch('a')
        NamedPitch("c'")
        NamedPitch("e'")
        NamedPitch("g'")

    ::

        >>> for p in pitchtools.make_n_middle_c_centered_pitches(4): p
        NamedPitch('g')
        NamedPitch('b')
        NamedPitch("d'")
        NamedPitch("f'")

    Returns list of zero or more named pitches.
    '''
    from abjad.tools import pitchtools

    if n == 0:
        return []
    indices = list(range(0, 2 * abs(n), 2))
    if n < 0:
        indices.reverse()
    average = int(sum(indices) / float(abs(n)))
    centered = [x - average for x in indices]
    letters = ['c', 'd', 'e', 'f', 'g', 'a', 'b']
    tups = [divmod(x, 7) for x in centered]
    pitch_names = [
        letters[x[1]] + str(pitchtools.Octave(x[0] + 4)) for x in tups
    ]
    return [pitchtools.NamedPitch(x) for x in pitch_names]
Ejemplo n.º 7
0
 def _from_pitch_class_octave(pitch_class, octave):
     from abjad.tools import pitchtools
     pitch_class = pitchtools.NumberedPitchClass(pitch_class)
     octave = pitchtools.Octave(octave)
     number = 12 * (octave.number - 4) + pitch_class.number
     return NumberedPitch(number)
Ejemplo n.º 8
0
    def _transpose_enharmonically(pitch_a, pitch_b, pitch_c):
        r'''Transpose `pitch_c` by the distance between `pitch_b`
        and `pitch_a`.

        This function was reverse-engineered from LilyPond's source code.

        Returns named pitch.
        '''
        def normalize_alteration(step, alteration):
            while 2. < alteration:
                alteration -= step_size(step)
                step += 1.
            while alteration < -2.:
                step -= 1.
                alteration += step_size(step)
            return step, alteration

        def normalize_octave(octave, step):
            normalized_step = step % len(scale)
            octave += (step - normalized_step) / len(scale)
            return octave, normalized_step

        def step_size(step):
            normalized_step = step % len(scale)
            if normalized_step == 6:
                return 1.  # b to c
            return scale[normalized_step + 1] - scale[normalized_step]

        if not isinstance(pitch_a, pitchtools.NamedPitch):
            pitch_a = pitchtools.NamedPitch(pitch_a)
        if not isinstance(pitch_b, pitchtools.NamedPitch):
            pitch_b = pitchtools.NamedPitch(pitch_b)
        if not isinstance(pitch_c, pitchtools.NamedPitch):
            pitch_c = pitchtools.NamedPitch(pitch_c)
        scale = [0., 2., 4., 5., 7., 9., 11.]
        a_oct, a_step, a_alt = pitch_a.octave_number, \
            pitch_a.diatonic_pitch_class_number, pitch_a.accidental.semitones
        b_oct, b_step, b_alt = pitch_b.octave_number, \
            pitch_b.diatonic_pitch_class_number, pitch_b.accidental.semitones
        c_oct, c_step, c_alt = pitch_c.octave_number, \
            pitch_c.diatonic_pitch_class_number, pitch_c.accidental.semitones
        d_oct, d_step, d_alt, d_tones = b_oct - a_oct, b_step - a_step, \
            b_alt - a_alt, float(pitch_b) - float(pitch_a)
        tmp_alt = float(pitch_c) + d_tones
        # print 'TMP_ALT: %f' % tmp_alt
        new_oct = c_oct + d_oct
        new_step = c_step + d_step
        new_alt = c_alt
        # print 'NEW:', new_oct, new_step, new_alt
        new_step, new_alt = normalize_alteration(new_step, new_alt)
        new_oct, new_step = normalize_octave(new_oct, new_step)
        # print 'NEW(norm):', new_oct, new_step, new_alt
        octave_ticks = str(pitchtools.Octave(new_oct))
        pitch_class_name = \
            pitchtools.PitchClass._diatonic_pitch_class_number_to_diatonic_pitch_class_name[
                new_step % 7]
        #pitch_class_name = str(pitchtools.NamedDiatonicPitchClass(
        #    int(new_step)))
        accidental = str(pitchtools.Accidental(new_alt))
        tmp_pitch = pitchtools.NamedPitch(pitch_class_name + accidental +
                                          octave_ticks)
        # print 'TMP(pitch): %r' % tmp_pitch
        new_alt += tmp_alt - float(tmp_pitch)
        # print 'NEW(alt): %f' % new_alt
        new_step, new_alt = normalize_alteration(new_step, new_alt)
        new_oct, new_step = normalize_octave(new_oct, new_step)
        # print 'NEW(norm):', new_oct, new_step, new_alt
        octave_ticks = str(pitchtools.Octave(new_oct))
        #pitch_class_name = str(pitchtools.NamedDiatonicPitchClass(
        #    int(new_step)))
        pitch_class_name = \
            pitchtools.PitchClass._diatonic_pitch_class_number_to_diatonic_pitch_class_name[
                new_step % 7]
        accidental = str(pitchtools.Accidental(new_alt))
        return pitchtools.NamedPitch(pitch_class_name + accidental +
                                     octave_ticks)