def _lilypond_format(self): result = [] if self._do_not_format: return result instrument_name_markup = self.instrument_name_markup if instrument_name_markup.direction is not None: instrument_name_markup = new( instrument_name_markup, direction=None, ) line = r'\set {!s}.instrumentName = {!s}' line = line.format( self._scope_name, instrument_name_markup, ) result.append(line) line = r'\set {!s}.shortInstrumentName = {!s}' short_instrument_name_markup = self.short_instrument_name_markup if short_instrument_name_markup.direction is not None: short_instrument_name_markup = new( short_instrument_name_markup, direction=None, ) line = line.format( self._scope_name, short_instrument_name_markup, ) result.append(line) return result
def allocate(self, desired_block_size=1): desired_block_size = int(desired_block_size) assert 0 < desired_block_size block_id = None with self._lock: free_block = None for block in self._free_heap: if desired_block_size <= block.duration: free_block = block break if free_block is not None: split_offset = free_block.start_offset + desired_block_size self._free_heap.remove(free_block) if desired_block_size < free_block.duration: new_free_block = new( free_block, start_offset=split_offset, used=False, ) self._free_heap.insert(new_free_block) used_block = new( free_block, stop_offset=split_offset, used=True, ) else: used_block = new( free_block, used=True, ) self._used_heap.insert(used_block) block_id = used_block.start_offset return block_id
def __add__(self, expr): r'''Adds typed tuple to `expr`. Returns new typed tuple. ''' if isinstance(expr, type(self)): items = expr._collection return new(self, items=self._collection[:] + items) elif isinstance(expr, type(self._collection)): items = expr[:] return new(self, items=self._collection[:] + items) raise NotImplementedError
def __or__(self, set_expression): r'''Logical OR of timespan-delimited single-context set expression and `set_expression`. Raises exception when timespan-delimited single-context set expression can not fuse with `set_expression`. Returns timespan inventory. ''' assert self._can_fuse(set_expression) stop_offset = self.target_timespan.stop_offset + \ set_expression.target_timespan.duration target_timespan = new(self.target_timespan, stop_offset=stop_offset) result = new(self, target_timespan=target_timespan) return timespantools.TimespanInventory([result])
def with_color(self, color): r"""LilyPond ``\with-color`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.with_color('blue') :: >>> print(format(markup)) \markup { \with-color #blue "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) color = schemetools.Scheme(color) command = markuptools.MarkupCommand("with-color", color, contents) return new(self, contents=command)
def transpose(self, expr): r'''Transposes pitch segment by `expr`. Returns new pitch segment. ''' items = (pitch.transpose(expr) for pitch in self) return new(self, items=items)
def finger(self): r'''LilyPond ``\finger`` markup command. .. container:: example :: >>> markup = Markup(1) >>> markup = markup.finger() :: >>> print(format(markup)) \markup { \finger 1 } :: >>> show(markup) # doctest: +SKIP Returns new markup ''' from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand( 'finger', contents, ) return new(self, contents=command)
def fontsize(self, fontsize): r'''LilyPond ``\fontsize`` markup command. .. container:: example :: >>> markup = Markup('foo') >>> markup = markup.fontsize(-3) :: >>> print(format(markup)) \markup { \fontsize #-3 foo } :: >>> show(markup) # doctest: +SKIP Returns new markup ''' from abjad.tools import markuptools fontsize = float(fontsize) fontsize = mathtools.integer_equivalent_number_to_integer(fontsize) contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand( 'fontsize', fontsize, contents, ) return new(self, contents=command)
def caps(self): r'''LilyPond ``\caps`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.caps() :: >>> print(format(markup)) \markup { \caps "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. ''' from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand( 'caps', contents, ) return new(self, contents=command)
def dynamic(self): r"""LilyPond ``\dynamic`` markup command. .. container:: example :: >>> markup = Markup('sffz') >>> markup = markup.dynamic() :: >>> print(format(markup)) \markup { \dynamic sffz } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("dynamic", contents) return new(self, contents=command)
def invert(self, axis): r'''Inverts pitch set about `axis`. Returns new pitch set. ''' tokens = (pitch.invert(axis) for pitch in self) return new(self, tokens=tokens)
def override(self, new_property): r"""LilyPond ``\override`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.parenthesize() >>> markup = markup.override(('padding', 0.75)) :: >>> f(markup) \markup { \override #'(padding . 0.75) \parenthesize "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) new_property = schemetools.SchemePair(new_property) command = markuptools.MarkupCommand("override", new_property, contents) return new(self, contents=command)
def larger(self): r"""LilyPond ``\larger`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.larger() :: >>> print(format(markup)) \markup { \larger "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("larger", contents) return new(self, contents=command)
def scale(self, factor_pair): r"""LilyPond ``\scale`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.scale((0.75, 0.75)) :: >>> print(format(markup)) \markup { \scale #'(0.75 . 0.75) "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) factor_pair = schemetools.SchemePair(factor_pair) command = markuptools.MarkupCommand("scale", factor_pair, contents) return new(self, contents=command)
def rotate(self, angle): r"""LilyPond ``\rotate`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.rotate(45) :: >>> print(format(markup)) \markup { \rotate #45 "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("rotate", angle, contents) return new(self, contents=command)
def parenthesize(self): r"""LilyPond ``\parenthesie`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.parenthesize() :: >>> f(markup) \markup { \parenthesize "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("parenthesize", contents) return new(self, contents=command)
def reverse(self): r'''Reverses incised rhythm-maker. Returns newly constructed rhythm-maker. ''' from abjad.tools import rhythmmakertools extra_counts_per_division = \ self._reverse_tuple(self.extra_counts_per_division) split_divisions_by_counts = \ self._reverse_tuple(self.split_divisions_by_counts) duration_spelling_specifier = self.duration_spelling_specifier if duration_spelling_specifier is None: duration_spelling_specifier = \ rhythmmakertools.DurationSpellingSpecifier() duration_spelling_specifier = duration_spelling_specifier.reverse() incise_specifier = self.incise_specifier if incise_specifier is not None: incise_specifier = incise_specifier.reverse() maker = new( self, duration_spelling_specifier=duration_spelling_specifier, extra_counts_per_division=extra_counts_per_division, incise_specifier=incise_specifier, split_divisions_by_counts=split_divisions_by_counts, ) return maker
def translate(self, offset_pair): r"""LilyPond ``translate`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.translate((2, 1)) :: >>> print(format(markup)) \markup { \translate #'(2 . 1) "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) offset_pair = schemetools.SchemePair(offset_pair) command = markuptools.MarkupCommand("translate", offset_pair, contents) return new(self, contents=command)
def transpose(self, expr): r'''Transposes all pitch-classes in pitch-class set by `expr`. Returns new pitch-class set. ''' tokens = (pitch_class + expr for pitch_class in self) return new(self, tokens=tokens)
def pad_around(self, padding): r"""LilyPond ``\pad-around`` markup command. .. container:: example :: >>> markup = Markup('Allegro assai') >>> markup = markup.pad_around(10) >>> markup = markup.box() :: >>> print(format(markup)) \markup { \box \pad-around #10 "Allegro assai" } :: >>> show(markup) # doctest: +SKIP Returns new markup. """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("pad-around", padding, contents) return new(self, contents=command)
def reflect(self): r'''Reflect division list about axis. :: >>> divisions = [(3, 16), (4, 16), (3, 16), (4, 16)] >>> division_list = musicexpressiontools.DivisionList( ... divisions, Offset(5), 'Voice 1') :: >>> result = division_list.reflect() :: >>> print(format(result)) musicexpressiontools.DivisionList( [ durationtools.Division(4, 16), durationtools.Division(3, 16), durationtools.Division(4, 16), durationtools.Division(3, 16), ], start_offset=durationtools.Offset(5, 1), voice_name='Voice 1', ) Emit newly constructed division list. ''' return new(self, divisions=reversed(self.divisions))
def rotate(self, rotation): r'''Rotate division list by `rotation`. :: >>> divisions = [(3, 16), (4, 16), (3, 16), (4, 16)] >>> division_list = musicexpressiontools.DivisionList( ... divisions, Offset(5), 'Voice 1') :: >>> result = division_list.rotate(-1) :: >>> print(format(result)) musicexpressiontools.DivisionList( [ durationtools.Division(4, 16), durationtools.Division(3, 16), durationtools.Division(4, 16), durationtools.Division(3, 16), ], start_offset=durationtools.Offset(5, 1), voice_name='Voice 1', ) Emit newly constructed division list. ''' divisions = sequencetools.rotate_sequence(self.divisions, rotation) return new(self, divisions=divisions)
def repeat_to_duration(self, duration): r'''Repeat payload expression to duration. :: >>> result = \ ... payload_expression.repeat_to_duration(Duration(13, 16)) :: >>> print(format(result)) musicexpressiontools.IterablePayloadExpression( payload=( durationtools.Division(4, 16), durationtools.Division(2, 16), durationtools.Division(4, 16), durationtools.Division(2, 16), durationtools.Division(1, 16), ), ) Returns newly constructed payload expression. ''' if not mathtools.all_are_numbers(self.payload): payload = [durationtools.Division(_) for _ in self.payload] else: payload = self.payload payload = sequencetools.repeat_sequence_to_weight(payload, duration) payload = [durationtools.Division(_) for _ in payload] result = new(self, payload=payload) return result
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 __mul__(self, expr): r'''Multiplies typed tuple by `expr`. Returns new typed tuple. ''' items = self._collection * expr return new(self, items=items)
def partition_by_ratio(self, ratio): r'''Partition payload expression by ratio. :: >>> result = payload_expression.partition_by_ratio((1, 1)) :: >>> for element in result: ... print(format(element)) musicexpressiontools.IterablePayloadExpression( payload=( (4, 16), ), ) musicexpressiontools.IterablePayloadExpression( payload=( (2, 16), ), ) Returns list of newly constructed payload expressions. ''' parts = sequencetools.partition_sequence_by_ratio_of_lengths( self.payload, ratio) result = [] for part in parts: part = new(self, payload=part) result.append(part) return result
def alpha(self): r"""Morris alpha transform of pitch-class segment: :: >>> pitch_class_segment = pitchtools.PitchClassSegment( ... tokens=[-2, -1.5, 6, 7, -1.5, 7], ... ) >>> pitch_class_segment.alpha() PitchClassSegment([11, 11.5, 7, 6, 11.5, 6]) Returns new pitch-class segment. """ from abjad.tools import mathtools numbers = [] for pc in self: pc = abs(float(pc)) is_integer = True if not mathtools.is_integer_equivalent_number(pc): is_integer = False fraction_part = pc - int(pc) pc = int(pc) if abs(pc) % 2 == 0: number = (abs(pc) + 1) % 12 else: number = abs(pc) - 1 if not is_integer: number += fraction_part else: number = int(number) numbers.append(number) return new(self, tokens=numbers)
def transpose(self, expr): r'''Transposes all pitches in pitch set by `expr`. Returns new pitch set. ''' items = (pitch.transpose(expr) for pitch in self) return new(self, items=items)
def invert(self, axis): r'''Inverts pitch segment about `axis`. Returns new pitch segment. ''' items = (pitch.invert(axis) for pitch in self) return new(self, items=items)
def circle(self): r"""LilyPond ``\circle`` markup command. .. container:: example :: >>> markup = Markup.fraction(3, 5) >>> markup = markup.circle() >>> markup = markup.override(('circle-padding', 0.45)) :: >>> print(format(markup)) \markup { \override #'(circle-padding . 0.45) \circle \fraction 3 5 } :: >>> show(markup) # doctest: +SKIP Returns new markup """ from abjad.tools import markuptools contents = self._parse_markup_command_argument(self) command = markuptools.MarkupCommand("circle", contents) return new(self, contents=command)
def _repr_specification(self): return new( self._storage_format_specification, is_indented=False, )
def __call__(self, expr): r'''Calls rotation on `expr`. .. container:: example **Example 1.** Rotates pitch classes with transposition. :: >>> operator_ = pitchtools.Rotation(index=1) >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7]) >>> operator_(pitch_classes) PitchClassSegment([0, 5, 6, 9]) .. container:: example **Example 2.** Rotates pitch classes without transposition. :: >>> operator_ = pitchtools.Rotation(index=1, transpose=False) >>> pitch_classes = pitchtools.PitchClassSegment([0, 1, 4, 7]) >>> operator_(pitch_classes) PitchClassSegment([7, 0, 1, 4]) .. container:: example **Example 3.** Does not rotate single pitches or pitch-classes. :: >>> operator_ = pitchtools.Rotation(index=1) >>> pitch_class = pitchtools.NumberedPitchClass(6) >>> operator_(pitch_class) NumberedPitchClass(6) .. container:: example **Example 4.** Periodic rotation without transposition. :: >>> operator_ = pitchtools.Rotation( ... index=1, ... period=3, ... transpose=False, ... ) >>> pitches = pitchtools.PitchSegment("c' d' e' f' g' a' b' c''") >>> operator_(pitches) PitchSegment(["e'", "c'", "d'", "a'", "f'", "g'", "c''", "b'"]) .. container:: example **Example 5.** Periodic rotation with transposition. :: >>> operator_ = pitchtools.Rotation( ... index=1, ... period=3, ... ) >>> pitches = pitchtools.PitchSegment("c' d' e' f' g' a' b' c''") >>> operator_(pitches) PitchSegment(["c'", 'af', 'bf', "f'", "df'", "ef'", "b'", "as'"]) Returns new object with type equal to that of `expr`. ''' from abjad.tools import pitchtools if isinstance(expr, (pitchtools.Pitch, pitchtools.PitchClass)): return expr if not isinstance(expr, ( pitchtools.PitchSegment, pitchtools.PitchClassSegment, )): expr = pitchtools.PitchSegment(expr) if not self.period: return expr.rotate(self.index, transpose=self.transpose) result = new(expr, items=()) for shard in sequencetools.partition_sequence_by_counts( expr, [self.period], cyclic=True, overhang=True, ): shard = type(expr)(shard) shard = shard.rotate(self.index, transpose=self.transpose) result = result + shard return result
def silence(indices=None, inverted=None): r'''Makes silence mask that matches `indices`. .. container:: example **Example 1.** Silences divisions 1 and 2: :: >>> mask = rhythmmakertools.silence([1, 2]) :: >>> print(format(mask)) rhythmmakertools.SilenceMask( pattern=patterntools.Pattern( indices=(1, 2), ), ) :: >>> maker = rhythmmakertools.NoteRhythmMaker( ... division_masks=[mask], ... ) >>> 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) >>> print(format(staff)) \new RhythmicStaff { { \time 7/16 c'4.. } { \time 3/8 r4. } { \time 7/16 r4.. } { \time 3/8 c'4. } } .. container:: example **Example 2.** Silences divisions -1 and -2: :: >>> mask = rhythmmakertools.silence([-1, -2]) :: >>> print(format(mask)) rhythmmakertools.SilenceMask( pattern=patterntools.Pattern( indices=(-1, -2), ), ) :: >>> maker = rhythmmakertools.NoteRhythmMaker( ... division_masks=[ ... mask, ... ], ... ) >>> 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) >>> print(format(staff)) \new RhythmicStaff { { \time 7/16 c'4.. } { \time 3/8 c'4. } { \time 7/16 r4.. } { \time 3/8 r4. } } .. container:: example **Example 3.** Works with pattern input: :: >>> pattern_1 = patterntools.select_all() >>> pattern_2 = patterntools.select_first() >>> pattern_3 = patterntools.select_last() >>> pattern = pattern_1 ^ pattern_2 ^ pattern_3 >>> mask = rhythmmakertools.silence(pattern) :: >>> print(format(mask)) rhythmmakertools.SilenceMask( pattern=patterntools.CompoundPattern( ( patterntools.Pattern( indices=(0,), period=1, ), patterntools.Pattern( indices=(0,), ), patterntools.Pattern( indices=(-1,), ), ), operator='xor', ), ) :: >>> maker = rhythmmakertools.NoteRhythmMaker( ... division_masks=[ ... mask, ... ], ... ) >>> 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) >>> print(format(staff)) \new RhythmicStaff { { \time 7/16 c'4.. } { \time 3/8 r4. } { \time 7/16 r4.. } { \time 3/8 c'4. } } .. container:: example **Example 4.** Works with pattern input and inverted flag: :: >>> pattern_1 = patterntools.select_all() >>> pattern_2 = patterntools.select_first() >>> pattern_3 = patterntools.select_last() >>> pattern = pattern_1 ^ pattern_2 ^ pattern_3 >>> mask = rhythmmakertools.silence(pattern, inverted=True) :: >>> print(format(mask)) rhythmmakertools.SilenceMask( pattern=patterntools.CompoundPattern( ( patterntools.Pattern( indices=(0,), period=1, ), patterntools.Pattern( indices=(0,), ), patterntools.Pattern( indices=(-1,), ), ), inverted=True, operator='xor', ), ) :: >>> maker = rhythmmakertools.NoteRhythmMaker( ... division_masks=[ ... mask, ... ], ... ) >>> 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) >>> print(format(staff)) \new RhythmicStaff { { \time 7/16 r4.. } { \time 3/8 c'4. } { \time 7/16 c'4.. } { \time 3/8 r4. } } Returns silence mask. ''' from abjad.tools import rhythmmakertools indices = indices or [] prototype = (patterntools.Pattern, patterntools.CompoundPattern) if isinstance(indices, prototype): pattern = indices else: pattern = patterntools.Pattern( indices=indices, inverted=inverted, ) pattern = new(pattern, inverted=inverted) return rhythmmakertools.SilenceMask(pattern=pattern)
def __getslice__(self, start, stop): r'''Gets slice from `start` to `stop` in typed tuple. Returns new typed tuple. ''' return new(self, items=self._collection[start:stop])
def rotate(self, n): r'''Rotates interval segment by `n`. Returns new interval segment. ''' return new(self, self[-n:] + self[:-n])
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 patterntools 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 = patterntools.Pattern( 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
def _repr_specification(self): from abjad.tools.topleveltools import new return new( self._storage_format_specification, is_indented=False, )
def invert(self, axis=None): r'''Inverts twelve-tone row about `axis`. .. container:: example **Example 1.** Inverts twelve-tone row about first pitch-class in row when `axis` is none: :: >>> row = pitchtools.TwelveToneRow( ... [1, 11, 9, 3, 6, 7, 5, 4, 10, 2, 8, 0] ... ) :: >>> row.invert() TwelveToneRow([1, 3, 5, 11, 8, 7, 9, 10, 4, 0, 6, 2]) First pitch-classes of prime form and inversion are equal. .. container:: example **Example 2.** Inverts twelve-tone row about pitch-class 1: :: >>> row = pitchtools.TwelveToneRow( ... [1, 11, 9, 3, 6, 7, 5, 4, 10, 2, 8, 0] ... ) :: >>> row.invert(axis=1) TwelveToneRow([1, 3, 5, 11, 8, 7, 9, 10, 4, 0, 6, 2]) Same result as above because 1 is the first pitch-class in row. .. container:: example **Example 3.** Inverts twelve-tone row about pitch-class 0: :: >>> row = pitchtools.TwelveToneRow( ... [1, 11, 9, 3, 6, 7, 5, 4, 10, 2, 8, 0] ... ) :: >>> row.invert(axis=0) TwelveToneRow([11, 1, 3, 9, 6, 5, 7, 8, 2, 10, 4, 0]) .. container:: example **Example 4.** Inverts twelve-tone row about pitch-class 5: :: >>> row = pitchtools.TwelveToneRow( ... [1, 11, 9, 3, 6, 7, 5, 4, 10, 2, 8, 0] ... ) :: >>> row.invert(axis=5) TwelveToneRow([9, 11, 1, 7, 4, 3, 5, 6, 0, 8, 2, 10]) Returns new twelve-tone row. ''' from abjad.tools import pitchtools if axis is None: axis = self[0] items = [pc.invert(axis=axis) for pc in self] return new(self, items=items)
def _get_lilypond_format_bundle(self, component): lilypond_format_bundle = self._get_basic_lilypond_format_bundle( component, ) previous_annotations = self._get_previous_annotations(component) previous_markups = previous_annotations[0] previous_line_segment = previous_annotations[1] previous_segment = (previous_markups is not None or previous_line_segment is not None) current_annotations = self._get_annotations(component) current_markups = current_annotations[0] current_markup = bool(current_markups) current_line_segment = current_annotations[1] current_event = (current_markups is not None or current_line_segment is not None) start_spanner, stop_spanner = False, False # stop any previous segment if previous_segment and current_event: stop_spanner = True # start spanner if no markup if self._is_my_first_leaf(component) and not current_markup: start_spanner = True # start spanner if existing line segment elif current_line_segment: start_spanner = True # stop spanner if last component if self._is_my_last_leaf(component): stop_spanner = True if start_spanner: contributions = override(self)._list_format_contributions( 'override', is_once=False, ) lilypond_format_bundle.grob_overrides.extend(contributions) string = r'\startTextSpan' lilypond_format_bundle.right.spanner_starts.append(string) if stop_spanner: contributions = override(self)._list_format_contributions( 'revert', ) lilypond_format_bundle.grob_reverts.extend(contributions) string = r'\stopTextSpan' lilypond_format_bundle.right.spanner_stops.append(string) if current_markups is not None: # assign markup to spanner left text if start_spanner: markup = current_markups[0] if current_line_segment: if current_line_segment.left_hspace is not None: hspace = current_line_segment.left_hspace hspace = markuptools.Markup.hspace(hspace) markup = markuptools.Markup.concat([markup, hspace]) override_ = lilypondnametools.LilyPondGrobOverride( grob_name='TextSpanner', is_once=True, property_path=( 'bound-details', 'left', 'text', ), value=markup, ) override_string = '\n'.join(override_._override_format_pieces) lilypond_format_bundle.grob_overrides.append(override_string) # format markup normally else: current_markup = current_markups[0] markup = new(current_markup, direction=Up) string = format(markup, 'lilypond') lilypond_format_bundle.right.markup.append(string) if current_line_segment is not None: overrides = current_line_segment._get_lilypond_grob_overrides() for override_ in overrides: override_string = '\n'.join(override_._override_format_pieces) lilypond_format_bundle.grob_overrides.append(override_string) return lilypond_format_bundle
def __radd__(self, expr): r'''Right-adds `expr` to typed tuple. ''' items = expr + self._collection return new(self, items=items)