def list_ordered_named_pitch_pairs_from_expr_1_to_expr_2(expr_1, expr_2):
    '''List ordered named pitch pairs from `expr_1` to `expr_2`:

    ::

        >>> chord_1 = Chord([0, 1, 2], (1, 4))
        >>> chord_2 = Chord([3, 4], (1, 4))

    ::

        >>> for pair in pitchtools.list_ordered_named_pitch_pairs_from_expr_1_to_expr_2(
        ...     chord_1, chord_2):
        ...     pair
        (NamedPitch("c'"), NamedPitch("ef'"))
        (NamedPitch("c'"), NamedPitch("e'"))
        (NamedPitch("cs'"), NamedPitch("ef'"))
        (NamedPitch("cs'"), NamedPitch("e'"))
        (NamedPitch("d'"), NamedPitch("ef'"))
        (NamedPitch("d'"), NamedPitch("e'"))

    Returns generator.
    '''
    from abjad.tools import pitchtools

    pitches_1 = sorted(pitchtools.list_named_pitches_in_expr(expr_1))
    pitches_2 = sorted(pitchtools.list_named_pitches_in_expr(expr_2))
    for pair in sequencetools.yield_all_pairs_between_sequences(pitches_1, pitches_2):
        yield pair
def list_ordered_named_pitch_pairs_from_expr_1_to_expr_2(expr_1, expr_2):
    '''Lists ordered named pitch pairs from `expr_1` to `expr_2`.

    ::

        >>> chord_1 = Chord([0, 1, 2], (1, 4))
        >>> chord_2 = Chord([3, 4], (1, 4))

    ::

        >>> for pair in pitchtools.list_ordered_named_pitch_pairs_from_expr_1_to_expr_2(
        ...     chord_1, chord_2):
        ...     pair
        (NamedPitch("c'"), NamedPitch("ef'"))
        (NamedPitch("c'"), NamedPitch("e'"))
        (NamedPitch("cs'"), NamedPitch("ef'"))
        (NamedPitch("cs'"), NamedPitch("e'"))
        (NamedPitch("d'"), NamedPitch("ef'"))
        (NamedPitch("d'"), NamedPitch("e'"))

    Returns generator.
    '''
    from abjad.tools import pitchtools

    pitches_1 = sorted(pitchtools.list_named_pitches_in_expr(expr_1))
    pitches_2 = sorted(pitchtools.list_named_pitches_in_expr(expr_2))
    for pair in sequencetools.yield_all_pairs_between_sequences(pitches_1, pitches_2):
        yield pair
def list_unordered_named_pitch_pairs_in_expr(expr):
    '''List unordered named pitch pairs in `expr`:

    ::

        >>> chord = Chord("<c' cs' d' ef'>4")

    ::

        >>> for pair in pitchtools.list_unordered_named_pitch_pairs_in_expr(chord):
        ...     pair
        ...
        (NamedPitch("c'"), NamedPitch("cs'"))
        (NamedPitch("c'"), NamedPitch("d'"))
        (NamedPitch("c'"), NamedPitch("ef'"))
        (NamedPitch("cs'"), NamedPitch("d'"))
        (NamedPitch("cs'"), NamedPitch("ef'"))
        (NamedPitch("d'"), NamedPitch("ef'"))

    Returns generator.
    '''
    from abjad.tools import pitchtools

    for pair in sequencetools.yield_all_unordered_pairs_of_sequence(
        sorted(pitchtools.list_named_pitches_in_expr(expr))):
        yield pair
Esempio n. 4
0
    def __illustrate__(self):
        r'''Attempts to illustrate selection.

        Evaluates the storage format of the selection (to sever any references
        to the source score from which the selection was taken). Then tries to
        wrap the result in a staff; in the case that notes of only C4 are found
        then sets the staff context name to ``'RhythmicStaff'``. If this works
        then the staff is wrapped in a LilyPond file and the file is returned.
        If this doesn't work then the method raises an exception.

        The idea is that the illustration should work for simple selections of
        that represent an essentially contiguous snippet of a single voice of
        music.

        Returns LilyPond file.
        '''
        from abjad.tools import lilypondfiletools
        from abjad.tools import markuptools
        from abjad.tools import pitchtools
        from abjad.tools import scoretools
        from abjad.tools.topleveltools import mutate
        music = mutate(self).copy()
        staff = scoretools.Staff(music)
        found_different_pitch = False
        for pitch in pitchtools.list_named_pitches_in_expr(staff):
            if pitch != pitchtools.NamedPitch("c'"):
                found_different_pitch = True
                break
        if not found_different_pitch:
            staff.context_name = 'RhythmicStaff'
        score = scoretools.Score([staff])
        lilypond_file = lilypondfiletools.make_basic_lilypond_file(score)
        lilypond_file.header_block.tagline = markuptools.Markup('""')
        return lilypond_file
Esempio n. 5
0
    def __contains__(self, arg):
        r'''Is true when pitch range contains `arg`. Otherwise false.

        Returns boolean.
        '''
        from abjad.tools import pitchtools
        from abjad.tools import scoretools
        if hasattr(arg, '_has_effective_indicator') and \
            arg._has_effective_indicator(indicatortools.IsUnpitched):
            return True
        elif isinstance(arg, (int, long, float)):
            pitch = pitchtools.NamedPitch(arg)
            return self._contains_pitch(pitch)
        elif isinstance(arg, pitchtools.NamedPitch):
            return self._contains_pitch(arg)
        elif isinstance(arg, scoretools.Note):
            sounding_pitch = inspect_(arg).get_sounding_pitch()
            return self._contains_pitch(sounding_pitch)
        elif isinstance(arg, scoretools.Chord):
            sounding_pitches = inspect_(arg).get_sounding_pitches()
            return all(self._contains_pitch(x) for x in sounding_pitches)
        elif isinstance(arg, (scoretools.Rest, scoretools.Skip)):
            return True
        elif isinstance(arg, scoretools.Container):
            return all(x in self for x in arg.select_leaves())
        else:
            pitches = pitchtools.list_named_pitches_in_expr(arg)
            if pitches:
                return all(self._contains_pitch(x) for x in pitches)
            else:
                try:
                    return all(self._contains_pitch(x) for x in arg)
                except TypeError:
                    return False
        return False
Esempio n. 6
0
def list_unordered_named_pitch_pairs_in_expr(expr):
    '''Lists unordered named pitch pairs in `expr`.

    ::

        >>> chord = Chord("<c' cs' d' ef'>4")

    ::

        >>> for pair in pitchtools.list_unordered_named_pitch_pairs_in_expr(chord):
        ...     pair
        ...
        (NamedPitch("c'"), NamedPitch("cs'"))
        (NamedPitch("c'"), NamedPitch("d'"))
        (NamedPitch("c'"), NamedPitch("ef'"))
        (NamedPitch("cs'"), NamedPitch("d'"))
        (NamedPitch("cs'"), NamedPitch("ef'"))
        (NamedPitch("d'"), NamedPitch("ef'"))

    Returns generator.
    '''
    from abjad.tools import pitchtools

    for pair in sequencetools.yield_all_unordered_pairs_of_sequence(
            sorted(pitchtools.list_named_pitches_in_expr(expr))):
        yield pair
Esempio n. 7
0
    def __illustrate__(self):
        r'''Attempts to illustrate selection.

        Evaluates the storage format of the selection (to sever any references
        to the source score from which the selection was taken). Then tries to
        wrap the result in a staff; in the case that notes of only C4 are found
        then sets the staff context name to ``'RhythmicStaff'``. If this works
        then the staff is wrapped in a LilyPond file and the file is returned.
        If this doesn't work then the method raises an exception.

        The idea is that the illustration should work for simple selections of
        that represent an essentially contiguous snippet of a single voice of
        music.

        Returns LilyPond file.
        '''
        from abjad.tools import lilypondfiletools
        from abjad.tools import markuptools
        from abjad.tools import pitchtools
        from abjad.tools import scoretools
        from abjad.tools.topleveltools import mutate
        music = mutate(self).copy()
        staff = scoretools.Staff(music)
        found_different_pitch = False
        for pitch in pitchtools.list_named_pitches_in_expr(staff):
            if pitch != pitchtools.NamedPitch("c'"):
                found_different_pitch = True
                break
        if not found_different_pitch:
            staff.context_name = 'RhythmicStaff'
        score = scoretools.Score([staff])
        lilypond_file = lilypondfiletools.make_basic_lilypond_file(score)
        lilypond_file.header_block.tagline = markuptools.Markup('""')
        return lilypond_file
Esempio n. 8
0
 def __contains__(self, arg):
     from abjad.tools import containertools
     from abjad.tools import pitchtools
     from abjad.tools import resttools
     from abjad.tools import skiptools
     from abjad.tools.scoretools.Chord import Chord
     from abjad.tools.notetools.Note import Note
     if getattr(arg, 'written_pitch_indication_is_nonsemantic', False):
         return True
     elif isinstance(arg, (int, long, float)):
         pitch = pitchtools.NamedPitch(arg)
         return self._contains_pitch(pitch)
     elif isinstance(arg, pitchtools.NamedPitch):
         return self._contains_pitch(arg)
     elif isinstance(arg, Note):
         return self._contains_pitch(arg.sounding_pitch)
     elif isinstance(arg, Chord):
         return all(self._contains_pitch(x) for x in arg.sounding_pitches)
     elif isinstance(arg, (resttools.Rest, skiptools.Skip)):
         return True
     elif isinstance(arg, containertools.Container):
         return all(x in self for x in arg.select_leaves())
     else:
         pitches = pitchtools.list_named_pitches_in_expr(arg)
         if pitches:
             return all(self._contains_pitch(x) for x in pitches)
         else:
             try:
                 return all(self._contains_pitch(x) for x in arg)
             except TypeError:
                 return False
     return False
Esempio n. 9
0
def list_pitch_numbers_in_expr(expr):
    '''Lists pitch numbers in `expr`.

    ::

        >>> tuplet = scoretools.FixedDurationTuplet(Duration(2, 8), "c'8 d'8 e'8")
        >>> pitchtools.list_pitch_numbers_in_expr(tuplet)
        (0, 2, 4)

    Returns tuple of zero or more numbers.
    '''
    from abjad.tools import pitchtools

    pitches = pitchtools.list_named_pitches_in_expr(expr)

    pitch_numbers = [pitch.pitch_number for pitch in pitches]
    pitch_numbers = tuple(pitch_numbers)

    return pitch_numbers
Esempio n. 10
0
def list_pitch_numbers_in_expr(expr):
    '''List pitch numbers in `expr`:

    ::

        >>> tuplet = scoretools.FixedDurationTuplet(Duration(2, 8), "c'8 d'8 e'8")
        >>> pitchtools.list_pitch_numbers_in_expr(tuplet)
        (0, 2, 4)

    Returns tuple of zero or more numbers.
    '''
    from abjad.tools import pitchtools

    pitches = pitchtools.list_named_pitches_in_expr(expr)

    pitch_numbers = [pitch.pitch_number for pitch in pitches]
    pitch_numbers = tuple(pitch_numbers)

    return pitch_numbers
Esempio n. 11
0
    def __contains__(self, arg):
        r'''Is true when pitch range contains `arg`. Otherwise false.

        Returns boolean.
        '''
        from abjad.tools import pitchtools
        from abjad.tools import scoretools
        if (hasattr(arg, '_has_effective_indicator') and
            arg._has_effective_indicator(indicatortools.IsUnpitched)):
            return True
        elif isinstance(arg, (int, float)):
            pitch = pitchtools.NamedPitch(arg)
            return self._contains_pitch(pitch)
        elif isinstance(arg, pitchtools.NamedPitch):
            return self._contains_pitch(arg)
        elif isinstance(arg, scoretools.Note):
            sounding_pitch = inspect_(arg).get_sounding_pitch()
            return self._contains_pitch(sounding_pitch)
        elif isinstance(arg, scoretools.Chord):
            sounding_pitches = inspect_(arg).get_sounding_pitches()
            return all(self._contains_pitch(x) for x in sounding_pitches)
            try:
                arg = type(self)(arg)
                return self.__lt__(arg)
            except TypeError:
                pass
        elif isinstance(arg, (scoretools.Rest, scoretools.Skip)):
            return True
        elif isinstance(arg, scoretools.Container):
            return all(x in self for x in arg.select_leaves())
        else:
            pitches = pitchtools.list_named_pitches_in_expr(arg)
            if pitches:
                return all(self._contains_pitch(x) for x in pitches)
            else:
                try:
                    return all(self._contains_pitch(x) for x in arg)
                except TypeError:
                    return False
        return False
Esempio n. 12
0
    def from_selection(selection):
        r'''Makes clef from `selection`.

        ..  container:: example

            ::

                >>> numbers = list(range(-12, -6))
                >>> notes = scoretools.make_notes(numbers, [Duration(1, 4)])
                >>> staff = Staff(notes)
                >>> Clef.from_selection(staff)
                Clef(name='bass')

            Choses between treble and bass based on minimal number of ledger 
            lines.

        Returns new clef.
        '''
        from abjad.tools import pitchtools
        pitches = pitchtools.list_named_pitches_in_expr(selection)
        diatonic_pitch_numbers = [
            pitch.diatonic_pitch_number for pitch in pitches
        ]
        max_diatonic_pitch_number = max(diatonic_pitch_numbers)
        min_diatonic_pitch_number = min(diatonic_pitch_numbers)
        lowest_treble_line_pitch = pitchtools.NamedPitch('e', 4)
        lowest_treble_line_diatonic_pitch_number = \
            lowest_treble_line_pitch.diatonic_pitch_number
        candidate_steps_below_treble = \
            lowest_treble_line_diatonic_pitch_number - min_diatonic_pitch_number
        highest_bass_line_pitch = pitchtools.NamedPitch('a', 3)
        highest_bass_line_diatonic_pitch_number = \
            highest_bass_line_pitch.diatonic_pitch_number
        candidate_steps_above_bass = max_diatonic_pitch_number - highest_bass_line_diatonic_pitch_number
        if candidate_steps_above_bass < candidate_steps_below_treble:
            return Clef('bass')
        else:
            return Clef('treble')
Esempio n. 13
0
def suggest_clef_for_named_pitches(pitches):
    '''Suggest clef for named `pitches`:

    ::

        >>> staff = Staff(scoretools.make_notes(
        ...     list(range(-12, -6)), [(1, 4)]))
        >>> pitchtools.suggest_clef_for_named_pitches(staff)
        Clef(name='bass')

    Suggest clef based on minimal number of ledger lines.

    Returns clef.
    '''
    from abjad.tools import indicatortools
    from abjad.tools import pitchtools

    pitches = pitchtools.list_named_pitches_in_expr(pitches)

    diatonic_pitch_numbers = [pitch.diatonic_pitch_number for pitch in pitches]
    max_diatonic_pitch_number = max(diatonic_pitch_numbers)
    min_diatonic_pitch_number = min(diatonic_pitch_numbers)

    lowest_treble_line_pitch = pitchtools.NamedPitch('e', 4)
    lowest_treble_line_diatonic_pitch_number = \
        lowest_treble_line_pitch.diatonic_pitch_number
    candidate_steps_below_treble = \
        lowest_treble_line_diatonic_pitch_number - min_diatonic_pitch_number

    highest_bass_line_pitch = pitchtools.NamedPitch('a', 3)
    highest_bass_line_diatonic_pitch_number = \
        highest_bass_line_pitch.diatonic_pitch_number
    candidate_steps_above_bass = max_diatonic_pitch_number - highest_bass_line_diatonic_pitch_number

    if candidate_steps_above_bass < candidate_steps_below_treble:
        return indicatortools.Clef('bass')
    else:
        return indicatortools.Clef('treble')
Esempio n. 14
0
    def from_selection(selection):
        r'''Makes clef from `selection`.

        ..  container:: example

            ::

                >>> numbers = list(range(-12, -6))
                >>> notes = scoretools.make_notes(numbers, [Duration(1, 4)])
                >>> staff = Staff(notes)
                >>> Clef.from_selection(staff)
                Clef(name='bass')

            Choses between treble and bass based on minimal number of ledger 
            lines.

        Returns new clef.
        '''
        from abjad.tools import pitchtools
        pitches = pitchtools.list_named_pitches_in_expr(selection)
        diatonic_pitch_numbers = [pitch.diatonic_pitch_number for pitch in pitches]
        max_diatonic_pitch_number = max(diatonic_pitch_numbers)
        min_diatonic_pitch_number = min(diatonic_pitch_numbers)
        lowest_treble_line_pitch = pitchtools.NamedPitch('e', 4)
        lowest_treble_line_diatonic_pitch_number = \
            lowest_treble_line_pitch.diatonic_pitch_number
        candidate_steps_below_treble = \
            lowest_treble_line_diatonic_pitch_number - min_diatonic_pitch_number
        highest_bass_line_pitch = pitchtools.NamedPitch('a', 3)
        highest_bass_line_diatonic_pitch_number = \
            highest_bass_line_pitch.diatonic_pitch_number
        candidate_steps_above_bass = max_diatonic_pitch_number - highest_bass_line_diatonic_pitch_number
        if candidate_steps_above_bass < candidate_steps_below_treble:
            return Clef('bass')
        else:
            return Clef('treble')
Esempio n. 15
0
    def from_score(score, populate=True):
        r'''Makes pitch array from `score`.

        ..  container:: example

            **Example 1.** Makes empty pitch array from score:

            ::

                >>> score = Score([])
                >>> score.append(Staff("c'8 d'8 e'8 f'8"))
                >>> score.append(Staff("c'4 d'4"))
                >>> score.append(
                ...     Staff(
                ...     scoretools.FixedDurationTuplet(
                ...     Duration(2, 8), "c'8 d'8 e'8") * 2))

            ..  doctest::

                >>> print(format(score))
                \new Score <<
                    \new Staff {
                        c'8
                        d'8
                        e'8
                        f'8
                    }
                    \new Staff {
                        c'4
                        d'4
                    }
                    \new Staff {
                        \times 2/3 {
                            c'8
                            d'8
                            e'8
                        }
                        \times 2/3 {
                            c'8
                            d'8
                            e'8
                        }
                    }
                >>

            ::

                >>> show(score) # doctest: +SKIP

            ::

                >>> array = pitchtools.PitchArray.from_score(
                ...     score, populate=False)

            ::

                >>> print(array)
                [     ] [     ] [     ] [     ]
                [                 ] [                 ]
                [ ] [     ] [ ] [ ] [     ] [ ]

        ..  container:: example

            **Example 2.** Makes populated pitch array from `score`:

            ::

                >>> score = Score([])
                >>> score.append(Staff("c'8 d'8 e'8 f'8"))
                >>> score.append(Staff("c'4 d'4"))
                >>> score.append(
                ...     Staff(
                ...     scoretools.FixedDurationTuplet(
                ...     Duration(2, 8), "c'8 d'8 e'8") * 2))

            ..  doctest::

                >>> print(format(score))
                \new Score <<
                    \new Staff {
                        c'8
                        d'8
                        e'8
                        f'8
                    }
                    \new Staff {
                        c'4
                        d'4
                    }
                    \new Staff {
                        \times 2/3 {
                            c'8
                            d'8
                            e'8
                        }
                        \times 2/3 {
                            c'8
                            d'8
                            e'8
                        }
                    }
                >>

            ::

                >>> show(score) # doctest: +SKIP

            ::

                >>> array = pitchtools.PitchArray.from_score(
                ...     score, populate=True)

            ::

                >>> print(array)
                [c'     ] [d'     ] [e'     ] [f'     ]
                [c'                   ] [d'                   ]
                [c'] [d'     ] [e'] [c'] [d'     ] [e']

        Returns pitch array.
        '''
        from abjad.tools import pitchtools
        from abjad.tools import scoretools
        time_intervals = \
            PitchArray._get_composite_offset_difference_series_from_leaves_in_expr(
            score)
        array_width = len(time_intervals)
        array_depth = len(score)
        pitch_array = PitchArray(array_depth, array_width)
        items = scoretools.make_multiplied_quarter_notes(
            [0], time_intervals)
        for leaf_iterable, pitch_array_row in \
            zip(score, pitch_array.rows):
            durations = []
            for leaf in iterate(leaf_iterable).by_class(scoretools.Leaf):
                durations.append(leaf._get_duration())
            parts = mutate(items).split(
                durations,
                cyclic=False,
                fracture_spanners=False,
                )
            part_lengths = [len(part) for part in parts]
            cells = pitch_array_row.cells
            grouped_cells = sequencetools.partition_sequence_by_counts(
                cells,
                part_lengths,
                cyclic=False,
                overhang=False,
                )
            for group in grouped_cells:
                pitch_array_row.merge(group)
            leaves = iterate(leaf_iterable).by_class(scoretools.Leaf)
            if populate:
                for cell, leaf in zip(pitch_array_row.cells, leaves):
                    cell.pitches.extend(
                        pitchtools.list_named_pitches_in_expr(leaf))
        return pitch_array