def test_scoretools_Mutation_fuse_05(): r'''Fuses leaves with differing LilyPond multipliers. ''' staff = abjad.Staff([abjad.Skip((1, 1)), abjad.Skip((1, 1))]) abjad.attach(abjad.Multiplier(1, 16), staff[0]) abjad.attach(abjad.Multiplier(5, 16), staff[1]) assert format(staff) == abjad.String.normalize(r''' \new Staff { s1 * 1/16 s1 * 5/16 } ''') assert abjad.inspect(staff).get_duration() == abjad.Duration(3, 8) abjad.mutate(staff[:]).fuse() assert format(staff) == abjad.String.normalize(r''' \new Staff { s1 * 3/8 } ''') assert abjad.inspect(staff).get_duration() == abjad.Duration(3, 8) assert abjad.inspect(staff).is_well_formed()
def with_power_of_two_denominator(self, contents_multiplier=1): r'''Makes new time signature equivalent to current time signature with power-of-two denominator. .. container:: example Non-power-of-two denominator with power-of-two denominator: >>> time_signature = abjad.TimeSignature((3, 12)) >>> time_signature.with_power_of_two_denominator() TimeSignature((2, 8)) Returns new time signature. ''' import abjad contents_multiplier = abjad.Multiplier(contents_multiplier) contents_multiplier = abjad.Multiplier(contents_multiplier) non_power_of_two_denominator = self.denominator if contents_multiplier == abjad.Multiplier(1): power_of_two_denominator = \ abjad.mathtools.greatest_power_of_two_less_equal( non_power_of_two_denominator) else: power_of_two_denominator = \ abjad.mathtools.greatest_power_of_two_less_equal( non_power_of_two_denominator, 1) non_power_of_two_pair = abjad.NonreducedFraction(self.pair) power_of_two_fraction = non_power_of_two_pair.with_denominator( power_of_two_denominator) power_of_two_pair = power_of_two_fraction.pair return type(self)(power_of_two_pair)
def test_scoretools_Mutation_fuse_08(): r'''Fuses fixed-multiplier tuplets with same multiplier in score. ''' tuplet_1 = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8") beam = abjad.Beam() abjad.attach(beam, tuplet_1[:]) tuplet_2 = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8 f'8 g'8") slur = abjad.Slur() abjad.attach(slur, tuplet_2[:]) voice = abjad.Voice([tuplet_1, tuplet_2]) assert format(voice) == abjad.String.normalize(r''' \new Voice { \times 2/3 { c'8 [ d'8 e'8 ] } \tweak edge-height #'(0.7 . 0) \times 2/3 { c'8 ( d'8 e'8 f'8 g'8 ) } } ''') tuplets = voice[:] abjad.mutate(tuplets).fuse() assert format(voice) == abjad.String.normalize(r''' \new Voice { \tweak edge-height #'(0.7 . 0) \times 2/3 { c'8 [ d'8 e'8 ] c'8 ( d'8 e'8 f'8 g'8 ) } } ''') assert abjad.inspect(voice).is_well_formed()
def _scale(self, multiplier=None): import abjad if multiplier is None: return multiplier = abjad.Multiplier(multiplier) old_time_signature = self.time_signature if (abjad.mathtools.is_nonnegative_integer_power_of_two(multiplier) and 1 <= multiplier): old_numerator = old_time_signature.numerator old_denominator = old_time_signature.denominator new_denominator = old_denominator // multiplier.numerator pair = (old_numerator, new_denominator) new_time_signature = abjad.TimeSignature(pair) else: old_denominator = old_time_signature.denominator old_duration = old_time_signature.duration new_duration = multiplier * old_duration new_time_signature = self._duration_to_time_signature( new_duration, [old_denominator], multiplier.denominator, ) abjad.detach(abjad.TimeSignature, self) abjad.attach(new_time_signature, self) contents_multiplier_denominator = \ abjad.mathtools.greatest_power_of_two_less_equal( multiplier.denominator) pair = (multiplier.numerator, contents_multiplier_denominator) contents_multiplier = abjad.Multiplier(*pair) self._scale_contents(contents_multiplier)
def test_Mutation_fuse_08(): """ Fuses fixed-multiplier tuplets with same multiplier in score. """ tuplet_1 = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8") abjad.beam(tuplet_1[:]) tuplet_2 = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8 f'8 g'8") abjad.slur(tuplet_2[:]) voice = abjad.Voice([tuplet_1, tuplet_2]) assert format(voice) == abjad.String.normalize(r""" \new Voice { \times 2/3 { c'8 [ d'8 e'8 ] } \tweak edge-height #'(0.7 . 0) \times 2/3 { c'8 ( d'8 e'8 f'8 g'8 ) } } """), print(format(voice)) tuplets = voice[:] abjad.mutate(tuplets).fuse() assert format(voice) == abjad.String.normalize(r""" \new Voice { \tweak edge-height #'(0.7 . 0) \times 2/3 { c'8 [ d'8 e'8 ] c'8 ( d'8 e'8 f'8 g'8 ) } } """), print(format(voice)) assert abjad.inspect(voice).wellformed()
def prolations(self): r'''Prolations of rhythm tree node. Returns tuple. ''' import abjad prolations = [abjad.Multiplier(1)] improper_parentage = self.improper_parentage pairs = abjad.sequence(improper_parentage).nwise() for child, parent in pairs: prolations.append(abjad.Multiplier( parent.preprolated_duration, parent._get_contents_duration())) return tuple(prolations)
def test_get_timespan_19(): """ Offsets works on sequential tuplets. """ voice = abjad.Voice([ abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8"), abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8"), abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8"), ]) assert abjad.get.timespan(voice[0]).start_offset == 0 * abjad.Offset(1, 4) assert abjad.get.timespan(voice[1]).start_offset == 1 * abjad.Offset(1, 4) assert abjad.get.timespan(voice[2]).start_offset == 2 * abjad.Offset(1, 4)
def durate_pitch_contour_reservoir(pitch_contour_reservoir): """ Durates pitch contour reservoir. """ names = ["First Violin", "Second Violin", "Viola", "Cello", "Bass"] durated_reservoir = {} for i, name in enumerate(names): long_duration = abjad.Duration(1, 2) * pow(2, i) short_duration = long_duration / 2 rest_duration = long_duration * abjad.Multiplier(3, 2) div = rest_duration // abjad.Duration(3, 2) mod = rest_duration % abjad.Duration(3, 2) initial_rest = abjad.MultimeasureRest((3, 2)) * div maker = abjad.LeafMaker() if mod: initial_rest += maker([None], mod) durated_contours = [tuple(initial_rest)] pitch_contours = pitch_contour_reservoir[name] durations = [long_duration, short_duration] counter = 0 maker = abjad.LeafMaker() for pitch_contour in pitch_contours: contour = [] for pitch in pitch_contour: leaves = maker([pitch], [durations[counter]]) contour.extend(leaves) counter = (counter + 1) % 2 durated_contours.append(tuple(contour)) durated_reservoir[name] = tuple(durated_contours) return durated_reservoir
def test_Mutation_scale_03(): """ Scales leaves by tuplet-generating multiplier. """ voice = abjad.Voice("c'8 d'8 e'8 f'8") abjad.mutate(voice).scale(abjad.Multiplier(4, 3)) assert format(voice) == abjad.String.normalize(r""" \new Voice { \tweak edge-height #'(0.7 . 0) \times 2/3 { c'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { d'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { e'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { f'4 } } """) assert abjad.inspect(voice).wellformed()
def test_Mutation_scale_02(): """ Scales leaves by tie-generating multiplier. """ voice = abjad.Voice("c'8 d'8 e'8 f'8") abjad.mutate(voice).scale(abjad.Multiplier(5, 4)) assert format(voice) == abjad.String.normalize(r""" \new Voice { c'8 ~ c'32 d'8 ~ d'32 e'8 ~ e'32 f'8 ~ f'32 } """) assert abjad.inspect(voice).wellformed()
def test_scoretools_Leaf__multiplied_duration_01(): r'''Mulplied duration == written * multiplier. ''' note = abjad.Note("c'4") abjad.attach(abjad.Multiplier(1, 2), note) assert note._get_multiplied_duration() == abjad.Duration(1, 8)
def implied_prolation(self): r'''Gets implied prolation. .. container:: example Gets implied prolation: >>> for denominator in range(1, 16 + 1): ... duration = abjad.Duration(1, denominator) ... result = duration.implied_prolation ... print('{!s}\t{!s}'.format(duration, result)) ... 1 1 1/2 1 1/3 2/3 1/4 1 1/5 4/5 1/6 2/3 1/7 4/7 1/8 1 1/9 8/9 1/10 4/5 1/11 8/11 1/12 2/3 1/13 8/13 1/14 4/7 1/15 8/15 1/16 1 Returns multipler. ''' import abjad numerator = \ mathtools.greatest_power_of_two_less_equal(self.denominator) return abjad.Multiplier(numerator, self.denominator)
def multiply_by(n, note): new_n = deepcopy(note) new_n.written_duration /= 4 tup = abj.Tuplet(abj.Multiplier(2, n), []) for varied_note in vary(new_n, n): tup.append(varied_note) return tup
def test_scoretools_Mutation_scale_03(): r'''Scales leaves by tuplet-generating multiplier. ''' voice = abjad.Voice("c'8 d'8 e'8 f'8") abjad.mutate(voice).scale(abjad.Multiplier(4, 3)) assert format(voice) == abjad.String.normalize(r''' \new Voice { \tweak edge-height #'(0.7 . 0) \times 2/3 { c'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { d'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { e'4 } \tweak edge-height #'(0.7 . 0) \times 2/3 { f'4 } } ''') assert abjad.inspect(voice).is_well_formed()
def test_scoretools_Mutation_scale_02(): r'''Scales leaves by tie-generating multiplier. ''' voice = abjad.Voice("c'8 d'8 e'8 f'8") abjad.mutate(voice).scale(abjad.Multiplier(5, 4)) assert format(voice) == abjad.String.normalize(r''' \new Voice { c'8 ~ c'32 d'8 ~ d'32 e'8 ~ e'32 f'8 ~ f'32 } ''') assert abjad.inspect(voice).is_well_formed()
def test_scoretools_Tuplet___setattr___01(): r'''Slots constrain tuplet attributes. ''' tuplet = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8") assert pytest.raises(AttributeError, "tuplet.foo = 'bar'")
def test_scoretools_Component__remove_and_shrink_durated_parent_containers_04( ): r'''Remove leaf from tuplet. ''' tuplet = abjad.Tuplet(abjad.Multiplier(4, 5), "c'8 d'8 e'8 f'8 g'8") assert format(tuplet) == abjad.String.normalize(r''' \times 4/5 { c'8 d'8 e'8 f'8 g'8 } ''') tuplet[0]._remove_and_shrink_durated_parent_containers() assert format(tuplet) == abjad.String.normalize(r''' \tweak edge-height #'(0.7 . 0) \times 4/5 { d'8 e'8 f'8 g'8 } ''') assert abjad.inspect(tuplet).is_well_formed()
def multipliers(self): r'''Gets multipliers of ratio. .. container:: example Ratio of two numbers: >>> ratio = abjad.Ratio((2, 4)) >>> ratio.multipliers (Multiplier(1, 3), Multiplier(2, 3)) .. container:: example Ratio of three numbers: >>> ratio = abjad.Ratio((2, 4, 2)) >>> ratio.multipliers (Multiplier(1, 4), Multiplier(1, 2), Multiplier(1, 4)) Returns tuple of multipliers. ''' import abjad weight = sum(self.numbers) multipliers = [abjad.Multiplier((_, weight)) for _ in self.numbers] multipliers = tuple(multipliers) return multipliers
def __div__(self, *arguments): """ Divides duration by ``arguments``. .. container:: example >>> abjad.Duration(1) / abjad.NonreducedFraction(3, 3) NonreducedFraction(3, 3) >>> abjad.NonreducedFraction(3, 3) / abjad.Duration(1) NonreducedFraction(3, 3) Returns multiplier. """ import abjad if len(arguments) == 1 and isinstance(arguments[0], type(self)): fraction = Fraction.__truediv__(self, *arguments) result = abjad.Multiplier(fraction) elif len(arguments) == 1 and isinstance( arguments[0], abjad.NonreducedFraction): result = arguments[0].__rdiv__(self) else: result = type(self)(Fraction.__truediv__(self, *arguments)) return result
def __call__(self, time_signatures): r'''Calls measure-maker on `time_signatures`. Returns measures. ''' import abjad measures = [] for time_signature in time_signatures: time_signature = abjad.TimeSignature(time_signature) measure = abjad.Measure( time_signature, implicit_scaling=self.implicit_scaling, ) measures.append(measure) for i, measure in enumerate(measures): skip = abjad.Skip(1) # allow zero-update iteration time_signature = measure.time_signature duration = time_signature.duration if measure.implicit_scaling: implied_prolation = time_signature.implied_prolation multiplier = duration.__div__(implied_prolation) else: multiplier = abjad.Multiplier(duration) abjad.attach(multiplier, skip) measure[:] = [skip] # REMOVE: spanners attach only to leaves: #for spanner in measure._get_spanners(): # spanner._remove(measure) return abjad.select(measures)
def _prolations(self): import abjad prolations = [] default = abjad.Multiplier(1) for parent in self: prolation = getattr(parent, 'implied_prolation', default) prolations.append(prolation) return prolations
def prolations(self): """ Prolations of rhythm tree node. Returns tuple. """ import abjad prolations = [abjad.Multiplier(1)] pairs = abjad.sequence(self.parentage).nwise() for child, parent in pairs: prolations.append( abjad.Multiplier( parent.preprolated_duration, parent._get_contents_duration(), )) return tuple(prolations)
def prolation(self): r'''Gets prolation. Returns multiplier. ''' import abjad prolations = [abjad.Multiplier(1)] + self._prolations products = mathtools.cumulative_products(prolations) return products[-1]
def _all_contents_are_scalable_by_multiplier(self, multiplier): import abjad multiplier = abjad.Multiplier(multiplier) for component in self: if isinstance(component, abjad.Leaf): candidate_duration = multiplier * component.written_duration if not candidate_duration.is_assignable: return False return True
def test_scoretools_Tuplet___init___01(): r'''Initializes tuplet from empty input. ''' tuplet = abjad.Tuplet() assert format(tuplet) == '\\times 2/3 {\n}' assert tuplet.multiplier == abjad.Multiplier(2, 3) assert not len(tuplet)
def scale_and_chop_staff(voice_number, staff, time_signature): # Scales a staff's durations by a factor and then chops & ties everything at 3/4 measure boundaries. scale_factor = 2 ** voice_number copy = abjad.mutate(staff).copy() for leaf in copy: leaf.written_pitch -= voice_number * 12 abjad.mutate(copy).scale(abjad.Multiplier(scale_factor, 1)) abjad.mutate(copy[:]).split([time_signature], cyclic=True) return copy
def __init__( self, contact_point=None, ): import abjad if contact_point is not None: contact_point = abjad.Multiplier(contact_point) assert 0 <= contact_point <= 1 self._contact_point = contact_point
def test_Tuplet___setattr___01(): """ Slots constrain tuplet attributes. """ tuplet = abjad.Tuplet(abjad.Multiplier(2, 3), "c'8 d'8 e'8") with pytest.raises(AttributeError): tuplet.foo = "bar"
def __illustrate__(self, range_=None, scale=None): r'''Illustrates offset counter. .. container:: example >>> timespans = abjad.TimespanList([ ... abjad.Timespan(0, 16), ... abjad.Timespan(5, 12), ... abjad.Timespan(-2, 8), ... ]) >>> timespan_operand = abjad.Timespan(6, 10) >>> timespans = timespans - timespan_operand >>> offset_counter = abjad.OffsetCounter(timespans) >>> abjad.show(offset_counter, scale=0.5) # doctest: +SKIP Returns LilyPond file. ''' import abjad if not self: return markuptools.Markup.null().__illustrate__() if isinstance(range_, abjad.Timespan): minimum, maximum = range_.start_offset, range_.stop_offset elif range_ is not None: minimum, maximum = range_ else: minimum, maximum = min(self), max(self) minimum = float(abjad.Offset(minimum)) maximum = float(abjad.Offset(maximum)) if scale is None: scale = 1. assert 0 < scale postscript_scale = 150. / (maximum - minimum) postscript_scale *= float(scale) postscript_x_offset = (minimum * postscript_scale) - 1 ps = markuptools.Postscript() ps = ps.setlinewidth(0.2) ps = ps.setdash([2, 1]) for offset, count in sorted(self.items()): offset = (float(offset) * postscript_scale) offset -= postscript_x_offset ps = ps.moveto(offset, -1) ps = ps.rlineto(0, (float(count) * -3) + 1) ps = ps.stroke() markup = markuptools.Markup.postscript(ps) pieces = [markup] for offset in sorted(self): offset = abjad.Multiplier(offset) numerator, denominator = offset.numerator, offset.denominator fraction = abjad.Markup.fraction(numerator, denominator) fraction = fraction.center_align().fontsize(-3).sans() x_translation = (float(offset) * postscript_scale) x_translation -= postscript_x_offset fraction = fraction.translate((x_translation, 1)) pieces.append(fraction) markup = abjad.Markup.overlay(pieces) return markup.__illustrate__()
def test_Tuplet___init___01(): """ Initializes tuplet from empty input. """ tuplet = abjad.Tuplet() assert format(tuplet) == "\\times 2/3 {\n}" assert tuplet.multiplier == abjad.Multiplier(2, 3) assert not len(tuplet)