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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

use std::cmp::Ordering;
use std::ops::{Add, Sub, Mul, Div, Rem, Neg};
use super::{
    calc,
    DEFAULT_SCALE_WEIGHT,
    LetterOctave,
    Letter,
    Mel,
    Octave,
    Perc,
    ScaledPerc,
    ScaleWeight,
    Step,
    letter_octave_from_hz,
    mel_from_hz,
    perc_from_hz,
    scaled_perc_from_hz,
    step_from_hz,
};

pub const MAX: calc::Hz = 20_000.0;
pub const MIN: calc::Hz = 20.0;

/// Pitch representation in the form of a frequency (hz).
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
pub struct Hz(pub calc::Hz);

impl Hz {

    /// Return the unit value of the Hz struct.
    #[inline]
    pub fn hz(&self) -> calc::Hz {
        let Hz(hz) = *self;
        hz
    }

    /// Convert to (Letter, Octave) tuple.
    #[inline]
    pub fn letter_octave(&self) -> (Letter, Octave) {
        let Hz(hz) = *self;
        letter_octave_from_hz(hz)
    }

    /// Convert to Letter.
    #[inline]
    pub fn letter(&self) -> Letter {
        let (letter, _) = self.letter_octave();
        letter
    }

    /// Convert to Octave.
    #[inline]
    pub fn octave(&self) -> Octave {
        let (_, octave) = self.letter_octave();
        octave
    }

    /// Convert to a LetterOctave struct with the same pitch.
    #[inline]
    pub fn to_letter_octave(&self) -> LetterOctave {
        let (letter, octave) = self.letter_octave();
        LetterOctave(letter, octave)
    }

    /// Convert to the unit value of a Mel.
    #[inline]
    pub fn mel(&self) -> calc::Mel {
        mel_from_hz(self.hz())
    }

    /// Convert to a Mel struct.
    #[inline]
    pub fn to_mel(&self) -> Mel {
        Mel(self.mel())
    }

    /// Convert to the unit value of a Perc struct.
    #[inline]
    pub fn perc(&self) -> calc::Perc {
        let Hz(hz) = *self;
        perc_from_hz(hz)
    }

    /// Convert to a percentage of the human hearing range.
    #[inline]
    pub fn to_perc(&self) -> Perc {
        Perc(self.perc())
    }

    /// Convert to a scaled percentage of the human hearing range with a given weight.
    #[inline]
    pub fn scaled_perc_with_weight(&self, weight: ScaleWeight) -> calc::Perc {
        let Hz(hz) = *self;
        scaled_perc_from_hz(hz, weight)
    }

    /// Convert to a scaled percentage of the human hearing range.
    #[inline]
    pub fn scaled_perc(&self) -> calc::Perc {
        self.scaled_perc_with_weight(DEFAULT_SCALE_WEIGHT)
    }

    /// Convert to a scaled percentage of the human hearing range with a given weight.
    #[inline]
    pub fn to_scaled_perc_with_weight(&self, weight: ScaleWeight) -> ScaledPerc {
        ScaledPerc(self.scaled_perc_with_weight(weight), weight)
    }

    /// Convert to a scaled percentage of the human hearing range.
    #[inline]
    pub fn to_scaled_perc(&self) -> ScaledPerc {
        self.to_scaled_perc_with_weight(DEFAULT_SCALE_WEIGHT)
    }

    /// Convert to the unit value of a Step.
    #[inline]
    pub fn step(&self) -> calc::Step {
        let Hz(hz) = *self;
        step_from_hz(hz)
    }

    /// Convert to a floating point MIDI-esque Step.
    #[inline]
    pub fn to_step(&self) -> Step {
        Step(self.step())
    }

}

impl Add for Hz {
    type Output = Hz;
    #[inline]
    fn add(self, rhs: Hz) -> Hz {
        Hz(self.hz() + rhs.hz())
    }
}

impl Sub for Hz {
    type Output = Hz;
    #[inline]
    fn sub(self, rhs: Hz) -> Hz {
        Hz(self.hz() - rhs.hz())
    }
}

impl Mul for Hz {
    type Output = Hz;
    #[inline]
    fn mul(self, rhs: Hz) -> Hz {
        Hz(self.hz() * rhs.hz())
    }
}

impl Div for Hz {
    type Output = Hz;
    #[inline]
    fn div(self, rhs: Hz) -> Hz {
        Hz(self.hz() / rhs.hz())
    }
}

impl Rem for Hz {
    type Output = Hz;
    #[inline]
    fn rem(self, rhs: Hz) -> Hz {
        Hz(self.hz() % rhs.hz())
    }
}

impl Neg for Hz {
    type Output = Hz;
    #[inline]
    fn neg(self) -> Hz {
        Hz(-self.hz())
    }
}

impl PartialEq for Hz {
    #[inline]
    fn eq(&self, other: &Hz) -> bool {
        self.hz() == other.hz()
    }
}

impl Eq for Hz {}

impl PartialOrd for Hz {
    #[inline]
    fn partial_cmp(&self, other: &Hz) -> Option<Ordering> {
        self.hz().partial_cmp(&other.hz())
    }
}

impl Ord for Hz {
    #[inline]
    fn cmp(&self, other: &Hz) -> Ordering {
        self.partial_cmp(other).unwrap()
    }
}