Пример #1
0
def mask_none():
    r'''Makes boolean pattern equal to all ones.

    ..  container:: example

        **Example 1.** Makes mask:

        ::

            >>> mask = rhythmmakertools.mask_none()

        ::

            >>> print(format(mask))
            rhythmmakertools.BooleanPattern(
                indices=(),
                period=1,
                )

    ..  container:: example

        **Example 2.** Makes note rhythm-maker. Effectively applies no mask:

        ::

            >>> mask = rhythmmakertools.mask_none()
            >>> maker = rhythmmakertools.NoteRhythmMaker(
            ...     output_masks=[mask],
            ...     )

        ::

            >>> print(format(maker))
            rhythmmakertools.NoteRhythmMaker(
                output_masks=(
                    rhythmmakertools.BooleanPattern(
                        indices=(),
                        period=1,
                        ),
                    ),
                )

    Returns boolean pattern.
    '''
    from abjad.tools import rhythmmakertools

    return rhythmmakertools.BooleanPattern(
        indices=[],
        period=1,
    )
Пример #2
0
def select(indices=[]):
    r'''Makes acyclic boolean pattern equal to `indices`.

    ..  container:: example

        **Example 1.** Selects index 2:

        ::

            >>> pattern = rhythmmakertools.select([2])

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(2,),
                )

    ..  container:: example

        **Example 2.** Selects indices 2, 3 and 5:

        ::

            >>> pattern = rhythmmakertools.select([2, 3, 5])

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(2, 3, 5),
                )

    Returns boolean pattern.
    '''
    from abjad.tools import rhythmmakertools

    assert all(isinstance(_, int) for _ in indices), repr(indices)

    return rhythmmakertools.BooleanPattern(indices=indices, )
Пример #3
0
    def __call__(self, expr):
        r'''Calls rotation on `expr`.

        ..  container:: example

            **Example 1.** Duplicates once without period.

            ::

                >>> operator_ = sequencetools.Duplication(counts=1)
                >>> numbers = [1, 2, 3, 4]
                >>> operator_(numbers)
                [1, 2, 3, 4, 1, 2, 3, 4]

        ..  container:: example

            **Example 2.** Duplicates twice without period.

            ::

                >>> operator_ = sequencetools.Duplication(counts=2)
                >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7])
                >>> operator_(pitch_classes)
                PitchClassSegment([0, 1, 4, 7, 0, 1, 4, 7, 0, 1, 4, 7])

        ..  container:: example

            **Example 3.** Duplicates periodically.

            ::

                >>> operator_ = sequencetools.Duplication(counts=1, period=3)
                >>> pitches = pitchtools.PitchSegment("c' d' e' f' g' a' b' c''")
                >>> for pitch in operator_(pitches):
                ...     pitch
                ...
                NamedPitch("c'")
                NamedPitch("d'")
                NamedPitch("e'")
                NamedPitch("c'")
                NamedPitch("d'")
                NamedPitch("e'")
                NamedPitch("f'")
                NamedPitch("g'")
                NamedPitch("a'")
                NamedPitch("f'")
                NamedPitch("g'")
                NamedPitch("a'")
                NamedPitch("b'")
                NamedPitch("c''")
                NamedPitch("b'")
                NamedPitch("c''")

        ..  container:: example

            **Example 4.** Duplicate indices.

            ::

                >>> operator_ = sequencetools.Duplication(
                ...     counts=1,
                ...     indices=(0, -1),
                ...     )
                >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7])
                >>> operator_(pitch_classes)
                PitchClassSegment([0, 0, 1, 4, 7, 7])

        ..  container:: example

            **Example 5.** Duplicate indices periodically.

            ::

                >>> operator_ = sequencetools.Duplication(
                ...     counts=1,
                ...     indices=(0,),
                ...     period=2,
                ...     )
                >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7, 9])
                >>> operator_(pitch_classes)
                PitchClassSegment([0, 0, 1, 4, 4, 7, 9, 9])

        ..  container:: example

            **Example 6.** Duplicate indices periodically with different
            counts.

            ::

                >>> operator_ = sequencetools.Duplication(
                ...     counts=(1, 2),
                ...     indices=(0,),
                ...     period=2,
                ...     )
                >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7, 9])
                >>> operator_(pitch_classes)
                PitchClassSegment([0, 0, 1, 4, 4, 4, 7, 9, 9])

        ..  container:: example

            **Example 7.** Cyclic counts.

            ::

                >>> operator_ = sequencetools.Duplication(counts=(0, 1, 2, 3))
                >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7, 9])
                >>> operator_(pitch_classes)
                PitchClassSegment([0, 1, 1, 4, 4, 4, 7, 7, 7, 7, 9])
                
        Returns new object with type equal to that of `expr`.
        '''
        from abjad.tools import datastructuretools
        from abjad.tools import rhythmmakertools
        from abjad.tools import sequencetools

        if not isinstance(expr, collections.Sequence):
            expr = (expr, )

        counts = self.counts
        if isinstance(counts, int):
            counts = counts + 1
        else:
            counts = [_ + 1 for _ in counts]

        if not self.period and not self.indices:
            if isinstance(counts, int):
                return type(expr)(expr * counts)
            else:
                counts = datastructuretools.CyclicTuple(counts)
                result = []
                for i, x in enumerate(expr):
                    count = counts[i]
                    result.extend([x] * count)
                if isinstance(expr, datastructuretools.TypedCollection):
                    result = new(expr, items=result)
                else:
                    result = type(expr)(result)
                return result

        if isinstance(counts, int):
            counts = [counts]
        counts = datastructuretools.CyclicTuple(counts)

        if not self.indices:
            if isinstance(expr, datastructuretools.TypedCollection):
                result = new(expr, items=())
            else:
                result = type(expr)()
            iterator = sequencetools.partition_sequence_by_counts(
                expr, [self.period], cyclic=True, overhang=True)
            for i, shard in enumerate(iterator):
                shard = type(expr)(shard) * counts[i]
                result = result + shard
            return result

        pattern = rhythmmakertools.BooleanPattern(
            indices=self.indices,
            period=self.period,
        )
        result = []
        length = len(expr)
        j = 0
        for i, x in enumerate(expr):
            if pattern.matches_index(i, length):
                count = counts[j]
                result.extend([x] * count)
                j += 1
            else:
                result.append(x)
        if isinstance(expr, datastructuretools.TypedCollection):
            result = new(expr, items=result)
        else:
            result = type(expr)(result)
        return result
Пример #4
0
def select_first(n=1):
    r'''Makes acyclic boolean pattern equal to first `n` indices.

    ..  container:: example

        **Example 1.** Selects first division for tie creation:

        ::

            >>> pattern = rhythmmakertools.select_first()

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(0,),
                )

        ::

            >>> maker = rhythmmakertools.NoteRhythmMaker(
            ...     tie_specifier=rhythmmakertools.TieSpecifier(
            ...         tie_across_divisions=pattern,
            ...         use_messiaen_style_ties=True,
            ...         ),
            ...     )
            >>> divisions = [(7, 16), (3, 8), (7, 16), (3, 8)]
            >>> music = maker(divisions)
            >>> lilypond_file = rhythmmakertools.make_lilypond_file(
            ...     music,
            ...     divisions,
            ...     )
            >>> show(lilypond_file) # doctest: +SKIP

        ..  doctest::

            >>> staff = maker._get_rhythmic_staff(lilypond_file)
            >>> f(staff)
            \new RhythmicStaff {
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4. \repeatTie
                }
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4.
                }
            }
            
    ..  container:: example

        **Example 2.** Selects first two divisions for tie creation:

        ::

            >>> pattern = rhythmmakertools.select_first(n=2)

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(0, 1),
                )

        ::

            >>> maker = rhythmmakertools.NoteRhythmMaker(
            ...     tie_specifier=rhythmmakertools.TieSpecifier(
            ...         tie_across_divisions=pattern,
            ...         use_messiaen_style_ties=True,
            ...         ),
            ...     )
            >>> divisions = [(7, 16), (3, 8), (7, 16), (3, 8)]
            >>> music = maker(divisions)
            >>> lilypond_file = rhythmmakertools.make_lilypond_file(
            ...     music,
            ...     divisions,
            ...     )
            >>> show(lilypond_file) # doctest: +SKIP

        ..  doctest::

            >>> staff = maker._get_rhythmic_staff(lilypond_file)
            >>> f(staff)
            \new RhythmicStaff {
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4. \repeatTie
                }
                {
                    \time 7/16
                    c'4.. \repeatTie
                }
                {
                    \time 3/8
                    c'4.
                }
            }

    ..  container:: example

        **Example 3.** Selects no divisions for tie creation:

        ::

            >>> pattern = rhythmmakertools.select_first(n=0)

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(),
                )

        ::

            >>> maker = rhythmmakertools.NoteRhythmMaker(
            ...     tie_specifier=rhythmmakertools.TieSpecifier(
            ...         tie_across_divisions=pattern,
            ...         use_messiaen_style_ties=True,
            ...         ),
            ...     )
            >>> divisions = [(7, 16), (3, 8), (7, 16), (3, 8)]
            >>> music = maker(divisions)
            >>> lilypond_file = rhythmmakertools.make_lilypond_file(
            ...     music,
            ...     divisions,
            ...     )
            >>> show(lilypond_file) # doctest: +SKIP

        ..  doctest::

            >>> staff = maker._get_rhythmic_staff(lilypond_file)
            >>> f(staff)
            \new RhythmicStaff {
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4.
                }
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4.
                }
            }

    Returns boolean pattern.
    '''
    from abjad.tools import rhythmmakertools

    assert 0 <= n, repr(n)
    indices = list(range(n))

    return rhythmmakertools.BooleanPattern(indices=indices, )
Пример #5
0
def select_all():
    r'''Makes cyclic boolean pattern equal to all indices.

    ..  container:: example

        **Example 1.** Selects all divisions for tie creation:

        ::

            >>> pattern = rhythmmakertools.select_all()

        ::

            >>> print(format(pattern))
            rhythmmakertools.BooleanPattern(
                indices=(0,),
                period=1,
                )

        ::

            >>> maker = rhythmmakertools.NoteRhythmMaker(
            ...     tie_specifier=rhythmmakertools.TieSpecifier(
            ...         tie_across_divisions=pattern,
            ...         use_messiaen_style_ties=True,
            ...         ),
            ...     )
            >>> divisions = [(7, 16), (3, 8), (7, 16), (3, 8)]
            >>> music = maker(divisions)
            >>> lilypond_file = rhythmmakertools.make_lilypond_file(
            ...     music,
            ...     divisions,
            ...     )
            >>> show(lilypond_file) # doctest: +SKIP

        ..  doctest::

            >>> staff = maker._get_rhythmic_staff(lilypond_file)
            >>> f(staff)
            \new RhythmicStaff {
                {
                    \time 7/16
                    c'4..
                }
                {
                    \time 3/8
                    c'4. \repeatTie
                }
                {
                    \time 7/16
                    c'4.. \repeatTie
                }
                {
                    \time 3/8
                    c'4. \repeatTie
                }
            }

    Returns boolean pattern.
    '''
    from abjad.tools import rhythmmakertools

    return rhythmmakertools.BooleanPattern(
        indices=[0],
        period=1,
    )
Пример #6
0
 def coerce_(expr):
     if not isinstance(expr, rhythmmakertools.BooleanPattern):
         expr = rhythmmakertools.BooleanPattern(*expr)
     return expr