def _divide(self, pitch=None): from abjad.tools import scoretools from abjad.tools import markuptools from abjad.tools import pitchtools pitch = pitch or pitchtools.NamedPitch('b', 3) pitch = pitchtools.NamedPitch(pitch) treble = copy.copy(self) bass = copy.copy(self) detach(markuptools.Markup, treble) detach(markuptools.Markup, bass) if isinstance(treble, scoretools.Note): if treble.written_pitch < pitch: treble = scoretools.Rest(treble) elif isinstance(treble, scoretools.Rest): pass elif isinstance(treble, scoretools.Chord): for note_head in reversed(treble.note_heads): if note_head.written_pitch < pitch: treble.note_heads.remove(note_head) else: raise TypeError if isinstance(bass, scoretools.Note): if pitch <= bass.written_pitch: bass = scoretools.Rest(bass) elif isinstance(bass, scoretools.Rest): pass elif isinstance(bass, scoretools.Chord): for note_head in reversed(bass.note_heads): if pitch <= note_head.written_pitch: bass.note_heads.remove(note_head) else: raise TypeError treble = self._cast_defective_chord(treble) bass = self._cast_defective_chord(bass) up_markup = self._get_markup(direction=Up) up_markup = [copy.copy(markup) for markup in up_markup] down_markup = self._get_markup(direction=Down) down_markup = [copy.copy(markup) for markup in down_markup] for markup in up_markup: markup(treble) for markup in down_markup: markup(bass) return treble, bass
def __call__(self, q_events): r'''Calls concatenating grace handler. ''' from abjad.tools import quantizationtools grace_events, final_event = q_events[:-1], q_events[-1] if isinstance(final_event, quantizationtools.PitchedQEvent): pitches = final_event.pitches else: pitches = () if grace_events: grace_container = scoretools.GraceContainer() for q_event in grace_events: if isinstance(q_event, quantizationtools.PitchedQEvent): if len(q_event.pitches) == 1: leaf = scoretools.Note(q_event.pitches[0], self.grace_duration) else: leaf = scoretools.Chord(q_event.pitches, self.grace_duration) else: leaf = scoretools.Rest(self.grace_duration) grace_container.append(leaf) else: grace_container = None return pitches, grace_container
def _cast_defective_chord(chord): from abjad.tools import scoretools if isinstance(chord, Chord): note_head_count = len(chord.note_heads) if not note_head_count: return scoretools.Rest(chord) elif note_head_count == 1: return scoretools.Note(chord) return chord
def __init__(self, *args): from abjad.tools import scoretools if len(args) == 0: args = ((1, 4), ) rest = scoretools.Rest(*args) Leaf.__init__( self, rest.written_duration, )
def _divide(self, pitch=None): from abjad.tools import markuptools from abjad.tools import pitchtools from abjad.tools import scoretools pitch = pitch or pitchtools.NamedPitch('b', 3) pitch = pitchtools.NamedPitch(pitch) treble = copy.copy(self) bass = copy.copy(self) detach(markuptools.Markup, treble) detach(markuptools.Markup, bass) if treble.written_pitch < pitch: treble = scoretools.Rest(treble) if pitch <= bass.written_pitch: bass = scoretools.Rest(bass) up_markup = self._get_markup(direction=Up) up_markup = [copy.copy(markup) for markup in up_markup] down_markup = self._get_markup(direction=Down) down_markup = [copy.copy(markup) for markup in down_markup] for markup in up_markup: markup(treble) for markup in down_markup: markup(bass) return treble, bass
def _notate_leaves( self, grace_handler=None, voice=None, ): for leaf in iterate(voice).by_leaf(): if leaf._has_indicator(indicatortools.Annotation): annotation = leaf._get_indicator(indicatortools.Annotation) pitches, grace_container = grace_handler(annotation.value) if not pitches: new_leaf = scoretools.Rest(leaf) elif 1 < len(pitches): new_leaf = scoretools.Chord(leaf) new_leaf.written_pitches = pitches else: new_leaf = scoretools.Note(leaf) new_leaf.written_pitch = pitches[0] if grace_container: attach(grace_container, new_leaf) tie = spannertools.Tie() if tie._attachment_test(new_leaf): attach(tie, new_leaf) mutate(leaf).replace(new_leaf) else: previous_leaf = leaf._get_leaf(-1) if isinstance(previous_leaf, scoretools.Rest): new_leaf = type(previous_leaf)( leaf.written_duration, ) elif isinstance(previous_leaf, scoretools.Note): new_leaf = type(previous_leaf)( previous_leaf.written_pitch, leaf.written_duration, ) else: new_leaf = type(previous_leaf)( previous_leaf.written_pitch, leaf.written_duration, ) mutate(leaf).replace(new_leaf) tie = inspect_(previous_leaf).get_spanner(spannertools.Tie) if tie is not None: tie._append(new_leaf) if leaf._has_indicator(indicatortools.Tempo): tempo = leaf._get_indicator(indicatortools.Tempo) detach(indicatortools.Tempo, leaf) attach(tempo, new_leaf)
def _make_repeated_rests_from_time_signature(time_signature): from abjad.tools import indicatortools from abjad.tools import scoretools # afford basic input polymorphism time_signature = indicatortools.TimeSignature(time_signature) # check input if time_signature.has_non_power_of_two_denominator: message = 'TODO: extend this function for time signatures' message += ' with non-power-of-two denominators.' raise NotImplementedError(message) # make and return repeated rests rest = scoretools.Rest((1, time_signature.denominator)) result = time_signature.numerator * rest result = selectiontools.Selection(result) return result
def _apply_logical_tie_masks(self, selections): from abjad.tools import rhythmmakertools if self.logical_tie_masks is None: return selections # wrap every selection in a temporary container; # this allows the call to mutate().replace() to work containers = [] for selection in selections: container = scoretools.Container(selection) attach('temporary container', container) containers.append(container) logical_ties = iterate(selections).by_logical_tie() logical_ties = list(logical_ties) total_logical_ties = len(logical_ties) for index, logical_tie in enumerate(logical_ties[:]): matching_mask = self.logical_tie_masks.get_matching_pattern( index, total_logical_ties, ) if not isinstance(matching_mask, rhythmmakertools.SilenceMask): continue if isinstance(logical_tie.head, scoretools.Rest): continue for leaf in logical_tie: rest = scoretools.Rest(leaf.written_duration) inspector = inspect_(leaf) if inspector.has_indicator(durationtools.Multiplier): multiplier = inspector.get_indicator( durationtools.Multiplier, ) multiplier = durationtools.Multiplier(multiplier) attach(multiplier, rest) mutate(leaf).replace([rest]) detach(spannertools.Tie, rest) # remove every temporary container and recreate selections new_selections = [] for container in containers: inspector = inspect_(container) assert inspector.get_indicator(str) == 'temporary container' new_selection = mutate(container).eject_contents() new_selections.append(new_selection) return new_selections
def __illustrate__(self): r'''Illustrates clef inventory. :: >>> show(inventory) # doctest: +SKIP Returns LilyPond file. ''' from abjad.tools import lilypondfiletools from abjad.tools import scoretools staff = scoretools.Staff() for clef in self: rest = scoretools.Rest((1, 8)) clef = copy.copy(clef) attach(clef, rest) staff.append(rest) override(staff).clef.full_size_change = True override(staff).rest.transparent = True override(staff).time_signature.stencil = False lilypond_file = lilypondfiletools.make_basic_lilypond_file(staff) lilypond_file.header_block.tagline = False return lilypond_file
def p_rest_body__negative_leaf_duration(self, p): r'''rest_body : negative_leaf_duration ''' p[0] = scoretools.Rest(p[1])
def p_rest_body__RESTNAME__positive_leaf_duration(self, p): r'''rest_body : RESTNAME positive_leaf_duration ''' p[0] = scoretools.Rest(p[2])
def p_rest_body__RESTNAME(self, p): r'''rest_body : RESTNAME ''' p[0] = scoretools.Rest(self._default_duration)
def make_percussion_note(pitch, total_duration, max_note_duration=(1, 8)): r'''Makes short note with `max_note_duration` followed by rests together totaling `total_duration`. .. container:: example >>> leaves = scoretools.make_percussion_note(2, (1, 4), (1, 8)) >>> staff = Staff(leaves) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { d'8 r8 } .. container:: example >>> leaves = scoretools.make_percussion_note(2, (1, 64), (1, 8)) >>> staff = Staff(leaves) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { d'64 } .. container:: example >>> leaves = scoretools.make_percussion_note(2, (5, 64), (1, 8)) >>> staff = Staff(leaves) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { d'16 r64 } .. container:: example >>> leaves = scoretools.make_percussion_note(2, (5, 4), (1, 8)) >>> staff = Staff(leaves) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { d'8 r1 r8 } Returns list of newly constructed note followed by zero or more newly constructed rests. Durations of note and rests returned will sum to `total_duration`. Duration of note returned will be no greater than `max_note_duration`. Duration of rests returned will sum to note duration taken from `total_duration`. Useful for percussion music where attack duration is negligible and tied notes undesirable. ''' from abjad.tools import scoretools from abjad.tools import scoretools from abjad.tools import selectiontools # check input total_duration = durationtools.Duration(total_duration) max_note_duration = durationtools.Duration(max_note_duration) # make note and rest if max_note_duration < total_duration: rest_duration = total_duration - max_note_duration rests = scoretools.make_tied_leaf( scoretools.Rest, rest_duration, pitches=None, ) notes = scoretools.make_tied_leaf( scoretools.Note, max_note_duration, pitches=pitch, ) else: notes = scoretools.make_tied_leaf( scoretools.Note, total_duration, pitches=pitch, tie_parts=False, ) if 1 < len(notes): new_notes = [] new_notes.append(notes[0]) for i in range(1, len(notes)): rest = scoretools.Rest(notes[i]) new_notes.append(rest) notes = new_notes rests = [] # return list of percussion note followed by rest result = notes + rests result = selectiontools.Selection(result) return result