concertina_helper.notes_on_layout

 1from __future__ import annotations
 2from dataclasses import dataclass
 3from collections.abc import Iterable
 4
 5from .layouts.bisonoric import BisonoricLayout, AnnotatedBisonoricFingering
 6from .finger_finder import find_best_fingerings
 7from .penalties import PenaltyFunction
 8from .type_defs import Annotation
 9
10
11@dataclass
12class NotesOnLayout:
13    '''
14    Represents a sequence of notes on a particular layout
15    '''
16    notes: Iterable[Annotation]
17    layout: BisonoricLayout
18
19    def get_all_fingerings(self) -> \
20            Iterable[tuple[Annotation, set[AnnotatedBisonoricFingering]]]:
21        '''
22        For each note in the tune, returns all possible fingerings.
23        '''
24        return [
25            (
26                annotation,
27                {
28                    AnnotatedBisonoricFingering(
29                        fingering=f,
30                        annotation=annotation)
31                    for f in self.layout.get_fingerings(annotation.pitch)
32                }
33            )
34            for annotation in self.notes
35        ]
36
37    def get_best_fingerings(self, penalty_functions: Iterable[PenaltyFunction]) \
38            -> Iterable[AnnotatedBisonoricFingering]:
39        '''
40        Returns a list of fingerings that minimizes the cost for the entire tune,
41        as measured by the provided `penalty_functions`.
42        '''
43        f_sets = []
44        for annotation, f_set in self.get_all_fingerings():
45            if not f_set:
46                a = annotation
47                raise ValueError(f'No fingerings for {a.pitch} in measure {a.measure}')
48            f_sets.append(f_set)
49        return find_best_fingerings(f_sets, penalty_functions)
@dataclass
class NotesOnLayout:
12@dataclass
13class NotesOnLayout:
14    '''
15    Represents a sequence of notes on a particular layout
16    '''
17    notes: Iterable[Annotation]
18    layout: BisonoricLayout
19
20    def get_all_fingerings(self) -> \
21            Iterable[tuple[Annotation, set[AnnotatedBisonoricFingering]]]:
22        '''
23        For each note in the tune, returns all possible fingerings.
24        '''
25        return [
26            (
27                annotation,
28                {
29                    AnnotatedBisonoricFingering(
30                        fingering=f,
31                        annotation=annotation)
32                    for f in self.layout.get_fingerings(annotation.pitch)
33                }
34            )
35            for annotation in self.notes
36        ]
37
38    def get_best_fingerings(self, penalty_functions: Iterable[PenaltyFunction]) \
39            -> Iterable[AnnotatedBisonoricFingering]:
40        '''
41        Returns a list of fingerings that minimizes the cost for the entire tune,
42        as measured by the provided `penalty_functions`.
43        '''
44        f_sets = []
45        for annotation, f_set in self.get_all_fingerings():
46            if not f_set:
47                a = annotation
48                raise ValueError(f'No fingerings for {a.pitch} in measure {a.measure}')
49            f_sets.append(f_set)
50        return find_best_fingerings(f_sets, penalty_functions)

Represents a sequence of notes on a particular layout

NotesOnLayout( notes: collections.abc.Iterable[concertina_helper.type_defs.Annotation], layout: concertina_helper.layouts.bisonoric.BisonoricLayout)
def get_all_fingerings( self) -> collections.abc.Iterable[tuple[concertina_helper.type_defs.Annotation, set[concertina_helper.layouts.bisonoric.AnnotatedBisonoricFingering]]]:
20    def get_all_fingerings(self) -> \
21            Iterable[tuple[Annotation, set[AnnotatedBisonoricFingering]]]:
22        '''
23        For each note in the tune, returns all possible fingerings.
24        '''
25        return [
26            (
27                annotation,
28                {
29                    AnnotatedBisonoricFingering(
30                        fingering=f,
31                        annotation=annotation)
32                    for f in self.layout.get_fingerings(annotation.pitch)
33                }
34            )
35            for annotation in self.notes
36        ]

For each note in the tune, returns all possible fingerings.

def get_best_fingerings( self, penalty_functions: collections.abc.Iterable[collections.abc.Callable[[concertina_helper.layouts.bisonoric.AnnotatedBisonoricFingering, concertina_helper.layouts.bisonoric.AnnotatedBisonoricFingering], float]]) -> collections.abc.Iterable[concertina_helper.layouts.bisonoric.AnnotatedBisonoricFingering]:
38    def get_best_fingerings(self, penalty_functions: Iterable[PenaltyFunction]) \
39            -> Iterable[AnnotatedBisonoricFingering]:
40        '''
41        Returns a list of fingerings that minimizes the cost for the entire tune,
42        as measured by the provided `penalty_functions`.
43        '''
44        f_sets = []
45        for annotation, f_set in self.get_all_fingerings():
46            if not f_set:
47                a = annotation
48                raise ValueError(f'No fingerings for {a.pitch} in measure {a.measure}')
49            f_sets.append(f_set)
50        return find_best_fingerings(f_sets, penalty_functions)

Returns a list of fingerings that minimizes the cost for the entire tune, as measured by the provided penalty_functions.