rstmt_core/compose/impls/
impl_scale.rs1use crate::compose::Scale;
7use crate::freq::{
8 Frequency, IntoFrequency, RawFrequency, classify_freq_with_scale, get_frequency_of_pitch,
9};
10use num_traits::{Float, FromPrimitive};
11
12impl<T> Scale<T> {
13 pub const fn new(root: T) -> Self {
14 Self {
15 root: Frequency(root),
16 }
17 }
18 pub fn a4() -> Self
20 where
21 T: FromPrimitive,
22 {
23 let root = <T>::from_usize(440).unwrap();
24 Self::new(root)
25 }
26
27 pub const fn from_freq(root: Frequency<T>) -> Self {
29 Self { root }
30 }
31 pub const fn root(&self) -> &Frequency<T> {
33 &self.root
34 }
35 pub const fn root_mut(&mut self) -> &mut Frequency<T> {
37 &mut self.root
38 }
39 pub fn classify(&self, Frequency(freq): Frequency<T>) -> Option<isize>
45 where
46 T: RawFrequency + Float + FromPrimitive,
47 {
48 if freq <= T::zero() {
50 return None;
51 }
52 classify_freq_with_scale(freq, self.root().value())
53 }
54 pub fn get_freq_of_class(&self, n: isize) -> Frequency<T>
60 where
61 T: RawFrequency + Float + FromPrimitive,
62 {
63 get_frequency_of_pitch(n, self.root().value()).into_frequency()
64 }
65}