How to use the abjad.tools.durationtools.Offset 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 / timeintervaltools / TimeIntervalTree.py View on Github external
if node == self._sentinel:
                return intervals
            if node.key <= offset and offset <= node.latest_stop:
                for interval in node.payload:
                    if interval.start_offset <= offset and offset <= interval.stop_offset:
                        intervals.append(interval)
            if node.left != self._sentinel and \
                self._find_minimum(node.left).key <= offset and \
                offset <= node.left.latest_stop:
                intervals.extend(recurse(node.left, offset))
            if node.right != self._sentinel and \
                self._find_minimum(node.right).key <= offset and \
                offset <= node.right.latest_stop:
                intervals.extend(recurse(node.right, offset))
            return intervals
        offset = durationtools.Offset(offset)
        return type(self)(recurse(self._root, offset))
github Abjad / abjad / trunk / abjad / tools / quantizationtools / QEventSequence / QEventSequence.py View on Github external
assert all(isinstance(x, numbers.Number) for x in pair[1])
        # fuse silences
        g = itertools.groupby(pairs, lambda x: x[1] is not None)
        groups = []
        for value, group in g:
            if value:
                groups.extend(list(group))
            else:
                duration = sum(x[0] for x in group)
                groups.append((duration, None))
        # find offsets
        offsets = mathtools.cumulative_sums_zero([abs(x[0]) for x in groups])
        # build QEvents
        q_events = []
        for pair in zip(offsets, groups):
            offset = durationtools.Offset(pair[0])
            pitches = pair[1][1]
            if isinstance(pitches, collections.Iterable):
                assert all(isinstance(x, numbers.Number) for x in pitches)
                q_events.append(quantizationtools.PitchedQEvent(offset, pitches))
            elif isinstance(pitches, type(None)):
                q_events.append(quantizationtools.SilentQEvent(offset))
            elif isinstance(pitches, numbers.Number):
                q_events.append(quantizationtools.PitchedQEvent(offset, [pitches]))
        q_events.append(quantizationtools.TerminalQEvent(
            durationtools.Offset(offsets[-1])))
        return cls(q_events)
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / TimeIntervalTree.py View on Github external
TimeInterval(Offset(2, 1), Offset(4, 1), {'name': 'three'})
            ])

        ::

            >>> result = tree.shift_to_rational(100)
            >>> result
            TimeIntervalTree([
                TimeInterval(Offset(100, 1), Offset(101, 1), {'name': 'one'}),
                TimeInterval(Offset(201, 2), Offset(205, 2), {'name': 'two'}),
                TimeInterval(Offset(102, 1), Offset(104, 1), {'name': 'three'})
            ])

        Return `TimeIntervalTree` instance.
        '''
        rational = durationtools.Offset(rational)
        return type(self)([
            x.shift_by_rational(rational - self.start_offset) for x in self
        ])
github Abjad / abjad / experimental / tools / musicexpressiontools / StartPositionedRhythmPayloadExpression / StartPositionedRhythmPayloadExpression.py View on Github external
Operates in place and returns start-positioned rhythm 
        payload expression.
        '''
        stop_offset = durationtools.Offset(stop_offset)
        assert self.stop_offset <= stop_offset
        additional_duration = stop_offset - self.stop_offset
        needed_copies = int(
            math.ceil(additional_duration / self.payload._get_duration()))
        copies = []
        for i in range(needed_copies):
            copies.append(copy.deepcopy(self.payload))
        for element in copies:
            self.payload.extend(element)
        assert stop_offset <= self.stop_offset
        stop_offset = durationtools.Offset(stop_offset)
        assert stop_offset <= self.stop_offset
        assert self.start_offset < stop_offset
        duration_to_trim = self.stop_offset - stop_offset
        duration_to_keep = self.payload._get_duration() - duration_to_trim
        shards = self._split_payload_at_offsets([duration_to_keep])
        assert len(shards) in (1, 2), repr(shards)
        self._payload = shards[0]
        return self
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / TimeIntervalTreeDictionary.py View on Github external
]),
            })

        ::

            >>> result[3]
            TimeIntervalTreeDictionary({
                'three': TimeIntervalTree([
                    TimeInterval(Offset(3, 1), Offset(4, 1), {'name': 'three'}),
                ]),
            })

        Returns tuple of `TimeIntervalTreeDictionary` instances.
        '''
        assert 0 < len(rationals)
        rationals = sorted([durationtools.Offset(x) for x in rationals])
        rationals = [x for x in rationals if self.start_offset < x < self.stop_offset]
        dicts = []
        carried = dict(self)
        to_remove = []
        for rational in rationals:
            result = {}
            for key, tree in carried.iteritems():
                if tree is None:
                    continue
                splits = tree.split_at_rationals(rational)
                if len(splits) == 2:
                    result[key] = splits[0]
                    carried[key] = splits[1]
                elif splits[0].stop_offset <= rational:
                    result[key] = splits[0]
                    carried[key] = None
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / TimeInterval.py View on Github external
def shift_to_rational(self, rational):
        rational = durationtools.Offset(rational)
        if rational != self.start_offset:
            duration = self.stop_offset - self.start_offset
            return type(self)(rational, rational + duration, self)
        else:
            return self
github Abjad / abjad / trunk / abjad / tools / quantizationtools / QTargetMeasure.py View on Github external
def __init__(self, 
        offset_in_ms=None, 
        search_tree=None, 
        time_signature=None,
        tempo=None, 
        use_full_measure=False,
        ):

        from abjad.tools import quantizationtools

        offset_in_ms = durationtools.Offset(offset_in_ms)

        if search_tree is None:
            search_tree = quantizationtools.UnweightedSearchTree()
        assert isinstance(search_tree, quantizationtools.SearchTree)
        tempo = marktools.TempoMark(tempo)
        assert not tempo.is_imprecise
        time_signature = marktools.TimeSignatureMark(time_signature)
        use_full_measure = bool(use_full_measure)

        beats = []

        if use_full_measure:
            beatspan = time_signature.duration
            beat = quantizationtools.QTargetBeat(
                beatspan=beatspan,
                offset_in_ms=offset_in_ms,
github Abjad / abjad / trunk / abjad / tools / quantizationtools / QEventSequence / QEventSequence.py View on Github external
Return ``QEventSequence`` instance.
        '''
        from abjad.tools import quantizationtools
        durations = [durationtools.Duration(x) for x in durations]
        assert isinstance(tempo, contexttools.TempoMark)
        durations = [x for x in 
            sequencetools.sum_consecutive_sequence_elements_by_sign(
                durations, 
                sign=[-1],
                ) if x]
        durations = [tempo.duration_to_milliseconds(x) for x in durations]
        offsets = mathtools.cumulative_sums_zero(abs(x) for x in durations)
        q_events = []
        for pair in zip(offsets, durations):
            offset = durationtools.Offset(pair[0])
            duration = pair[1]
            if duration < 0: # negative duration indicates silence
                q_event = quantizationtools.SilentQEvent(offset)
            else: # otherwise, use middle-C
                q_event = quantizationtools.PitchedQEvent(offset, [0])
            q_events.append(q_event)
        # insert terminating silence QEvent
        q_events.append(quantizationtools.TerminalQEvent(offsets[-1]))
        return cls(q_events)
github Abjad / abjad / trunk / abjad / tools / timeintervaltools / TimeIntervalTree.py View on Github external
    @property
    def latest_stop(self):
        r'''The maximum stop_offset value of all intervals in the tree:

        ::

            >>> ti1 = TimeInterval(1, 2)
            >>> ti2 = TimeInterval(3, (7, 2))
            >>> tree = TimeIntervalTree([ti1, ti2])
            >>> tree.latest_stop
            Offset(7, 2)

        Return ``Offset`` instance, or None if tree is empty.
        '''
        if self:
            return durationtools.Offset(self._root.latest_stop)
        else:
            return None
github Abjad / abjad / experimental / tools / expressiontools / StartPositionedPayloadExpression / StartPositionedPayloadExpression.py View on Github external
def __and__(self, timespan):
        '''Keep intersection of start-positioned payload expression and `timespan`.

        Operate in place and return timespan inventory.
        '''
        if timespan.contains_timespan_improperly(self):
            result = timespantools.TimespanInventory([self]) 
        elif timespan.delays_timespan(self):
            split_offset = durationtools.Offset(timespan.stop_offset)
            duration_to_keep = split_offset - self.start_offset
            result = self._split_payload_at_offsets([duration_to_keep])
            trimmed_payload = result[0]
            self._payload = trimmed_payload
            result = timespantools.TimespanInventory([self])
        elif timespan.curtails_timespan(self):
            split_offset = durationtools.Offset(timespan.start_offset)
            duration_to_trim = split_offset - self.start_offset
            result = self._split_payload_at_offsets([duration_to_trim])
            trimmed_payload = result[-1]
            self._payload = trimmed_payload
            self._start_offset = split_offset
            result = timespantools.TimespanInventory([self])
        elif timespan.trisects_timespan(self):
            split_offsets = []
            split_offsets.append(timespan.start_offset - self.start_offset)