rstmt_core/pitch/impls/
impl_pclass.rs

1/*
2    Appellation: impl_pitch_class <module>
3    Created At: 2025.12.20:08:51:41
4    Contrib: @FL03
5*/
6use crate::freq::{Frequency, RawFrequency};
7use crate::pitch::{Accidental, Flat, Natural, PitchClass, RawPitchClass, Sharp};
8use num_traits::{Float, FromPrimitive};
9
10impl<P, K> PitchClass<P, K>
11where
12    P: RawPitchClass<Tag = K>,
13    K: Accidental,
14{
15    pub fn new() -> Self {
16        Self {
17            class: P::new(),
18            kind: K::new(),
19        }
20    }
21
22    pub fn from_class(class: P) -> Self {
23        Self {
24            class,
25            kind: K::new(),
26        }
27    }
28
29    pub fn from_kind(kind: K) -> Self {
30        Self {
31            class: P::new(),
32            kind,
33        }
34    }
35    /// returns a pointer to the class
36    pub const fn as_ptr(&self) -> *const P {
37        core::ptr::from_ref(self.get())
38    }
39    /// returns a mutable pointer to the class
40    pub const fn as_mut_ptr(&mut self) -> *mut P {
41        core::ptr::from_mut(self.get_mut())
42    }
43    /// convert the pitch class into a [`Frequency`] based on the standard A4 tuning
44    pub fn as_frequency<T>(&self) -> Frequency<T>
45    where
46        P: RawPitchClass<Tag = K>,
47        K: Accidental,
48        T: RawFrequency + Float + FromPrimitive,
49    {
50        Frequency::from_class_on_a4(self.get().index())
51    }
52    /// returns a reference to the defined class
53    pub const fn get(&self) -> &P {
54        &self.class
55    }
56    /// returns a mutable reference to the defined class
57    pub const fn get_mut(&mut self) -> &mut P {
58        &mut self.class
59    }
60    /// returns true if the class is considered natural
61    pub fn is_natural(&self) -> bool
62    where
63        K: 'static,
64    {
65        Natural::of::<K>()
66    }
67    /// returns true if the class is considered flat
68    pub fn is_flat(&self) -> bool
69    where
70        K: 'static,
71    {
72        Flat::of::<K>()
73    }
74    /// returns true if the class is considered sharp
75    pub fn is_sharp(&self) -> bool
76    where
77        K: 'static,
78    {
79        Sharp::of::<K>()
80    }
81}