use crate::compose::Scale;
use crate::freq::{
Frequency, IntoFrequency, RawFrequency, classify_freq_with_scale, get_frequency_of_pitch,
};
use num_traits::{Float, FromPrimitive};
impl<T> Scale<T> {
pub const fn new(root: T) -> Self {
Self {
root: Frequency(root),
}
}
pub fn a4() -> Self
where
T: FromPrimitive,
{
let root = <T>::from_usize(440).unwrap();
Self::new(root)
}
pub const fn from_freq(root: Frequency<T>) -> Self {
Self { root }
}
pub const fn root(&self) -> &Frequency<T> {
&self.root
}
pub const fn root_mut(&mut self) -> &mut Frequency<T> {
&mut self.root
}
pub fn classify(&self, Frequency(freq): Frequency<T>) -> Option<isize>
where
T: RawFrequency + Float + FromPrimitive,
{
if freq <= T::zero() {
return None;
}
classify_freq_with_scale(freq, self.root().value())
}
pub fn get_freq_of_class(&self, n: isize) -> Frequency<T>
where
T: RawFrequency + Float + FromPrimitive,
{
get_frequency_of_pitch(n, self.root().value()).into_frequency()
}
}