[docs]classMutwoPitchToHEJIAbjadPitch(MutwoPitchToAbjadPitch):"""Convert Mutwo :obj:`~mutwo.ext.parameters.pitches.JustIntonationPitch` objects to Abjad Pitch objects. :param reference_pitch: The reference pitch (1/1). Should be a diatonic pitch name (see :const:`~mutwo.ext.parameters.pitches_constants.DIATONIC_PITCH_CLASS_CONTAINER`) in English nomenclature. For any other reference pitch than 'c', Lilyponds midi rendering for pitches with the diatonic pitch 'c' will be slightly out of tune (because the first value of :arg:`global_scale` always have to be 0). :type reference_pitch: str, optional :param prime_to_heji_accidental_name: Mapping of a prime number to a string which indicates the respective prime number in the resulting accidental name. See :const:`mutwo.ekmelily_converters.configurations.DEFAULT_PRIME_TO_HEJI_ACCIDENTAL_NAME_DICT` for the default mapping. :type prime_to_heji_accidental_name: dict[int, str], optional :param otonality_indicator: String which indicates that the respective prime alteration is otonal. See :const:`mutwo.ekmelily_converters.configurations.DEFAULT_OTONALITY_INDICATOR` for the default value. :type otonality_indicator: str, optional :param utonality_indicator: String which indicates that the respective prime alteration is utonal. See :const:`mutwo.ekmelily_converters.configurations.DEFAULT_OTONALITY_INDICATOR` for the default value. :type utonality_indicator: str, optional :param exponent_to_exponent_indicator: Function to convert the exponent of a prime number to string which indicates the respective exponent. See :func:`mutwo.ekmelily_converters.configurations.DEFAULT_EXPONENT_TO_EXPONENT_INDICATOR` for the default function. :type exponent_to_exponent_indicator: typing.Callable[[int], str], optional :param tempered_pitch_indicator: String which indicates that the respective accidental is tempered (12 EDO). See :const:`mutwo.ekmelily_converters.configurations.DEFAULT_TEMPERED_PITCH_INDICATOR` for the default value. :type tempered_pitch_indicator: str, optional The resulting Abjad pitches are expected to be used in combination with tuning files that are generated by :class:`~mutwo.converters.frontends.ekmelily.HEJIEkmelilyTuningFileConverter` and with the Lilypond extension `Ekmelily <http://www.ekmelic-music.org/en/extra/ekmelily.htm>`_. You can find pre-generated tuning files `here <https://github.com/levinericzimmermann/ekme-heji.ily>`_. **Example:** >>> from mutwo.ext.parameters import pitches >>> from mutwo.converters.frontends import abjad >>> my_ji_pitch = pitches.JustIntonationPitch('5/4') >>> converter_on_a = abjad.MutwoPitchToHEJIAbjadPitch(reference_pitch='a') >>> converter_on_c = abjad.MutwoPitchToHEJIAbjadPitch(reference_pitch='c') >>> converter_on_a.convert(my_ji_pitch) NamedPitch("csoaa''") >>> converter_on_c.convert(my_ji_pitch) NamedPitch("eoaa'") """class_HEJIAccidental(object):"""Fake abjad accidental Only for internal usage within the :class:`MutwoPitchToHEJIAbjadPitch`. """def__init__(self,accidental:str):self._accidental=accidentaldef__str__(self)->str:returnself._accidental# necessary attributes, although they# won't be used at allsemitones=0arrow=Nonedef__init__(self,reference_pitch:str="a",prime_to_heji_accidental_name:typing.Optional[dict[int,str]]=None,otonality_indicator:str=None,utonality_indicator:str=None,exponent_to_exponent_indicator:typing.Callable[[int],str]=None,tempered_pitch_indicator:str=None,):# set default valuesifprime_to_heji_accidental_nameisNone:prime_to_heji_accidental_name=(ekmelily_converters.configurations.DEFAULT_PRIME_TO_HEJI_ACCIDENTAL_NAME_DICT)ifotonality_indicatorisNone:otonality_indicator=(ekmelily_converters.configurations.DEFAULT_OTONALITY_INDICATOR)ifutonality_indicatorisNone:utonality_indicator=(ekmelily_converters.configurations.DEFAULT_UTONALITY_INDICATOR)ifexponent_to_exponent_indicatorisNone:exponent_to_exponent_indicator=(ekmelily_converters.configurations.DEFAULT_EXPONENT_TO_EXPONENT_INDICATOR)iftempered_pitch_indicatorisNone:tempered_pitch_indicator=(ekmelily_converters.configurations.DEFAULT_TEMPERED_PITCH_INDICATOR)self._reference_pitch=reference_pitchself._otonality_indicator=otonality_indicatorself._utonality_indicator=utonality_indicatorself._exponent_to_exponent_indicator=exponent_to_exponent_indicatorself._tempered_pitch_indicator=tempered_pitch_indicatorself._reference_index=(music_parameters.constants.DIATONIC_PITCH_CLASS_CONTAINER[reference_pitch].index)self._prime_to_heji_accidental_name=prime_to_heji_accidental_namedef_find_western_octave_for_just_intonation_pitch(self,pitch_to_convert:music_parameters.JustIntonationPitch,closest_pythagorean_pitch_name:str,)->int:octave=pitch_to_convert.octave+4closest_pythagorean_pitch_index=(music_parameters.constants.DIATONIC_PITCH_CLASS_CONTAINER[closest_pythagorean_pitch_name[0]].index)ifclosest_pythagorean_pitch_index<self._reference_index:octave+=1pitch_as_western_pitch=music_parameters.WesternPitch(closest_pythagorean_pitch_name[0],octave)reference_pitch_as_western_pitch=music_parameters.WesternPitch(self._reference_pitch,4)expected_difference_in_cents=pitch_to_convert.intervalwhile(expected_difference_in_cents-((pitch_as_western_pitch.midi_pitch_number-reference_pitch_as_western_pitch.midi_pitch_number)*100)>300):pitch_as_western_pitch.octave+=1while(expected_difference_in_cents-((pitch_as_western_pitch.midi_pitch_number-reference_pitch_as_western_pitch.midi_pitch_number)*100)<-300):pitch_as_western_pitch.octave-=1returnpitch_as_western_pitch.octavedef_find_heji_accidental_for_just_intonation_pitch(self,pitch_to_convert:music_parameters.JustIntonationPitch,abjad_pitch_class:abjad.NamedPitchClass,):# find additional commasaccidental_part_list=[str(abjad_pitch_class.accidental)]prime_to_exponent=(pitch_to_convert.helmholtz_ellis_just_intonation_notation_commas.prime_to_exponent_dict)forprimeinsorted(prime_to_exponent.keys()):exponent=prime_to_exponent[prime]ifexponent!=0:tonality=(self._otonality_indicatorifexponent>0elseself._utonality_indicator)heji_accidental_name=self._prime_to_heji_accidental_name[prime]exponent_indicator=self._exponent_to_exponent_indicator(abs(exponent)-1)accidental_part_list.append(f"{tonality}{heji_accidental_name}{exponent_indicator}")accidental=self._HEJIAccidental("".join(accidental_part_list))returnaccidentaldef_convert_just_intonation_pitch(self,pitch_to_convert:music_parameters.JustIntonationPitch,)->abjad.Pitch:# find pythagorean pitchclosest_pythagorean_pitch_name=(pitch_to_convert.get_closest_pythagorean_pitch_name(self._reference_pitch))abjad_pitch_class=abjad.NamedPitchClass(closest_pythagorean_pitch_name)accidental=self._find_heji_accidental_for_just_intonation_pitch(pitch_to_convert,abjad_pitch_class)abjad_pitch_class._accidental=accidentaloctave=self._find_western_octave_for_just_intonation_pitch(pitch_to_convert,closest_pythagorean_pitch_name)abjad_pitch=abjad.NamedPitch(octave=octave)abjad_pitch._pitch_class=abjad_pitch_classreturnabjad_pitch