def _beam_rhythm_containers(self, rhythm_containers):
     beam_cells_together = getattr(
         self.source_expression, 'beam_cells_together', False)
     beam_each_cell = getattr(
         self.source_expression, 'beam_each_cell', False)
     if beam_cells_together:
         for container in iterationtools.iterate_components_in_expr(
             rhythm_containers):
             spanners = container._get_spanners()
             for spanner in spanners:
                 spanner.detach()
         durations = [x._get_duration() for x in rhythm_containers]
         beam = spannertools.DuratedComplexBeamSpanner(
             durations=durations, 
             span=1,
             )
         attach(beam, rhythm_containers)
     elif beam_each_cell:
         for container in iterationtools.iterate_components_in_expr(
             rhythm_containers):
             spanners = container._get_spanners()
             for spanner in spanners:
                 spanner.detach()
         for rhythm_container in rhythm_containers:
             beam = spannertools.DuratedComplexBeamSpanner(
                 durations=[rhythm_container._get_duration()], 
                 span=1,
                 )
             attach(beam, rhythm_container) 
Example #2
0
 def _get_my_last_leaf(self):
     from abjad.tools import iterationtools
     from abjad.tools import leaftools
     component_classes=(leaftools.Leaf,)
     for leaf in iterationtools.iterate_components_in_expr(
         self, component_class=component_classes, reverse=True):
         return leaf
def set_line_breaks_by_line_duration(
    expr,
    line_duration,
    line_break_class=None,
    kind='prolated',
    adjust_eol=False,
    add_empty_bars=False,
    ):
    r'''Iterate `line_break_class` instances in `expr` 
    and accumulate `kind` duration.

    Add line break after every total less than or equal to `line_duration`.

    Set `line_break_class` to measure when `line_break_class` is none.
    '''

    if line_break_class is None:
        line_break_class = measuretools.Measure

    prev = None
    cum_duration = durationtools.Duration(0)
    for cur in iterationtools.iterate_components_in_expr(
        expr, line_break_class):
        # compress these 4 lines to only the 4th line after duration migration
        if kind == 'seconds':
            current_duration = cur._get_duration(in_seconds=True)
        elif kind == 'prolated':
            current_duration = cur._get_duration()
        elif kind == 'preprolated':
            current_duration = cur._preprolated_duration
        else:
            current_duration = getattr(cur._get_duration(), kind)
        candidate_duration = cum_duration + current_duration
        if candidate_duration < line_duration:
            cum_duration += current_duration
        elif candidate_duration == line_duration:
            command = marktools.LilyPondCommandMark('break', 'closing')
            attach(command, cur)
            if adjust_eol:
                marktools.LilyPondCommandMark(
                    'adjustEOLTimeSignatureBarlineExtraOffset',
                    'closing')(cur)
            if add_empty_bars:
                if cur.bar_line.kind is None:
                    cur.bar_line.kind = ''
            cum_duration = durationtools.Duration(0)
        else:
            if prev is not None:
                command = marktools.LilyPondCommandMark('break', 'closing')
                attach(command, prev)
                if adjust_eol:
                    marktools.LilyPondCommandMark(
                        'adjustEOLTimeSignatureBarlineExtraOffset',
                        'closing')(prev)
                if add_empty_bars:
                    if cur.bar_line.kind is None:
                        cur.bar_line.kind = ''
            cum_duration = current_duration
        prev = cur
 def _make_spanner_schema(self):
     from abjad.tools import iterationtools
     schema = {}
     spanners = set()
     for component in iterationtools.iterate_components_in_expr(self):
         spanners.update(component._get_spanners())
     for spanner in spanners:
         schema[spanner] = []
     for i, component in \
         enumerate(iterationtools.iterate_components_in_expr(self)):
         attached_spanners = component._get_spanners()
         for attached_spanner in attached_spanners:
             try:
                 schema[attached_spanner].append(i)
             except KeyError:
                 pass
     return schema
def iterate_contexts_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterates contexts in `expr`.

    ::

        >>> staff = Staff([Voice("c'8 d'8"), Voice("e'8 f'8 g'8")])
        >>> Tuplet(Multiplier(2, 3), staff[1][:])
        Tuplet(2/3, [e'8, f'8, g'8])
        >>> staff.is_simultaneous = True

    ..  doctest::

        >>> f(staff)
        \new Staff <<
            \new Voice {
                c'8
                d'8
            }
            \new Voice {
                \times 2/3 {
                    e'8
                    f'8
                    g'8
                }
            }
        >>

    ::

        >>> for x in iterationtools.iterate_contexts_in_expr(staff):
        ...   x
        Staff<<2>>
        Voice{2}
        Voice{1}

    Iterate contexts backward in `expr`:

    ::

        >>> for x in iterationtools.iterate_contexts_in_expr(staff, reverse=True):
        ...   x
        Staff<<2>>
        Voice{1}
        Voice{2}

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=contexttools.Context,
        reverse=reverse,
        start=start,
        stop=stop,
        )
 def __call__(self, expr):
     assert len(self.swell_dynamics) == 5, repr(self.swell_dynamics)
     assert leaftools.all_are_leaves(expr), repr(expr)
     start_dynamic, left_hairpin, peak_dynamic, right_hairpin, stop_dynamic = self.swell_dynamics
     #leaves = list(iterationtools.iterate_leaves_in_expr(expr))
     #leaves = leaftools.remove_outer_rests_from_sequence(leaves)
     leaves = expr
     for leaf in iterationtools.iterate_components_in_expr(leaves):
         spanners = leaf._get_spanners(spannertools.HairpinSpanner)
         for spanner in spanners:
             spanner.detach()
     # TODO: this should eventually be changed to remove dynamic marks only
     for leaf in leaves:
         marktools.detach_lilypond_command_marks_attached_to_component(leaf)
     if 3 <= len(leaves):
         #contexttools.DynamicMark(start_dynamic)(leaves[0])
         #contexttools.DynamicMark(stop_dynamic)(leaves[-1])
         marktools.LilyPondCommandMark(start_dynamic, 'right')(leaves[0])
         marktools.LilyPondCommandMark(stop_dynamic, 'right')(leaves[-1])
         middle_index = int(len(leaves) / 2.0)
         middle_leaf = leaves[middle_index]
         #contexttools.DynamicMark(peak_dynamic)(middle_leaf)
         marktools.LilyPondCommandMark(peak_dynamic, 'right')(middle_leaf)
         half_count = middle_index + 1
         left_leaves = leaves[:half_count]
         if len(leaves) % 2 == 0:
             right_leaves = leaves[-middle_index:]
         else:
             right_leaves = leaves[-(middle_index+1):]
         if left_hairpin == '<':
             crescendo = spannertools.CrescendoSpanner()
             attach(crescendo, left_leaves)
         else:
             decrescendo = spannertools.DecrescendoSpanner()
             attach(decrescendo, left_leaves)
         if right_hairpin == '<':
             crescendo = spannertools.CrescendoSpanner()
             attach(crescendo, right_leaves)
         else:
             decrescendo = spannertools.DecrescendoSpanner()
             attach(decrescendo, right_leaves)
         return leaves
     elif len(leaves) == 2:
         command = marktools.LilyPondCommandMark(start_dynamic, 'right')
         attach(command, leaves[0])
         command = marktools.LilyPondCommandMark(peak_dynamic, 'right')
         attach(command, leaves[-1])
         if left_hairpin == '<':
             crescendo = spannertools.CrescendoSpanner()
             attach(crescendo, leaves)
         else:
             decrescendo = spannertools.DecrescendoSpanner()
             attach(decrescendo, leaves)
     elif len(leaves) == 1:
         command = marktools.LilyPondCommandMark(peak_dynamic, 'right')
         attach(command, leaves[0])
     else:
         raise ValueError(len(leaves))
def iterate_tuplets_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterates tuplets in `expr`.

    ::

        >>> staff = Staff("c'8 d'8 e'8 f'8 g'8 a'8 b'8 c''8")
        >>> Tuplet(Multiplier(2, 3), staff[:3])
        Tuplet(2/3, [c'8, d'8, e'8])
        >>> Tuplet(Multiplier(2, 3), staff[-3:])
        Tuplet(2/3, [a'8, b'8, c''8])

        >>> f(staff)
        \new Staff {
            \times 2/3 {
                c'8
                d'8
                e'8
            }
            f'8
            g'8
            \times 2/3 {
                a'8
                b'8
                c''8
            }
        }

        >>> for tuplet in iterationtools.iterate_tuplets_in_expr(staff):
        ...     tuplet
        ...
        Tuplet(2/3, [c'8, d'8, e'8])
        Tuplet(2/3, [a'8, b'8, c''8])

    Iterate tuplets backward in `expr`:

    ::

        >>> for tuplet in iterationtools.iterate_tuplets_in_expr(
        ...     staff, reverse=True):
        ...     tuplet
        ...
        Tuplet(2/3, [a'8, b'8, c''8])
        Tuplet(2/3, [c'8, d'8, e'8])

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=tuplettools.Tuplet,
        reverse=reverse,
        start=start,
        stop=stop,
        )
def iterate_voices_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate voices forward in `expr`:

    ::

        >>> voice_1 = Voice("c'8 d'8 e'8 f'8")
        >>> voice_2 = Voice("c'4 b4")
        >>> staff = Staff([voice_1, voice_2])
        >>> staff.is_simultaneous = True

    ..  doctest::

        >>> f(staff)
        \new Staff <<
            \new Voice {
                c'8
                d'8
                e'8
                f'8
            }
            \new Voice {
                c'4
                b4
            }
        >>

    ::

        >>> for voice in iterationtools.iterate_voices_in_expr(staff):
        ...     voice
        Voice{4}
        Voice{2}

    Iterate voices backward in `expr`:

    ::

    ::

        >>> for voice in iterationtools.iterate_voices_in_expr(
        ...     staff, reverse=True):
        ...     voice
        Voice{2}
        Voice{4}

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=scoretools.Voice,
        reverse=reverse,
        start=start,
        stop=stop,
        )
def iterate_staves_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate staves forward in `expr`:

    ::

        >>> score = Score(4 * Staff([]))

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
            }
            \new Staff {
            }
            \new Staff {
            }
            \new Staff {
            }
        >>

    ::

        >>> for staff in iterationtools.iterate_staves_in_expr(score):
        ...     staff
        ...
        Staff{}
        Staff{}
        Staff{}
        Staff{}

    Iterate staves backward in `expr`:

    ::

    ::

        >>> for staff in iterationtools.iterate_staves_in_expr(score, reverse=True):
        ...     staff
        ...
        Staff{}
        Staff{}
        Staff{}
        Staff{}

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=stafftools.Staff,
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #10
0
 def _is_my_first(self, leaf, component_classes):
     from abjad.tools import iterationtools
     for component in iterationtools.iterate_components_in_expr(
         self, 
         component_class=component_classes,
         ):
         if component is leaf:
             return True
         else:
             return False
 def _run(self, expr):
     violators = []
     total, bad = 0, 0
     for measure in iterationtools.iterate_components_in_expr(
         expr, measuretools.Measure):
         if measure.is_misfilled:
             violators.append(measure)
             bad += 1
         total += 1
     return violators, total
Example #12
0
 def _run(self, expr):
     violators = []
     bad, total = 0, 0
     for t in iterationtools.iterate_components_in_expr(
         expr, containertools.Container):
         if len(t) == 0:
             violators.append(t)
             bad += 1
         total += 1
     return violators, total
Example #13
0
 def _is_my_only(self, leaf, component_classes):
     from abjad.tools import iterationtools
     i, components = None, iterationtools.iterate_components_in_expr(
         self, 
         component_class=component_class,
         )
     for i, component in enumerate(components):
         if 0 < i:
             return False
     return i == 0
Example #14
0
 def _get_my_nth_leaf(self, n):
     from abjad.tools import iterationtools
     from abjad.tools import leaftools
     if not isinstance(n, (int, long)):
         raise TypeError
     component_classes = (leaftools.Leaf,)
     if 0 <= n:
         leaves = iterationtools.iterate_components_in_expr(
             self, component_class=component_classes)
         for leaf_index, leaf in enumerate(leaves):
             if leaf_index == n:
                 return leaf
     else:
         leaves = iterationtools.iterate_components_in_expr(
             self, component_class=component_classes, reverse=True)
         for leaf_index, leaf in enumerate(leaves):
             leaf_number = -leaf_index - 1
             if leaf_number == n:
                 return leaf
     raise IndexError
Example #15
0
 def _run(self, expr):
     violators = []
     components = iterationtools.iterate_components_in_expr(expr)
     total_ids = [id(x) for x in components]
     unique_ids = sequencetools.truncate_runs_in_sequence(total_ids)
     if len(unique_ids) < len(total_ids):
         for current_id in unique_ids:
             if 1 < total_ids.count(current_id):
                 violators.extend([x for x in components 
                     if id(x) == current_id])
     return violators, len(total_ids)
Example #16
0
 def _get_crossing_spanners(self):
     r'''Assert logical-voice-contiguous components.
     Collect spanners that attach to any component in selection.
     Returns unordered set of crossing spanners.
     A spanner P crosses a list of logical-voice-contiguous components C
     when P and C share at least one component and when it is the
     case that NOT ALL of the components in P are also in C.
     In other words, there is some intersection -- but not total
     intersection -- between the components of P and C.
     '''
     from abjad.tools import iterationtools
     from abjad.tools import selectiontools
     assert self._all_are_contiguous_components_in_same_logical_voice(self)
     all_components = set(iterationtools.iterate_components_in_expr(self))
     contained_spanners = set()
     for component in iterationtools.iterate_components_in_expr(self):
         contained_spanners.update(component._get_spanners())
     crossing_spanners = set([])
     for spanner in contained_spanners:
         spanner_components = set(spanner[:])
         if not spanner_components.issubset(all_components):
             crossing_spanners.add(spanner)
     return crossing_spanners
def iterate_notes_and_chords_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate notes and chords forward in `expr`:

    ::

        >>> staff = Staff("<e' g' c''>8 a'8 r8 <d' f' b'>8 r2")

    ..  doctest::

        >>> f(staff)
        \new Staff {
            <e' g' c''>8
            a'8
            r8
            <d' f' b'>8
            r2
        }

    ::

        >>> for leaf in iterationtools.iterate_notes_and_chords_in_expr(staff):
        ...   leaf
        Chord("<e' g' c''>8")
        Note("a'8")
        Chord("<d' f' b'>8")

    Iterate notes and chords backward in `expr`:

    ::

        >>> for leaf in iterationtools.iterate_notes_and_chords_in_expr(staff, reverse=True):
        ...   leaf
        Chord("<d' f' b'>8")
        Note("a'8")
        Chord("<e' g' c''>8")

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=(notetools.Note, scoretools.Chord), 
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #18
0
 def _get_component(self, component_classes=None, n=0, recurse=True):
     from abjad.tools import scoretools
     from abjad.tools import iterationtools
     component_classes = component_classes or (scoretools.Component,)
     if not isinstance(component_classes, tuple):
         component_classes = (component_classes,)
     if 0 <= n:
         if recurse:
             components = iterationtools.iterate_components_in_expr(
                 self, component_classes)
         else:
             components = self._music
         for i, x in enumerate(components):
             if i == n:
                 return x
     else:
         if recurse:
             components = iterationtools.iterate_components_in_expr(
                 self, component_classes, reverse=True)
         else:
             components = reversed(self._music)
         for i, x in enumerate(components):
             if i == abs(n) - 1:
                 return x
def iterate_rests_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate rests forward in `expr`:

    ::

        >>> staff = Staff("<e' g' c''>8 a'8 r8 <d' f' b'>8 r2")

    ..  doctest::

        >>> f(staff)
        \new Staff {
            <e' g' c''>8
            a'8
            r8
            <d' f' b'>8
            r2
        }

    ::

        >>> for rest in iterationtools.iterate_rests_in_expr(staff):
        ...   rest
        Rest('r8')
        Rest('r2')

    Iterate rests backward in `expr`:

    ::

        >>> for rest in iterationtools.iterate_rests_in_expr(
        ...     staff, reverse=True):
        ...     rest
        Rest('r2')
        Rest('r8')

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=resttools.Rest,
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #20
0
 def _withdraw_from_crossing_spanners(self):
     r'''Not composer-safe.
     '''
     from abjad.tools import scoretools
     from abjad.tools import iterationtools
     from abjad.tools import spannertools
     assert self._all_are_contiguous_components_in_same_logical_voice(self)
     crossing_spanners = self._get_crossing_spanners()
     components_including_children = \
         list(iterationtools.iterate_components_in_expr(self))
     for crossing_spanner in list(crossing_spanners):
         spanner_components = crossing_spanner._components[:]
         for component in components_including_children:
             if component in spanner_components:
                 crossing_spanner._components.remove(component)
                 component._spanners.discard(crossing_spanner)
def iterate_skips_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate skips forward in `expr`:

    ::

        >>> staff = Staff("<e' g' c''>8 a'8 s8 <d' f' b'>8 s2")

    ..  doctest::

        >>> f(staff)
        \new Staff {
            <e' g' c''>8
            a'8
            s8
            <d' f' b'>8
            s2
        }

    ::

        >>> for skip in iterationtools.iterate_skips_in_expr(staff):
        ...   skip
        Skip('s8')
        Skip('s2')

    Iterate skips backwards in `expr`:

    ::

        >>> for skip in iterationtools.iterate_skips_in_expr(staff, reverse=True):
        ...   skip
        Skip('s2')
        Skip('s8')

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=skiptools.Skip,
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #22
0
 def _run(self, expr):
     r'''Check for mispitched notes.
     Do not check tied rests or skips.
     Implement chord-checking later.
     '''
     violators = []
     total = 0
     spanner_classes = (spannertools.TieSpanner,)
     for leaf in iterationtools.iterate_components_in_expr(
         expr, notetools.Note):
         total += 1
         spanners = leaf._get_spanners(spanner_classes)
         if spanners:
             spanner = spanners.pop()
             if not spanner._is_my_last_leaf(leaf):
                 next_leaf = leaf._get_leaf(1)
                 if next_leaf:
                     if leaf.written_pitch != next_leaf.written_pitch:
                         violators.append(leaf)
     return violators, total
def iterate_scores_in_expr(expr, reverse=False, start=0, stop=None):
    '''Iterate scores forward in `expr`:

    ::

        >>> score_1 = Score([Staff("c'8 d'8 e'8 f'8")])
        >>> score_2 = Score([Staff("c'1"), Staff("g'1")])
        >>> scores = [score_1, score_2]

    ::

        >>> for score in iterationtools.iterate_scores_in_expr(scores):
        ...   score
        Score<<1>>
        Score<<2>>

    Iterate scores backward in `expr`:

    ::

    ::

        >>> for score in iterationtools.iterate_scores_in_expr(scores, reverse=True):
        ...   score
        Score<<2>>
        Score<<1>>

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=scoretools.Score,
        reverse=reverse,
        start=start,
        stop=stop,
        )
def iterate_measures_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Iterate measures forward in `expr`:

    ::

        >>> staff = Staff("abj: | 2/8 c'8 d'8 || 2/8 e'8 f'8 || 2/8 g'8 a'8 |")

    ..  doctest::

        >>> f(staff)
        \new Staff {
            {
                \time 2/8
                c'8
                d'8
            }
            {
                e'8
                f'8
            }
            {
                g'8
                a'8
            }
        }

    ::

        >>> for measure in iterationtools.iterate_measures_in_expr(staff):
        ...     measure
        ...
        Measure(2/8, [c'8, d'8])
        Measure(2/8, [e'8, f'8])
        Measure(2/8, [g'8, a'8])

    Use the optional `start` and `stop` keyword parameters to control
    the start and stop indices of iteration. ::

        >>> for measure in iterationtools.iterate_measures_in_expr(
        ...     staff, start=1):
        ...     measure
        ...
        Measure(2/8, [e'8, f'8])
        Measure(2/8, [g'8, a'8])

    ::

        >>> for measure in iterationtools.iterate_measures_in_expr(
        ...     staff, start=0, stop=2):
        ...     measure
        ...
        Measure(2/8, [c'8, d'8])
        Measure(2/8, [e'8, f'8])

    Iterate measures backward in `expr`:

    ::

        >>> for measure in iterationtools.iterate_measures_in_expr(
        ...     staff, reverse=True):
        ...     measure
        ...
        Measure(2/8, [g'8, a'8])
        Measure(2/8, [e'8, f'8])
        Measure(2/8, [c'8, d'8])

    Use the optional `start` and `stop` keyword parameters
    to control indices of iteration. ::

        >>> for measure in iterationtools.iterate_measures_in_expr(
        ...     staff, start=1, reverse=True):
        ...     measure
        ...
        Measure(2/8, [e'8, f'8])
        Measure(2/8, [c'8, d'8])

    ::

        >>> for measure in iterationtools.iterate_measures_in_expr(
        ...     staff, start=0, stop=2, reverse=True):
        ...     measure
        ...
        Measure(2/8, [g'8, a'8])
        Measure(2/8, [e'8, f'8])

    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=measuretools.Measure,
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #25
0
 def _set_item(
     self, 
     i, 
     expr, 
     withdraw_components_in_expr_from_crossing_spanners=True,
     ):
     r'''This method exists beacuse __setitem__ can not accept keywords.
     Note that setting 
     withdraw_components_in_expr_from_crossing_spanners=False
     constitutes a composer-unsafe use of this method.
     Only private methods should set this keyword.
     '''
     from abjad.tools import scoretools
     from abjad.tools import containertools
     from abjad.tools import contexttools
     from abjad.tools import iterationtools
     from abjad.tools import selectiontools
     from abjad.tools import spannertools
     # cache context marks attached to expr
     expr_context_marks = []
     for component in iterationtools.iterate_components_in_expr(expr):
         context_marks = component._get_marks(contexttools.ContextMark)
         expr_context_marks.extend(context_marks)
     # item assignment
     if isinstance(i, int):
         if isinstance(expr, str):
             expr = self._parse_string(expr)[:]
             assert len(expr) == 1, repr(expr)
             expr = expr[0]
         assert all(isinstance(x, scoretools.Component) for x in [expr])
         if any(isinstance(x, containertools.GraceContainer) for x in [expr]):
             message = 'must attach grace container to note or chord.'
             raise Exception(message)
         old = self[i]
         selection = selectiontools.ContiguousSelection(old)
         spanners_receipt = selection._get_dominant_spanners()
         for child in iterationtools.iterate_components_in_expr([old]):
             for spanner in child._get_spanners():
                 spanner._remove(child)
         if i < 0:
             i = len(self) + i
         del(self[i])
         # must withdraw from spanners before withdrawing from parentage!
         # otherwise begin / end assessments don't work!
         if withdraw_components_in_expr_from_crossing_spanners:
             selection = selectiontools.SliceSelection([expr])
             selection._withdraw_from_crossing_spanners()
         expr._set_parent(self)
         self._music.insert(i, expr)
         for spanner, index in spanners_receipt:
             spanner._insert(index, expr)
             expr._spanners.add(spanner)
     # slice assignment
     else:
         if isinstance(expr, str):
             expr = self._parse_string(expr)[:]
         elif isinstance(expr, list) and \
             len(expr) == 1 and \
             isinstance(expr[0], str):
             expr = self._parse_string(expr[0])[:]
         assert all(isinstance(x, scoretools.Component) for x in expr)
         if any(isinstance(x, containertools.GraceContainer) for x in expr):
             message = 'must attach grace container to note or chord.'
             raise Exception(message)
         if i.start == i.stop and i.start is not None \
             and i.stop is not None and i.start <= -len(self):
             start, stop = 0, 0
         else:
             start, stop, stride = i.indices(len(self))
         old = self[start:stop]
         spanners_receipt = self._get_spanners_that_dominate_slice(
             start, stop)
         for component in old:
             for child in iterationtools.iterate_components_in_expr(
                 [component]):
                 for spanner in child._get_spanners():
                     spanner._remove(child)
         del(self[start:stop])
         # must withdraw before setting in self!
         # otherwise circular withdraw ensues!
         if withdraw_components_in_expr_from_crossing_spanners:
             selection = selectiontools.SliceSelection(expr)
             if selection._all_are_contiguous_components_in_same_logical_voice(
                 selection):
                 selection._withdraw_from_crossing_spanners()
         self._music.__setitem__(slice(start, start), expr)
         for component in expr:
             component._set_parent(self)
         for spanner, index in spanners_receipt:
             for component in reversed(expr):
                 spanner._insert(index, component)
                 component._spanners.add(spanner)
     for expr_context_mark in expr_context_marks:
         expr_context_mark._update_effective_context()
def append_spacer_skips_to_underfull_measures_in_expr(expr):
    r'''Append spacer skips to underfull measures in `expr`:

    ::

        >>> staff = Staff(Measure((3, 8), "c'8 d'8 e'8") * 3)
        >>> time_signature = inspect(staff[1]).get_mark(
        ...     contexttools.TimeSignatureMark)
        >>> time_signature.detach()
        TimeSignatureMark((3, 8))
        >>> new_time_signature = contexttools.TimeSignatureMark((4, 8))
        >>> attach(new_time_signature, staff[1])
        TimeSignatureMark((4, 8))(|4/8 c'8 d'8 e'8|)
        >>> time_signature = inspect(
        ...     staff[2]).get_mark(contexttools.TimeSignatureMark)
        >>> time_signature.detach()
        TimeSignatureMark((3, 8))
        >>> new_time_signature = contexttools.TimeSignatureMark((5, 8))
        >>> attach(new_time_signature, staff[2])
        TimeSignatureMark((5, 8))(|5/8 c'8 d'8 e'8|)
        >>> staff[1].is_underfull
        True
        >>> staff[2].is_underfull
        True

    ::

        >>> measuretools.append_spacer_skips_to_underfull_measures_in_expr(staff)
        [Measure(4/8, [c'8, d'8, e'8, s1 * 1/8]), Measure(5/8, [c'8, d'8, e'8, s1 * 1/4])]

    ..  doctest::

        >>> f(staff)
        \new Staff {
            {
                \time 3/8
                c'8
                d'8
                e'8
            }
            {
                \time 4/8
                c'8
                d'8
                e'8
                s1 * 1/8
            }
            {
                \time 5/8
                c'8
                d'8
                e'8
                s1 * 1/4
            }
        }

    Returns measures treated.
    '''
    from abjad.tools import iterationtools
    from abjad.tools import measuretools

    treated_measures = []
    for measure in iterationtools.iterate_components_in_expr(expr, measuretools.Measure):
        if measure.is_underfull:
            measuretools.append_spacer_skip_to_underfull_measure(measure)
            treated_measures.append(measure)

    return treated_measures
def iterate_notes_in_expr(expr, reverse=False, start=0, stop=None):
    r'''Yield left-to-right notes in `expr`:

    ::

        >>> staff = Staff()
        >>> staff.append(Measure((2, 8), "c'8 d'8"))
        >>> staff.append(Measure((2, 8), "e'8 f'8"))
        >>> staff.append(Measure((2, 8), "g'8 a'8"))

    ..  doctest::

        >>> f(staff)
        \new Staff {
            {
                \time 2/8
                c'8
                d'8
            }
            {
                e'8
                f'8
            }
            {
                g'8
                a'8
            }
        }

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(staff):
        ...     note
        ...
        Note("c'8")
        Note("d'8")
        Note("e'8")
        Note("f'8")
        Note("g'8")
        Note("a'8")

    Use optional `start` and `stop` keyword parameters to control
    start and stop indices of iteration:

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(staff, start=3):
        ...     note
        ...
        Note("f'8")
        Note("g'8")
        Note("a'8")

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, start=0, stop=3):
        ...     note
        ...
        Note("c'8")
        Note("d'8")
        Note("e'8")

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, start=2, stop=4):
        ...     note
        ...
        Note("e'8")
        Note("f'8")

    Yield right-to-left notes in `expr`:

    ::

        >>> staff = Staff()
        >>> staff.append(Measure((2, 8), "c'8 d'8"))
        >>> staff.append(Measure((2, 8), "e'8 f'8"))
        >>> staff.append(Measure((2, 8), "g'8 a'8"))

    ..  doctest::

        >>> f(staff)
        \new Staff {
            {
                \time 2/8
                c'8
                d'8
            }
            {
                e'8
                f'8
            }
            {
                g'8
                a'8
            }
        }

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, reverse=True):
        ...     note
        ...
        Note("a'8")
        Note("g'8")
        Note("f'8")
        Note("e'8")
        Note("d'8")
        Note("c'8")

    Use optional `start` and `stop` keyword parameters to control
    indices of iteration:

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, reverse=True, start=3):
        ...     note
        ...
        Note("e'8")
        Note("d'8")
        Note("c'8")

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, reverse=True, start=0, stop=3):
        ...     note
        ...
        Note("a'8")
        Note("g'8")
        Note("f'8")

    ::

        >>> for note in iterationtools.iterate_notes_in_expr(
        ...     staff, reverse=True, start=2, stop=4):
        ...     note
        ...
        Note("f'8")
        Note("e'8")


    Iterates across different logical voices.

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

    return iterationtools.iterate_components_in_expr(
        expr,
        component_class=notetools.Note,
        reverse=reverse,
        start=start,
        stop=stop,
        )
Example #28
0
 def _iterate_components(self, recurse=True, reverse=False):
     from abjad.tools import iterationtools
     if recurse:
         return iterationtools.iterate_components_in_expr(self)
     else:
         return self._iterate_top_level_components(reverse=reverse)
Example #29
0
    def _copy(self, n=1, include_enclosing_containers=False):
        r'''Copies components in selection and fractures crossing spanners.

        Components in selection must be logical-voice-contiguous.

        The steps this function takes are as follows:

            * Deep copy `components`.

            * Deep copy spanners that attach to any component in `components`.

            * Fracture spanners that attach to components not in `components`.

            * Returns Python list of copied components.

        ..  container:: example

            **Example 1.** Copy components one time:

            ::

                >>> staff = Staff(r"c'8 ( d'8 e'8 f'8 )")
                >>> staff.append(r"g'8 a'8 b'8 c''8")
                >>> time_signature = contexttools.TimeSignatureMark((2, 4))
                >>> time_signature = attach(time_signature, staff)
                >>> show(staff) # doctest: +SKIP

            ..  doctest:: 
            
                >>> f(staff)
                \new Staff {
                    \time 2/4
                    c'8 (
                    d'8
                    e'8
                    f'8 )
                    g'8
                    a'8
                    b'8
                    c''8
                }

            ::

                >>> selection = staff.select_leaves()[2:4]
                >>> result = selection._copy()
                >>> new_staff = Staff(result)
                >>> show(new_staff) # doctest: +SKIP

            ..  doctest::

                >>> f(new_staff)
                \new Staff {
                    e'8 (
                    f'8 )
                }

            ::

                >>> staff.select_leaves()[2] is new_staff.select_leaves()[0]
                False

        ..  container:: example

            **Example 2.** Copy components multiple times:

            Copy `components` a total of `n` times:
            
            ::

                >>> selection = staff.select_leaves()[2:4]
                >>> result = selection._copy(n=4)
                >>> new_staff = Staff(result)
                >>> show(new_staff) # doctest: +SKIP

            ::

                >>> f(new_staff)
                \new Staff {
                    e'8 (
                    f'8 )
                    e'8 (
                    f'8 )
                    e'8 (
                    f'8 )
                    e'8 (
                    f'8 )
                }

        ..  container:: example

            **Example 3.** Copy leaves and include enclosing conatiners:

                >>> voice = Voice(r"\times 2/3 { c'4 d'4 e'4 }")
                >>> voice.append(r"\times 2/3 { f'4 e'4 d'4 }")
                >>> staff = Staff([voice])
                >>> show(staff) # doctest: +SKIP

            ..  doctest::

                >>> f(staff)
                \new Staff {
                    \new Voice {
                        \times 2/3 {
                            c'4
                            d'4
                            e'4
                        }
                        \times 2/3 {
                            f'4
                            e'4
                            d'4
                        }
                    }
                }

            ::

                >>> leaves = staff.select_leaves(1, 5)
                >>> new_staff = leaves._copy(include_enclosing_containers=True)
                >>> show(new_staff) # doctest: +SKIP

            ..  doctest::

                >>> f(new_staff)
                \new Staff {
                    \new Voice {
                        \times 2/3 {
                            d'4
                            e'4
                        }
                        \times 2/3 {
                            f'4
                            e'4
                        }
                    }
                }

        Returns contiguous selection.
        '''
        from abjad.tools import spannertools
        from abjad.tools import scoretools
        from abjad.tools import iterationtools
        # check input
        assert self._all_are_contiguous_components_in_same_logical_voice(self)
        # return empty list when nothing to copy
        if n < 1:
            return []
        new_components = [
            component._copy_with_children_and_marks_but_without_spanners() 
            for component in self
            ]
        if include_enclosing_containers:
            return self._copy_and_include_enclosing_containers()
        new_components = type(self)(new_components)
        # make schema of spanners contained by components
        schema = self._make_spanner_schema()
        # copy spanners covered by components
        for covered_spanner, component_indices in schema.items():
            new_covered_spanner = copy.copy(covered_spanner)
            del(schema[covered_spanner])
            schema[new_covered_spanner] = component_indices
        # reverse schema
        reversed_schema = {}
        for new_covered_spanner, component_indices in schema.items():
            for component_index in component_indices:
                try:
                    reversed_schema[component_index].append(
                        new_covered_spanner)
                except KeyError:
                    reversed_schema[component_index] = [new_covered_spanner]
        # iterate components and add new components to new spanners
        for component_index, new_component in enumerate(
            iterationtools.iterate_components_in_expr(new_components)):
            try:
                new_covered_spanners = reversed_schema[component_index]
                for new_covered_spanner in new_covered_spanners:
                    new_covered_spanner.append(new_component)
            except KeyError:
                pass
        # repeat as specified by input
        for i in range(n - 1):
            new_components += self._copy()
        # return new components
        return new_components
def iterate_vertical_moments_in_expr(expr, reverse=False):
    r'''Iterate vertical moments forward in `expr`:

    ::

        >>> score = Score([])
        >>> staff = Staff(r"\times 4/3 { d''8 c''8 b'8 }")
        >>> score.append(staff)

    ::

        >>> piano_staff = scoretools.PianoStaff([])
        >>> piano_staff.append(Staff("a'4 g'4"))
        >>> piano_staff.append(Staff(r"""\clef "bass" f'8 e'8 d'8 c'8"""))
        >>> score.append(piano_staff)

    ..  doctest::

        >>> f(score)
        \new Score <<
            \new Staff {
                \tweak #'text #tuplet-number::calc-fraction-text
                \times 4/3 {
                    d''8
                    c''8
                    b'8
                }
            }
            \new PianoStaff <<
                \new Staff {
                    a'4
                    g'4
                }
                \new Staff {
                    \clef "bass"
                    f'8
                    e'8
                    d'8
                    c'8
                }
            >>
        >>

    ::

        >>> for x in iterationtools.iterate_vertical_moments_in_expr(score):
        ...     x.leaves
        ...
        (Note("d''8"), Note("a'4"), Note("f'8"))
        (Note("d''8"), Note("a'4"), Note("e'8"))
        (Note("c''8"), Note("a'4"), Note("e'8"))
        (Note("c''8"), Note("g'4"), Note("d'8"))
        (Note("b'8"), Note("g'4"), Note("d'8"))
        (Note("b'8"), Note("g'4"), Note("c'8"))

    ::

        >>> for x in iterationtools.iterate_vertical_moments_in_expr(
        ...     piano_staff):
        ...     x.leaves
        ...
        (Note("a'4"), Note("f'8"))
        (Note("a'4"), Note("e'8"))
        (Note("g'4"), Note("d'8"))
        (Note("g'4"), Note("c'8"))

    Iterate vertical moments backward in `expr`:

    ::

    ::

        >>> for x in iterationtools.iterate_vertical_moments_in_expr(
        ...     score, reverse=True):
        ...     x.leaves
        ...
        (Note("b'8"), Note("g'4"), Note("c'8"))
        (Note("b'8"), Note("g'4"), Note("d'8"))
        (Note("c''8"), Note("g'4"), Note("d'8"))
        (Note("c''8"), Note("a'4"), Note("e'8"))
        (Note("d''8"), Note("a'4"), Note("e'8"))
        (Note("d''8"), Note("a'4"), Note("f'8"))

    ::

        >>> for x in iterationtools.iterate_vertical_moments_in_expr(
        ...     piano_staff, reverse=True):
        ...     x.leaves
        ...
        (Note("g'4"), Note("c'8"))
        (Note("g'4"), Note("d'8"))
        (Note("a'4"), Note("e'8"))
        (Note("a'4"), Note("f'8"))

    Returns generator.
    '''
    from abjad.tools import scoretools
    from abjad.tools import containertools
    from abjad.tools import durationtools
    from abjad.tools import iterationtools
    from abjad.tools import selectiontools

    def _buffer_components_starting_with(component, buffer, stop_offsets):
        if not isinstance(component, scoretools.Component):
            raise TypeError
        buffer.append(component)
        stop_offsets.append(component._get_timespan().stop_offset)
        if isinstance(component, containertools.Container):
            if component.is_simultaneous:
                for x in component:
                    _buffer_components_starting_with(x, buffer, stop_offsets)
            else:
                if component:
                    _buffer_components_starting_with(
                        component[0], buffer, stop_offsets)

    def _iterate_vertical_moments_forward_in_expr(expr):
        if not isinstance(expr, scoretools.Component):
            raise TypeError
        governors = (expr, )
        current_offset, stop_offsets, buffer = durationtools.Offset(0), [], []
        _buffer_components_starting_with(expr, buffer, stop_offsets)
        while buffer:
            vertical_moment = selectiontools.VerticalMoment()
            offset = durationtools.Offset(current_offset)
            components = list(buffer)
            components.sort(key=lambda x: x._get_parentage().score_index)
            vertical_moment._offset = offset
            vertical_moment._governors = governors
            vertical_moment._components = components
            yield vertical_moment
            current_offset, stop_offsets = min(stop_offsets), []
            _update_buffer(current_offset, buffer, stop_offsets)

    def _next_in_parent(component):
        if not isinstance(component, scoretools.Component):
            raise TypeError
        selection = component.select(sequential=True)
        parent, start, stop = selection._get_parent_and_start_stop_indices()
        assert start == stop
        if parent is None:
            raise StopIteration
        # can not advance within simultaneous parent
        if parent.is_simultaneous:
            raise StopIteration
        try:
            return parent[start + 1]
        except IndexError:
            raise StopIteration

    def _update_buffer(current_offset, buffer, stop_offsets):
        #print 'At %s with %s ...' % (current_offset, buffer)
        for component in buffer[:]:
            if component._get_timespan().stop_offset <= current_offset:
                buffer.remove(component)
                try:
                    next_component = _next_in_parent(component)
                    _buffer_components_starting_with(
                        next_component, buffer, stop_offsets)
                except StopIteration:
                    pass
            else:
                stop_offsets.append(component._get_timespan().stop_offset)

    if not reverse:
        for x in _iterate_vertical_moments_forward_in_expr(expr):
            yield x
    else:
        moments_in_governor = []
        for component in iterationtools.iterate_components_in_expr(expr):
            offset = component._get_timespan().start_offset
            if offset not in moments_in_governor:
                moments_in_governor.append(offset)
        moments_in_governor.sort()
        for moment_in_governor in reversed(moments_in_governor):
            yield expr._get_vertical_moment_at(moment_in_governor)