def test_scoretools_Component__measure_number_02(): staff = abjad.Staff() staff.append(abjad.Measure((2, 4), "c'4 d'4")) staff.append(abjad.Measure((2, 4), "e'4 f'4")) staff.append(abjad.Measure((3, 4), "g'4 a'4 b'4")) staff.append(abjad.Measure((1, 4), "c''4")) staff._update_measure_numbers() assert staff._measure_number == 1 assert staff[0]._measure_number == 1 assert staff[1]._measure_number == 2 assert staff[2]._measure_number == 3 assert staff[3]._measure_number == 4 leaves = abjad.iterate(staff).leaves() leaves = list(leaves) assert leaves[0]._measure_number == 1 assert leaves[1]._measure_number == 1 assert leaves[2]._measure_number == 2 assert leaves[3]._measure_number == 2 assert leaves[4]._measure_number == 3 assert leaves[5]._measure_number == 3 assert leaves[6]._measure_number == 3 assert leaves[7]._measure_number == 4
def test_scoretools_Measure___add___01(): r'''Add outside-of-score measures. ''' measure_1 = abjad.Measure((1, 8), "c'16 d'16") beam = abjad.Beam() abjad.attach(beam, measure_1[:]) measure_2 = abjad.Measure((2, 16), "c'16 d'16") slur = abjad.Slur() abjad.attach(slur, measure_2[:]) new = measure_1 + measure_2 assert format(new) == abjad.String.normalize(r''' { \time 2/8 c'16 [ d'16 ] c'16 ( d'16 ) } ''') assert new is not measure_1 and new is not measure_2 assert len(measure_1) == 0 assert len(measure_2) == 0 assert abjad.inspect(new).is_well_formed()
def add_bell_music_to_score(score): r'''Adds bell music to score. ''' bell_voice = score['Bell Voice'] def make_bell_phrase(): phrase = [] for _ in range(3): measure = abjad.Measure((6, 4), r"r2. a'2.") abjad.attach(abjad.LaissezVibrer(), measure[-1]) phrase.append(measure) phrase.append(abjad.Measure((6, 4), 'R1.')) for _ in range(2): phrase.append(abjad.Measure((6, 4), 'R1.')) return phrase for _ in range(11): bell_voice.extend(make_bell_phrase()) for _ in range(19): bell_voice.append(abjad.Measure((6, 4), 'R1.')) measure = abjad.Measure((6,4), r"a'1.") abjad.attach(abjad.LaissezVibrer(), measure[-1]) bell_voice.append(measure)
def test_scoretools_Mutation_swap_07(): r'''Moves parentage, children and spanners from one measure to another. ''' measure = abjad.Measure((4, 8), "c'8 d'8 e'8 f'8") assert format(measure) == abjad.String.normalize(r''' { % measure \time 4/8 c'8 d'8 e'8 f'8 } % measure ''') new_measure = abjad.Measure((4, 8), []) abjad.mutate(measure).swap(new_measure) assert format(new_measure) == abjad.String.normalize(r''' { % measure \time 4/8 c'8 d'8 e'8 f'8 } % measure ''') assert abjad.inspect(new_measure).is_well_formed()
def make_bell_phrase(): phrase = [] for _ in range(3): measure = abjad.Measure((6, 4), r"r2. a'2.") abjad.attach(abjad.LaissezVibrer(), measure[-1]) phrase.append(measure) phrase.append(abjad.Measure((6, 4), 'R1.')) for _ in range(2): phrase.append(abjad.Measure((6, 4), 'R1.')) return phrase
def test_scoretools_Mutation_fuse_11(): r'''Fuses unicorporated measures carrying time signatures with power-of-two denominators. ''' measure_1 = abjad.Measure((1, 8), "c'16 d'16") beam = abjad.Beam() abjad.attach(beam, measure_1[:]) measure_2 = abjad.Measure((2, 16), "c'16 d'16") slur = abjad.Slur() abjad.attach(slur, measure_2[:]) staff = abjad.Staff([measure_1, measure_2]) assert format(staff) == abjad.String.normalize(r''' \new Staff { { % measure \time 1/8 c'16 [ d'16 ] } % measure { % measure \time 2/16 c'16 ( d'16 ) } % measure } ''') new = abjad.mutate(staff[:]).fuse() assert new is not measure_1 and new is not measure_2 assert len(measure_1) == 0 assert len(measure_2) == 0 assert format(new) == abjad.String.normalize(r''' { % measure \time 2/8 c'16 [ d'16 ] c'16 ( d'16 ) } % measure ''') assert abjad.inspect(new).is_well_formed()
def test_scoretools_Mutation_fuse_14(): r'''Fuses measures with power-of-two-denominators together with measures without power-of-two denominators. Helpers abjad.selects least common multiple of denominators. Beams are OK because they abjad.attach to leaves rather than containers. ''' measure_1 = abjad.Measure((1, 8), "c'8") measure_1.implicit_scaling = True measure_2 = abjad.Measure((1, 12), "d'8") measure_2.implicit_scaling = True voice = abjad.Voice([measure_1, measure_2]) leaves = abjad.select(voice).leaves() beam = abjad.Beam() abjad.attach(beam, leaves) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 1/8 c'8 [ } % measure { % measure \time 1/12 \scaleDurations #'(2 . 3) { d'8 ] } } % measure } ''') abjad.mutate(voice[:]).fuse() assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 5/24 \scaleDurations #'(2 . 3) { c'8. [ d'8 ] } } % measure } ''') assert abjad.inspect(voice).is_well_formed()
def test_three_measures_mixed(self): time_signatures = [ abjad.TimeSignature((3, 8)), abjad.TimeSignature((2, 4)), abjad.TimeSignature((5, 16)) ] measures = make_spacer_skip_measures(time_signatures) test_measures = [ abjad.Measure((3, 8), 's4.'), abjad.Measure((2, 4), 's2'), abjad.Measure((5, 16), 's4 s16') ] self.assertEqual(str(measures), str(test_measures))
def test_scoretools_Inspection_get_duration_01(): r'''Spanner duration in seconds equals sum of duration of all leaves in spanner, in seconds. ''' voice = abjad.Voice([ abjad.Measure((2, 12), "c'8 d'8", implicit_scaling=True), abjad.Measure((2, 8), "c'8 d'8") ]) leaves = abjad.select(voice).leaves() mark = abjad.MetronomeMark(abjad.Duration(1, 8), 42) abjad.attach(mark, leaves[0], context='Voice') beam = abjad.Beam() abjad.attach(beam, leaves) crescendo = abjad.Hairpin('<') abjad.attach(crescendo, voice[0][:]) diminuendo = abjad.Hairpin('>') abjad.attach(diminuendo, voice[1][:]) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 2/12 \scaleDurations #'(2 . 3) { \tempo 8=42 c'8 [ \< d'8 \! } } % measure { % measure \time 2/8 c'8 \> d'8 ] \! } % measure } ''') assert abjad.inspect(beam).get_duration(in_seconds=True) == abjad.Duration( 100, 21) assert abjad.inspect(crescendo).get_duration( in_seconds=True) == abjad.Duration(40, 21) assert abjad.inspect(diminuendo).get_duration(in_seconds=True) == \ abjad.Duration(20, 7)
def test_scoretools_Mutation_scale_07(): r'''Scales measures by 5/4. ''' voice = abjad.Voice() voice.append(abjad.Measure((2, 8), "c'8 d'8")) voice.append(abjad.Measure((2, 8), "e'8 f'8")) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 2/8 c'8 d'8 } % measure { % measure e'8 f'8 } % measure } ''') abjad.mutate(voice).scale(abjad.Multiplier(5, 4)) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 20/64 c'8 ~ c'32 d'8 ~ d'32 } % measure { % measure e'8 ~ e'32 f'8 ~ f'32 } % measure } ''') assert abjad.inspect(voice).is_well_formed()
def test_scoretools_Measure___add___02(): r'''Add measures in score. ''' measure_1 = abjad.Measure((1, 8), "c'16 d'16") beam = abjad.Beam() abjad.attach(beam, measure_1[:]) measure_2 = abjad.Measure((2, 16), "c'16 d'16") slur = abjad.Slur() abjad.attach(slur, measure_2[:]) staff = abjad.Staff([measure_1, measure_2]) assert format(staff) == abjad.String.normalize(r''' \new Staff { { \time 1/8 c'16 [ d'16 ] } { \time 2/16 c'16 ( d'16 ) } } ''') new = measure_1 + measure_2 assert new is not measure_1 and new is not measure_2 assert len(measure_1) == 0 assert len(measure_2) == 0 assert format(staff) == abjad.String.normalize(r''' \new Staff { { \time 2/8 c'16 [ d'16 ] c'16 ( d'16 ) } } ''') assert abjad.inspect(staff).is_well_formed()
def __call__(self, time_signatures): r'''Calls measure-maker on `time_signatures`. Returns measures. ''' import abjad measures = [] for time_signature in time_signatures: time_signature = abjad.TimeSignature(time_signature) measure = abjad.Measure( time_signature, implicit_scaling=self.implicit_scaling, ) measures.append(measure) for i, measure in enumerate(measures): skip = abjad.Skip(1) # allow zero-update iteration time_signature = measure.time_signature duration = time_signature.duration if measure.implicit_scaling: implied_prolation = time_signature.implied_prolation multiplier = duration.__div__(implied_prolation) else: multiplier = abjad.Multiplier(duration) abjad.attach(multiplier, skip) measure[:] = [skip] # REMOVE: spanners attach only to leaves: #for spanner in measure._get_spanners(): # spanner._remove(measure) return abjad.select(measures)
def test_scoretools_Measure___setitem___01(): measure = abjad.Measure((3, 4), "c' d' e'") measure[:2] = 'r8' assert measure.is_underfull assert pytest.raises(Exception, 'f(measure)')
def test_scoretools_Measure___setattr___01(): r'''Slots constraint measure attributes. ''' measure = abjad.Measure((3, 8), "c'8 d'8 e'8") assert pytest.raises(AttributeError, "measure.foo = 'bar'")
def add_string_music_to_score(score): """ Adds string music to score. """ # generate some pitch and rhythm information pitch_contour_reservoir = create_pitch_contour_reservoir() shadowed_contour_reservoir = shadow_pitch_contour_reservoir( pitch_contour_reservoir) durated_reservoir = durate_pitch_contour_reservoir( shadowed_contour_reservoir) # add six dotted-whole notes and the durated contours to each string voice for name, descents in durated_reservoir.items(): instrument_voice = score['%s Voice' % name] instrument_voice.extend("R1. R1. R1. R1. R1. R1.") for descent in descents: instrument_voice.extend(descent) # apply instrument-specific edits edit_first_violin_voice(score, durated_reservoir) edit_second_violin_voice(score, durated_reservoir) edit_viola_voice(score, durated_reservoir) edit_cello_voice(score, durated_reservoir) edit_bass_voice(score, durated_reservoir) # chop all string parts into 6/4 measures strings_staff_group = score['Strings Staff Group'] with abjad.ForbidUpdate(score): for voice in abjad.iterate(strings_staff_group).components(abjad.Voice): shards = abjad.mutate(voice[:]).split([(6, 4)], cyclic=True) for shard in shards: measure = abjad.Measure((6, 4), []) abjad.mutate(shard).wrap(measure)
def clean_up_rhythm(music, time_signature_pairs): # THIS IS HOW WE CLEAN UP THE RHYTHM shards = abjad.mutate(music[:]).split(time_signature_pairs) for i, shard in enumerate(shards): measure = abjad.Measure(time_signature_pairs[i]) abjad.mutate(shard).wrap(measure) return music
def make_drone_staff(anchor_tones, instrument_dict): staff = abj.Staff() for tone in anchor_tones: m = abj.Measure((1, 1), [abj.Note(tone % 12, abj.Duration(1, 1))]) tlt.override(staff).time_signature.stencil = False staff.append(m) staff.append(m) return staff
def test_scoretools_Measure___init___01(): r'''Initializes measure from empty input. ''' measure = abjad.Measure() assert measure.time_signature == abjad.TimeSignature((4, 4)) assert not len(measure)
def _notate( self, attach_tempos=True, attack_point_optimizer=None, grace_handler=None, ): import abjad voice = abjad.Voice() # generate the first q_target_measure = self.items[0] measure = abjad.Measure(q_target_measure.time_signature) for beat in q_target_measure.beats: measure.extend(beat.q_grid(beat.beatspan)) if attach_tempos: tempo = copy.copy(q_target_measure.tempo) #abjad.attach(tempo, measure) leaf = abjad.inspect(measure).get_leaf(0) abjad.attach(tempo, leaf) voice.append(measure) # generate the rest pairwise, comparing tempi pairs = abjad.sequence(self.items).nwise() for q_target_measure_one, q_target_measure_two in pairs: measure = abjad.Measure(q_target_measure_two.time_signature) for beat in q_target_measure_two.beats: measure.extend(beat.q_grid(beat.beatspan)) if ((q_target_measure_two.tempo != q_target_measure_one.tempo) and attach_tempos): tempo = copy.copy(q_target_measure_two.tempo) #abjad.attach(tempo, measure) leaf = abjad.inspect(measure).get_leaf(0) abjad.attach(tempo, leaf) voice.append(measure) # apply logical ties, pitches, grace containers self._notate_leaves( grace_handler=grace_handler, voice=voice, ) # partition logical ties in each measure for measure in voice: attack_point_optimizer(measure) return voice
def test_scoretools_Measure_append_01(): r'''Time signature does not automatically adjust. ''' measure = abjad.Measure((3, 4), "c' d' e'") measure.append('r') assert measure.is_overfull assert pytest.raises(Exception, 'f(measure)')
def test_scoretools_Measure_implicit_scaling_01(): r'''Measures with power-of-two time signature denominator contribute trivially to contents prolation. ''' measure = abjad.Measure((4, 4), abjad.Note("c'4") * 4) assert measure[0].written_duration == abjad.Duration(1, 4) assert abjad.inspect(measure[0]).get_duration() == abjad.Duration(1, 4)
def test_scoretools_Mutation_fuse_16(): r'''Fusing selection of only one measure returns measure unaltered. ''' measure = abjad.Measure((3, 8), "c'8 d'8 e'8") staff = abjad.Staff([measure]) new = abjad.mutate(staff[:]).fuse() assert new is measure
def test_scoretools_Measure_extend_01(): r'''Do not adjust time signature automatically. ''' measure = abjad.Measure((3, 4), "c' d' e'") measure.extend("f' g'") assert measure.is_overfull assert pytest.raises(Exception, 'f(measure)')
def test_scoretools_Measure_implicit_scaling_04(): r'''Measures with power-of-two time signature denominators and implicit scaling scale the duration of their contents. ''' measure = abjad.Measure((4, 5), "c'4 d'4 e'4 f'4") measure.implicit_scaling = True assert measure[0].written_duration == abjad.Duration(1, 4) assert abjad.inspect(measure[0]).get_duration() == abjad.Duration(1, 5)
def test_spannertools_Spanner__get_my_nth_leaf_01(): staff = abjad.Staff() staff.append(abjad.Measure((2, 8), "c'8 d'8")) staff.append(abjad.Measure((2, 8), "e'8 f'8")) leaves = abjad.select(staff).leaves() beam = abjad.Beam() abjad.attach(beam, leaves) assert format(staff) == abjad.String.normalize( r''' \new Staff { { % measure \time 2/8 c'8 [ d'8 } % measure { % measure e'8 f'8 ] } % measure } ''' ) selector = abjad.select().leaves() leaves = selector(staff) assert beam._get_my_nth_leaf(0) is leaves[0] assert beam._get_my_nth_leaf(1) is leaves[1] assert beam._get_my_nth_leaf(2) is leaves[2] assert beam._get_my_nth_leaf(3) is leaves[3] assert beam._get_my_nth_leaf(-1) is leaves[-1] assert beam._get_my_nth_leaf(-2) is leaves[-2] assert beam._get_my_nth_leaf(-3) is leaves[-3] assert beam._get_my_nth_leaf(-4) is leaves[-4] assert pytest.raises(IndexError, 'beam._get_my_nth_leaf(99)') assert pytest.raises(IndexError, 'beam._get_my_nth_leaf(-99)')
def test_scoretools_Measure_empty_01(): measure = abjad.Measure((4, 4), []) assert str(measure) == 'Measure((4, 4))' assert pytest.raises(abjad.UnderfullContainerError, 'format(measure)') assert len(measure) == 0 assert measure._get_preprolated_duration() == 0 assert abjad.inspect(measure).get_duration() == 0 assert not abjad.inspect(measure).is_well_formed()
def test_scoretools_Measure_duration_03(): r'''Improperly filled measure with power-of-two time signature. ''' measure = abjad.Measure((3, 8), "c'8 d'8 e'8 f'8") assert pytest.raises(abjad.OverfullContainerError, 'format(measure)') assert measure._get_contents_duration() == abjad.Duration(4, 8) assert measure._get_preprolated_duration() == abjad.Duration(4, 8) assert abjad.inspect(measure).get_duration() == abjad.Duration(4, 8)
def test_scoretools_Inspection_get_duration_02(): voice = abjad.Voice([ abjad.Measure((2, 12), "c'8 d'8", implicit_scaling=True), abjad.Measure((2, 8), "c'8 d'8") ]) leaves = abjad.select(voice).leaves() beam = abjad.Beam() abjad.attach(beam, leaves) crescendo = abjad.Hairpin('<') abjad.attach(crescendo, voice[0][:]) diminuendo = abjad.Hairpin('>') abjad.attach(diminuendo, voice[1][:]) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 2/12 \scaleDurations #'(2 . 3) { c'8 [ \< d'8 \! } } % measure { % measure \time 2/8 c'8 \> d'8 ] \! } % measure } ''') assert abjad.inspect(beam).get_duration() == abjad.Duration(5, 12) assert abjad.inspect(crescendo).get_duration() == abjad.Duration(2, 12) assert abjad.inspect(diminuendo).get_duration() == abjad.Duration(2, 8)
def test_scoretools_Mutation_scale_06(): r'''Doubles measures. ''' voice = abjad.Voice() voice.append(abjad.Measure((2, 8), "c'8 d'8")) voice.append(abjad.Measure((2, 8), "e'8 f'8")) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 2/8 c'8 d'8 } % measure { % measure e'8 f'8 } % measure } ''') abjad.mutate(voice).scale(abjad.Multiplier(2)) assert format(voice) == abjad.String.normalize(r''' \new Voice { { % measure \time 2/4 c'4 d'4 } % measure { % measure e'4 f'4 } % measure } ''') assert abjad.inspect(voice).is_well_formed()
def test_scoretools_Measure___delitem___07(): r'''Nonnegative indices work. Do NOT automatically update time signature. ''' measure = abjad.Measure((4, 8), "c'8 c' c' c'") del (measure[:1]) assert len(measure) == 3 assert abjad.inspect(measure).get_indicator(abjad.TimeSignature) assert not abjad.inspect(measure).is_well_formed()