def test_Quantizer___call___11(): quantizer = nauert.Quantizer() durations = [250, 1250, 750, 1000, 250] pitches = range(5) all_attachments = [(x,) for x in pitches] pitches = [(x, x + 7) for x in pitches] q_event_sequence = nauert.QEventSequence.from_millisecond_pitch_attachment_tuples( tuple(zip(durations, pitches, all_attachments)) ) time_signature = abjad.TimeSignature((7, 8)) search_tree = nauert.UnweightedSearchTree( definition={ 2: {2: None, 3: None}, 3: {2: None, 3: None}, 5: {2: None}, 7: {2: None}, 13: None, } ) q_schema = nauert.MeasurewiseQSchema( time_signature=time_signature, search_tree=search_tree, use_full_measure=True, ) grace_handler = nauert.ConcatenatingGraceHandler( replace_rest_with_final_grace_note=True ) attack_point_optimizer = nauert.MeasurewiseAttackPointOptimizer() result = quantizer( q_event_sequence, q_schema=q_schema, grace_handler=grace_handler, attack_point_optimizer=attack_point_optimizer, ) staff = abjad.Staff([result]) string = abjad.lilypond(staff) assert string == abjad.string.normalize( r""" \new Staff { \new Voice { { \tempo 4=60 \time 7/8 <c' g'>16 <cs' af'>16 ~ <cs' af'>4 <d' a'>8. <ef' bf'>16 ~ <ef' bf'>8. <e' b'>16 } } } """ ), print(string) assert_q_event_attachments(result, all_attachments)
def test_Quantizer___call___15(): durations = [1000, 1000, 1000, 400, 50, 50] pitches = range(len(durations)) quantizer = nauert.Quantizer() q_event_sequence = nauert.QEventSequence.from_millisecond_pitch_pairs( tuple(zip(durations, pitches)) ) search_tree = nauert.UnweightedSearchTree() attack_point_optimizer = nauert.MeasurewiseAttackPointOptimizer() q_schema = nauert.MeasurewiseQSchema( search_tree=search_tree, time_signature=(7, 8), use_full_measure=True ) result = quantizer( q_event_sequence, q_schema=q_schema, attach_tempos=True, attack_point_optimizer=attack_point_optimizer, ) staff = abjad.Staff([result]) string = abjad.lilypond(staff) assert string == abjad.string.normalize( r""" \new Staff { \new Voice { { \tempo 4=60 \time 7/8 c'4 cs'8 ~ cs'8 d'8 ~ d'8 \afterGrace ef'8 { e'16 f'16 } } } } """ ), print(string)
def test_Quantizer___call___09(): q_schema = nauert.BeatwiseQSchema( {"search_tree": nauert.UnweightedSearchTree({2: None})}, {"search_tree": nauert.UnweightedSearchTree({3: None})}, {"search_tree": nauert.UnweightedSearchTree({5: None})}, {"search_tree": nauert.UnweightedSearchTree({7: None})}, ) milliseconds = [250, 250, 250, 250] * 4 q_events = nauert.QEventSequence.from_millisecond_durations(milliseconds) attack_point_optimizer = nauert.MeasurewiseAttackPointOptimizer() quantizer = nauert.Quantizer() try: quantizer( q_events, q_schema=q_schema, attack_point_optimizer=attack_point_optimizer, ) assert False except TypeError as error: assert ( str(error) == "BeatwiseQTarget is not supposed to be used together with MeasurewiseAttackPointOptimizer." )
def test_DistanceHeuristic___call___01(): heuristic = nauert.DistanceHeuristic() q_event_a = nauert.PitchedQEvent(250, [0, 1]) q_event_b = nauert.SilentQEvent(500) q_event_c = nauert.PitchedQEvent(750, [3, 7]) proxy_a = nauert.QEventProxy(q_event_a, 0.25) proxy_b = nauert.QEventProxy(q_event_b, 0.5) proxy_c = nauert.QEventProxy(q_event_c, 0.75) definition = {2: {2: None}, 3: None, 5: None} search_tree = nauert.UnweightedSearchTree(definition) job = nauert.QuantizationJob(1, search_tree, [proxy_a, proxy_b, proxy_c]) job() q_target_beat = nauert.QTargetBeat() q_target_beat._q_grids = job.q_grids q_target_beats = heuristic((q_target_beat,)) q_target_beat = q_target_beats[0] q_grid = q_target_beat.q_grid assert q_grid.distance == 0 rtm = q_grid.rtm_format assert rtm == "(1 ((1 (1 1)) (1 (1 1))))"
class TaggedSimultaneousEventToAbjadScoreConverter(converters_abc.Converter): search_tree = nauert.UnweightedSearchTree(definition={ 2: { 2: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, 3: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, }, 3: { 2: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, 3: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, }, }, ) def __init__( self, time_signatures: typing.Sequence[abjad.TimeSignature] = ( abjad.TimeSignature((4, 4)), ), tempo_envelope: expenvelope.Envelope = None, ): self._time_signatures = time_signatures self._tempo_envelope = tempo_envelope self._instrument_id_to_sequential_event_to_abjad_voice_converter = ( self. _make_instrument_id_to_sequential_event_to_abjad_voice_converter()) @staticmethod def _prepare_percussion_sequential_event( sequential_event: basic.SequentialEvent[music.NoteLike], ) -> basic.SequentialEvent: # absolute_times = sequential_event.absolute_times # for start, event in zip(reversed(absolute_times), reversed(sequential_event)): # if event.duration > fractions.Fraction(1, 4) and event.pitch_or_pitches: # sequential_event.squash_in( # start + fractions.Fraction(1, 4), # music.NoteLike([], event.duration - fractions.Fraction(1, 4)), # ) return sequential_event @staticmethod def _prepare_voice(voice: abjad.Voice, instrument_id: str): # Preparation that applies to all voices first_leaf = abjad.get.leaf(voice, 0) abjad.attach( abjad.LilyPondLiteral( "\\set Staff.instrumentName = \\markup { \\teeny {" + instruments. INSTRUMENT_ID_TO_LONG_INSTRUMENT_NAME[instrument_id] + " } }"), first_leaf, ) abjad.attach( abjad.LilyPondLiteral( "\\set Staff.shortInstrumentName = \\markup { \\teeny { " + instruments. INSTRUMENT_ID_TO_SHORT_INSTRUMENT_NAME[instrument_id] + " } }"), first_leaf, ) abjad.attach( abjad.LilyPondLiteral( "\\override Staff.TimeSignature.style = #'single-digit"), first_leaf, ) abjad.attach( abjad.LilyPondLiteral( "\\set Score.proportionalNotationDuration = #(ly:make-moment 1/8)" ), first_leaf, ) abjad.attach( abjad.LilyPondLiteral( "\\override Score.SpacingSpanner.strict-note-spacing = ##t"), first_leaf, ) abjad.attach(abjad.LilyPondLiteral("\\hide Staff.BarLine"), first_leaf) last_leaf = abjad.get.leaf(voice, -1) abjad.attach(abjad.BarLine("|.", format_slot="absolute_after"), last_leaf) abjad.attach( abjad.LilyPondLiteral(r"\undo \hide Staff.BarLine", format_slot="absolute_after"), last_leaf, ) @staticmethod def _prepare_duration_line_voice(voice: abjad.Voice): # Preparation that applies to voices with duration line notation first_leaf = abjad.get.leaf(voice, 0) abjad.attach(abjad.LilyPondLiteral('\\accidentalStyle "dodecaphonic"'), first_leaf) @staticmethod def _prepare_drone_voice(voice: abjad.Voice): # Preparation that applies to drone first_leaf = abjad.get.leaf(voice, 0) abjad.attach(abjad.Clef("bass"), first_leaf) @staticmethod def _prepare_percussion_voice(voice: abjad.Voice): # Preparation that applies to drone first_leaf = abjad.get.leaf(voice, 0) abjad.attach(abjad.Clef("percussion"), first_leaf) def _make_sequential_event_to_abjad_voice_converter_for_sustaining_instrument( self, ) -> mutwo_abjad.SequentialEventToAbjadVoiceConverter: return mutwo_abjad.SequentialEventToAbjadVoiceConverter( mutwo_abjad. SequentialEventToDurationLineBasedQuantizedAbjadContainerConverter( self._time_signatures, tempo_envelope=self._tempo_envelope, # search_tree=self.search_tree, ), mutwo_pitch_to_abjad_pitch_converter=mutwo_abjad. MutwoPitchToHEJIAbjadPitchConverter(), ) # def _make_sequential_event_to_abjad_voice_converter_for_percussion_instrument( # self, # ) -> mutwo_abjad.SequentialEventToAbjadVoiceConverter: # return mutwo_abjad.SequentialEventToAbjadVoiceConverter( # mutwo_abjad.SequentialEventToQuantizedAbjadContainerConverter( # self._time_signatures, # tempo_envelope=self._tempo_envelope, # search_tree=self.search_tree, # ), # mutwo_pitch_to_abjad_pitch_converter=ColotomicPitchToMutwoPitchConverter(), # ) def _make_instrument_id_to_sequential_event_to_abjad_voice_converter( self, ) -> typing.Dict[str, mutwo_abjad.SequentialEventToAbjadVoiceConverter]: sequential_event_to_abjad_voice_converter_for_sustaining_instrument = ( self. _make_sequential_event_to_abjad_voice_converter_for_sustaining_instrument( )) # sequential_event_to_abjad_voice_converter_for_percussion_instrument = ( # self._make_sequential_event_to_abjad_voice_converter_for_percussion_instrument() # ) return { instruments.ID_SUS0: sequential_event_to_abjad_voice_converter_for_sustaining_instrument, instruments.ID_SUS1: sequential_event_to_abjad_voice_converter_for_sustaining_instrument, instruments.ID_SUS2: sequential_event_to_abjad_voice_converter_for_sustaining_instrument, instruments.ID_DRONE: sequential_event_to_abjad_voice_converter_for_sustaining_instrument, # instruments.ID_PERCUSSIVE: sequential_event_to_abjad_voice_converter_for_percussion_instrument, # instruments.ID_NOISE: sequential_event_to_abjad_voice_converter_for_percussion_instrument, } def convert( self, tagged_simultaneous_event: ot2_basic.TaggedSimultaneousEvent[ basic.SimultaneousEvent[basic.SequentialEvent[music.NoteLike]]], ) -> abjad.Score: staff_group = abjad.StaffGroup([]) duration = tagged_simultaneous_event.duration for instrument_id in sorted( tagged_simultaneous_event.tag_to_event_index.keys(), key=lambda tag: tagged_simultaneous_event.tag_to_event_index[ tag], ): converter = self._instrument_id_to_sequential_event_to_abjad_voice_converter[ instrument_id] simultaneous_event = tagged_simultaneous_event[instrument_id] staff = abjad.Staff([], simultaneous=True) # staff.remove_commands.append("Time_signature_engraver") for sequential_event in simultaneous_event: difference = duration - sequential_event.duration if difference: sequential_event.append(music.NoteLike([], difference)) if instrument_id == instruments.ID_PERCUSSIVE: sequential_event = self._prepare_percussion_sequential_event( sequential_event) abjad_voice = converter.convert(sequential_event) if instrument_id not in ( instruments.ID_PERCUSSIVE, instruments.ID_NOISE, ): self._prepare_duration_line_voice(abjad_voice) if instrument_id == instruments.ID_PERCUSSIVE: self._prepare_percussion_voice(abjad_voice) elif instrument_id == instruments.ID_DRONE: self._prepare_drone_voice(abjad_voice) elif instrument_id == instruments.ID_NOISE: self._prepare_percussion_voice(abjad_voice) self._prepare_voice(abjad_voice, instrument_id) staff.append(abjad_voice) staff_group.append(staff) abjad_score = abjad.Score([staff_group]) return abjad_score
def illustrate_cengkoks(applied_melodies): import abjad from abjadext import nauert from mutwo.converters.frontends import abjad as mutwo_abjad time_signatures = tuple( abjad.TimeSignature((int(event.duration * 2), 2)) for event in applied_cantus_firmus.APPLIED_CANTUS_FIRMUS) search_tree = nauert.UnweightedSearchTree(definition={ 2: { 2: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, 3: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, }, 3: { 2: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, 3: { 2: { 2: { 2: { 2: None, }, }, }, 3: { 2: { 2: { 2: { 2: None, }, }, }, } }, }, }, ) abjad_converter = mutwo_abjad.SequentialEventToAbjadVoiceConverter( mutwo_abjad.SequentialEventToQuantizedAbjadContainerConverter( time_signatures, search_tree=search_tree), mutwo_pitch_to_abjad_pitch_converter=mutwo_abjad. MutwoPitchToHEJIAbjadPitchConverter(), ) abjad_voice = abjad_converter.convert(applied_melodies) abjad.attach(abjad.LilyPondLiteral('\\accidentalStyle "dodecaphonic"'), abjad_voice[0][0]) abjad.attach( abjad.LilyPondLiteral( "\\override Staff.TimeSignature.style = #'single-digit"), abjad_voice[0][0], ) abjad_score = abjad.Score([abjad.Staff([abjad_voice])]) lilypond_file = abjad.LilyPondFile(items=[abjad_score], includes=["ekme-heji-ref-c.ily"]) abjad.persist.as_pdf(lilypond_file, "builds/applied_cengkoks.pdf")
def test_Quantizer___call___08(): q_schema = nauert.BeatwiseQSchema( {"search_tree": nauert.UnweightedSearchTree({2: None})}, {"search_tree": nauert.UnweightedSearchTree({3: None})}, {"search_tree": nauert.UnweightedSearchTree({5: None})}, {"search_tree": nauert.UnweightedSearchTree({7: None})}, ) milliseconds = [250, 250, 250, 250] * 4 q_events = nauert.QEventSequence.from_millisecond_durations(milliseconds) attack_point_optimizer = nauert.NaiveAttackPointOptimizer() quantizer = nauert.Quantizer() result = quantizer( q_events, q_schema=q_schema, attack_point_optimizer=attack_point_optimizer, ) staff = abjad.Staff([result], lilypond_type="RhythmicStaff") score = abjad.Score([staff]) assert abjad.lilypond(score) == abjad.string.normalize( r""" \new Score << \new RhythmicStaff { \new Voice { \grace { c'16 } \tempo 4=60 c'8 \grace { c'16 } c'8 \times 2/3 { c'8 \grace { c'16 } c'8 c'8 } \times 4/5 { c'16 c'16 c'8 c'16 } \times 4/7 { c'8 c'16 c'8 c'8 } } } >> """ ), print(abjad.lilypond(score))
}, }, ) """ SEARCH_TREE = nauert.UnweightedSearchTree(definition={ 2: { 2: { 2: { 2: None, }, 3: { 2: None, }, }, 3: { 2: None, 3: None, }, }, 3: { 2: { 2: None, 3: None, }, 3: { 2: None, 3: None, }, }, }, )