rstmt_core/traits/
notable.rs

1/*
2    Appellation: notable <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::pitch::{Pitch, Pitches};
6use crate::Octave;
7
8/// The American Standard Pitch Notation (ASPN) is a system popularized for its
9/// ability to simplify the representation of musical notes, combining the
10/// traditional symbols or pitch classes used to describe a particular pitch
11/// as well as leverging a subscript to denote the octave of the given pitch.
12///
13///
14/// the existing symbolic framework described by the [Pitch](crate::Pitch) and
15/// [Octave](crate::Octave) types.
16pub trait ASPN {
17    fn aspn(&self) -> String {
18        format!("{}.{}", self.pitch_class(), self.octave())
19    }
20
21    fn octave(&self) -> Octave;
22
23    fn pitch(&self) -> Pitch;
24
25    fn pitch_class(&self) -> Pitches {
26        self.pitch().class()
27    }
28}
29
30/// [Notable] is used to describe objects capable of being represented as a pitch
31pub trait Notable: Copy + Sized + core::fmt::Display {
32    /// Find the modular index of the given pitch
33    fn pitch(&self) -> Pitch;
34    /// Classify the pitch into a pitch class
35    fn pitch_class(&self) -> Pitches {
36        self.pitch().class()
37    }
38}
39
40/*
41 ************* Implementations *************
42*/
43
44impl Notable for Pitch {
45    fn pitch_class(&self) -> Pitches {
46        self.class()
47    }
48
49    fn pitch(&self) -> Pitch {
50        *self
51    }
52}
53
54impl Notable for Pitches {
55    fn pitch_class(&self) -> Pitches {
56        *self
57    }
58
59    fn pitch(&self) -> Pitch {
60        Pitch(self.pitch_class().value())
61    }
62}
63
64impl Notable for crate::Note {
65    fn pitch(&self) -> Pitch {
66        *self.pitch()
67    }
68}
69
70impl Notable for crate::PitchTy {
71    fn pitch(&self) -> Pitch {
72        Pitch(*self)
73    }
74}