def _duration_and_possible_denominators_to_time_signature( duration, denominators=None, factor=None, ): # check input duration = durationtools.Duration(duration) if denominators is not None: if factor is not None: denominators = [ d for d in denominators if factor in mathtools.factors(d) ] for desired_denominator in sorted(denominators): nonreduced_fraction = mathtools.NonreducedFraction(duration) candidate_pair = \ nonreduced_fraction.with_denominator(desired_denominator) if candidate_pair.denominator == desired_denominator: return indicatortools.TimeSignature(candidate_pair) if factor is not None: if factor in mathtools.factors(duration.denominator): return indicatortools.TimeSignature(duration) else: time_signature_numerator = factor * duration.numerator time_signature_denominator = factor * duration.denominator return indicatortools.TimeSignature( (time_signature_numerator, time_signature_denominator)) else: return indicatortools.TimeSignature(duration)
def test_systemtools_StorageFormatAgent_get_import_statements_03(): subject = [ indicatortools.TimeSignature((3, 4)), indicatortools.TimeSignature((4, 4)), ] agent = systemtools.StorageFormatAgent(subject) assert agent.get_import_statements() == ( 'from abjad.tools import indicatortools', )
def __init__( self, time_signatures=None, divisions=None, rh_rhythm_maker=None, lh_rhythm_maker=None, rh_pitch_range=None, lh_pitch_range=None, ): SegmentMaker.__init__(self) time_signatures = time_signatures or [] time_signatures = [ indicatortools.TimeSignature(x) for x in time_signatures ] self._time_signatures = time_signatures self._divisions = divisions if rh_rhythm_maker is None: rh_rhythm_maker = rhythmmakertools.NoteRhythmMaker() assert isinstance(rh_rhythm_maker, rhythmmakertools.RhythmMaker) self._rh_rhythm_maker = rh_rhythm_maker if lh_rhythm_maker is None: lh_rhythm_maker = rhythmmakertools.NoteRhythmMaker() assert isinstance(lh_rhythm_maker, rhythmmakertools.RhythmMaker) self._lh_rhythm_maker = lh_rhythm_maker rh_pitch_range = rh_pitch_range or '[C4, C6]' rh_pitch_range = pitchtools.PitchRange(rh_pitch_range) self._rh_pitch_range = rh_pitch_range lh_pitch_range = lh_pitch_range or '[C2, C4)' lh_pitch_range = pitchtools.PitchRange(lh_pitch_range) self._lh_pitch_range = lh_pitch_range
def _make_container(self, division): from abjad.tools import rhythmmakertools duration_spelling_specifier = self.duration_spelling_specifier if duration_spelling_specifier is None: duration_spelling_specifier = \ rhythmmakertools.DurationSpellingSpecifier() forbidden_written_duration = \ duration_spelling_specifier.forbidden_written_duration time_signature = indicatortools.TimeSignature(division) implied_prolation = time_signature.implied_prolation numerator, denominator = division.pair denominator = mathtools.greatest_power_of_two_less_equal(denominator) assert mathtools.is_positive_integer_power_of_two(denominator) exponent = self.exponent or 0 denominator_multiplier = 2**exponent denominator *= denominator_multiplier unit_duration = durationtools.Duration(1, denominator) if forbidden_written_duration is not None: multiplier = 1 while forbidden_written_duration <= unit_duration: unit_duration /= 2 multiplier *= 2 numerator *= multiplier numerator *= denominator_multiplier notes = scoretools.make_notes(numerator * [0], [unit_duration]) if implied_prolation == 1: result = scoretools.Container(notes) else: multiplier = implied_prolation result = scoretools.Tuplet(multiplier, notes) return result
def _scale(self, multiplier=None): from abjad.tools import indicatortools if multiplier is None: return multiplier = durationtools.Multiplier(multiplier) old_time_signature = self.time_signature if mathtools.is_nonnegative_integer_power_of_two(multiplier) and \ 1 <= multiplier: old_numerator = old_time_signature.numerator old_denominator = old_time_signature.denominator new_denominator = old_denominator // multiplier.numerator pair = (old_numerator, new_denominator) new_time_signature = indicatortools.TimeSignature(pair) else: old_denominator = old_time_signature.denominator old_duration = old_time_signature.duration new_duration = multiplier * old_duration new_time_signature = \ self._duration_and_possible_denominators_to_time_signature( new_duration, [old_denominator], multiplier.denominator, ) detach(indicatortools.TimeSignature, self) attach(new_time_signature, self) contents_multiplier_denominator = \ mathtools.greatest_power_of_two_less_equal(multiplier.denominator) pair = (multiplier.numerator, contents_multiplier_denominator) contents_multiplier = durationtools.Multiplier(*pair) self._scale_contents(contents_multiplier)
def to_measure(self, cell_duration_denominator=8): r'''Changes pitch array row to measures. Sets time signature numerators equal to pitch array row widths and time signature denominators equal to `cell_duration_denominator`. .. container:: example **Example 1.** Changes row to measure: :: >>> array = pitchtools.PitchArray([ ... [1, (2, 1), ([-2, -1.5], 2)], ... [(7, 2), (6, 1), 1]]) :: >>> print(array) [ ] [d'] [bf bqf ] [g' ] [fs' ] [ ] :: >>> measure = array.rows[0].to_measure() >>> show(measure) # doctest: +SKIP .. doctest:: >>> print(format(measure)) { \time 4/8 r8 d'8 <bf bqf>4 } Returns measure. ''' from abjad.tools import scoretools time_signature = indicatortools.TimeSignature( (self.width, cell_duration_denominator)) measure = scoretools.Measure(time_signature, []) basic_cell_duration = \ durationtools.Duration(1, cell_duration_denominator) measure_pitches, measure_durations = [], [] for cell in self.cells: cell_pitches = cell.pitches if not cell_pitches: measure_pitches.append(None) elif len(cell_pitches) == 1: measure_pitches.append(cell_pitches[0]) else: measure_pitches.append(cell_pitches) measure_duration = cell.width * basic_cell_duration measure_durations.append(measure_duration) leaves = scoretools.make_leaves(measure_pitches, measure_durations) measure.extend(leaves) return measure
def __init__( self, offset_in_ms=None, search_tree=None, time_signature=None, tempo=None, use_full_measure=False, ): from abjad.tools import quantizationtools offset_in_ms = offset_in_ms or 0 offset_in_ms = durationtools.Offset(offset_in_ms) if search_tree is None: search_tree = quantizationtools.UnweightedSearchTree() assert isinstance(search_tree, quantizationtools.SearchTree) tempo = tempo or indicatortools.Tempo((1, 4), 60) #tempo = indicatortools.Tempo(tempo) if isinstance(tempo, tuple): tempo = indicatortools.Tempo(*tempo) assert not tempo.is_imprecise time_signature = time_signature or (4, 4) time_signature = indicatortools.TimeSignature(time_signature) use_full_measure = bool(use_full_measure) beats = [] if use_full_measure: beatspan = time_signature.duration beat = quantizationtools.QTargetBeat( beatspan=beatspan, offset_in_ms=offset_in_ms, search_tree=search_tree, tempo=tempo ) beats.append(beat) else: beatspan = durationtools.Duration(1, time_signature.denominator) current_offset_in_ms = offset_in_ms beatspan_duration_in_ms = \ tempo.duration_to_milliseconds(beatspan) for i in range(time_signature.numerator): beat = quantizationtools.QTargetBeat( beatspan=beatspan, offset_in_ms=current_offset_in_ms, search_tree=search_tree, tempo=tempo ) beats.append(beat) current_offset_in_ms += beatspan_duration_in_ms self._beats = tuple(beats) self._offset_in_ms = offset_in_ms self._search_tree = search_tree self._tempo = tempo self._time_signature = time_signature self._use_full_measure = use_full_measure
def coerce(expr): if isinstance(expr, tuple): return indicatortools.TimeSignature(expr) elif isinstance(expr, indicatortools.TimeSignature): return copy.copy(expr) else: message = 'must be pair or time signature: {!r}.' message = message.format(expr) raise Exception(message)
def move_full_measure_tuplet_prolation_to_measure_time_signature(expr): r'''Moves prolation of full-measure tuplet to time signature of measure. Measures usually become non-power-of-two as as result. .. container:: example :: >>> tuplet = scoretools.FixedDurationTuplet(Duration(2, 8), "c'8 d'8 e'8") >>> measure = Measure((2, 8), [tuplet]) >>> measure.implicit_scaling = True >>> scoretools.move_full_measure_tuplet_prolation_to_measure_time_signature(measure) .. doctest:: >>> print(format(measure)) { \time 3/12 \scaleDurations #'(2 . 3) { c'8 d'8 e'8 } } Returns none. ''' from abjad.tools import indicatortools from abjad.tools import scoretools for measure in iterate(expr).by_class(scoretools.Measure): if len(measure) == 1: if isinstance(measure[0], scoretools.Tuplet): tuplet = measure[0] tuplet_multiplier = tuplet.multiplier tuplet_denominator = tuplet_multiplier.denominator reduced_denominator = mathtools.remove_powers_of_two( tuplet_denominator) time_signature = measure.time_signature time_signature_rational = durationtools.Duration( time_signature.numerator, time_signature.denominator) numerator = time_signature_rational.numerator * reduced_denominator denominator = time_signature_rational.denominator * reduced_denominator time_signature = indicatortools.TimeSignature( (numerator, denominator)) detach(indicatortools.TimeSignature, measure) attach(time_signature, measure) if time_signature.has_non_power_of_two_denominator: measure.implicit_scaling = True time_signature_multiplier = \ measure.time_signature.implied_prolation written_adjustment = tuplet_multiplier.__div__( time_signature_multiplier) tuplet._extract() measure._scale_contents(written_adjustment)
def _conditionally_adjust_time_signature(self, old_denominator): if self.automatically_adjust_time_signature: naive_time_signature = self._preprolated_duration better_time_signature = \ mathtools.NonreducedFraction(naive_time_signature) better_time_signature = \ better_time_signature.with_denominator(old_denominator) better_time_signature = \ indicatortools.TimeSignature(better_time_signature) detach(indicatortools.TimeSignature, self) attach(better_time_signature, self)
def _remove_and_shrink_durated_parent_containers(self): from abjad.tools import indicatortools from abjad.tools import scoretools prolated_leaf_duration = self._get_duration() parentage = self._get_parentage(include_self=False) prolations = parentage._prolations current_prolation, i = durationtools.Duration(1), 0 parent = self._parent while parent is not None and not parent.is_simultaneous: current_prolation *= prolations[i] if isinstance(parent, scoretools.FixedDurationTuplet): candidate_new_parent_dur = ( parent.target_duration - current_prolation * self.written_duration) if durationtools.Duration(0) < candidate_new_parent_dur: parent.target_duration = candidate_new_parent_dur elif isinstance(parent, scoretools.Measure): indicator = parent._get_indicator(indicatortools.TimeSignature) parent_time_signature = indicator old_prolation = parent_time_signature.implied_prolation naive_time_signature = (parent_time_signature.duration - prolated_leaf_duration) better_time_signature = mathtools.NonreducedFraction( naive_time_signature) better_time_signature = better_time_signature.with_denominator( parent_time_signature.denominator) better_time_signature = indicatortools.TimeSignature( better_time_signature) detach(indicatortools.TimeSignature, parent) attach(better_time_signature, parent) indicator = parent._get_indicator(indicatortools.TimeSignature) parent_time_signature = indicator new_prolation = parent_time_signature.implied_prolation adjusted_prolation = old_prolation / new_prolation for x in parent: if isinstance(x, scoretools.FixedDurationTuplet): x.target_duration *= adjusted_prolation else: if adjusted_prolation != 1: new_target = \ x._preprolated_duration * adjusted_prolation scoretools.FixedDurationTuplet(new_target, [x]) parent = parent._parent i += 1 parentage = self._get_parentage(include_self=False) parent = self._parent if parent: index = parent.index(self) del (parent[index]) for x in parentage: if not len(x): x._extract() else: break
def set_measure_denominator_and_adjust_numerator(measure, denominator): r'''Set `measure` time signature `denominator` and multiply time signature numerator accordingly: :: >>> measure = Measure((3, 8), "c'8 d'8 e'8") >>> beam = spannertools.Beam() >>> attach(beam, measure[:]) .. doctest:: >>> print(format(measure)) { \time 3/8 c'8 [ d'8 e'8 ] } :: >>> scoretools.set_measure_denominator_and_adjust_numerator(measure, 16) Measure((6, 16), "c'8 d'8 e'8") .. doctest:: >>> print(format(measure)) { \time 6/16 c'8 [ d'8 e'8 ] } Leave `measure` contents unchanged. Return `measure`. ''' from abjad.tools import scoretools if isinstance(measure, scoretools.Measure): # to allow iteration inside zero-update loop old_time_signature = measure.time_signature old_time_signature_pair = (old_time_signature.numerator, old_time_signature.denominator) new_time_signature = mathtools.NonreducedFraction( old_time_signature_pair) new_time_signature = new_time_signature.with_denominator(denominator) new_time_signature = indicatortools.TimeSignature(new_time_signature) detach(indicatortools.TimeSignature, measure) attach(new_time_signature, measure) return measure
def __illustrate__(self): r'''Illustrates tempo inventory. :: >>> show(inventory) # doctest: +SKIP Returns LilyPond file. ''' from abjad.tools import durationtools from abjad.tools import lilypondfiletools from abjad.tools import indicatortools from abjad.tools import markuptools from abjad.tools import pitchtools from abjad.tools import scoretools from abjad.tools import spannertools from abjad.tools.topleveltools import attach from abjad.tools.topleveltools import iterate from abjad.tools.topleveltools import override from abjad.tools.topleveltools import new staff = scoretools.Staff() score = scoretools.Score([staff]) time_signature = indicatortools.TimeSignature((2, 4)) attach(time_signature, staff) # the zero note avoids a lilypond spacing problem: # score-initial tempo indications slip to the left zero_note = scoretools.Note("c'2") staff.append(zero_note) command = indicatortools.LilyPondCommand('break') attach(command, zero_note) for tempo in self.items: note = scoretools.Note("c'2") attach(tempo, note) staff.append(note) command = indicatortools.LilyPondCommand('break') attach(command, note) override(score).bar_line.transparent = True override(score).bar_number.stencil = False override(score).clef.stencil = False override(score).note_head.no_ledgers = True override(score).note_head.transparent = True override(score).staff_symbol.transparent = True override(score).stem.transparent = True override(score).time_signature.stencil = False lilypond_file = lilypondfiletools.make_basic_lilypond_file(score) lilypond_file.layout_block.indent = 0 lilypond_file.layout_block.ragged_right = True lilypond_file.items.remove(lilypond_file['paper']) lilypond_file.header_block.tagline = markuptools.Markup('""') return lilypond_file
def implied_time_signature(self): r'''Gets implied time signature of meter. .. container:: example :: >>> metertools.Meter((4, 4)).implied_time_signature TimeSignature((4, 4)) Returns time signature. ''' return indicatortools.TimeSignature( self.root_node.preprolated_duration)
def __init__(self, *arguments, **keywords): from abjad.tools import quantizationtools search_tree = keywords.get('search_tree', quantizationtools.UnweightedSearchTree()) assert isinstance(search_tree, quantizationtools.SearchTree) self._search_tree = search_tree tempo = keywords.get('tempo', ((1, 4), 60)) if isinstance(tempo, tuple): tempo = indicatortools.MetronomeMark(*tempo) self._tempo = tempo self._time_signature = indicatortools.TimeSignature( keywords.get('time_signature', (4, 4))) self._use_full_measure = bool(keywords.get('use_full_measure')) QSchema.__init__(self, *arguments, **keywords)
def make_score(tuplet_duration, row_count, column_count): r'''Makes score. ''' score = scoretools.Score() rows_of_nested_tuplets = \ abjad.demos.ferneyhough.make_rows_of_nested_tuplets( tuplet_duration, row_count, column_count) for row_of_nested_tuplets in rows_of_nested_tuplets: staff = scoretools.Staff(row_of_nested_tuplets) staff.context_name = 'RhythmicStaff' time_signature = indicatortools.TimeSignature((1, 4)) attach(time_signature, staff) score.append(staff) return score
def __init__( self, time_signature=None, music=None, implicit_scaling=False, ): # set time signature adjustment before contents initialization self._automatically_adjust_time_signature = False time_signature = time_signature or (4, 4) self.implicit_scaling = implicit_scaling FixedDurationContainer.__init__(self, time_signature, music) self._always_format_time_signature = False self._measure_number = None time_signature = indicatortools.TimeSignature(time_signature) attach(time_signature, self)
def make_spacer_skip_measures(time_signatures, implicit_scaling=False): r'''Makes measures with full-measure spacer skips from `time_signatures`. .. container:: example :: >>> measures = scoretools.make_spacer_skip_measures( ... [(1, 8), (5, 16), (5, 16)]) >>> staff = Staff(measures) >>> show(staff) # doctest: +SKIP .. doctest:: >>> print(format(staff)) \new Staff { { \time 1/8 s1 * 1/8 } { \time 5/16 s1 * 5/16 } { s1 * 5/16 } } Returns selection of unincorporated measures. ''' from abjad.tools import indicatortools from abjad.tools import scoretools # make measures measures = [] for time_signature in time_signatures: time_signature = indicatortools.TimeSignature(time_signature) measure = scoretools.Measure( time_signature, implicit_scaling=implicit_scaling, ) measures.append(measure) scoretools.fill_measures_in_expr_with_full_measure_spacer_skips(measures) # return measures measures = selectiontools.Selection(measures) return measures
def __init__(self, search_tree=None, tempo=None, time_signature=None, use_full_measure=None): QSchemaItem.__init__( self, search_tree=search_tree, tempo=tempo, ) if time_signature is not None: time_signature = indicatortools.TimeSignature(time_signature) self._time_signature = time_signature if use_full_measure is not None: use_full_measure = bool(use_full_measure) self._use_full_measure = use_full_measure
def _get_logical_measure_start_offsets(self, component): from abjad.tools import durationtools from abjad.tools import indicatortools from abjad.tools import sequencetools from abjad.tools.topleveltools import inspect_ from abjad.tools.topleveltools import iterate expressions = [] prototype = indicatortools.TimeSignature components = self._iterate_entire_score(component) for component in components: expressions_ = component._get_indicators( prototype, unwrap=False, ) expressions.extend(expressions_) pairs = [] for expression in expressions: inspector = inspect_(expression.component) start_offset = inspector.get_timespan().start_offset time_signature = expression.indicator pair = start_offset, time_signature pairs.append(pair) offset_zero = durationtools.Offset(0) default_time_signature = indicatortools.TimeSignature((4, 4)) default_pair = (offset_zero, default_time_signature) if pairs and not pairs[0] == offset_zero: pairs.insert(0, default_pair) elif not pairs: pairs = [default_pair] pairs.sort(key=lambda x: x[0]) parentage = component._get_parentage() score_root = parentage.root inspector = inspect_(score_root) score_stop_offset = inspector.get_timespan().stop_offset dummy_last_pair = (score_stop_offset, None) pairs.append(dummy_last_pair) measure_start_offsets = [] pairs = sequencetools.iterate_sequence_nwise(pairs, n=2) for current_pair, next_pair in pairs: current_start_offset, current_time_signature = current_pair next_start_offset, next_time_signature = next_pair measure_start_offset = current_start_offset while measure_start_offset < next_start_offset: measure_start_offsets.append(measure_start_offset) measure_start_offset += current_time_signature.duration return measure_start_offsets
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 make_repeated_notes_from_time_signature(time_signature, pitch="c'"): '''Make repeated notes from `time_signature`: :: >>> scoretools.make_repeated_notes_from_time_signature((5, 32)) Selection([Note("c'32"), Note("c'32"), Note("c'32"), Note("c'32"), Note("c'32")]) Make repeated notes with `pitch` from `time_signature`: :: >>> scoretools.make_repeated_notes_from_time_signature((5, 32), pitch="d''") Selection([Note("d''32"), Note("d''32"), Note("d''32"), Note("d''32"), Note("d''32")]) Returns list of notes. ''' 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: raise NotImplementedError( 'TODO: extend this function for time signatures with a non-power-of-two denominators.' ) # make and return repeated notes duration = (1, time_signature.denominator) result = time_signature.numerator * scoretools.Note(pitch, duration) # return result result = selectiontools.Selection(result) return result
def _split_at_index( self, i, fracture_spanners=False, ): r'''Splits container to the left of index `i`. Preserves tuplet multiplier when container is a tuplet. Preserves time signature denominator when container is a measure. Resizes resizable containers. Returns split parts. ''' from abjad.tools import indicatortools from abjad.tools import scoretools from abjad.tools import selectiontools from abjad.tools import spannertools # partition my music left_music = self[:i] right_music = self[i:] # instantiate new left and right containers if isinstance(self, scoretools.Measure): time_signature = self._get_effective(indicatortools.TimeSignature) denominator = time_signature.denominator left_duration = sum([x._get_duration() for x in left_music]) left_pair = mathtools.NonreducedFraction(left_duration) left_pair = left_pair.with_multiple_of_denominator(denominator) left_time_signature = indicatortools.TimeSignature(left_pair) left = type(self)(left_time_signature, left_music) left.implicit_scaling = self.implicit_scaling right_duration = sum([x._get_duration() for x in right_music]) right_pair = mathtools.NonreducedFraction(right_duration) right_pair = right_pair.with_multiple_of_denominator(denominator) right_time_signature = indicatortools.TimeSignature(right_pair) right = type(self)(right_time_signature, right_music) right.implicit_scaling = self.implicit_scaling elif isinstance(self, scoretools.FixedDurationTuplet): multiplier = self.multiplier left = type(self)(1, left_music) right = type(self)(1, right_music) target_duration = multiplier * left._contents_duration left.target_duration = target_duration target_duration = multiplier * right._contents_duration right.target_duration = target_duration elif isinstance(self, scoretools.Tuplet): multiplier = self.multiplier left = type(self)(multiplier, left_music) right = type(self)(multiplier, right_music) else: left = type(self)(left_music) right = type(self)(right_music) # save left and right containers together for iteration halves = (left, right) nonempty_halves = [half for half in halves if len(half)] # give my attached spanners to my children self._move_spanners_to_children() # incorporate left and right parents in score if possible selection = selectiontools.SliceSelection(self) parent, start, stop = selection._get_parent_and_start_stop_indices() if parent is not None: parent._music.__setitem__(slice(start, stop + 1), nonempty_halves) for part in nonempty_halves: part._set_parent(parent) else: left._set_parent(None) right._set_parent(None) # fracture spanners if requested if fracture_spanners: for spanner in left._get_spanners(): index = spanner._index(left) spanner._fracture(index, direction=Right) # return new left and right containers return halves
def time(self, number_list, fraction): r'''Handles LilyPond ``\time`` command. ''' n, d = fraction.numerator, fraction.denominator return indicatortools.TimeSignature((n, d))
def scale_and_adjust_time_signature(self, multiplier=None): r'''Scales `measure` by `multiplier` and adjusts time signature. .. container:: example Scales measure by non-power-of-two multiplier: :: >>> measure = Measure((3, 8), "c'8 d'8 e'8") >>> measure.implicit_scaling = True >>> show(measure) # doctest: +SKIP .. doctest:: >>> print(format(measure)) { \time 3/8 c'8 d'8 e'8 } :: >>> measure.scale_and_adjust_time_signature(Multiplier(2, 3)) >>> show(measure) # doctest: +SKIP .. doctest:: >>> print(format(measure)) { \time 3/12 \scaleDurations #'(2 . 3) { c'8 d'8 e'8 } } Returns none. ''' from abjad.tools import indicatortools from abjad.tools import scoretools if multiplier == 0: raise ZeroDivisionError old_time_signature = self.time_signature old_pair = \ (old_time_signature.numerator, old_time_signature.denominator) old_multiplier = old_time_signature.implied_prolation old_multiplier_pair = \ (old_multiplier.numerator, old_multiplier.denominator) multiplied_pair = mathtools.NonreducedFraction(old_multiplier_pair) multiplied_pair = multiplied_pair.multiply_without_reducing(multiplier) multiplied_pair = multiplied_pair.pair reduced_pair = mathtools.NonreducedFraction(old_multiplier_pair) reduced_pair = reduced_pair.multiply_with_cross_cancelation(multiplier) reduced_pair = reduced_pair.pair if reduced_pair != multiplied_pair: new_pair = mathtools.NonreducedFraction(old_pair) new_pair = \ new_pair.multiply_with_numerator_preservation(multiplier) new_time_signature = indicatortools.TimeSignature(new_pair) detach(indicatortools.TimeSignature, self) attach(new_time_signature, self) remaining_multiplier = durationtools.Multiplier(reduced_pair) if remaining_multiplier != durationtools.Multiplier(1): self._scale_contents(remaining_multiplier) elif self._all_contents_are_scalable_by_multiplier(multiplier): self._scale_contents(multiplier) if (old_time_signature.has_non_power_of_two_denominator or not mathtools.is_nonnegative_integer_power_of_two( multiplier)): new_pair = mathtools.NonreducedFraction(old_pair) new_pair = new_pair.multiply_with_cross_cancelation(multiplier) new_pair = new_pair.pair # multiplier is a negative power of two, like 1/2, 1/4, etc. elif multiplier < durationtools.Multiplier(0): new_pair = \ mathtools.NonreducedFraction.multiply_without_reducing( old_pair, multiplier) # multiplier is a nonnegative power of two, like 0, 1, 2, 4, etc. elif durationtools.Multiplier(0) < multiplier: new_pair = mathtools.NonreducedFraction(old_pair) new_pair = new_pair.multiply_with_numerator_preservation( multiplier) elif multiplier == durationtools.Multiplier(0): raise ZeroDivisionError new_time_signature = indicatortools.TimeSignature(new_pair) detach(indicatortools.TimeSignature, self) attach(new_time_signature, self) if new_time_signature.has_non_power_of_two_denominator: self.implicit_scaling = True else: new_pair = mathtools.NonreducedFraction(old_pair) new_pair = new_pair.multiply_with_numerator_preservation( multiplier) new_time_signature = indicatortools.TimeSignature(new_pair) detach(indicatortools.TimeSignature, self) attach(new_time_signature, self) if new_time_signature.has_non_power_of_two_denominator: self.implicit_scaling = True remaining_multiplier = \ multiplier / new_time_signature.implied_prolation if remaining_multiplier != durationtools.Multiplier(1): self._scale_contents(remaining_multiplier)
def make_mozart_score(): r'''Makes Mozart score. ''' score_template = templatetools.TwoStaffPianoScoreTemplate() score = score_template() # select the measures to use choices = abjad.demos.mozart.choose_mozart_measures() # create and populate the volta containers treble_volta = scoretools.Container() bass_volta = scoretools.Container() for choice in choices[:7]: treble, bass = abjad.demos.mozart.make_mozart_measure(choice) treble_volta.append(treble) bass_volta.append(bass) # attach indicators to the volta containers command = indicatortools.LilyPondCommand('repeat volta 2', 'before') attach(command, treble_volta) command = indicatortools.LilyPondCommand('repeat volta 2', 'before') attach(command, bass_volta) # append the volta containers to our staves score['RH Voice'].append(treble_volta) score['LH Voice'].append(bass_volta) # create and populate the alternative ending containers treble_alternative = scoretools.Container() bass_alternative = scoretools.Container() for choice in choices[7:9]: treble, bass = abjad.demos.mozart.make_mozart_measure(choice) treble_alternative.append(treble) bass_alternative.append(bass) # attach indicators to the alternative containers command = indicatortools.LilyPondCommand('alternative', 'before') attach(command, treble_alternative) command = indicatortools.LilyPondCommand('alternative', 'before') attach(command, bass_alternative) # append the alternative containers to our staves score['RH Voice'].append(treble_alternative) score['LH Voice'].append(bass_alternative) # create the remaining measures for choice in choices[9:]: treble, bass = abjad.demos.mozart.make_mozart_measure(choice) score['RH Voice'].append(treble) score['LH Voice'].append(bass) # attach indicators time_signature = indicatortools.TimeSignature((3, 8)) attach(time_signature, score['RH Staff']) bar_line = indicatortools.BarLine('|.') attach(bar_line, score['RH Voice'][-1]) bar_line = indicatortools.BarLine('|.') attach(bar_line, score['LH Voice'][-1]) # remove the old, default piano instrument attached to the piano staff # and attach a custom instrument mark detach(instrumenttools.Instrument, score['Piano Staff']) klavier = instrumenttools.Piano( instrument_name='Katzenklavier', short_instrument_name='kk.', ) attach(klavier, score['Piano Staff']) return score
def __call__(self): '''Calls score template. Returns LilyPond file. ''' # make bell voice and staff bell_voice = scoretools.Voice(name='Bell Voice') bell_staff = scoretools.Staff([bell_voice], name='Bell Staff') clef = indicatortools.Clef('treble') attach(clef, bell_staff) bells = instrumenttools.Instrument( instrument_name='Campana in La', short_instrument_name='Camp.', pitch_range='[C4, C6]', ) attach(bells, bell_staff) tempo = indicatortools.Tempo((1, 4), (112, 120)) attach(tempo, bell_staff) time_signature = indicatortools.TimeSignature((6, 4)) attach(time_signature, bell_staff) # make first violin voice and staff first_violin_voice = scoretools.Voice(name='First Violin Voice') first_violin_staff = scoretools.Staff( [first_violin_voice], name='First Violin Staff', ) clef = indicatortools.Clef('treble') attach(clef, first_violin_staff) violin = instrumenttools.Violin( instrument_name_markup=markuptools.Markup('Violin I'), short_instrument_name_markup=markuptools.Markup('Vl. I'), ) attach(violin, first_violin_staff) # make second violin voice and staff second_violin_voice = scoretools.Voice(name='Second Violin Voice') second_violin_staff = scoretools.Staff( [second_violin_voice], name='Second Violin Staff', ) clef = indicatortools.Clef('treble') attach(clef, second_violin_staff) violin = instrumenttools.Violin( instrument_name_markup=markuptools.Markup('Violin II'), short_instrument_name_markup=markuptools.Markup('Vl. II'), ) attach(violin, second_violin_staff) # make viola voice and staff viola_voice = scoretools.Voice(name='Viola Voice') viola_staff = scoretools.Staff([viola_voice], name='Viola Staff') clef = indicatortools.Clef('alto') attach(clef, viola_staff) viola = instrumenttools.Viola() attach(viola, viola_staff) # make cello voice and staff cello_voice = scoretools.Voice(name='Cello Voice') cello_staff = scoretools.Staff([cello_voice], name='Cello Staff') clef = indicatortools.Clef('bass') attach(clef, cello_staff) cello = instrumenttools.Cello() attach(cello, cello_staff) # make bass voice and staff bass_voice = scoretools.Voice(name='Bass Voice') bass_staff = scoretools.Staff([bass_voice], name='Bass Staff') clef = indicatortools.Clef('bass') attach(clef, bass_staff) contrabass = instrumenttools.Contrabass( short_instrument_name_markup=markuptools.Markup('Cb.'), ) attach(contrabass, bass_staff) # make strings staff group strings_staff_group = scoretools.StaffGroup( [ first_violin_staff, second_violin_staff, viola_staff, cello_staff, bass_staff, ], name='Strings Staff Group', ) # make score score = scoretools.Score([ bell_staff, strings_staff_group, ], name='Pärt Cantus Score') # return Pärt Cantus score return score