1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::{
    intervals::{OCTAVE, TONIC},
    math::{gpf, power_of_two},
    ratio::Ratio,
};
use std::ops::{Add, AddAssign};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub enum Ordinal {
    Otonal,
    Utonal,
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Pitch {
    pub cents: f32,
    pub ratio: Ratio,
    pub limit: i32,
    pub ordinal: Ordinal,
}

pub trait Pitchable {
    fn to_pitch(&self) -> Pitch;
}

impl Pitchable for (i32, i32) {
    fn to_pitch(&self) -> Pitch {
        let ratio = Ratio::new(*self);
        Pitch {
            cents: cents(ratio),
            ratio: flatten(ratio),
            limit: limit(ratio),
            ordinal: determine_ordinal(ratio),
        }
    }
}

impl Pitchable for i32 {
    fn to_pitch(&self) -> Pitch {
        let ratio = Ratio::new((*self, 1));
        Pitch {
            cents: cents(ratio),
            ratio: flatten(ratio),
            limit: limit(ratio),
            ordinal: determine_ordinal(ratio),
        }
    }
}

impl Pitchable for Ratio {
    fn to_pitch(&self) -> Pitch {
        let ratio = flatten(*self);
        Pitch {
            cents: cents(ratio),
            ratio,
            limit: limit(ratio),
            ordinal: determine_ordinal(ratio),
        }
    }
}

impl Pitchable for Pitch {
    fn to_pitch(&self) -> Pitch {
        Pitch {
            cents: self.cents,
            ratio: flatten(self.ratio),
            limit: self.limit,
            ordinal: self.ordinal,
        }
    }
}

impl Add for Pitch {
    type Output = Pitch;

    fn add(self, rhs: Pitch) -> Pitch {
        Pitch::new(self.ratio * rhs.ratio)
    }
}

impl AddAssign for Pitch {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl Pitch {
    pub fn new<T: Pitchable>(pitchable: T) -> Pitch {
        pitchable.to_pitch()
    }

    pub fn walk(&self, times: i32) -> Vec<Pitch> {
        let mut pitches = vec![Pitch::new((1, 1))];
        for _ in 1..times {
            let last_pitch = pitches.last().unwrap();
            let next_pitch = *last_pitch + *self;
            pitches.push(next_pitch)
        }
        pitches
    }
}

fn cents(ratio: Ratio) -> f32 {
    let cents_per_octave = 1_200f32;
    let ratio_as_float = ratio.numerator as f32 / ratio.denominator as f32;

    (cents_per_octave / 2f32.log10()) * ratio_as_float.log10()
}

fn flatten(ratio: Ratio) -> Ratio {
    let tonic = Ratio::new(TONIC);
    let octave = Ratio::new(OCTAVE);
    let mut r = ratio;
    loop {
        if r > octave {
            r /= octave;
        } else if r < tonic {
            r *= octave;
        } else {
            return r;
        }
    }
}

fn limit(ratio: Ratio) -> i32 {
    let gpf_from_numerator = gpf(ratio.numerator);
    let gpf_from_denominator = gpf(ratio.denominator);

    gpf_from_numerator.max(gpf_from_denominator)
}

pub fn determine_ordinal(ratio: Ratio) -> Ordinal {
    if power_of_two(ratio.denominator) {
        Ordinal::Otonal
    } else {
        Ordinal::Utonal
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32;

    fn p(n: i32, d: i32) -> Pitch {
        Pitch::new((n, d))
    }

    #[test]
    fn cents_works() {
        let fifth = Ratio::new((3, 2));
        let value = cents(fifth);
        let abs_diff = (value.abs() - value).abs();

        assert!(abs_diff <= f32::EPSILON);
    }

    #[test]
    fn flatten_works() {
        let a = p(18, 4);

        assert_eq!(a.ratio, Ratio::new((9, 8)));
    }

    #[test]
    fn walk_works() {
        let expected = vec![p(1, 1), p(3, 2), p(9, 8)];
        let actual = Pitch::new((3, 2)).walk(3);

        assert_eq!(expected, actual);
    }
}