def durations_to_nonreduced_fractions( durations: typing.List, ) -> typing.List[mathtools.NonreducedFraction]: """ Changes ``durations`` to nonreduced fractions sharing least common denominator. .. container:: example Changes durations to nonreduced fractions: >>> durations = [abjad.Duration(2, 4), 3, (5, 16)] >>> result = abjad.Duration.durations_to_nonreduced_fractions(durations) >>> for x in result: ... x ... NonreducedFraction(8, 16) NonreducedFraction(48, 16) NonreducedFraction(5, 16) """ durations_ = [Duration(_) for _ in durations] denominators = [_.denominator for _ in durations_] lcd = mathtools.least_common_multiple(*denominators) nonreduced_fractions = [ mathtools.NonreducedFraction(_).with_denominator(lcd) for _ in durations_ ] return nonreduced_fractions
def _make_tied_leaf( class_, duration, increase_monotonic=None, forbidden_duration=None, multiplier=None, pitches=None, tag=None, tie_parts=True, repeat_ties=False, ): from abjad.spanners import tie as abjad_tie duration = Duration(duration) if forbidden_duration is not None: assert forbidden_duration.is_assignable assert forbidden_duration.numerator == 1 # find preferred numerator of written durations if necessary if forbidden_duration is not None and forbidden_duration <= duration: denominators = [ 2 * forbidden_duration.denominator, duration.denominator, ] denominator = mathtools.least_common_multiple(*denominators) forbidden_duration = NonreducedFraction(forbidden_duration) forbidden_duration = forbidden_duration.with_denominator( denominator ) duration = NonreducedFraction(duration) duration = duration.with_denominator(denominator) forbidden_numerator = forbidden_duration.numerator assert forbidden_numerator % 2 == 0 preferred_numerator = forbidden_numerator / 2 # make written duration numerators numerators = [] parts = mathtools.partition_integer_into_canonic_parts( duration.numerator ) if forbidden_duration is not None and forbidden_duration <= duration: for part in parts: if forbidden_numerator <= part: better_parts = LeafMaker._partition_less_than_double( part, preferred_numerator ) numerators.extend(better_parts) else: numerators.append(part) else: numerators = parts # reverse numerators if necessary if increase_monotonic: numerators = list(reversed(numerators)) # make one leaf per written duration result = [] for numerator in numerators: written_duration = Duration(numerator, duration.denominator) if pitches is not None: arguments = (pitches, written_duration) else: arguments = (written_duration,) result.append(class_(*arguments, multiplier=multiplier, tag=tag)) result = Selection(result) # tie if required if tie_parts and 1 < len(result): if not issubclass(class_, (Rest, Skip)): abjad_tie(result, repeat=repeat_ties) return result
def _make_tied_leaf( class_, duration, increase_monotonic=None, forbidden_duration=None, multiplier=None, pitches=None, tag=None, tie_parts=True, repeat_ties=False, ): from abjad.spanners import tie as abjad_tie duration = Duration(duration) if forbidden_duration is not None: assert forbidden_duration.is_assignable assert forbidden_duration.numerator == 1 # find preferred numerator of written durations if necessary if (forbidden_duration is not None and forbidden_duration <= duration): denominators = [ 2 * forbidden_duration.denominator, duration.denominator, ] denominator = mathtools.least_common_multiple(*denominators) forbidden_duration = NonreducedFraction(forbidden_duration) forbidden_duration = forbidden_duration.with_denominator( denominator) duration = NonreducedFraction(duration) duration = duration.with_denominator(denominator) forbidden_numerator = forbidden_duration.numerator assert forbidden_numerator % 2 == 0 preferred_numerator = forbidden_numerator / 2 # make written duration numerators numerators = [] parts = mathtools.partition_integer_into_canonic_parts( duration.numerator) if (forbidden_duration is not None and forbidden_duration <= duration): for part in parts: if forbidden_numerator <= part: better_parts = LeafMaker._partition_less_than_double( part, preferred_numerator, ) numerators.extend(better_parts) else: numerators.append(part) else: numerators = parts # reverse numerators if necessary if increase_monotonic: numerators = list(reversed(numerators)) # make one leaf per written duration result = [] for numerator in numerators: written_duration = Duration( numerator, duration.denominator, ) if pitches is not None: arguments = (pitches, written_duration) else: arguments = (written_duration, ) result.append(class_(*arguments, multiplier=multiplier, tag=tag)) result = Selection(result) # tie if required if tie_parts and 1 < len(result): if not issubclass(class_, (Rest, Skip)): abjad_tie(result, repeat=repeat_ties) return result