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
use crate::{
    midi::MidiNote,
    note::{Accidental, Note, PitchNote},
    pitch::Pitch,
    Interval,
};

/// The degree of a `Scale`.
pub trait Degree: Copy {
    fn next_degree(self, interval: Interval) -> Self;
}

impl Degree for Pitch {
    fn next_degree(self, interval: Interval) -> Self {
        self + interval
    }
}

impl Degree for PitchNote {
    fn next_degree(self, interval: Interval) -> Self {
        let pitch = self.pitch().add_interval(interval);
        let letter = self.note().letter.next();
        let natural_pitch = Pitch::natural(letter);

        let accidental = if natural_pitch.into_byte() >= pitch.into_byte() {
            match natural_pitch.sub(pitch) {
                Interval::UNISON => Accidental::Natural,
                Interval::MINOR_SECOND => Accidental::Flat,
                Interval::MAJOR_SECOND => Accidental::DoubleFlat,
                Interval::MAJOR_SEVENTH => Accidental::Sharp,
                _ => unimplemented!(),
            }
        } else {
            match pitch.sub(natural_pitch) {
                Interval::MINOR_SECOND => Accidental::Sharp,
                Interval::MAJOR_SECOND => Accidental::DoubleSharp,
                _ => unimplemented!(),
            }
        };

        PitchNote::new_unchecked(pitch, Note::new(letter, accidental))
    }
}

impl Degree for MidiNote {
    fn next_degree(self, interval: Interval) -> Self {
        self + interval
    }
}