def _initialize_by_symbolic_string(self, symbolic_string): groups = self._symbolic_string_regex.match(symbolic_string).groups() accidental, roman_numeral = groups accidental = pitchtools.Accidental(accidental) roman_numeral = roman_numeral.upper() try: number = self._roman_numeral_string_to_scale_degree_number[ roman_numeral] except KeyError: number = int(roman_numeral) return accidental, number
def accidental(self): r'''Accidental of numbered pitch-class. :: >>> pitchtools.NumberedPitchClass(1).accidental Accidental('s') Returns accidental. ''' from abjad.tools import pitchtools return pitchtools.Accidental(self.alteration_in_semitones)
def accidental(self): r'''Gets accidental. .. container:: example >>> abjad.NamedPitchClass('cs').accidental Accidental('sharp') Returns accidental. ''' from abjad.tools import pitchtools return pitchtools.Accidental(self._alteration)
def accidental(self): r'''Accidental of named pitch. :: >>> NamedPitch("cs''").accidental Accidental('s') Returns accidental. ''' from abjad.tools import pitchtools return pitchtools.Accidental(self._alteration_in_semitones)
def apply_accidental(self, accidental): r'''Applies accidental to scale degree. :: >>> scale_degree.apply_accidental('ff') ScaleDegree('flat', 4) Returns new scale degree. ''' accidental = pitchtools.Accidental(accidental) new_accidental = self.accidental + accidental return type(self)(new_accidental, self.number)
def apply_accidental(self, accidental=None): '''Applies `accidental` to numbered pitch-class. :: >>> pitchtools.NumberedPitchClass(1).apply_accidental('flat') NumberedPitchClass(0) Returns new numbered pitch-class. ''' from abjad.tools import pitchtools accidental = pitchtools.Accidental(accidental) semitones = self.pitch_class_number + accidental.semitones return type(self)(semitones)
def apply_accidental(self, accidental): r'''Applies `accidental` to named pitch-class. :: >>> pitchtools.NamedPitchClass('cs').apply_accidental('qs') NamedPitchClass('ctqs') Returns new named pitch-class. ''' from abjad.tools import pitchtools accidental = pitchtools.Accidental(accidental) new_accidental = self.accidental + accidental new_name = self.diatonic_pitch_class_name + new_accidental.abbreviation return type(self)(new_name)
def apply_accidental(self, accidental=None): '''Applies `accidental` to named pitch. :: >>> NamedPitch("cs''").apply_accidental('s') NamedPitch("css''") Returns new named pitch. ''' from abjad.tools import pitchtools accidental = pitchtools.Accidental(accidental) new_accidental = self.accidental + accidental new_name = self.diatonic_pitch_class_name new_name += new_accidental.abbreviation return type(self)(new_name, self.octave_number)
def spell_pitch_number(pitch_number, diatonic_pitch_class_name): '''Spell `pitch_number` according to `diatonic_pitch_class_name`: :: >>> pitchtools.spell_pitch_number(14, 'c') (Accidental('ss'), 5) Returns accidental / octave-number pair. ''' from abjad.tools import pitchtools # check input if not isinstance(pitch_number, (int, float)): raise TypeError if not isinstance(diatonic_pitch_class_name, str): raise TypeError if not diatonic_pitch_class_name in ['c', 'd', 'e', 'f', 'g', 'a', 'b']: raise ValueError # find accidental semitones pc = \ pitchtools.PitchClass._diatonic_pitch_class_name_to_pitch_class_number[ diatonic_pitch_class_name] nearest_neighbor = pitchtools.transpose_pitch_class_number_to_pitch_number_neighbor( pitch_number, pc) semitones = pitch_number - nearest_neighbor # find accidental alphabetic string abbreviation = \ pitchtools.Accidental._semitones_to_abbreviation[ semitones] accidental = pitchtools.Accidental(abbreviation) # find octave octave_number = int(math.floor((pitch_number - semitones) / 12)) + 4 # return accidental and octave return accidental, octave_number
def _transpose_enharmonically(pitch_a, pitch_b, pitch_c): r'''Transpose `pitch_c` by the distance between `pitch_b` and `pitch_a`. This function was reverse-engineered from LilyPond's source code. Returns named pitch. ''' def normalize_alteration(step, alteration): while 2. < alteration: alteration -= step_size(step) step += 1. while alteration < -2.: step -= 1. alteration += step_size(step) return step, alteration def normalize_octave(octave, step): normalized_step = step % len(scale) octave += (step - normalized_step) / len(scale) return octave, normalized_step def step_size(step): normalized_step = step % len(scale) if normalized_step == 6: return 1. # b to c return scale[normalized_step + 1] - scale[normalized_step] if not isinstance(pitch_a, pitchtools.NamedPitch): pitch_a = pitchtools.NamedPitch(pitch_a) if not isinstance(pitch_b, pitchtools.NamedPitch): pitch_b = pitchtools.NamedPitch(pitch_b) if not isinstance(pitch_c, pitchtools.NamedPitch): pitch_c = pitchtools.NamedPitch(pitch_c) scale = [0., 2., 4., 5., 7., 9., 11.] a_oct, a_step, a_alt = pitch_a.octave_number, \ pitch_a.diatonic_pitch_class_number, pitch_a.accidental.semitones b_oct, b_step, b_alt = pitch_b.octave_number, \ pitch_b.diatonic_pitch_class_number, pitch_b.accidental.semitones c_oct, c_step, c_alt = pitch_c.octave_number, \ pitch_c.diatonic_pitch_class_number, pitch_c.accidental.semitones d_oct, d_step, d_alt, d_tones = b_oct - a_oct, b_step - a_step, \ b_alt - a_alt, float(pitch_b) - float(pitch_a) tmp_alt = float(pitch_c) + d_tones # print 'TMP_ALT: %f' % tmp_alt new_oct = c_oct + d_oct new_step = c_step + d_step new_alt = c_alt # print 'NEW:', new_oct, new_step, new_alt new_step, new_alt = normalize_alteration(new_step, new_alt) new_oct, new_step = normalize_octave(new_oct, new_step) # print 'NEW(norm):', new_oct, new_step, new_alt octave_ticks = str(pitchtools.Octave(new_oct)) pitch_class_name = \ pitchtools.PitchClass._diatonic_pitch_class_number_to_diatonic_pitch_class_name[ new_step % 7] #pitch_class_name = str(pitchtools.NamedDiatonicPitchClass( # int(new_step))) accidental = str(pitchtools.Accidental(new_alt)) tmp_pitch = pitchtools.NamedPitch(pitch_class_name + accidental + octave_ticks) # print 'TMP(pitch): %r' % tmp_pitch new_alt += tmp_alt - float(tmp_pitch) # print 'NEW(alt): %f' % new_alt new_step, new_alt = normalize_alteration(new_step, new_alt) new_oct, new_step = normalize_octave(new_oct, new_step) # print 'NEW(norm):', new_oct, new_step, new_alt octave_ticks = str(pitchtools.Octave(new_oct)) #pitch_class_name = str(pitchtools.NamedDiatonicPitchClass( # int(new_step))) pitch_class_name = \ pitchtools.PitchClass._diatonic_pitch_class_number_to_diatonic_pitch_class_name[ new_step % 7] accidental = str(pitchtools.Accidental(new_alt)) return pitchtools.NamedPitch(pitch_class_name + accidental + octave_ticks)
def _initialize_by_number(self, number): accidental = pitchtools.Accidental(None) return accidental, number
def _initialize_by_accidental_and_number(self, accidental, number): accidental = pitchtools.Accidental(accidental) return accidental, number
def _apply_accidental(self, accidental=None): from abjad.tools import pitchtools accidental = pitchtools.Accidental(accidental) new_accidental = self.accidental + accidental new_name = self._get_diatonic_pitch_class_name() + str(new_accidental) return type(self)(new_name)
def _apply_accidental(self, accidental=None): from abjad.tools import pitchtools accidental = pitchtools.Accidental(accidental) semitones = self.number + accidental.semitones return type(self)(semitones)