Exemple #1
0
 def _should_format_last_leaf_markup(self, component):
     if inspect(self[-1]).get_piecewise(self, Markup, None) is None:
         return
     leaf = None
     for leaf in reversed(self[:-1]):
         markup = inspect(leaf).get_piecewise(self, Markup, None)
         if markup is not None:
             break
         line_segment = inspect(leaf).get_piecewise(self, LineSegment, None)
         if line_segment is not None:
             break
     return component is leaf
Exemple #2
0
 def _get_piecewise_indicators(self, leaf, prototype=None):
     assert leaf in self, repr(leaf)
     indicators = []
     for wrapper in inspect(leaf).wrappers(prototype):
         if wrapper.spanner is self:
             indicators.append(wrapper.indicator)
     return indicators
Exemple #3
0
    def ratio(self) -> Ratio:
        """
        Gets ratio of metric modulation.

        ..  container:: example

            >>> metric_modulation = abjad.MetricModulation(
            ...     left_rhythm=abjad.Tuplet((2, 3), [abjad.Note("c'4")]),
            ...     right_rhythm=abjad.Note("c'4"),
            ...     )
            >>> metric_modulation.ratio
            Ratio((2, 3))

        """
        left_duration = inspect(self.left_rhythm).get_duration()
        right_duration = inspect(self.right_rhythm).get_duration()
        duration = left_duration / right_duration
        ratio = Ratio(duration.pair)
        return ratio
Exemple #4
0
 def _add_container_identifiers(self):
     if (self.environment == 'docs' and
         not getattr(self, 'test_container_identifiers', False)):
         return
     segment_name = self.segment_name or ''
     segment_name = String(segment_name).to_segment_lilypond_identifier()
     contexts = []
     try:
         context = self.score['GlobalSkips']
         contexts.append(context)
     except ValueError:
         pass
     try:
         context = self.score['GlobalRests']
         contexts.append(context)
     except ValueError:
         pass
     for voice in iterate(self.score).components(Voice):
         contexts.append(voice)
     container_to_part_assignment = OrderedDict()
     for context in contexts:
         if segment_name:
             context_identifier = f'{segment_name}_{context.name}'
         else:
             context_identifier = context.name
         context.identifier = f'%*% {context_identifier}'
         part_container_count = 0
         for container in iterate(context).components(Container):
             if not container.identifier:
                 continue
             if container.identifier.startswith('%*% Part'):
                 part_container_count += 1
                 globals_ = globals()
                 part = container.identifier.strip('%*% ')
                 part = eval(part, globals_)
                 suffix = String().base_26(part_container_count).lower()
                 container_identifier = f'{context_identifier}_{suffix}'
                 container_identifier = String(container_identifier)
                 assert container_identifier.is_lilypond_identifier()
                 assert container_identifier not in \
                     container_to_part_assignment
                 timespan = inspect(container).get_timespan()
                 pair = (part, timespan)
                 container_to_part_assignment[container_identifier] = pair
                 container.identifier = f'%*% {container_identifier}'
     for staff in iterate(self.score).components(Staff):
         if segment_name:
             context_identifier = f'{segment_name}_{staff.name}'
         else:
             context_identifier = staff.name
         staff.identifier = f'%*% {context_identifier}'
     self._container_to_part_assignment = container_to_part_assignment
Exemple #5
0
 def _get_lilypond_format_bundle(self, component=None):
     bundle = self._get_basic_lilypond_format_bundle(component)
     markup = inspect(component).get_piecewise(self, Markup, None)
     line_segment = inspect(component).get_piecewise(
         self, LineSegment, None)
     if self._should_format_last_leaf_markup(component):
         last_leaf_markup = inspect(self[-1]).get_piecewise(self, Markup)
     else:
         last_leaf_markup = None
     indicators = (markup, line_segment, last_leaf_markup)
     has_indicators = any(_ is not None for _ in indicators)
     if not has_indicators:
         if component is self[0]:
             if self._wrappers:
                 string = r'- \tweak Y-extent ##f'
                 bundle.right.spanner_starts.append(string)
                 line_segment = self._make_invisible_line_segment()
                 tweaks = line_segment._get_lilypond_grob_overrides(
                     tweaks=True)
                 bundle.right.spanner_starts.extend(tweaks)
             bundle.right.spanner_starts.append(self._start_command())
         if component is self[-1]:
             bundle.right.spanner_stops.append(self._stop_command())
         return bundle
     if has_indicators and not component is self[0]:
         bundle.right.spanner_stops.append(self._stop_command())
     if not component is self[-1]:
         if self._wrappers:
             string = r'- \tweak Y-extent ##f'
             bundle.right.spanner_starts.append(string)
         if line_segment is None:
             line_segment = self._make_invisible_line_segment()
         if markup is not None:
             if line_segment.left_hspace is not None:
                 left_hspace = line_segment.left_hspace
                 left_hspace = Markup.hspace(left_hspace)
                 markup = Markup.concat([markup, left_hspace])
             override = LilyPondGrobOverride(
                 grob_name='TextSpanner',
                 once=True,
                 property_path=(
                     'bound-details',
                     'left',
                     'text',
                 ),
                 value=markup,
             )
             string = override.tweak_string
             bundle.right.spanner_starts.append(string)
         tweaks = line_segment._get_lilypond_grob_overrides(tweaks=True)
         bundle.right.spanner_starts.extend(tweaks)
     if last_leaf_markup is not None:
         right_hspace = line_segment.right_padding or 0
         # optical correction to draw last markup left:
         right_hspace -= 0.5
         right_hspace = Markup.hspace(right_hspace)
         last_leaf_markup = Markup.concat(
             [right_hspace, last_leaf_markup], )
         last_leaf_markup = new(last_leaf_markup, direction=None)
         override = LilyPondGrobOverride(
             grob_name='TextSpanner',
             once=True,
             property_path=(
                 'bound-details',
                 'right',
                 'text',
             ),
             value=last_leaf_markup,
         )
         string = override.tweak_string
         bundle.right.spanner_starts.append(string)
     # all tweaks must appear immediately before start command:
     if not component is self[-1]:
         bundle.right.spanner_starts.append(self._start_command())
     return bundle
Exemple #6
0
    def attach_defaults(self, argument) -> typing.List:
        r'''Attaches defaults to all staff and staff group contexts in
        ``argument`` when ``argument`` is a score.

        Attaches defaults to ``argument`` (without iterating ``argument``) when
        ``argument`` is a staff or staff group.

        Returns list of one wrapper for every indicator attached.
        '''
        assert isinstance(argument, (Score, Staff, StaffGroup)), repr(argument)
        wrappers: typing.List[Wrapper] = []
        tag = Tags().REMOVE_ALL_EMPTY_STAVES
        empty_prototype = (MultimeasureRest, Skip)
        prototype = (Staff, StaffGroup)
        if isinstance(argument, Score):
            staff__groups = select(argument).components(prototype)
            staves = select(argument).components(Staff)
        elif isinstance(argument, Staff):
            staff__groups = [argument]
            staves = [argument]
        else:
            assert isinstance(argument, StaffGroup), repr(argument)
            staff__groups = [argument]
            staves = []
        for staff__group in staff__groups:
            leaf = None
            voices = select(staff__group).components(Voice)
            # find first leaf in first nonempty voice
            for voice in voices:
                leaves = select(voice).leaves()
                if not all(isinstance(_, empty_prototype) for _ in leaves):
                    leaf = inspect(voice).get_leaf(0)
            # otherwise, find first leaf in voice in non-removable staff
            if leaf is None:
                for voice in voices:
                    voice_might_vanish = False
                    for component in inspect(voice).get_parentage():
                        if inspect(component).get_annotation(tag) is True:
                            voice_might_vanish = True
                    if not voice_might_vanish:
                        leaf = inspect(voice).get_leaf(0)
                        if leaf is not None:
                            break
            # otherwise, as last resort find first leaf in first voice
            if leaf is None:
                leaf = inspect(voices[0]).get_leaf(0)
            if leaf is None:
                continue
            instrument = inspect(leaf).get_indicator(Instrument)
            if instrument is None:
                string = 'default_instrument'
                instrument = inspect(staff__group).get_annotation(string)
                if instrument is not None:
                    wrapper = attach(instrument, leaf, tag='ST1', wrapper=True)
                    wrappers.append(wrapper)
            margin_markup = inspect(leaf).get_indicator(MarginMarkup)
            if margin_markup is None:
                string = 'default_margin_markup'
                margin_markup = inspect(staff__group).get_annotation(string)
                if margin_markup is not None:
                    wrapper = attach(
                        margin_markup,
                        leaf,
                        tag=Tag('-PARTS').prepend('ST2'),
                        wrapper=True,
                    )
                    wrappers.append(wrapper)
        for staff in staves:
            leaf = inspect(staff).get_leaf(0)
            clef = inspect(leaf).get_indicator(Clef)
            if clef is not None:
                continue
            clef = inspect(staff).get_annotation('default_clef')
            if clef is not None:
                wrapper = attach(clef, leaf, tag='ST3', wrapper=True)
                wrappers.append(wrapper)
        return wrappers
Exemple #7
0
 def _start_offset_in_me(self, leaf):
     leaf_start_offset = inspect(leaf).get_timespan().start_offset
     self_start_offset = inspect(self).get_timespan().start_offset
     return leaf_start_offset - self_start_offset