Exemple #1
0
def make_windungen_score(
        bandwidth=3,
        compress_reflections=True,
        leaf_duration=durationtools.Duration(1, 16),
        length=32,
        pitches=('c', 'd', 'e'),
        staff_count=12,
):

    from experimental.demos.windungen.WindungenScoreTemplate import WindungenScoreTemplate

    bandwidth = int(bandwidth)
    compress_reflections = bool(compress_reflections)
    leaf_duration = durationtools.Duration(leaf_duration)
    length = int(length)
    pitches = [pitchtools.NamedPitch(x) for x in pitches]
    staff_count = int(staff_count)

    assert 0 < bandwidth
    assert 0 < leaf_duration
    assert 0 < length
    assert 0 < len(pitches)
    assert 0 < staff_count

    score_template = WindungenScoreTemplate(staff_count=staff_count)
    score = score_template()

    all_pitches = sequencetools.repeat_sequence_to_length(length)

    matrix = make_cyclic_matrix_for_rotation_by_bandwidth()
Exemple #2
0
 def __init__(self, calculation_rate=None, channel_count=1, default=0):
     if not isinstance(default, collections.Sequence):
         default = (default,)
     default = [float(_) for _ in default]
     default = sequencetools.repeat_sequence_to_length(default, channel_count)
     default = default[:channel_count]
     MultiOutUGen.__init__(self, calculation_rate=calculation_rate, channel_count=channel_count, default=default)
    def repeat_to_length(self, length):
        r'''Repeat payload expression to length.

        ::

            >>> result = payload_expression.repeat_to_length(4)

        ::

            >>> print(format(result))
            musicexpressiontools.IterablePayloadExpression(
                payload=(
                    (4, 16),
                    (2, 16),
                    (4, 16),
                    (2, 16),
                    ),
                )

        Returns newly constructed payload expression.
        '''
        payload = sequencetools.repeat_sequence_to_length(
            self.payload, length)
        result = new(self, payload=payload)
        return result
    def repeat_to_length(self, length):
        r'''Repeat start-positioned division payload expression to `length`.

        ::

            >>> payload = [(6, 8), (6, 8), (3, 4)]
            >>> expression = musicexpressiontools.StartPositionedDivisionPayloadExpression(
            ...     payload, Offset(0))

        ::

            >>> result = expression.repeat_to_length(5)

        ::

            >>> print(format(result))
            musicexpressiontools.StartPositionedDivisionPayloadExpression(
                payload=musicexpressiontools.DivisionList(
                    [
                        durationtools.Division(6, 8),
                        durationtools.Division(6, 8),
                        durationtools.Division(3, 4),
                        durationtools.Division(6, 8),
                        durationtools.Division(6, 8),
                        ],
                    start_offset=durationtools.Offset(0, 1),
                    ),
                start_offset=durationtools.Offset(0, 1),
                )

        Returns newly constructed start-positioned division payload expression.
        '''
        divisions = sequencetools.repeat_sequence_to_length(self.payload, length)
        result = type(self)(payload=divisions, voice_name=self.voice_name, start_offset=self.start_offset)
        return result
def make_windungen_score(
    bandwidth=3,
    compress_reflections=True,
    leaf_duration=durationtools.Duration(1, 16),
    length=32,
    pitches=('c', 'd', 'e'),
    staff_count=12,
    ):

    from experimental.demos.windungen.WindungenScoreTemplate import WindungenScoreTemplate

    bandwidth = int(bandwidth)
    compress_reflections = bool(compress_reflections)
    leaf_duration = durationtools.Duration(leaf_duration)
    length = int(length)
    pitches = [pitchtools.NamedPitch(x) for x in pitches]
    staff_count = int(staff_count)

    assert 0 < bandwidth
    assert 0 < leaf_duration
    assert 0 < length
    assert 0 < len(pitches)
    assert 0 < staff_count

    score_template = WindungenScoreTemplate(staff_count=staff_count)
    score = score_template()

    all_pitches = sequencetools.repeat_sequence_to_length(length)

    matrix = make_cyclic_matrix_for_rotation_by_bandwidth()
Exemple #6
0
 def __init__(
     self,
     calculation_rate=None,
     channel_count=1,
     default=0,
 ):
     if not isinstance(default, collections.Sequence):
         default = (default, )
     default = [float(_) for _ in default]
     default = sequencetools.repeat_sequence_to_length(
         default,
         channel_count,
     )
     default = default[:channel_count]
     MultiOutUGen.__init__(
         self,
         calculation_rate=calculation_rate,
         channel_count=channel_count,
         default=default,
     )
Exemple #7
0
    def repeat_to_length(self, length):
        r'''Repeat start-positioned division payload expression to `length`.

        ::

            >>> payload = [(6, 8), (6, 8), (3, 4)]
            >>> expression = musicexpressiontools.StartPositionedDivisionPayloadExpression(
            ...     payload, Offset(0))

        ::

            >>> result = expression.repeat_to_length(5)

        ::

            >>> print(format(result))
            musicexpressiontools.StartPositionedDivisionPayloadExpression(
                payload=musicexpressiontools.DivisionList(
                    [
                        durationtools.Division(6, 8),
                        durationtools.Division(6, 8),
                        durationtools.Division(3, 4),
                        durationtools.Division(6, 8),
                        durationtools.Division(6, 8),
                        ],
                    start_offset=durationtools.Offset(0, 1),
                    ),
                start_offset=durationtools.Offset(0, 1),
                )

        Returns newly constructed start-positioned division payload expression.
        '''
        divisions = sequencetools.repeat_sequence_to_length(
            self.payload, length)
        result = type(self)(payload=divisions,
                            voice_name=self.voice_name,
                            start_offset=self.start_offset)
        return result
Exemple #8
0
    def repeat_to_length(self, length):
        r'''Repeat payload expression to length.

        ::

            >>> result = payload_expression.repeat_to_length(4)

        ::

            >>> print(format(result))
            musicexpressiontools.IterablePayloadExpression(
                payload=(
                    (4, 16),
                    (2, 16),
                    (4, 16),
                    (2, 16),
                    ),
                )

        Returns newly constructed payload expression.
        '''
        payload = sequencetools.repeat_sequence_to_length(self.payload, length)
        result = new(self, payload=payload)
        return result
Exemple #9
0
def make_notes(
    pitches, 
    durations, 
    decrease_durations_monotonically=True,
    use_messiaen_style_ties=False,
    ):
    r'''Makes notes according to `pitches` and `durations`.


    ..  container:: example

        **Example 1.** Cycles through `pitches` when the length of `pitches` is
        less than the length of `durations`:

        ::

            >>> notes = scoretools.make_notes([0], [(1, 16), (1, 8), (1, 8)])
            >>> notes
            Selection(Note("c'16"), Note("c'8"), Note("c'8"))
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'16
                c'8
                c'8
            }

    ..  container:: example

        **Example 2.** Cycles through `durations` when the length of `durations`
        is less than the length of `pitches`:

        ::

            >>> notes = scoretools.make_notes(
            ...     [0, 2, 4, 5, 7],
            ...     [(1, 16), (1, 8), (1, 8)],
            ...     )
            >>> notes
            Selection(Note("c'16"), Note("d'8"), Note("e'8"), Note("f'16"), Note("g'8"))
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'16
                d'8
                e'8
                f'16
                g'8
            }

    ..  container:: example

        **Example 3.** Creates ad hoc tuplets for nonassignable durations:

        ::

            >>> notes = scoretools.make_notes([0], [(1, 16), (1, 12), (1, 8)])
            >>> notes
            Selection(Note("c'16"), Tuplet(Multiplier(2, 3), "c'8"), Note("c'8"))
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'16
                \tweak #'edge-height #'(0.7 . 0)
                \times 2/3 {
                    c'8
                }
                c'8
            }

    ..  container:: example

        **Example 4.** Set ``decrease_durations_monotonically=True`` to express
        tied values in decreasing duration:

        ::

            >>> notes = scoretools.make_notes(
            ...     [0],
            ...     [(13, 16)],
            ...     decrease_durations_monotonically=True,
            ...     )
            >>> notes
            Selection(Note("c'2."), Note("c'16"))
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'2. ~
                c'16
            }

    ..  container:: example

        **Example 5.** Set ``decrease_durations_monotonically=False`` to
        express tied values in increasing duration:

        ::

            >>> notes = scoretools.make_notes(
            ...     [0],
            ...     [(13, 16)],
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> notes
            Selection(Note("c'16"), Note("c'2."))
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'16 ~
                c'2.
            }

    ..  container:: example

        **Example 6.** Uses Messiaen-style ties:

        ::

            >>> notes = scoretools.make_notes(
            ...     [0],
            ...     [(13, 16)],
            ...     use_messiaen_style_ties=True,
            ...     )
            >>> staff = Staff(notes)

        ::

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

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'2.
                c'16 \repeatTie
            }

    Set `pitches` to a single pitch or a sequence of pitches.

    Set `durations` to a single duration or a list of durations.

    Returns selection.
    '''
    from abjad.tools import scoretools
    from abjad.tools import selectiontools

    if isinstance(pitches, str):
        pitches = pitches.split()

    if not isinstance(pitches, list):
        pitches = [pitches]

    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]

    nonreduced_fractions = [mathtools.NonreducedFraction(_) for _ in durations]
    size = max(len(nonreduced_fractions), len(pitches))
    nonreduced_fractions = sequencetools.repeat_sequence_to_length(
        nonreduced_fractions, 
        size,
        )
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)
    Duration = durationtools.Duration
    durations = Duration._group_nonreduced_fractions_by_implied_prolation(
        nonreduced_fractions)

    def _make_unprolated_notes(
        pitches,
        durations,
        decrease_durations_monotonically=decrease_durations_monotonically,
        use_messiaen_style_ties=False,
        ):
        assert len(pitches) == len(durations)
        result = []
        for pitch, duration in zip(pitches, durations):
            result.extend(
                scoretools.make_tied_leaf(
                    scoretools.Note,
                    duration,
                    pitches=pitch,
                    decrease_durations_monotonically=decrease_durations_monotonically,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                    )
                )
        return result

    result = []
    for duration in durations:
        # get factors in denominator of duration group duration not 1 or 2
        factors = set(mathtools.factors(duration[0].denominator))
        factors.discard(1)
        factors.discard(2)
        ps = pitches[0:len(duration)]
        pitches = pitches[len(duration):]
        if len(factors) == 0:
            result.extend(
                _make_unprolated_notes(
                    ps,
                    duration,
                    decrease_durations_monotonically=decrease_durations_monotonically,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                    )
                )
        else:
            # compute prolation
            denominator = duration[0].denominator
            numerator = mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / fractions.Fraction(*multiplier)
            duration = [ratio * durationtools.Duration(d) for d in duration]
            ns = _make_unprolated_notes(
                ps,
                duration,
                decrease_durations_monotonically=decrease_durations_monotonically,
                use_messiaen_style_ties=use_messiaen_style_ties,
                )
            t = scoretools.Tuplet(multiplier, ns)
            result.append(t)

    # return result
    result = selectiontools.Selection(result)
    return result
Exemple #10
0
def make_notes(pitches, durations, decrease_durations_monotonically=True):
    r'''Make notes according to `pitches` and `durations`.

    Cycle through `pitches` when the length of `pitches` is less than the
    length of `durations`:

    ::

        >>> notetools.make_notes([0], [(1, 16), (1, 8), (1, 8)])
        Selection(Note("c'16"), Note("c'8"), Note("c'8"))

    Cycle through `durations` when the length of `durations` is less than the
    length of `pitches`:

    ::

        >>> notetools.make_notes([0, 2, 4, 5, 7], [(1, 16), (1, 8), (1, 8)])
        Selection(Note("c'16"), Note("d'8"), Note("e'8"), Note("f'16"), Note("g'8"))

    Create ad hoc tuplets for nonassignable durations:

    ::

        >>> notetools.make_notes([0], [(1, 16), (1, 12), (1, 8)])
        Selection(Note("c'16"), Tuplet(2/3, [c'8]), Note("c'8"))

    Set ``decrease_durations_monotonically=True`` to express tied values 
    in decreasing duration:

    ::

        >>> notetools.make_notes(
        ...     [0], 
        ...     [(13, 16)], 
        ...     decrease_durations_monotonically=True,
        ...     )
        Selection(Note("c'2."), Note("c'16"))

    Set ``decrease_durations_monotonically=False`` to express tied 
    values in increasing duration:

    ::

        >>> notetools.make_notes(
        ...     [0], 
        ...     [(13, 16)], 
        ...     decrease_durations_monotonically=False,
        ...     )
        Selection(Note("c'16"), Note("c'2."))

    Set `pitches` to a single pitch or a sequence of pitches.

    Set `durations` to a single duration or a list of durations.

    Returns list of newly constructed notes.
    '''
    from abjad.tools import leaftools
    from abjad.tools import notetools
    from abjad.tools import selectiontools
    from abjad.tools import tuplettools

    if isinstance(pitches, str):
        pitches = pitches.split()

    if not isinstance(pitches, list):
        pitches = [pitches]

    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]

    duration_pairs = [durationtools.Duration(duration) 
        for duration in durations]

    # set lists of pitches and duration pairs to the same length
    size = max(len(duration_pairs), len(pitches))
    duration_pairs = \
        sequencetools.repeat_sequence_to_length(duration_pairs, size)
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)

    Duration = durationtools.Duration
    durations = Duration._group_nonreduced_fractions_by_implied_prolation(
        duration_pairs)

    def _make_unprolated_notes(
        pitches,
        durations,
        decrease_durations_monotonically=decrease_durations_monotonically,
        ):
        assert len(pitches) == len(durations)
        result = []
        for pitch, duration in zip(pitches, durations):
            result.extend(leaftools.make_tied_leaf(
                notetools.Note,
                duration,
                pitches=pitch,
                decrease_durations_monotonically=decrease_durations_monotonically,
                ))
        return result

    result = []
    for duration in durations:
        # get factors in denominator of duration group duration other than 1, 2.
        factors = set(mathtools.factors(duration[0].denominator))
        factors.discard(1)
        factors.discard(2)
        ps = pitches[0:len(duration)]
        pitches = pitches[len(duration):]
        if len(factors) == 0:
            result.extend(_make_unprolated_notes(ps, duration,
                decrease_durations_monotonically=decrease_durations_monotonically))
        else:
            # compute prolation
            denominator = duration[0].denominator
            numerator = mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / fractions.Fraction(*multiplier)
            duration = [ratio * durationtools.Duration(d) for d in duration]
            ns = _make_unprolated_notes(ps, duration,
                decrease_durations_monotonically=decrease_durations_monotonically)
            t = tuplettools.Tuplet(multiplier, ns)
            result.append(t)

    # return result
    result = selectiontools.Selection(result)
    return result
Exemple #11
0
def make_leaves(
    pitches,
    durations,
    decrease_durations_monotonically=True,
    forbidden_written_duration=None,
    is_diminution=True,
    metrical_hiearchy=None,
    use_messiaen_style_ties=False,
    use_multimeasure_rests=False,
    ):
    r'''Makes leaves.

    ..  container:: example

        **Example 1.** Integer and string elements in `pitches` result in
        notes:

        ::

            >>> pitches = [2, 4, 'F#5', 'G#5']
            >>> duration = Duration(1, 4)
            >>> leaves = scoretools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                d'4
                e'4
                fs''4
                gs''4
            }

    ..  container:: example

        **Example 2.** Tuple elements in `pitches` result in chords:

        ::

            >>> pitches = [(0, 2, 4), ('F#5', 'G#5', 'A#5')]
            >>> duration = Duration(1, 2)
            >>> leaves = scoretools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                <c' d' e'>2
                <fs'' gs'' as''>2
            }

    ..  container:: example

        **Example 3.** None-valued elements in `pitches` result in rests:

        ::

            >>> pitches = 4 * [None]
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> staff.context_name = 'RhythmicStaff'
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new RhythmicStaff {
                r4
                r4
                r4
                r4
            }

    ..  container:: example

        **Example 4.** You can mix and match values passed to `pitches`:

        ::

            >>> pitches = [(0, 2, 4), None, 'C#5', 'D#5']
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                <c' d' e'>4
                r4
                cs''4
                ds''4
            }

    ..  container:: example

        **Example 5.** Read `pitches` cyclically when the length of `pitches`
        is less than the length of `durations`:

        ::

            >>> pitches = ['C5']
            >>> durations = 2 * [Duration(3, 8), Duration(1, 8)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                c''4.
                c''8
                c''4.
                c''8
            }

    ..  container:: example

        **Example 6.** Read `durations` cyclically when the length of
        `durations` is less than the length of `pitches`:

        ::

            >>> pitches = "c'' d'' e'' f''"
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                c''4
                d''4
                e''4
                f''4
            }

    ..  container:: example

        **Example 7.** Elements in `durations` with non-power-of-two
        denominators result in tuplet-nested leaves:

        ::

            >>> pitches = ['D5']
            >>> durations = [Duration(1, 3), Duration(1, 3), Duration(1, 3)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \times 2/3 {
                    d''2
                    d''2
                    d''2
                }
            }

    ..  container:: example

        **Example 8.** Set `decrease_durations_monotonically` to true to
        return nonassignable durations tied from greatest to least:

        ::

            >>> pitches = ['D#5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((13, 16))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 13/16
                ds''2. ~
                ds''16
            }

    ..  container:: example

        **Example 9.** Set `decrease_durations_monotonically` to false
        to return nonassignable durations tied from least to greatest:

        ::

            >>> pitches = ['E5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((13, 16))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 13/16
                e''16 ~
                e''2.
            }

    ..  container:: example

        **Example 10.** Set `forbidden_written_duration` to avoid notes
        greater than or equal to a certain written duration:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 4))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/4
                f'4 ~
                f'4 ~
                f'8
                g'4 ~
                g'4 ~
                g'8
            }

    ..  container:: example

        **Example 11.** You may set `forbidden_written_duration` and
        `decrease_durations_monotonically` together:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 4))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/4
                f'8 ~
                f'4 ~
                f'4
                g'8 ~
                g'4 ~
                g'4
            }

    ..  container:: example

        **Example 12.** Set `is_diminution` to true to produce
        diminished tuplets:

        ::

            >>> pitches = "f'"
            >>> durations = [Duration(5, 14)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     is_diminution=True
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 14))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/14
                \tweak edge-height #'(0.7 . 0)
                \times 4/7 {
                    f'2 ~
                    f'8
                }
            }

        This is default behavior.

    ..  container:: example

        **Example 13.** Set `is_diminution` to false to produce
        agumented tuplets:

        ::

            >>> pitches = "f'"
            >>> durations = [Duration(5, 14)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     is_diminution=False
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 14))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/14
                \tweak text #tuplet-number::calc-fraction-text
                \tweak edge-height #'(0.7 . 0)
                \times 8/7 {
                    f'4 ~
                    f'16
                }
            }

    ..  container:: example

        **Example 14.** None-valued elements in `pitches` result in
        multimeasure rests when the multimeasure rest keyword is set:

        ::

            >>> pitches = [None]
            >>> durations = [Duration(3, 8), Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     use_multimeasure_rests=True,
            ...     )
            >>> leaves
            Selection(MultimeasureRest('R1 * 3/8'), MultimeasureRest('R1 * 5/8'))

        ::

            >>> staff = Staff([
            ...     Measure((3, 8), [leaves[0]]),
            ...     Measure((5, 8), [leaves[1]]),
            ...     ])
            >>> staff.context_name = 'RhythmicStaff'
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new RhythmicStaff {
                {
                    \time 3/8
                    R1 * 3/8
                }
                {
                    \time 5/8
                    R1 * 5/8
                }
            }

    ..  container:: example

        **Example 15.** Uses Messiaen-style ties:

        ::

            >>> pitches = [0]
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     use_messiaen_style_ties=True,
            ...     )
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                c'2.
                c'16 \repeatTie
            }

    ..  container:: example

        **Example 16.** Works with numbered pitch-class:

        ::

            >>> pitches = [pitchtools.NumberedPitchClass(6)]
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     )
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                fs'2. ~
                fs'16
            }

    Returns selection of leaves.
    '''
    from abjad.tools import scoretools
    if isinstance(pitches, str):
        pitches = pitches.split()
    if not isinstance(pitches, list):
        pitches = [pitches]
    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]
    nonreduced_fractions = [mathtools.NonreducedFraction(_) for _ in durations]
    size = max(len(nonreduced_fractions), len(pitches))
    nonreduced_fractions = sequencetools.repeat_sequence_to_length(
        nonreduced_fractions, 
        size,
        )
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)
    Duration = durationtools.Duration
    duration_groups = \
        Duration._group_nonreduced_fractions_by_implied_prolation(
        nonreduced_fractions)
    result = []
    for duration_group in duration_groups:
        # get factors in denominator of duration group other than 1, 2.
        factors = set(mathtools.factors(duration_group[0].denominator))
        factors.discard(1)
        factors.discard(2)
        current_pitches = pitches[0:len(duration_group)]
        pitches = pitches[len(duration_group):]
        if len(factors) == 0:
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=decrease_durations_monotonically,
                    forbidden_written_duration=forbidden_written_duration,
                    use_multimeasure_rests=use_multimeasure_rests,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                    )
                result.extend(leaves)
        else:
            # compute tuplet prolation
            denominator = duration_group[0].denominator
            numerator = mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / durationtools.Duration(*multiplier)
            duration_group = [ratio * durationtools.Duration(duration)
                for duration in duration_group]
            # make tuplet leaves
            tuplet_leaves = []
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=\
                        decrease_durations_monotonically,
                    use_multimeasure_rests=use_multimeasure_rests,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                    )
                tuplet_leaves.extend(leaves)
            tuplet = scoretools.Tuplet(multiplier, tuplet_leaves)
            if is_diminution and not tuplet.is_diminution:
                tuplet.toggle_prolation()
            elif not is_diminution and tuplet.is_diminution:
                tuplet.toggle_prolation()
            result.append(tuplet)
    result = selectiontools.Selection(result)
    return result
Exemple #12
0
def make_leaves(
    pitches,
    durations,
    decrease_durations_monotonically=True,
    forbidden_written_duration=None,
    is_diminution=True,
    metrical_hiearchy=None,
    use_messiaen_style_ties=False,
    use_multimeasure_rests=False,
):
    r'''Makes leaves.

    ..  container:: example

        **Example 1.** Integer and string elements in `pitches` result in
        notes:

        ::

            >>> pitches = [2, 4, 'F#5', 'G#5']
            >>> duration = Duration(1, 4)
            >>> leaves = scoretools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                d'4
                e'4
                fs''4
                gs''4
            }

    ..  container:: example

        **Example 2.** Tuple elements in `pitches` result in chords:

        ::

            >>> pitches = [(0, 2, 4), ('F#5', 'G#5', 'A#5')]
            >>> duration = Duration(1, 2)
            >>> leaves = scoretools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                <c' d' e'>2
                <fs'' gs'' as''>2
            }

    ..  container:: example

        **Example 3.** None-valued elements in `pitches` result in rests:

        ::

            >>> pitches = 4 * [None]
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> staff.context_name = 'RhythmicStaff'
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new RhythmicStaff {
                r4
                r4
                r4
                r4
            }

    ..  container:: example

        **Example 4.** You can mix and match values passed to `pitches`:

        ::

            >>> pitches = [(0, 2, 4), None, 'C#5', 'D#5']
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                <c' d' e'>4
                r4
                cs''4
                ds''4
            }

    ..  container:: example

        **Example 5.** Read `pitches` cyclically when the length of `pitches`
        is less than the length of `durations`:

        ::

            >>> pitches = ['C5']
            >>> durations = 2 * [Duration(3, 8), Duration(1, 8)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c''4.
                c''8
                c''4.
                c''8
            }

    ..  container:: example

        **Example 6.** Read `durations` cyclically when the length of
        `durations` is less than the length of `pitches`:

        ::

            >>> pitches = "c'' d'' e'' f''"
            >>> durations = [Duration(1, 4)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c''4
                d''4
                e''4
                f''4
            }

    ..  container:: example

        **Example 7.** Elements in `durations` with non-power-of-two
        denominators result in tuplet-nested leaves:

        ::

            >>> pitches = ['D5']
            >>> durations = [Duration(1, 3), Duration(1, 3), Duration(1, 3)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \times 2/3 {
                    d''2
                    d''2
                    d''2
                }
            }

    ..  container:: example

        **Example 8.** Set `decrease_durations_monotonically` to true to
        return nonassignable durations tied from greatest to least:

        ::

            >>> pitches = ['D#5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((13, 16))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 13/16
                ds''2. ~
                ds''16
            }

    ..  container:: example

        **Example 9.** Set `decrease_durations_monotonically` to false
        to return nonassignable durations tied from least to greatest:

        ::

            >>> pitches = ['E5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((13, 16))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 13/16
                e''16 ~
                e''2.
            }

    ..  container:: example

        **Example 10.** Set `forbidden_written_duration` to avoid notes
        greater than or equal to a certain written duration:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 4))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 5/4
                f'4 ~
                f'4 ~
                f'8
                g'4 ~
                g'4 ~
                g'8
            }

    ..  container:: example

        **Example 11.** You may set `forbidden_written_duration` and
        `decrease_durations_monotonically` together:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 4))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 5/4
                f'8 ~
                f'4 ~
                f'4
                g'8 ~
                g'4 ~
                g'4
            }

    ..  container:: example

        **Example 12.** Set `is_diminution` to true to produce
        diminished tuplets:

        ::

            >>> pitches = "f'"
            >>> durations = [Duration(5, 14)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     is_diminution=True
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 14))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 5/14
                \tweak #'edge-height #'(0.7 . 0)
                \times 4/7 {
                    f'2 ~
                    f'8
                }
            }

        This is default behavior.

    ..  container:: example

        **Example 13.** Set `is_diminution` to false to produce
        agumented tuplets:

        ::

            >>> pitches = "f'"
            >>> durations = [Duration(5, 14)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     is_diminution=False
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = TimeSignature((5, 14))
            >>> attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                \time 5/14
                \tweak #'text #tuplet-number::calc-fraction-text
                \tweak #'edge-height #'(0.7 . 0)
                \times 8/7 {
                    f'4 ~
                    f'16
                }
            }

    ..  container:: example

        **Example 14.** None-valued elements in `pitches` result in
        multimeasure rests when the multimeasure rest keyword is set:

        ::

            >>> pitches = [None]
            >>> durations = [Duration(3, 8), Duration(5, 8)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     use_multimeasure_rests=True,
            ...     )
            >>> leaves
            Selection(MultimeasureRest('R1 * 3/8'), MultimeasureRest('R1 * 5/8'))

        ::

            >>> staff = Staff([
            ...     Measure((3, 8), [leaves[0]]),
            ...     Measure((5, 8), [leaves[1]]),
            ...     ])
            >>> staff.context_name = 'RhythmicStaff'
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new RhythmicStaff {
                {
                    \time 3/8
                    R1 * 3/8
                }
                {
                    \time 5/8
                    R1 * 5/8
                }
            }

    ..  container:: example

        **Example 15.** Uses Messiaen-style ties:

        ::

            >>> pitches = [0]
            >>> durations = [Duration(13, 16)]
            >>> leaves = scoretools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     use_messiaen_style_ties=True,
            ...     )
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> print(format(staff))
            \new Staff {
                c'2.
                c'16 \repeatTie
            }

    Returns selection of leaves.
    '''
    from abjad.tools import scoretools

    if isinstance(pitches, str):
        pitches = pitches.split()

    if not isinstance(pitches, list):
        pitches = [pitches]

    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]

    nonreduced_fractions = [mathtools.NonreducedFraction(_) for _ in durations]
    size = max(len(nonreduced_fractions), len(pitches))
    nonreduced_fractions = sequencetools.repeat_sequence_to_length(
        nonreduced_fractions,
        size,
    )
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)
    Duration = durationtools.Duration
    duration_groups = \
        Duration._group_nonreduced_fractions_by_implied_prolation(
        nonreduced_fractions)

    result = []
    for duration_group in duration_groups:
        # get factors in denominator of duration group other than 1, 2.
        factors = set(mathtools.factors(duration_group[0].denominator))
        factors.discard(1)
        factors.discard(2)
        current_pitches = pitches[0:len(duration_group)]
        pitches = pitches[len(duration_group):]
        if len(factors) == 0:
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=
                    decrease_durations_monotonically,
                    forbidden_written_duration=forbidden_written_duration,
                    use_multimeasure_rests=use_multimeasure_rests,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                )
                result.extend(leaves)
        else:
            # compute tuplet prolation
            denominator = duration_group[0].denominator
            numerator = mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / durationtools.Duration(*multiplier)
            duration_group = [
                ratio * durationtools.Duration(duration)
                for duration in duration_group
            ]
            # make tuplet leaves
            tuplet_leaves = []
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=\
                        decrease_durations_monotonically,
                    use_multimeasure_rests=use_multimeasure_rests,
                    use_messiaen_style_ties=use_messiaen_style_ties,
                    )
                tuplet_leaves.extend(leaves)
            tuplet = scoretools.Tuplet(multiplier, tuplet_leaves)
            if is_diminution and not tuplet.is_diminution:
                tuplet.toggle_prolation()
            elif not is_diminution and tuplet.is_diminution:
                tuplet.toggle_prolation()
            result.append(tuplet)

    result = selectiontools.Selection(result)
    return result
Exemple #13
0
#mask = tools.rout(mask, window)
window_period = len(window)
window_true_indices = sequencetools.true_indices(window)
mask = sequencetools.negate_elements(
    mask, 
    absolute=True, 
    window_true_indices=indices, 
    window_period=period,
    )

mask = sequencetools.split_sequence(mask, measures, cyclic=True, overhang=True)
mask = sequencetools.flatten_sequence(mask)
mask = sequencetools.split_sequence(mask, [count], cyclic=True, overhang=True)
mask = sequencetools.flatten_sequence(mask)

window = sequencetools.repeat_sequence_to_length(window, count)
#mask = tools.rout(mask, window)
window_period = len(window)
window_true_indices = sequencetools.true_indices(window)
mask = sequencetools.negate_elements(
    mask, 
    absolute=True,
    indices=window_true_indices, 
    period=window_period,
    )

mask = sequencetools.split_sequence(mask, measures, cyclic=True, overhang=True)
mask = [transforms.lengths_to_joined_positive_counts(row) for row in mask]
mask = sequencetools.rotate(mask, -int(len(reflections) / 2))

print('reflect ...')
Exemple #14
0
def make_leaves(
    pitches,
    durations,
    decrease_durations_monotonically=True,
    tie_rests=False,
    forbidden_written_duration=None,
    metrical_hiearchy=None,
    ):
    r'''Make leaves.

    ..  container:: example
    
        **Example 1.** Integer and string elements in `pitches` result in 
        notes:

        ::

            >>> pitches = [2, 4, 'F#5', 'G#5']
            >>> duration = Duration(1, 4)
            >>> leaves = leaftools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                d'4
                e'4
                fs''4
                gs''4
            }

    ..  container:: example
    
        **Example 2.** Tuple elements in `pitches` result in chords:

        ::

            >>> pitches = [(0, 2, 4), ('F#5', 'G#5', 'A#5')]
            >>> duration = Duration(1, 2)
            >>> leaves = leaftools.make_leaves(pitches, duration)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                <c' d' e'>2
                <fs'' gs'' as''>2
            }

    ..  container:: example
        
        **Example 3.** None-valued elements in `pitches` result in rests:

        ::

            >>> pitches = 4 * [None]
            >>> durations = [Duration(1, 4)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = stafftools.RhythmicStaff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new RhythmicStaff {
                r4
                r4
                r4
                r4
            }

    ..  container:: example
        
        **Example 4.** You can mix and match values passed to `pitches`:

        ::

            >>> pitches = [(0, 2, 4), None, 'C#5', 'D#5']
            >>> durations = [Duration(1, 4)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                <c' d' e'>4
                r4
                cs''4
                ds''4
            }

    ..  container:: example
            
        **Example 5.** Read `pitches` cyclically when the length of `pitches`
        is less than the length of `durations`:

        ::

            >>> pitches = ['C5']
            >>> durations = 2 * [Duration(3, 8), Duration(1, 8)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                c''4.
                c''8
                c''4.
                c''8
            }

    ..  container:: example
            
        **Example 6.** Read `durations` cyclically when the length of 
        `durations` is less than the length of `pitches`:

        ::

            >>> pitches = "c'' d'' e'' f''"
            >>> durations = [Duration(1, 4)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                c''4
                d''4
                e''4
                f''4
            }

    ..  container:: example
            
        **Example 7.** Elements in `durations` with non-power-of-two 
        denominators result in tuplet-nested leaves: 
        
        ::

            >>> pitches = ['D5']
            >>> durations = [Duration(1, 3), Duration(1, 3), Duration(1, 3)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \times 2/3 {
                    d''2
                    d''2
                    d''2
                }
            }

    ..  container:: example
            
        **Example 8.** Set `decrease_durations_monotonically` to true to 
        return nonassignable durations tied from greatest to least:

        ::

            >>> pitches = ['D#5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = leaftools.make_leaves(pitches, durations)
            >>> staff = Staff(leaves)
            >>> time_signature = contexttools.TimeSignatureMark((13, 16))
            >>> time_signature = attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 13/16
                ds''2. ~
                ds''16
            }

    ..  container:: example
            
        **Example 9.** Set `decrease_durations_monotonically` to false 
        to return nonassignable durations tied from least to greatest:

        ::

            >>> pitches = ['E5']
            >>> durations = [Duration(13, 16)]
            >>> leaves = leaftools.make_leaves(
            ...     pitches, 
            ...     durations,
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = contexttools.TimeSignatureMark((13, 16))
            >>> time_signature = attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 13/16
                e''16 ~
                e''2.
            }

    ..  container:: example
        
        **Example 10.** Set `tie_rests` to true to return tied rests for 
        nonassignable durations. Note that LilyPond does not engrave 
        ties between rests:

        ::

            >>> pitches = [None]
            >>> durations = [Duration(5, 8)]
            >>> leaves = leaftools.make_leaves(
            ...     pitches, 
            ...     durations, 
            ...     tie_rests=True,
            ...     )
            >>> staff = stafftools.RhythmicStaff(leaves)
            >>> time_signature = contexttools.TimeSignatureMark((5, 8))
            >>> time_signature = attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new RhythmicStaff {
                \time 5/8
                r2 ~
                r8
            }

    ..  container:: example
    
        **Example 11.** Set `forbidden_written_duration` to avoid notes 
        greater than or equal to a certain written duration:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = leaftools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = contexttools.TimeSignatureMark((5, 4))
            >>> time_signature = attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/4
                f'4 ~
                f'4 ~
                f'8
                g'4 ~
                g'4 ~
                g'8
            }

    ..  container:: example
        
        **Example 12.** You may set `forbidden_written_duration` and
        `decrease_durations_monotonically` together:

        ::

            >>> pitches = "f' g'"
            >>> durations = [Duration(5, 8)]
            >>> leaves = leaftools.make_leaves(
            ...     pitches,
            ...     durations,
            ...     forbidden_written_duration=Duration(1, 2),
            ...     decrease_durations_monotonically=False,
            ...     )
            >>> staff = Staff(leaves)
            >>> time_signature = contexttools.TimeSignatureMark((5, 4))
            >>> time_siganture = attach(time_signature, staff)
            >>> show(staff) # doctest: +SKIP

        ..  doctest::

            >>> f(staff)
            \new Staff {
                \time 5/4
                f'8 ~
                f'4 ~
                f'4
                g'8 ~
                g'4 ~
                g'4
            }

    Returns selection of unincorporated leaves.
    '''
    from abjad.tools import scoretools
    from abjad.tools import notetools
    from abjad.tools import resttools
    from abjad.tools import tuplettools

    if isinstance(pitches, str):
        pitches = pitches.split()

    if not isinstance(pitches, list):
        pitches = [pitches]

    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]

    # make duration pairs
    duration_pairs = [durationtools.Duration(duration) 
        for duration in durations]

    # set lists of pitches and duration pairs to the same length
    size = max(len(duration_pairs), len(pitches))
    duration_pairs = \
        sequencetools.repeat_sequence_to_length(duration_pairs, size)
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)

    Duration = durationtools.Duration
    duration_groups = \
        Duration._group_nonreduced_fractions_by_implied_prolation(
        duration_pairs)

    result = []
    for duration_group in duration_groups:
        # get factors in denominator of duration group other than 1, 2.
        factors = set(mathtools.factors(duration_group[0].denominator))
        factors.discard(1)
        factors.discard(2)
        current_pitches = pitches[0:len(duration_group)]
        pitches = pitches[len(duration_group):]
        if len(factors) == 0:
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=decrease_durations_monotonically,
                    forbidden_written_duration=forbidden_written_duration,
                    tie_rests=tie_rests,
                    )
                result.extend(leaves)
        else:
            # compute tuplet prolation
            denominator = duration_group[0].denominator
            numerator = \
                mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / durationtools.Duration(*multiplier)
            duration_group = [ratio * durationtools.Duration(duration) 
                for duration in duration_group]
            # make tuplet leaves
            tuplet_leaves = []
            for pitch, duration in zip(current_pitches, duration_group):
                leaves = _make_leaf_on_pitch(
                    pitch,
                    duration,
                    decrease_durations_monotonically=decrease_durations_monotonically,
                    )
                tuplet_leaves.extend(leaves)
            tuplet = tuplettools.Tuplet(multiplier, tuplet_leaves)
            result.append(tuplet)

    result = selectiontools.Selection(result)
    return result
Exemple #15
0
def make_notes(pitches, durations, decrease_durations_monotonically=True):
    r'''Make notes according to `pitches` and `durations`.

    Cycle through `pitches` when the length of `pitches` is less than the
    length of `durations`:

    ::

        >>> scoretools.make_notes([0], [(1, 16), (1, 8), (1, 8)])
        Selection(Note("c'16"), Note("c'8"), Note("c'8"))

    Cycle through `durations` when the length of `durations` is less than the
    length of `pitches`:

    ::

        >>> scoretools.make_notes([0, 2, 4, 5, 7], [(1, 16), (1, 8), (1, 8)])
        Selection(Note("c'16"), Note("d'8"), Note("e'8"), Note("f'16"), Note("g'8"))

    Create ad hoc tuplets for nonassignable durations:

    ::

        >>> scoretools.make_notes([0], [(1, 16), (1, 12), (1, 8)])
        Selection(Note("c'16"), Tuplet(Multiplier(2, 3), "c'8"), Note("c'8"))

    Set ``decrease_durations_monotonically=True`` to express tied values
    in decreasing duration:

    ::

        >>> scoretools.make_notes(
        ...     [0],
        ...     [(13, 16)],
        ...     decrease_durations_monotonically=True,
        ...     )
        Selection(Note("c'2."), Note("c'16"))

    Set ``decrease_durations_monotonically=False`` to express tied
    values in increasing duration:

    ::

        >>> scoretools.make_notes(
        ...     [0],
        ...     [(13, 16)],
        ...     decrease_durations_monotonically=False,
        ...     )
        Selection(Note("c'16"), Note("c'2."))

    Set `pitches` to a single pitch or a sequence of pitches.

    Set `durations` to a single duration or a list of durations.

    Returns selection.
    '''
    from abjad.tools import scoretools
    from abjad.tools import selectiontools

    if isinstance(pitches, str):
        pitches = pitches.split()

    if not isinstance(pitches, list):
        pitches = [pitches]

    if isinstance(durations, (numbers.Number, tuple)):
        durations = [durations]

    nonreduced_fractions = [mathtools.NonreducedFraction(_) for _ in durations]
    size = max(len(nonreduced_fractions), len(pitches))
    nonreduced_fractions = sequencetools.repeat_sequence_to_length(
        nonreduced_fractions,
        size,
    )
    pitches = sequencetools.repeat_sequence_to_length(pitches, size)
    Duration = durationtools.Duration
    durations = Duration._group_nonreduced_fractions_by_implied_prolation(
        nonreduced_fractions)

    def _make_unprolated_notes(
        pitches,
        durations,
        decrease_durations_monotonically=decrease_durations_monotonically,
    ):
        assert len(pitches) == len(durations)
        result = []
        for pitch, duration in zip(pitches, durations):
            result.extend(
                scoretools.make_tied_leaf(
                    scoretools.Note,
                    duration,
                    pitches=pitch,
                    decrease_durations_monotonically=
                    decrease_durations_monotonically,
                ))
        return result

    result = []
    for duration in durations:
        # get factors in denominator of duration group duration other than 1, 2.
        factors = set(mathtools.factors(duration[0].denominator))
        factors.discard(1)
        factors.discard(2)
        ps = pitches[0:len(duration)]
        pitches = pitches[len(duration):]
        if len(factors) == 0:
            result.extend(
                _make_unprolated_notes(ps,
                                       duration,
                                       decrease_durations_monotonically=
                                       decrease_durations_monotonically))
        else:
            # compute prolation
            denominator = duration[0].denominator
            numerator = mathtools.greatest_power_of_two_less_equal(denominator)
            multiplier = (numerator, denominator)
            ratio = 1 / fractions.Fraction(*multiplier)
            duration = [ratio * durationtools.Duration(d) for d in duration]
            ns = _make_unprolated_notes(ps,
                                        duration,
                                        decrease_durations_monotonically=
                                        decrease_durations_monotonically)
            t = scoretools.Tuplet(multiplier, ns)
            result.append(t)

    # return result
    result = selectiontools.Selection(result)
    return result