rstmt_core/comp/impls/
impl_scale.rs

1/*
2    Appellation: impl_freq_to_scale <module>
3    Created At: 2025.12.21:08:58:02
4    Contrib: @FL03
5*/
6use crate::comp::Scale;
7use crate::freq::{Frequency, IntoFrequency, RawFrequency};
8use num_traits::{Float, FromPrimitive};
9
10impl<T> Scale<T> {
11    pub const fn new(root: T) -> Self {
12        Self {
13            root: Frequency(root),
14        }
15    }
16    /// creates a new [`Scale`] from the given root frequency
17    pub const fn from_freq(root: Frequency<T>) -> Self {
18        Self { root }
19    }
20    /// returns a reference to the base frequency
21    pub const fn root(&self) -> &Frequency<T> {
22        &self.root
23    }
24    /// returns a mutable reference to the base frequency
25    pub const fn root_mut(&mut self) -> &mut Frequency<T> {
26        &mut self.root
27    }
28    /// classifies the given frequency as a pitch class `n`, using the formula:
29    ///
30    /// ```math
31    /// n=\text{round}\Bigg(12\cdot\log_{2}\bigg(\frac{F}{\beta}\bigg)\Bigg)
32    /// ```
33    pub fn classify(&self, Frequency(freq): Frequency<T>) -> Option<isize>
34    where
35        T: RawFrequency + Float + FromPrimitive,
36    {
37        // Ensure frequency is positive
38        if freq <= T::zero() {
39            return None;
40        }
41        crate::classify_freq_with_scale(freq, self.root().value())
42    }
43    /// returns the frequency [Hz] of the given pitch class `n`, using the formula:
44    ///
45    /// ```math
46    /// F= \beta \cdot 2^{\frac{n}{12}}
47    /// ```
48    pub fn get_freq_of_class(&self, n: isize) -> Frequency<T>
49    where
50        T: RawFrequency + Float + FromPrimitive,
51    {
52        crate::compute_freq_of_pitch(n, self.root().value()).into_frequency()
53    }
54}