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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Abstractions for working with notes, letters and octaves.

use crate::math;
use crate::pitch::{Pitch, Pitched};
use crate::tuning::ConcertPitch;
use crate::{key::PianoKey, ratio::Ratio};
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;

/// A musical note encapsulating a clearly defined pitch.
///
/// The pitch can be derived using the [`Pitched`] impl on the [`Note`] type itself, assuming
/// standard 440 Hz tuning, or on [`NoteAtConcertPitch`], given a specific concert pitch.
#[derive(Copy, Clone, Debug, Ord, Eq, Hash, PartialEq, PartialOrd)]
pub struct Note {
    midi_number: i32,
}

impl Note {
    pub fn from_midi_number(midi_number: i32) -> Self {
        Self { midi_number }
    }

    /// Creates a [`Note`] instance given a [`NoteLetter`] and an octave.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tune::note::HelmholtzOctave;
    /// # use tune::note::Note;
    /// # use tune::note::NoteLetter;
    /// # use tune::note::Octave;
    /// let a4 = Note::from_midi_number(69);
    /// assert_eq!(Note::from_letter_and_octave(NoteLetter::A, 4), a4);
    /// assert_eq!(Note::from_letter_and_octave(NoteLetter::A, Octave::from_octave_number(4)), a4);
    /// assert_eq!(Note::from_letter_and_octave(NoteLetter::A, HelmholtzOctave::OneLined), a4);
    /// ```
    pub fn from_letter_and_octave(note_letter: NoteLetter, octave: impl Into<Octave>) -> Self {
        let semitone = match note_letter {
            NoteLetter::C => 0,
            NoteLetter::Csh => 1,
            NoteLetter::D => 2,
            NoteLetter::Dsh => 3,
            NoteLetter::E => 4,
            NoteLetter::F => 5,
            NoteLetter::Fsh => 6,
            NoteLetter::G => 7,
            NoteLetter::Gsh => 8,
            NoteLetter::A => 9,
            NoteLetter::Ash => 10,
            NoteLetter::B => 11,
        };
        Self::from_midi_number((octave.into().octave_number + 1) * 12 + semitone)
    }

    /// Creates a [`Note`] instance from a [`PianoKey`] assuming standard 12-EDO tuning.
    pub fn from_piano_key(piano_key: PianoKey) -> Self {
        Self::from_midi_number(piano_key.midi_number())
    }

    pub fn midi_number(self) -> i32 {
        self.midi_number
    }

    /// Splits the current note into a [`NoteLetter`] and an [`Octave`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tune::note::Note;
    /// # use tune::note::NoteLetter;
    /// # use tune::note::Octave;
    /// let a4 = Note::from_midi_number(69);
    /// assert_eq!(a4.letter_and_octave(), (NoteLetter::A, Octave::from_octave_number(4)));
    ///
    /// let midi_root = Note::from_midi_number(0);
    /// assert_eq!(midi_root.letter_and_octave(), (NoteLetter::C, Octave::from_octave_number(-1)));
    /// ```
    pub fn letter_and_octave(self) -> (NoteLetter, Octave) {
        let (midi_octave, semitone) = math::i32_dr_u32(self.midi_number, 12);
        let note_letter = match semitone {
            0 => NoteLetter::C,
            1 => NoteLetter::Csh,
            2 => NoteLetter::D,
            3 => NoteLetter::Dsh,
            4 => NoteLetter::E,
            5 => NoteLetter::F,
            6 => NoteLetter::Fsh,
            7 => NoteLetter::G,
            8 => NoteLetter::Gsh,
            9 => NoteLetter::A,
            10 => NoteLetter::Ash,
            11 => NoteLetter::B,
            other => unreachable!("value was {}", other),
        };
        (note_letter, Octave::from_octave_number(midi_octave - 1))
    }

    /// Retrieves the associated [`PianoKey`] assuming standard 12-EDO tuning.
    pub fn as_piano_key(self) -> PianoKey {
        PianoKey::from_midi_number(self.midi_number())
    }

    /// Creates a [`NoteAtConcertPitch`] instance with `self` sounding at a different pitch.
    ///
    /// # 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 c4_at_260_hz = NoteLetter::C.in_octave(4).at_pitch(Pitch::from_hz(260.0));
    /// assert_approx_eq!(c4_at_260_hz.pitch().as_hz(), 260.0);
    ///
    /// let (_note, concert_pitch) = c4_at_260_hz;
    /// assert_approx_eq!(concert_pitch.a4_pitch().as_hz(), 437.266136);
    /// ```
    pub fn at_pitch(self, pitched: impl Pitched) -> NoteAtConcertPitch {
        (self, ConcertPitch::from_note_and_pitch(self, pitched))
    }

    /// Convenience function creating a [`NoteAtConcertPitch`] instance.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tune::note::NoteLetter;
    /// # use tune::pitch::Pitch;
    /// # use tune::tuning::ConcertPitch;
    /// let a4 = NoteLetter::A.in_octave(4);
    /// let concert_pitch = ConcertPitch::from_a4_pitch(Pitch::from_hz(435.0));
    /// assert_eq!(a4.at_concert_pitch(concert_pitch), (a4, concert_pitch));
    /// ```
    pub fn at_concert_pitch(self, concert_pitch: ConcertPitch) -> NoteAtConcertPitch {
        (self, concert_pitch)
    }

    /// Counts the number of semitones [left inclusive, right exclusive) between `self` and `other`.
    pub fn num_semitones_before(self, other: Note) -> i32 {
        other.midi_number - self.midi_number
    }

    /// Retrieves the [`Note`] instance `num_semitones` above `self`.
    pub fn plus_semitones(self, num_semitones: i32) -> Note {
        Note::from_midi_number(self.midi_number() + num_semitones)
    }
}

impl Pitched for Note {
    fn pitch(self) -> Pitch {
        (self, ConcertPitch::default()).pitch()
    }
}

/// [`Note`]s are rendered in scientific pitch notation.
///
/// # Examples
///
/// ```
/// # use tune::note::Note;
/// assert_eq!(Note::from_midi_number(0).to_string(), "C -1");
/// assert_eq!(Note::from_midi_number(69).to_string(), "A 4");
/// assert_eq!(Note::from_midi_number(70).to_string(), "A#/Bb 4");
/// assert_eq!(Note::from_midi_number(71).to_string(), "B 4");
/// assert_eq!(Note::from_midi_number(72).to_string(), "C 5");
/// assert_eq!(Note::from_midi_number(127).to_string(), "G 9");
///
/// // Format flags
/// assert_eq!(format!("{:+}", Note::from_midi_number(70)), "A# 4");
/// assert_eq!(format!("{:-}", Note::from_midi_number(70)), "Bb 4");
/// assert_eq!(format!("{:10}", Note::from_midi_number(70)), "A#/Bb 4   ");
/// assert_eq!(format!("{:<10}", Note::from_midi_number(70)), "A#/Bb 4   ");
/// assert_eq!(format!("{:>10}", Note::from_midi_number(70)), "   A#/Bb 4");
/// ```
impl Display for Note {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let (letter, octave) = self.letter_and_octave();

        let formatted_note = match (f.sign_plus(), f.sign_minus()) {
            (false, false) => format!("{}", letter),
            (true, false) => format!("{:+}", letter),
            (false, true) => format!("{:-}", letter),
            (true, true) => unreachable!("Impossible format string"),
        };

        f.pad(&format!("{} {}", formatted_note, octave.octave_number))
    }
}

/// The speaking name of a note within its octave.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum NoteLetter {
    C,
    Csh,
    D,
    Dsh,
    E,
    F,
    Fsh,
    G,
    Gsh,
    A,
    Ash,
    B,
}

impl NoteLetter {
    /// Shortcut for [`Note::from_letter_and_octave`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use tune::note::Note;
    /// # use tune::note::NoteLetter;
    /// assert_eq!(NoteLetter::C.in_octave(4), Note::from_letter_and_octave(NoteLetter::C, 4));
    /// ```
    pub fn in_octave(self, octave: impl Into<Octave>) -> Note {
        Note::from_letter_and_octave(self, octave)
    }
}

impl Display for NoteLetter {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        enum Sign {
            Sharp,
            Flat,
            Both,
        };

        let sign = match (f.sign_plus(), f.sign_minus()) {
            (false, false) => Sign::Both,
            (true, false) => Sign::Sharp,
            (false, true) => Sign::Flat,
            (true, true) => unreachable!("Impossible format string"),
        };

        let note_name = match (self, sign) {
            (NoteLetter::C, _) => "C",
            (NoteLetter::Csh, Sign::Both) => "C#/Db",
            (NoteLetter::Csh, Sign::Sharp) => "C#",
            (NoteLetter::Csh, Sign::Flat) => "Db",
            (NoteLetter::D, _) => "D",
            (NoteLetter::Dsh, Sign::Both) => "D#/Eb",
            (NoteLetter::Dsh, Sign::Sharp) => "D#",
            (NoteLetter::Dsh, Sign::Flat) => "Eb",
            (NoteLetter::E, _) => "E",
            (NoteLetter::F, _) => "F",
            (NoteLetter::Fsh, Sign::Both) => "F#/Gb",
            (NoteLetter::Fsh, Sign::Sharp) => "F#",
            (NoteLetter::Fsh, Sign::Flat) => "Gb",
            (NoteLetter::G, _) => "G",
            (NoteLetter::Gsh, Sign::Both) => "G#/Ab",
            (NoteLetter::Gsh, Sign::Sharp) => "G#",
            (NoteLetter::Gsh, Sign::Flat) => "Ab",
            (NoteLetter::A, _) => "A",
            (NoteLetter::Ash, Sign::Both) => "A#/Bb",
            (NoteLetter::Ash, Sign::Sharp) => "A#",
            (NoteLetter::Ash, Sign::Flat) => "Bb",
            (NoteLetter::B, _) => "B",
        };

        f.pad(note_name)
    }
}

/// Typed representation of the octave of a note.
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Octave {
    octave_number: i32,
}

impl Octave {
    pub fn from_octave_number(octave_number: i32) -> Self {
        Self { octave_number }
    }

    pub fn octave_number(self) -> i32 {
        self.octave_number
    }
}

impl From<i32> for Octave {
    fn from(octave_number: i32) -> Self {
        Octave::from_octave_number(octave_number)
    }
}

impl From<HelmholtzOctave> for Octave {
    fn from(helmholtz_octave: HelmholtzOctave) -> Self {
        let octave_number = match helmholtz_octave {
            HelmholtzOctave::SubContra => 0,
            HelmholtzOctave::Contra => 1,
            HelmholtzOctave::Great => 2,
            HelmholtzOctave::Small => 3,
            HelmholtzOctave::OneLined => 4,
            HelmholtzOctave::TwoLined => 5,
            HelmholtzOctave::ThreeLined => 6,
            HelmholtzOctave::FourLined => 7,
            HelmholtzOctave::FiveLined => 8,
            HelmholtzOctave::SixLined => 9,
        };
        Self::from_octave_number(octave_number)
    }
}

/// The speaking name of the octave of a note.
#[derive(Copy, Clone, Debug, Eq, Ord, Hash, PartialEq, PartialOrd)]
pub enum HelmholtzOctave {
    SubContra,
    Contra,
    Great,
    Small,
    OneLined,
    TwoLined,
    ThreeLined,
    FourLined,
    FiveLined,
    SixLined,
}

/// Trait for objects that provide [`Pitch`] and [`Note`] information.
///
/// A [`Note`] has a unique pitch defined by the 440 Hz standard tuning.
/// For a note to sound at a different [`Pitch`] the type alias [`NoteAtConcertPitch`] is used.
pub trait PitchedNote: Pitched {
    /// Retrieves the [`Note`] part of `self`.
    ///
    /// ```
    /// # use tune::note::NoteLetter;
    /// # use tune::pitch::Pitch;
    /// use tune::note::PitchedNote;
    ///
    /// let c4 = NoteLetter::C.in_octave(4);
    /// assert_eq!(c4.note(), c4);
    ///
    /// let c4_altered = c4.at_pitch(Pitch::from_hz(256.0));
    /// assert_eq!(c4_altered.note(), c4);
    /// ```
    fn note(self) -> Note;

    /// Returns a new `PitchedNote` with the same [`Note`] part but a [`Pitch`] altered by `delta`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use assert_approx_eq::assert_approx_eq;
    /// # use tune::note::NoteLetter;
    /// # use tune::ratio::Ratio;
    /// use tune::note::PitchedNote;
    /// use tune::pitch::Pitched;
    ///
    /// let a4 = NoteLetter::A.in_octave(4);
    /// let a4_altered = a4.alter_pitch_by(Ratio::from_float(1.01));
    /// assert_eq!(a4_altered.note(), a4);
    /// assert_approx_eq!(a4_altered.pitch().as_hz(), 444.4);
    /// ```
    fn alter_pitch_by(self, delta: Ratio) -> NoteAtConcertPitch {
        let new_concert_pitch =
            ConcertPitch::from_note_and_pitch(self.note(), self.pitch() * delta);
        (self.note(), new_concert_pitch)
    }
}

impl PitchedNote for Note {
    fn note(self) -> Note {
        self
    }
}

/// Type alias for [`Note`]s that should sound at a [`Pitch`] different from standard 440 Hz tuning.
pub type NoteAtConcertPitch = (Note, ConcertPitch);

impl PitchedNote for NoteAtConcertPitch {
    fn note(self) -> Note {
        self.0
    }
}