示例#1
0
 def __init__(self, pitch_segment=None, ratio=None):
     if pitch_segment is not None:
         pitch_segment = abjad.PitchSegment(pitch_segment)
     self._pitch_segment = pitch_segment
     if ratio is not None:
         ratio = abjad.Ratio(ratio)
     self._ratio = ratio
示例#2
0
def test_new_02():

    old_aggregate = Aggregate(pitch_segment=abjad.PitchSegment('c d e f'),
                              ratio=abjad.Ratio([1, 2, 3]))

    assert format(old_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((1, 2, 3)),
            )
        """)

    new_aggregate = abjad.new(
        old_aggregate,
        ratio=(4, 5),
    )

    assert new_aggregate is not old_aggregate
    assert new_aggregate != old_aggregate
    assert format(old_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((1, 2, 3)),
            )
        """)
    assert format(new_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((4, 5)),
            )
        """)
示例#3
0
def test_new_01():

    old_aggregate = Aggregate(
        pitch_segment=abjad.PitchSegment("c d e f"),
        ratio=abjad.Ratio([1, 2, 3]),
    )

    assert abjad.storage(old_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((1, 2, 3)),
            )
        """)

    new_aggregate = abjad.new(old_aggregate)

    assert new_aggregate is not old_aggregate
    assert new_aggregate == old_aggregate
    assert abjad.storage(old_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((1, 2, 3)),
            )
        """)
    assert abjad.storage(new_aggregate) == abjad.String.normalize(r"""
        test_new.Aggregate(
            pitch_segment=abjad.PitchSegment(
                (
                    abjad.NamedPitch('c'),
                    abjad.NamedPitch('d'),
                    abjad.NamedPitch('e'),
                    abjad.NamedPitch('f'),
                    ),
                item_class=abjad.NamedPitch,
                ),
            ratio=abjad.Ratio((1, 2, 3)),
            )
        """)
 def __init__(self, parts=0, ratio=(1, 1), mask_timespan=None):
     if not isinstance(ratio, abjad.Ratio):
         ratio = abjad.Ratio(ratio)
     self._ratio = ratio
     if isinstance(parts, int):
         parts = (parts, )
     assert all(0 <= _ < len(ratio) for _ in parts)
     parts = tuple(sorted(set(parts)))
     self._parts = parts
     if mask_timespan is not None:
         assert isinstance(mask_timespan, abjad.Timespan)
     self._mask_timespan = mask_timespan
示例#5
0
 def __init__(
     self,
     prefix_talea=None,
     prefix_counts=None,
     suffix_talea=None,
     suffix_counts=None,
     talea_denominator=None,
     body_ratio=None,
     fill_with_notes=True,
     outer_divisions_only=None,
     ):
     import abjad
     prefix_talea = prefix_talea or ()
     prefix_talea = tuple(prefix_talea)
     assert self._is_integer_tuple(prefix_talea)
     self._prefix_talea = prefix_talea
     prefix_counts = prefix_counts or ()
     prefix_counts = tuple(prefix_counts)
     assert self._is_length_tuple(prefix_counts)
     self._prefix_counts = prefix_counts
     if prefix_counts and prefix_counts != (0,):
         assert prefix_talea
     if prefix_talea:
         assert prefix_counts
     suffix_talea = suffix_talea or ()
     suffix_talea = tuple(suffix_talea)
     assert self._is_integer_tuple(suffix_talea)
     self._suffix_talea = suffix_talea
     assert self._is_length_tuple(suffix_counts)
     suffix_counts = suffix_counts or ()
     suffix_counts = tuple(suffix_counts)
     self._suffix_counts = suffix_counts
     if suffix_counts and suffix_counts != (0,):
         assert suffix_talea
     if suffix_talea:
         assert suffix_counts
     if talea_denominator is not None:
         if not abjad.mathtools.is_nonnegative_integer_power_of_two(
             talea_denominator):
             message = 'talea denominator {!r} must be nonnegative'
             message += ' integer power of 2.'
             message = message.format(talea_denominator)
             raise Exception(message)
     self._talea_denominator = talea_denominator
     if prefix_talea or suffix_talea:
         assert talea_denominator is not None
     if body_ratio is not None:
         body_ratio = abjad.Ratio(body_ratio)
     self._body_ratio = body_ratio
     assert isinstance(fill_with_notes, bool)
     self._fill_with_notes = fill_with_notes
     assert isinstance(outer_divisions_only, (bool, type(None)))
     self._outer_divisions_only = outer_divisions_only
示例#6
0
    def ratio(self):
        r'''Gets ratio of metric modulation.

        ..  container:: example

            >>> metric_modulation = abjad.MetricModulation(
            ...     left_rhythm=abjad.Tuplet((2, 3), [abjad.Note("c'4")]),
            ...     right_rhythm=abjad.Note("c'4"),
            ...     )
            >>> metric_modulation.ratio
            Ratio((2, 3))

        Returns ratio.
        '''
        import abjad
        left_duration = abjad.inspect(self.left_rhythm).get_duration()
        right_duration = abjad.inspect(self.right_rhythm).get_duration()
        duration = left_duration / right_duration
        ratio = abjad.Ratio(duration.pair)
        return ratio
示例#7
0
 def _to_tuplet_with_ratio(self, proportions, is_diminution=True):
     import abjad
     # check input
     proportions = abjad.Ratio(proportions)
     # find target duration of tuplet
     target_duration = self.written_duration
     # find basic duration of note in tuplet
     basic_prolated_duration = target_duration / sum(proportions.numbers)
     # find basic written duration of note in tuplet
     basic_written_duration = \
         basic_prolated_duration.equal_or_greater_assignable
     # find written duration of each note in tuplet
     written_durations = [
         _ * basic_written_duration for _ in proportions.numbers
     ]
     # make tuplet notes
     maker = abjad.NoteMaker()
     try:
         notes = [abjad.Note(0, x) for x in written_durations]
     except AssignabilityError:
         denominator = target_duration.denominator
         note_durations = [
             abjad.Duration(_, denominator) for _ in proportions.numbers
         ]
         notes = maker(0, note_durations)
     # make tuplet
     contents_duration = abjad.inspect(notes).get_duration()
     multiplier = target_duration / contents_duration
     tuplet = abjad.Tuplet(multiplier, notes)
     # fix tuplet contents if necessary
     tuplet._fix()
     # change prolation if necessary
     if not tuplet.multiplier == 1:
         if is_diminution:
             if not tuplet.is_diminution:
                 tuplet.toggle_prolation()
         else:
             if tuplet.is_diminution:
                 tuplet.toggle_prolation()
     # return tuplet
     return tuplet
示例#8
0
    def to_tuplet(
        self,
        proportions,
        dotted=False,
        diminution=True,
    ):
        r'''Changes logical tie to tuplet.

        ..  container:: example

            Changes logical tie to diminished tuplet:

            >>> staff = abjad.Staff(r"c'8 ~ c'16 cqs''4")
            >>> crescendo = abjad.Hairpin('p < f')
            >>> abjad.attach(crescendo, staff[:])
            >>> abjad.override(staff).dynamic_line_spanner.staff_padding = 3
            >>> time_signature = abjad.TimeSignature((7, 16))
            >>> abjad.attach(time_signature, staff[0])
            >>> abjad.show(staff) # doctest: +SKIP

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                \with
                {
                    \override DynamicLineSpanner.staff-padding = #3
                }
                {
                    \time 7/16
                    c'8
                    ~
                    \<
                    \p
                    c'16
                    cqs''4
                    \f
                }

            >>> logical_tie = abjad.inspect(staff[0]).get_logical_tie()
            >>> logical_tie.to_tuplet([2, 1, 1, 1], diminution=True)
            Tuplet(Multiplier(3, 5), "c'8 c'16 c'16 c'16")

            >>> time_signature = abjad.TimeSignature((7, 16))
            >>> leaf = abjad.inspect(staff).get_leaf(0)
            >>> abjad.attach(time_signature, leaf)

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                \with
                {
                    \override DynamicLineSpanner.staff-padding = #3
                }
                {
                    \tweak text #tuplet-number::calc-fraction-text
                    \times 3/5 {
                        \time 7/16
                        c'8
                        \<
                        \p
                        c'16
                        c'16
                        c'16
                    }
                    cqs''4
                    \f
                }

            >>> abjad.show(staff) # doctest: +SKIP

        ..  container:: example

            Changes logical tie to augmented tuplet:

            >>> staff = abjad.Staff(r"c'8 ~ c'16 cqs''4")
            >>> crescendo = abjad.Hairpin(descriptor='p < f')
            >>> abjad.attach(crescendo, staff[:])
            >>> abjad.override(staff).dynamic_line_spanner.staff_padding = 3
            >>> time_signature = abjad.TimeSignature((7, 16))
            >>> abjad.attach(time_signature, staff[0])
            >>> abjad.show(staff) # doctest: +SKIP

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                \with
                {
                    \override DynamicLineSpanner.staff-padding = #3
                }
                {
                    \time 7/16
                    c'8
                    ~
                    \<
                    \p
                    c'16
                    cqs''4
                    \f
                }

            >>> logical_tie = abjad.inspect(staff[0]).get_logical_tie()
            >>> tuplet = logical_tie.to_tuplet(
            ...     [2, 1, 1, 1],
            ...     diminution=False,
            ...     )
            >>> time_signature = abjad.TimeSignature((7, 16))
            >>> leaf = abjad.inspect(staff).get_leaf(0)
            >>> abjad.attach(time_signature, leaf)
            >>> abjad.show(staff) # doctest: +SKIP

            ..  docs::

                >>> abjad.f(staff)
                \new Staff
                \with
                {
                    \override DynamicLineSpanner.staff-padding = #3
                }
                {
                    \tweak text #tuplet-number::calc-fraction-text
                    \times 6/5 {
                        \time 7/16
                        c'16
                        \<
                        \p
                        c'32
                        c'32
                        c'32
                    }
                    cqs''4
                    \f
                }

        Returns tuplet.
        '''
        import abjad
        # coerce input
        proportions = abjad.Ratio(proportions)
        # find target duration of tuplet
        target_duration = self._get_preprolated_duration()
        # find duration of each note in tuplet
        prolated_duration = target_duration / sum(proportions.numbers)
        # find written duration of each note in tuplet
        if diminution:
            if dotted:
                basic_written_duration = \
                    prolated_duration.equal_or_greater_assignable
            else:
                basic_written_duration = \
                    prolated_duration.equal_or_greater_power_of_two
        else:
            if dotted:
                basic_written_duration = \
                    prolated_duration.equal_or_lesser_assignable
            else:
                basic_written_duration = \
                    prolated_duration.equal_or_lesser_power_of_two
        # find written duration of each note in tuplet
        written_durations = [
            _ * basic_written_duration for _ in proportions.numbers
        ]
        # make tuplet notes
        maker = abjad.NoteMaker()
        try:
            notes = [abjad.Note(0, _) for _ in written_durations]
        except AssignabilityError:
            denominator = target_duration._denominator
            note_durations = [
                abjad.Duration(_, denominator) for _ in proportions.numbers
            ]
            notes = maker(0, note_durations)
        # make tuplet
        tuplet = abjad.Tuplet.from_duration(target_duration, notes)
        # remove tie spanner from leaves
        for leaf in self:
            for spanner in leaf._get_spanners(abjad.Tie):
                spanner._sever_all_leaves()
        # replace leaves with tuplet
        abjad.mutate(self).replace(tuplet)
        return tuplet
示例#9
0
def make_piano_material(staff, circuit):
    assert staff in ("rh", "lh")
    tag = baca.tags.function_name(inspect.currentframe())
    maker = abjad.makers.tuplet_from_ratio_and_pair
    pairs = {}
    pairs["rh"] = [(n, 16) for n in (4, 3, 3, 4, 3, 3, 4, 4)]
    pairs["lh"] = [(n, 16) for n in (3, 4, 3, 2, 4, 4, 4, 4)]
    proportions = {}
    proportions["rh"] = [
        abjad.Ratio((2, 2, 2, 1, 1, 1, 1)),
        abjad.Ratio((1, 1, 4, 4, 4)),
        abjad.Ratio((4, 4, 2, 2, 1, 1)),
        abjad.Ratio((4, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1)),
        abjad.Ratio((4, 2, 2, 2, 2, 1, 1, 1, 1)),
        abjad.Ratio((4, 4, 1, 1, 4, 1, 1)),
        abjad.Ratio((4, 12, 12)),
        abjad.Ratio((1, 1, 2, 2, 4, 4)),
    ]
    proportions["lh"] = [
        abjad.Ratio((4, 3, 3, 3, 1)),
        abjad.Ratio((4, 4, 3, 3, 2, 2, 2)),
        abjad.Ratio((2, 2, 2, 4, 4, 4)),
        abjad.Ratio((-8, )),
        abjad.Ratio((6, 6, 8)),
        abjad.Ratio((2, 2, 3, 3, 4)),
        abjad.Ratio((2, 2, 1, 1, 1, 1, 4, 4, 4)),
        abjad.Ratio((6, 6, 2, 2, 1, 1)),
    ]
    music = []
    for proportion, pair, aggregate in zip(proportions[staff], pairs[staff],
                                           circuit):
        if staff == "rh":
            aggregate = list(reversed(aggregate))
        tuplet = maker(proportion, pair, tag=tag)
        assert isinstance(tuplet, abjad.Tuplet)
        duration = abjad.get.duration(tuplet)
        duration = duration.with_denominator(32)
        tuplet.denominator = duration.numerator
        if tuplet.trivial():
            tuplet.hide = True
        leaves = abjad.select.leaves(tuplet)
        abjad.beam(leaves, tag=tag)
        notes = abjad.select.leaves(tuplet, pitched=True)
        for note, pitch_number in zip(notes, aggregate):
            note.written_pitch = pitch_number
        music.append(tuplet)
    music.insert(-1, abjad.Rest("r8", tag=tag))
    return music