summaryrefslogtreecommitdiff
path: root/src/types.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/types.rs')
-rw-r--r--src/types.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/types.rs b/src/types.rs
index b9ff15f..dd450bb 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1 +1,26 @@
pub type Sample = f32;
+
+pub enum Pitch {
+ Freq(f32),
+ MIDI(f32),
+}
+
+impl Pitch {
+ pub fn to_midi(&self) -> f32 {
+ match *self {
+ Pitch::MIDI(x) => x,
+ Pitch::Freq(x) =>
+ 12.0 * (x / 440.0).log2() + 69.0,
+ }
+ }
+ pub fn to_midi_pitch(&self) -> Pitch { Pitch::MIDI(self.to_midi()) }
+
+ pub fn to_freq(&self) -> f32 {
+ match *self {
+ Pitch::MIDI(x) =>
+ 440.0 * (2.0f32).powf((x - 69.0) / 12.0),
+ Pitch::Freq(x) => x,
+ }
+ }
+ pub fn to_freq_pitch(&self) -> Pitch { Pitch::Freq(self.to_freq()) }
+}