How to use the abjad.tools.durationtools.Duration function in abjad

To help you get started, we’ve selected a few abjad examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Abjad / abjad / trunk / abjad / tools / measuretools / multiply_and_scale_contents_of_measures_in_expr.py View on Github external
Return list of measures changed.
    '''
    from abjad.tools import iterationtools
    from abjad.tools import measuretools

    assert isinstance(multiplier_pairs, list)
    assert all(isinstance(pair, tuple) for pair in multiplier_pairs)

    result = []
    num_pairs = len(multiplier_pairs)
    for i, measure in enumerate(iterationtools.iterate_measures_in_expr(expr)):
        concentration_pair = multiplier_pairs[i % num_pairs]
        assert isinstance(concentration_pair, tuple)
        spin_count, scalar_denominator = concentration_pair
        measuretools.multiply_contents_of_measures_in_expr(measure, spin_count)
        multiplier = durationtools.Duration(1, scalar_denominator)
        measuretools.scale_measure_and_adjust_time_signature(measure, multiplier)
        result.append(measure)

    return result
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / round_interval_bounds_to_nearest_multiple_of_rational.py View on Github external
def round_interval_bounds_to_nearest_multiple_of_rational(intervals, duration):
    '''Round all start and stop offsets of `intervals` to the nearest multiple of `duration`.

    If both start and stop of an interval collapse on the same offset, that interval's stop
    will be adjusted to the next larger multiple of `duration`.

    Return TimeIntervalTree.
    '''
    from abjad.tools import timeintervaltools

    tree = timeintervaltools.TimeIntervalTree(intervals)

    duration = durationtools.Duration(duration)
    assert 0 < duration

    if not tree:
        return tree

    intervals = []
    for interval in tree:
        start = durationtools.Offset(int(round(interval.start / duration))) * duration
        stop = durationtools.Offset(int(round(interval.stop / duration))) * duration
        if start == stop:
            stop = start + duration
        intervals.append(interval.shift_to_rational(start).scale_to_rational(stop - start))

    return timeintervaltools.TimeIntervalTree(intervals)
github Abjad / abjad / abjad / tools / pitchtools / PitchArrayRow.py View on Github external
>>> f(measure)
                {
                    \time 4/8
                    r8
                    d'8
                    4
                }

        Returns measure.
        '''
        from abjad.tools import scoretools
        time_signature = indicatortools.TimeSignature(
            (self.width, cell_duration_denominator))
        measure = scoretools.Measure(time_signature, [])
        basic_cell_duration = \
            durationtools.Duration(1, cell_duration_denominator)
        measure_pitches, measure_durations = [], []
        for cell in self.cells:
            cell_pitches = cell.pitches
            if not cell_pitches:
                measure_pitches.append(None)
            elif len(cell_pitches) == 1:
                measure_pitches.append(cell_pitches[0])
            else:
                measure_pitches.append(cell_pitches)
            measure_duration = cell.width * basic_cell_duration
            measure_durations.append(measure_duration)
        leaves = scoretools.make_leaves(measure_pitches, measure_durations)
        measure.extend(leaves)
        return measure
github Abjad / abjad / experimental / tools / musicexpressiontools / OffsetCallbackMixin.py View on Github external
musicexpressiontools.OffsetExpression(
                anchor=musicexpressiontools.TimespanExpression(
                    anchor='red',
                    callbacks=musicexpressiontools.CallbackInventory(
                        []
                        ),
                    ),
                edge=Right,
                callbacks=musicexpressiontools.CallbackInventory(
                    ['self._translate(offset, Duration(9, 2))']
                    ),
                )

        Returns offset expression copy with callback.
        '''
        translation = durationtools.Duration(translation)
        callback = 'self._translate(offset, {!r})'
        callback = callback.format(translation)
        return self._copy_and_append_callback(callback)
github Abjad / abjad / trunk / abjad / tools / selectiontools / ContiguousSelection.py View on Github external
When `fill` is ``'less'`` then parts must be 
        less than or equal to `durations`.

        When `fill` is ``'greater'`` then parts must be 
        greater or equal to `durations`.

        Reads `durations` cyclically when `cyclic` is true.

        Reads component durations in seconds when `in_seconds` is true.

        Returns remaining components at end in final part when `overhang` 
        is true.
        '''
        # TODO: decide on correct assertion
        #assert self._all_are_contiguous_components_in_same_logical_voice(self)
        durations = [durationtools.Duration(x) for x in durations]
        if cyclic:
            durations = datastructuretools.CyclicTuple(durations)
        result = []
        part = []
        current_duration_index = 0
        target_duration = durations[current_duration_index]
        cumulative_duration = durationtools.Duration(0)
        components_copy = list(self)
        while True:
            try:
                component = components_copy.pop(0)
            except IndexError:
                break
            component_duration = component._get_duration()
            if in_seconds:
                component_duration = component._get_duration(in_seconds=True)
github Abjad / abjad / experimental / tools / musicexpressiontools / TimespanCallbackMixin.py View on Github external
self,
        original_start_offset,
        original_stop_offset,
        start_offset_translation,
        stop_offset_translation,
        ):
        if start_offset_translation is None:
            start_offset_translation = 0
        else:
            start_offset_translation = \
                durationtools.Duration(start_offset_translation)
        if stop_offset_translation is None:
            stop_offset_translation = 0
        else:
            stop_offset_translation = \
                durationtools.Duration(stop_offset_translation)
        new_start_offset = original_start_offset + start_offset_translation
        new_stop_offset = original_stop_offset + stop_offset_translation
        return new_start_offset, new_stop_offset
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / TimeIntervalTree.py View on Github external
>>> a = timeintervaltools.TimeInterval(-1, 3)
            >>> b = timeintervaltools.TimeInterval(6, 12)
            >>> c = timeintervaltools.TimeInterval(9, 16)
            >>> tree = timeintervaltools.TimeIntervalTree([a, b, c])
            >>> tree.scale_interval_durations_to_rational(Duration(1, 7))
            TimeIntervalTree([
                TimeInterval(Offset(-1, 1), Offset(-6, 7), {}),
                TimeInterval(Offset(6, 1), Offset(43, 7), {}),
                TimeInterval(Offset(9, 1), Offset(64, 7), {})
            ])

        Returns TimeIntervalTree.
        '''
        from abjad.tools import timeintervaltools
        rational = durationtools.Duration(rational)
        assert 0 < rational
        if not self:
            return self
        return timeintervaltools.TimeIntervalTree([
            x.scale_to_rational(rational) 
            for x in self
            ])
github Abjad / abjad / trunk / abjad / tools / componenttools / copy_governed_component_subtree_from_offset_to.py View on Github external
Returns ``(untrimmed_copy, first_dif, second_dif)`` tuple.
    '''
    from abjad.tools import componenttools
    from abjad.tools import containertools
    from abjad.tools import leaftools

    # check input
    assert isinstance(component, componenttools.Component)
    start_offset = durationtools.Duration(start_offset)
    if start_offset < 0:
        start_offset = durationtools.Duration(0)
    if stop_offset is None:
        stop_offset = component._get_duration()
    else:
        stop_offset = durationtools.Duration(stop_offset)
    assert start_offset <= stop_offset

    # copy component from start offset to stop offset and return
    if isinstance(component, leaftools.Leaf):
        return _copy_leaf_from_start_offset_to_stop_offset(
            component, start_offset, stop_offset)
    #elif isinstance(component, containertools.Container):
    else:
        return _copy_container_from_start_offset_to_stop_offset(
            component, start_offset, stop_offset)
    #else:
github Abjad / abjad / trunk / abjad / tools / selectiontools / TieChain.py View on Github external
def _add_or_remove_notes_to_achieve_written_duration(
        self, new_written_duration):
        from abjad.tools import scoretools
        from abjad.tools import scoretools
        from abjad.tools import spannertools
        from abjad.tools import scoretools
        from abjad.tools.functiontools import attach
        new_written_duration = durationtools.Duration(new_written_duration)
        if new_written_duration.is_assignable:
            self[0].written_duration = new_written_duration
            for leaf in self[1:]:
                parent = leaf._parent
                if parent:
                    index = parent.index(leaf)
                    del(parent[index])
            first = self[0]
            for spanner in first._get_spanners(spannertools.TieSpanner):
                spanner.detach()
        elif new_written_duration.has_power_of_two_denominator:
            durations = scoretools.make_notes(0, [new_written_duration])
            for leaf, token in zip(self, durations):
                leaf.written_duration = token.written_duration
            if len(self) == len(durations):
                pass
github Abjad / abjad / trunk / abjad / tools / rhythmtreetools / _RTMNode / _RTMNode.py View on Github external
def recurse(node, duration):
            tuplet = FixedDurationTuplet(duration, [])
            denominator =  greatest_power_of_two_less_equal((duration / node.width).denominator)
            for x in node:
                if isinstance(x, int):
                    if 0 < x:
                        tuplet.extend(make_notes(0, (x, denominator)))
                    else:
                        tuplet.extend(make_rests((abs(x), denominator)))
                else:
                    tuplet.append(recurse(x, durationtools.Duration(x.pulses_consumed, denominator)))
            return tuplet