def test_rhythmtreetools_RhythmTreeNode_pickle_01(): string = '(1 (1 (2 (1 1 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(string)[0] pickled = pickle.loads(pickle.dumps(tree)) assert pickled == tree assert pickled is not tree
def test_rhythmtreetools_RhythmTreeNode___call___01(): rtm = '(1 (1 1 1 1))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert isinstance(result, (list, selectiontools.Selection)) assert len(result) == 4 assert all(isinstance(x, Note) for x in result) assert all(x.written_duration == Duration(1, 16) for x in result)
def test_rhythmtreetools_RhythmTreeContainer___call___02(): rtm = '(1 (1 (2 (1 1 1 1)) 1))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert isinstance(result, abjad.Selection) assert len(result) == 6 assert [format(x) for x in result ] == ["c'16", "c'32", "c'32", "c'32", "c'32", "c'16"]
def test_rhythmtreetools_RhythmTreeNode_parentage_ratios_01(): string = '(1 (1 (2 (3 4)) 2))' tree = rhythmtreetools.RhythmTreeParser()(string)[0] assert tree.parentage_ratios == (1,) assert tree[0].parentage_ratios == (1, (1, 5)) assert tree[1].parentage_ratios == (1, (2, 5)) assert tree[1][0].parentage_ratios == (1, (2, 5), (3, 7)) assert tree[1][1].parentage_ratios == (1, (2, 5), (4, 7)) assert tree[2].parentage_ratios == (1, (2, 5))
def parse_rtm_syntax(rtm): r'''Parses RTM syntax. :: >>> rtm = '(1 (1 (1 (1 1)) 1))' >>> rhythmtreetools.parse_rtm_syntax(rtm) FixedDurationTuplet(Duration(1, 4), "c'8 c'16 c'16 c'8") Also supports fractional durations: :: >>> rtm = '(3/4 (1 1/2 (4/3 (1 -1/2 1))))' >>> rhythmtreetools.parse_rtm_syntax(rtm) FixedDurationTuplet(Duration(3, 16), 'c\'8 c\'16 FixedDurationTuplet(Duration(1, 6), "c\'8 r16 c\'8")') :: >>> print(format(_)) \tweak #'text #tuplet-number::calc-fraction-text \times 9/17 { c'8 c'16 \tweak #'edge-height #'(0.7 . 0) \times 8/15 { c'8 r16 c'8 } } Returns fixed-duration tuplet or container. ''' from abjad.tools import rhythmtreetools result = rhythmtreetools.RhythmTreeParser()(rtm) con = scoretools.Container() for node in result: tuplet = node((1, 4)) # following line added 2012-08-01. tb. tuplet = tuplet[0] if tuplet.is_trivial: con.extend(tuplet[:]) else: con.append(tuplet) if len(con) == 1: return con[0] return con
def test_rhythmtreetools_RhythmTreeNode___call___03(): rtm = '(1 (1 (2 (1 (2 (1 1)) 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert format(result[0]) == stringtools.normalize(r''' \times 4/5 { c'16 c'32 c'32 c'32 c'32 c'8 } ''')
def test_rhythmtreetools_RhythmTreeContainer___copy___01(): string = '(1 (1 (2 (3 (4 (1 1 1)))) 2))' tree = rhythmtreetools.RhythmTreeParser()(string)[0] copied = copy.copy(tree) assert format(tree) == format(copied) assert tree is not copied assert format(tree[0]) == format(copied[0]) assert tree[0] is not copied[0] assert format(tree[1]) == format(copied[1]) assert tree[1] is not copied[1] assert format(tree[2]) == format(copied[2]) assert tree[2] is not copied[2]
def test_rhythmtreetools_RhythmTreeNode___call___03(): rtm = '(1 (1 (2 (1 (2 (1 1)) 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert systemtools.TestManager.compare( result[0], r''' \times 4/5 { c'16 c'32 c'32 c'32 c'32 c'8 } ''' )
def test_rhythmtreetools_RhythmTreeNode___call___02(): rtm = '(1 (1 (2 (1 1 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert isinstance(result, (list, selectiontools.Selection)) assert len(result) == 1 assert isinstance(result[0], scoretools.FixedDurationTuplet) assert format(result[0]) == stringtools.normalize(r''' \times 4/5 { c'16 \times 2/3 { c'16 c'16 c'16 } c'8 } ''')
def test_rhythmtreetools_RhythmTreeContainer___call___01(): rtm = '(1 (1 (2 (1 1 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert isinstance(result, list) assert len(result) == 1 assert format(result[0]) == abjad.String.normalize(r''' \times 4/5 { c'16 \times 2/3 { c'16 c'16 c'16 } c'8 } ''')
def test_rhythmtreetools_RhythmTreeNode___call___02(): rtm = '(1 (1 (2 (1 1 1)) 2))' tree = rhythmtreetools.RhythmTreeParser()(rtm)[0] result = tree((1, 4)) assert isinstance(result, (list, selectiontools.SliceSelection)) assert len(result) == 1 assert isinstance(result[0], scoretools.FixedDurationTuplet) assert systemtools.TestManager.compare( result[0], r''' \times 4/5 { c'16 \times 2/3 { c'16 c'16 c'16 } c'8 } ''' )
def parse_rtm_syntax(rtm): r'''Parses RTM syntax. .. container:: example Parses tuplet: >>> rtm = '(1 (1 (1 (1 1)) 1))' >>> tuplet = abjad.rhythmtreetools.parse_rtm_syntax(rtm) >>> abjad.show(tuplet) # doctest: +SKIP .. docs:: >>> abjad.f(tuplet) \times 2/3 { c'8 c'16 c'16 c'8 } .. container:: example Also supports fractional durations: >>> rtm = '(3/4 (1 1/2 (4/3 (1 -1/2 1))))' >>> tuplet = abjad.rhythmtreetools.parse_rtm_syntax(rtm) >>> abjad.show(tuplet) # doctest: +SKIP .. docs:: >>> abjad.f(tuplet) \tweak text #tuplet-number::calc-fraction-text \times 9/17 { c'8 c'16 \tweak edge-height #'(0.7 . 0) \times 8/15 { c'8 r16 c'8 } } Returns tuplet or container. ''' from abjad.tools import rhythmtreetools result = rhythmtreetools.RhythmTreeParser()(rtm) con = scoretools.Container() for node in result: tuplet = node((1, 4)) # following line added 2012-08-01. tb. tuplet = tuplet[0] if tuplet.trivial(): con.extend(tuplet[:]) else: con.append(tuplet) if len(con) == 1: return con[0] return con
def __init__( self, arg=None, decrease_durations_monotonically=True, preferred_boundary_depth=None, ): arg = arg or (4, 4) assert isinstance(preferred_boundary_depth, (int, type(None))) self._preferred_boundary_depth = preferred_boundary_depth def recurse( node, factors, denominator, decrease_durations_monotonically, ): if factors: factor, factors = factors[0], factors[1:] preprolated_duration = \ node.preprolated_duration.__div__(factor) #if factor in (2, 3, 4, 5): if factor in (2, 3, 4): if factors: for _ in range(factor): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) node.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(factor): node.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) else: parts = [3] total = 3 while total < factor: if decrease_durations_monotonically: parts.append(2) else: parts.insert(0, 2) total += 2 for part in parts: grouping = rhythmtreetools.RhythmTreeContainer( preprolated_duration=part * preprolated_duration) if factors: for _ in range(part): child = rhythmtreetools.RhythmTreeContainer( preprolated_duration=preprolated_duration) grouping.append(child) recurse( child, factors, denominator, decrease_durations_monotonically, ) else: for _ in range(part): grouping.append( rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator))) node.append(grouping) else: node.extend([ rhythmtreetools.RhythmTreeLeaf( preprolated_duration=(1, denominator)) for _ in range(node.preprolated_duration.numerator) ]) decrease_durations_monotonically = \ bool(decrease_durations_monotonically) if isinstance(arg, type(self)): root = arg.root_node numerator, denominator = arg.numerator, arg.denominator decrease_durations_monotonically = \ arg.decrease_durations_monotonically elif isinstance(arg, (str, rhythmtreetools.RhythmTreeContainer)): if isinstance(arg, str): parsed = rhythmtreetools.RhythmTreeParser()(arg) assert len(parsed) == 1 root = parsed[0] else: root = arg for node in root.nodes: assert node.prolation == 1 numerator = root.preprolated_duration.numerator denominator = root.preprolated_duration.denominator elif (isinstance(arg, (tuple, scoretools.Measure)) or (hasattr(arg, 'numerator') and hasattr(arg, 'denominator'))): if isinstance(arg, tuple): fraction = mathtools.NonreducedFraction(arg) elif isinstance(arg, scoretools.Measure): time_signature = arg._get_effective( indicatortools.TimeSignature) fraction = mathtools.NonreducedFraction( time_signature.numerator, time_signature.denominator) else: fraction = mathtools.NonreducedFraction( arg.numerator, arg.denominator) numerator, denominator = fraction.numerator, fraction.denominator factors = mathtools.factors(numerator) # group two nested levels of 2s into a 4 if 1 < len(factors) and factors[0] == factors[1] == 2: factors[0:2] = [4] root = rhythmtreetools.RhythmTreeContainer( preprolated_duration=fraction) recurse( root, factors, denominator, decrease_durations_monotonically, ) else: message = 'can not initialize {}: {!r}.' message = message.format(type(self).__name__, arg) raise ValueError(message) self._root_node = root self._numerator = numerator self._denominator = denominator self._decrease_durations_monotonically = \ decrease_durations_monotonically