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
//
// Copyright 2019-2020 Hans W. Uhlig, Richard I. Christopher. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use crate::types::PitchClass;
use std::fmt;

/// Sequence of [PitchClasses](musictheory::types::PitchClass) in modal order for a given
/// [PitchGroup](musictheory::types::PitchGroup).
#[derive(Copy, Clone, PartialOrd, PartialEq, Debug)]
pub enum PitchMode {
    Ionian([PitchClass; 7]),
    Dorian([PitchClass; 7]),
    Phrygian([PitchClass; 7]),
    Lydian([PitchClass; 7]),
    Mixolydian([PitchClass; 7]),
    Aeolian([PitchClass; 7]),
    Locrian([PitchClass; 7]),
}

impl PitchMode {
    pub(crate) const fn ionian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Ionian(notes)
    }
    pub(crate) const fn dorian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Dorian(notes)
    }
    pub(crate) const fn phrygian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Phrygian(notes)
    }
    pub(crate) const fn lydian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Lydian(notes)
    }
    pub(crate) const fn mixolydian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Mixolydian(notes)
    }
    pub(crate) const fn aeolian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Aeolian(notes)
    }
    pub(crate) const fn locrian(notes: [PitchClass; 7]) -> Self {
        PitchMode::Locrian(notes)
    }
    pub(crate) fn notes(&self) -> &[PitchClass; 7] {
        match self {
            PitchMode::Ionian(notes) => notes,
            PitchMode::Dorian(notes) => notes,
            PitchMode::Phrygian(notes) => notes,
            PitchMode::Lydian(notes) => notes,
            PitchMode::Mixolydian(notes) => notes,
            PitchMode::Aeolian(notes) => notes,
            PitchMode::Locrian(notes) => notes,
        }
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Tonic position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn tonic(&self) -> PitchClass {
        self.notes()[0]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Supertonic position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn supertonic(&self) -> PitchClass {
        self.notes()[1]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Mediant position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn mediant(&self) -> PitchClass {
        self.notes()[2]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Subdominant position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn subdominant(&self) -> PitchClass {
        self.notes()[3]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Dominant position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn dominant(&self) -> PitchClass {
        self.notes()[4]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Submediant position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn submediant(&self) -> PitchClass {
        self.notes()[5]
    }
    /// Get [PitchClass](musictheory::types::PitchClass) in the Subtonic position of this
    /// [PitchMode](musictheory::types::PitchMode).
    pub fn subtonic(&self) -> PitchClass {
        self.notes()[6]
    }
}

impl fmt::Display for PitchMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            PitchMode::Ionian(notes) => {
                format_args!("Ionian({:?})", notes).fmt(f)
            }
            PitchMode::Dorian(notes) => {
                format_args!("Dorian({:?})", notes).fmt(f)
            }
            PitchMode::Phrygian(notes) => {
                format_args!("Phrygian({:?})", notes).fmt(f)
            }
            PitchMode::Lydian(notes) => {
                format_args!("Lydian({:?})", notes).fmt(f)
            }
            PitchMode::Mixolydian(notes) => {
                format_args!("Mixolydian({:?})", notes).fmt(f)
            }
            PitchMode::Aeolian(notes) => {
                format_args!("Aeolian({:?})", notes).fmt(f)
            }
            PitchMode::Locrian(notes) => {
                format_args!("Locrian({:?})", notes).fmt(f)
            }
        }
    }
}