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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Types for working with musical tunings.

#![allow(clippy::wrong_self_convention)] // Would require a breaking change. Fix when appropriate.

use crate::{
    note::{Note, NoteLetter},
    pitch::{Pitch, Pitched, Ratio},
};

/// A [`Tuning`] maps keys or notes of type `K` to a [`Pitch`] or vice versa.
pub trait Tuning<K> {
    /// Returns the [`Pitch`] of the given key or note `K` in the current [`Tuning`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use assert_approx_eq::assert_approx_eq;
    /// # use tune::note::NoteLetter;
    /// # use tune::tuning::ConcertPitch;
    /// use tune::tuning::Tuning;
    ///
    /// let standard_tuning = ConcertPitch::default();
    /// let a5 = NoteLetter::A.in_octave(5);
    ///
    /// assert_approx_eq!(standard_tuning.pitch_of(a5).as_hz(), 880.0);
    /// ```
    fn pitch_of(&self, key: K) -> Pitch;

    /// Finds a closest key or note [`Approximation`] for the given [`Pitch`] in the current [`Tuning`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use assert_approx_eq::assert_approx_eq;
    /// # use tune::note::NoteLetter;
    /// # use tune::pitch::Pitch;
    /// # use tune::pitch::Ratio;
    /// # use tune::tuning::ConcertPitch;
    /// use tune::tuning::Tuning;
    ///
    /// let standard_tuning = ConcertPitch::default();
    /// let a5 = NoteLetter::A.in_octave(5);
    /// let detuned_a5_pitch = Pitch::of(a5) * Ratio::from_cents(10.0);
    ///
    /// let approximation = standard_tuning.find_by_pitch(detuned_a5_pitch);
    /// assert_eq!(approximation.approx_value, a5);
    /// assert_approx_eq!(approximation.deviation.as_cents(), 10.0);
    /// ```
    fn find_by_pitch(&self, pitch: Pitch) -> Approximation<K>;

    /// Wraps `self` in a type adapter s.t. it can be used in functions that are generic over [`KeyboardMapping<K>`].
    fn as_linear_mapping(self) -> LinearMapping<Self>
    where
        Self: Sized,
    {
        LinearMapping { inner: self }
    }
}

/// `impl` forwarding for references.
impl<K, T: Tuning<K> + ?Sized> Tuning<K> for &T {
    fn pitch_of(&self, key: K) -> Pitch {
        T::pitch_of(self, key)
    }

    fn find_by_pitch(&self, pitch: Pitch) -> Approximation<K> {
        T::find_by_pitch(self, pitch)
    }
}

/// A key or note `K` paired with an appropriate [`Tuning<K>`] is considered [`Pitched`].
///
/// # Examples
///
/// ```
/// # use assert_approx_eq::assert_approx_eq;
/// # use tune::note::NoteLetter;
/// # use tune::pitch::Pitch;
/// # use tune::tuning::ConcertPitch;
/// use tune::pitch::Pitched;
///
/// let concert_pitch = ConcertPitch::from_a4_pitch(Pitch::from_hz(432.0));
/// assert_approx_eq!((NoteLetter::A.in_octave(5), concert_pitch).pitch().as_hz(), 864.0);
/// ```
impl<K: Copy, T: Tuning<K> + ?Sized> Pitched for (K, T) {
    fn pitch(&self) -> Pitch {
        self.1.pitch_of(self.0)
    }
}

/// A [`Scale`] is a tuning whose [`Pitch`]es can be accessed in a sorted manner.
///
/// Accessing pitches in order can be important, e.g. when handling pitches in a certain frequency window.
pub trait Scale {
    /// Returns the [`Pitch`] at the given scale degree in the current [`Scale`].
    fn sorted_pitch_of(&self, degree: i32) -> Pitch;

    /// Finds a closest scale degree [`Approximation`] for the given [`Pitch`] in the current [`Scale`].
    fn find_by_pitch_sorted(&self, pitch: Pitch) -> Approximation<i32>;

    /// Wraps `self` in a type adapter s.t. it can be used in functions that are generic over [`Tuning<i32>`].
    fn as_sorted_tuning(self) -> SortedTuning<Self>
    where
        Self: Sized,
    {
        SortedTuning { inner: self }
    }
}

/// `impl` forwarding for references.
impl<S: Scale + ?Sized> Scale for &S {
    fn sorted_pitch_of(&self, degree: i32) -> Pitch {
        S::sorted_pitch_of(self, degree)
    }

    fn find_by_pitch_sorted(&self, pitch: Pitch) -> Approximation<i32> {
        S::find_by_pitch_sorted(self, pitch)
    }
}

/// Type adapter returned by [`Scale::as_sorted_tuning`].
pub struct SortedTuning<S> {
    inner: S,
}

impl<S: Scale> Tuning<i32> for SortedTuning<S> {
    fn pitch_of(&self, key: i32) -> Pitch {
        self.inner.sorted_pitch_of(key)
    }

    fn find_by_pitch(&self, pitch: Pitch) -> Approximation<i32> {
        self.inner.find_by_pitch_sorted(pitch)
    }
}

/// Similar to a [`Tuning`] but not designed to be surjective or injecive.
///
/// An inversion operation is not provided.
/// In return, zero or multiple keys/notes can point to a [`Pitch`].
pub trait KeyboardMapping<K> {
    /// Returns the [`Pitch`] of the provided key or note.
    fn maybe_pitch_of(&self, key: K) -> Option<Pitch>;
}

/// `impl` forwarding for references.
impl<K, T: KeyboardMapping<K> + ?Sized> KeyboardMapping<K> for &T {
    fn maybe_pitch_of(&self, key: K) -> Option<Pitch> {
        T::maybe_pitch_of(self, key)
    }
}

/// Type adapter returned by [`Tuning::as_linear_mapping`].
pub struct LinearMapping<T> {
    inner: T,
}

impl<K, T: Tuning<K>> KeyboardMapping<K> for LinearMapping<T> {
    fn maybe_pitch_of(&self, key: K) -> Option<Pitch> {
        Some(self.inner.pitch_of(key))
    }
}

/// The result of a find operation on [`Scale`]s or [`Tuning`]s.
#[derive(Copy, Clone, Debug)]
pub struct Approximation<K> {
    /// The value to find.
    pub approx_value: K,

    /// The deviation from the ideal value.
    pub deviation: Ratio,
}

/// A [`ConcertPitch`] enables [`Note`]s to sound at a [`Pitch`] different to what would be expected in 440&nbsp;Hz standard tuning.
///
/// To access the full potential of [`ConcertPitch`]es have a look at the [`Tuning`] and [`PitchedNote`](crate::note::PitchedNote) traits.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
pub struct ConcertPitch {
    a4_pitch: Pitch,
}

impl ConcertPitch {
    /// Creates a [`ConcertPitch`] with the given `a4_pitch`.
    pub fn from_a4_pitch(a4_pitch: impl Pitched) -> Self {
        Self {
            a4_pitch: a4_pitch.pitch(),
        }
    }

    /// Creates a [`ConcertPitch`] from the given `note` and `pitched` value.
    ///
    /// # Examples
    ///
    /// ```
    /// # use assert_approx_eq::assert_approx_eq;
    /// # use tune::note::NoteLetter;
    /// # use tune::tuning::ConcertPitch;
    /// # use tune::pitch::Pitch;
    /// let c4 = NoteLetter::C.in_octave(4);
    /// let fixed_c4_tuning = ConcertPitch::from_note_and_pitch(c4, Pitch::from_hz(260.0));
    /// assert_approx_eq!(fixed_c4_tuning.a4_pitch().as_hz(), 437.266136);
    /// ```
    pub fn from_note_and_pitch(note: Note, pitched: impl Pitched) -> Self {
        Self {
            a4_pitch: pitched.pitch()
                * Ratio::from_semitones(f64::from(
                    note.num_semitones_before(NoteLetter::A.in_octave(4)),
                )),
        }
    }

    /// Returns the [`Pitch`] of A4.
    pub fn a4_pitch(self) -> Pitch {
        self.a4_pitch
    }
}

/// The default [`ConcertPitch`] is A4 sounding at 440&nbsp;Hz.
///
/// # Examples
///
/// ```
/// # use assert_approx_eq::assert_approx_eq;
/// # use tune::tuning::ConcertPitch;
/// assert_approx_eq!(ConcertPitch::default().a4_pitch().as_hz(), 440.0);
/// ```
impl Default for ConcertPitch {
    fn default() -> Self {
        Self::from_a4_pitch(Pitch::from_hz(440.0))
    }
}

/// A [`ConcertPitch`] maps [`Note`]s to [`Pitch`]es and is considered a [`Tuning`].
///
/// # Examples
///
/// ```rust
/// # use assert_approx_eq::assert_approx_eq;
/// # use tune::note::NoteLetter;
/// # use tune::tuning::ConcertPitch;
/// # use tune::pitch::Pitch;
/// use tune::tuning::Tuning;
///
/// let c4 = NoteLetter::C.in_octave(4);
/// let a4 = NoteLetter::A.in_octave(4);
///
/// let standard_tuning = ConcertPitch::default();
/// assert_approx_eq!(standard_tuning.pitch_of(c4).as_hz(), 261.625565);
/// assert_approx_eq!(standard_tuning.pitch_of(a4).as_hz(), 440.0);
///
/// let healing_tuning = ConcertPitch::from_a4_pitch(Pitch::from_hz(432.0));
/// assert_approx_eq!(healing_tuning.pitch_of(c4).as_hz(), 256.868737);
/// assert_approx_eq!(healing_tuning.pitch_of(a4).as_hz(), 432.0);
/// ```
impl Tuning<Note> for ConcertPitch {
    fn pitch_of(&self, note: Note) -> Pitch {
        self.a4_pitch * Ratio::from_semitones(NoteLetter::A.in_octave(4).num_semitones_before(note))
    }

    fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note> {
        let semitones_above_a4 = Ratio::between_pitches(self.a4_pitch, pitch).as_semitones();
        let round_to_lower_step = Ratio::from_float(1.000001);
        let approx_semitones_above_a4 =
            (semitones_above_a4 - round_to_lower_step.as_semitones()).round();

        Approximation {
            approx_value: Note::from_midi_number(
                approx_semitones_above_a4 as i32 + NoteLetter::A.in_octave(4).midi_number(),
            ),
            deviation: Ratio::from_semitones(semitones_above_a4 - approx_semitones_above_a4),
        }
    }
}

/// Convenience implementation enabling to write `()` instead of [`ConcertPitch::default()`].
///
/// # Examples
///
/// ```
/// # use tune::note::Note;
/// # use tune::pitch::Pitch;
/// use tune::pitch::Pitched;
///
/// assert_eq!(Pitch::from_hz(880.0).find_in_tuning(()).approx_value, Note::from_midi_number(81));
/// ```
impl Tuning<Note> for () {
    fn pitch_of(&self, note: Note) -> Pitch {
        ConcertPitch::default().pitch_of(note)
    }

    fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note> {
        ConcertPitch::default().find_by_pitch(pitch)
    }
}