def _midi_pitch_to_harte_class(self): """ Get the harte class from the midi pitch of our Pitch object :return: Harte class from the midi pitch of our Pitch object >>> Pitch(69)._midi_pitch_to_harte_class() ['G##', 'A', 'Bbb'] """ return PitchClass(self.midi_pitch % 12).harte_pitch_class
def _midi_pitch_to_pitch_class(self): """ Get the pitch class from the midi pitch of our Pitch object :return: Pitch class from the midi pitch of our Pitch object >>> str(Pitch(69)._midi_pitch_to_pitch_class()) 'A' """ return PitchClass(self.midi_pitch % 12)
def add_tab_block(self, tab_block_str: [Line]): """ Process the content of a tab block (6 subsequent lines of LineType Tablature), add chords to self.chords :param tab_block_str: Six subsequent Lines of the LineType Tablature """ smallest_line_length = min( [len(block_line_str) for block_line_str in tab_block_str]) for chord_x in range(0, smallest_line_length): # Read all six strings together per x-coordinate finger_list = [] for tab_block_line_str in reversed(tab_block_str): finger_list.append(tab_block_line_str[chord_x]) fingers = ''.join(finger_list) if re.match(r'[x0-9]{6}', fingers) and fingers != 'xxxxxx': # A chord sounds at this x position! fingering = Fingering('1', fingers[0], fingers[1], fingers[2], fingers[3], fingers[4], fingers[5]) fingering_chroma = fingering.get_extended_chroma_vector() # Find nearest chord from vocabulary smallest_distance = 2 best_matching_chord_str = 'X' for chord_template in ALL_CHORDS_LIST.chord_templates: cosine_distance = ssd.cosine(fingering_chroma[:12], chord_template.chroma_list) if cosine_distance < smallest_distance: smallest_distance = cosine_distance best_matching_chord_str = str( PitchClass( chord_template.key))[0] + chord_template.mode chord = Chord.from_common_tab_notation_string( best_matching_chord_str) # Fix bass note bass_note = PitchClass(fingering_chroma[12]) bass_interval = Interval.from_pitch_class_distances( chord.root_note, bass_note) chord.bass_degree = bass_interval # Add to self.chords self.chords.append((chord_x, chord))
def add_note(self, note: pretty_midi.Note): """ Add a Note to the Event :param note: The Note we add to the Event """ self.notes.append(note) self.pitches.add(Pitch(note.pitch)) pitch_class_nr = note.pitch % 12 self.pitch_classes.add(PitchClass(pitch_class_nr)) self.chroma[pitch_class_nr] += (self._note_duration_ratio_in_event(note) * note.velocity)
def find_most_likely_chord(self, chord_vocabulary: ChordVocabulary) -> Tuple[ChordAnnotationItem, float]: """ Find the chord to which the chroma of this event matches best. :param chord_vocabulary: List of all chords for classification """ best_matching_chord_score = -2.99 best_matching_chord_str = 'X' best_key_note_weight = 0 for chord_template in chord_vocabulary.chord_templates: chord_score = self._score_compared_to_template(chord_template) if chord_score > best_matching_chord_score or (chord_score == best_matching_chord_score and self.chroma[chord_template.key] > best_key_note_weight): best_matching_chord_score = chord_score best_matching_chord_str = str(PitchClass(chord_template.key)) + chord_template.mode best_key_note_weight = self.chroma[chord_template.key] if best_matching_chord_str == 'X': most_likely_chord = None else: most_likely_chord = Chord.from_common_tab_notation_string(best_matching_chord_str) return ChordAnnotationItem(self.start_time, self.end_time, most_likely_chord), best_matching_chord_score
def __init__(self, chord_vocabulary: ChordVocabulary): self.alphabet_list = ['N'] + [ str(PitchClass(chord_template.key)) + chord_template.mode for chord_template in chord_vocabulary.chord_templates ] self.chord_vocabulary_name = chord_vocabulary.name