kord 0.8.1

A tool to easily explore music theory principles.
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! A module for working with modes.

use std::fmt::{Display, Error, Formatter};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use pest::Parser;

use crate::core::{
    base::{HasDescription, HasName, HasPreciseName, HasStaticName, Parsable, Res},
    chord::HasRoot,
    interval::{HasIntervals, Interval},
    mode_kind::ModeKind,
    note::Note,
    parser::{mode_name_str_to_mode_kind, note_str_to_note, ChordParser, Rule},
};

// Traits.

/// A trait that represents a type that has a mode kind.
pub trait HasModeKind {
    /// Returns the mode kind of the implementor (most likely a [`Mode`]).
    fn kind(&self) -> ModeKind;
}

// Struct.

/// A mode with a root note.
///
/// This combines a root note with a mode kind to produce an actual mode
/// with specific notes.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Mode {
    /// The root note of the mode.
    root: Note,
    /// The kind of mode.
    kind: ModeKind,
}

// Impls.

impl Mode {
    /// Creates a new mode with the given root note and mode kind.
    pub fn new(root: Note, kind: ModeKind) -> Self {
        Self { root, kind }
    }

    /// Returns the intervals of this mode (delegates to the mode kind).
    pub fn intervals(&self) -> &'static [Interval] {
        self.kind.intervals()
    }

    /// Returns the notes of this mode (root + each interval).
    pub fn notes(&self) -> Vec<Note> {
        self.intervals().iter().map(|&interval| self.root + interval).collect()
    }
}

impl HasRoot for Mode {
    fn root(&self) -> Note {
        self.root
    }
}

impl HasModeKind for Mode {
    fn kind(&self) -> ModeKind {
        self.kind
    }
}

impl HasIntervals for Mode {
    fn intervals(&self) -> &'static [Interval] {
        self.kind.intervals()
    }
}

impl HasStaticName for Mode {
    fn static_name(&self) -> &'static str {
        self.kind.static_name()
    }
}

impl HasName for Mode {
    fn name(&self) -> String {
        format!("{} {}", self.root.static_name(), self.kind.static_name())
    }
}

impl HasPreciseName for Mode {
    fn precise_name(&self) -> String {
        format!("{} {}", self.root.name(), self.kind.static_name())
    }
}

impl HasDescription for Mode {
    fn description(&self) -> &'static str {
        self.kind.description()
    }
}

impl Display for Mode {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        let notes = self.notes().iter().map(|n| n.static_name()).collect::<Vec<_>>().join(", ");
        write!(f, "{}\n   {}\n   {}", self.name(), self.description(), notes)
    }
}

impl Parsable for Mode {
    fn parse(input: &str) -> Res<Self>
    where
        Self: Sized,
    {
        let pairs = ChordParser::parse(Rule::mode, input)?;
        let root = pairs.clone().next().unwrap();

        assert_eq!(Rule::mode, root.as_rule());

        let mut components = root.into_inner();

        let note = components.next().unwrap();
        assert_eq!(Rule::note_atomic, note.as_rule());
        let root_note = note_str_to_note(note.as_str().trim())?;

        let mode_name = components.next().unwrap();
        assert_eq!(Rule::mode_name, mode_name.as_rule());
        let mode_kind = mode_name_str_to_mode_kind(mode_name.as_str())?;

        Ok(Mode::new(root_note, mode_kind))
    }
}

// Tests.

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::named_pitch::{HasLetter, HasNamedPitch};
    use crate::core::note::*;
    use pretty_assertions::assert_eq;

    impl Mode {
        /// Validates that the mode has correct enharmonic spelling.
        ///
        /// For 7-note modes (most modes), each letter A-G should appear exactly once.
        /// For other modes, no letter should repeat unless it's a chromatic/octatonic exception.
        pub(crate) fn validate_spelling(&self) -> Result<(), String> {
            use crate::core::named_pitch::{HasLetter, HasNamedPitch};
            use std::collections::HashMap;

            let notes = self.notes();
            let intervals_count = self.intervals().len();

            // For chromatic scale (12 notes), we allow letter repeats
            if intervals_count == 12 {
                return Ok(());
            }

            // Check for letter uniqueness
            let mut letter_counts: HashMap<&str, usize> = HashMap::new();
            for note in &notes {
                let letter = note.named_pitch().letter();
                *letter_counts.entry(letter).or_insert(0) += 1;
            }

            // For 7-note collections, we expect exactly one of each letter
            if intervals_count == 7 {
                if letter_counts.len() != 7 {
                    return Err(format!(
                        "{} {} has {} unique letters, expected 7. Letters: {:?}",
                        self.root().static_name(),
                        self.kind().static_name(),
                        letter_counts.len(),
                        notes.iter().map(|n| n.static_name()).collect::<Vec<_>>()
                    ));
                }

                for (letter, count) in &letter_counts {
                    if *count != 1 {
                        return Err(format!(
                            "{} {} has letter {} appearing {} times, expected 1. Notes: {:?}",
                            self.root().static_name(),
                            self.kind().static_name(),
                            letter,
                            count,
                            notes.iter().map(|n| n.static_name()).collect::<Vec<_>>()
                        ));
                    }
                }
            } else {
                // For non-7-note collections (pentatonic, etc.), just check no duplicates
                for (letter, count) in &letter_counts {
                    if *count > 1 {
                        return Err(format!(
                            "{} {} has letter {} appearing {} times. Notes: {:?}",
                            self.root().static_name(),
                            self.kind().static_name(),
                            letter,
                            count,
                            notes.iter().map(|n| n.static_name()).collect::<Vec<_>>()
                        ));
                    }
                }
            }

            Ok(())
        }
    }

    #[test]
    fn test_mode_creation() {
        let mode = Mode::new(D, ModeKind::Dorian);
        assert_eq!(mode.root(), D);
        assert_eq!(mode.kind(), ModeKind::Dorian);
    }

    #[test]
    fn test_mode_intervals() {
        let mode = Mode::new(D, ModeKind::Dorian);
        assert_eq!(mode.intervals().len(), 7);
        assert_eq!(
            mode.intervals(),
            &[
                Interval::PerfectUnison,
                Interval::MajorSecond,
                Interval::MinorThird,
                Interval::PerfectFourth,
                Interval::PerfectFifth,
                Interval::MajorSixth,
                Interval::MinorSeventh,
            ]
        );
    }

    #[test]
    fn test_mode_notes() {
        // D Dorian
        let mode = Mode::new(D, ModeKind::Dorian);
        assert_eq!(mode.notes(), vec![D, E, F, G, A, B, CFive]);

        // C Ionian (same as C major)
        let mode = Mode::new(C, ModeKind::Ionian);
        assert_eq!(mode.notes(), vec![C, D, E, F, G, A, B]);

        // E Phrygian
        let mode = Mode::new(E, ModeKind::Phrygian);
        assert_eq!(mode.notes(), vec![E, F, G, A, B, CFive, DFive]);

        // F Lydian
        let mode = Mode::new(F, ModeKind::Lydian);
        assert_eq!(mode.notes(), vec![F, G, A, B, CFive, DFive, EFive]);

        // G Mixolydian
        let mode = Mode::new(G, ModeKind::Mixolydian);
        assert_eq!(mode.notes(), vec![G, A, B, CFive, DFive, EFive, FFive]);

        // A Aeolian (natural minor)
        let mode = Mode::new(A, ModeKind::Aeolian);
        assert_eq!(mode.notes(), vec![A, B, CFive, DFive, EFive, FFive, GFive]);

        // B Locrian
        let mode = Mode::new(B, ModeKind::Locrian);
        assert_eq!(mode.notes(), vec![B, CFive, DFive, EFive, FFive, GFive, AFive]);
    }

    #[test]
    fn test_mode_names() {
        let mode = Mode::new(D, ModeKind::Dorian);
        assert_eq!(mode.name(), "D dorian");
        assert_eq!(mode.static_name(), "dorian");

        let mode = Mode::new(FSharp, ModeKind::Lydian);
        assert_eq!(mode.name(), "F♯ lydian");

        let mode = Mode::new(BFlat, ModeKind::Mixolydian);
        assert_eq!(mode.name(), "Bâ™­ mixolydian");
    }

    #[test]
    fn test_mode_display() {
        let mode = Mode::new(D, ModeKind::Dorian);
        let display = format!("{}", mode);
        assert!(display.contains("D dorian"));
        assert!(display.contains("D, E, F, G, A, B, C"));
        assert!(display.contains("dorian"));
    }

    #[test]
    fn test_all_modes_of_c_major() {
        // All modes of C major scale should contain the same note classes (C, D, E, F, G, A, B)
        // but starting from different degrees. Notes may be in different octaves.

        let c_ionian = Mode::new(C, ModeKind::Ionian);
        assert_eq!(c_ionian.notes(), vec![C, D, E, F, G, A, B]);

        let d_dorian = Mode::new(D, ModeKind::Dorian);
        assert_eq!(d_dorian.notes(), vec![D, E, F, G, A, B, CFive]);

        let e_phrygian = Mode::new(E, ModeKind::Phrygian);
        assert_eq!(e_phrygian.notes(), vec![E, F, G, A, B, CFive, DFive]);

        let f_lydian = Mode::new(F, ModeKind::Lydian);
        assert_eq!(f_lydian.notes(), vec![F, G, A, B, CFive, DFive, EFive]);

        let g_mixolydian = Mode::new(G, ModeKind::Mixolydian);
        assert_eq!(g_mixolydian.notes(), vec![G, A, B, CFive, DFive, EFive, FFive]);

        let a_aeolian = Mode::new(A, ModeKind::Aeolian);
        assert_eq!(a_aeolian.notes(), vec![A, B, CFive, DFive, EFive, FFive, GFive]);

        let b_locrian = Mode::new(B, ModeKind::Locrian);
        assert_eq!(b_locrian.notes(), vec![B, CFive, DFive, EFive, FFive, GFive, AFive]);
    }

    #[test]
    fn test_mode_characteristic_intervals() {
        // D Dorian characteristic: major 6th (B) in minor context
        let mode = Mode::new(D, ModeKind::Dorian);
        let notes = mode.notes();
        assert_eq!(notes[5], B); // 6th degree is major 6th

        // E Phrygian characteristic: minor 2nd (F)
        let mode = Mode::new(E, ModeKind::Phrygian);
        let notes = mode.notes();
        assert_eq!(notes[1], F); // 2nd degree is minor 2nd

        // F Lydian characteristic: augmented 4th (B)
        let mode = Mode::new(F, ModeKind::Lydian);
        let notes = mode.notes();
        assert_eq!(notes[3], B); // 4th degree is augmented 4th

        // B Locrian characteristic: diminished 5th (F)
        let mode = Mode::new(B, ModeKind::Locrian);
        let notes = mode.notes();
        assert_eq!(notes[4], FFive); // 5th degree is diminished 5th
    }

    #[test]
    fn test_mode_parse() {
        // Test parsing various modes
        let mode = Mode::parse("D dorian").unwrap();
        assert_eq!(mode.root(), D);
        assert_eq!(mode.kind(), ModeKind::Dorian);

        let mode = Mode::parse("C ionian").unwrap();
        assert_eq!(mode.root(), C);
        assert_eq!(mode.kind(), ModeKind::Ionian);

        let mode = Mode::parse("E phrygian").unwrap();
        assert_eq!(mode.root(), E);
        assert_eq!(mode.kind(), ModeKind::Phrygian);

        let mode = Mode::parse("F lydian").unwrap();
        assert_eq!(mode.root(), F);
        assert_eq!(mode.kind(), ModeKind::Lydian);

        let mode = Mode::parse("G mixolydian").unwrap();
        assert_eq!(mode.root(), G);
        assert_eq!(mode.kind(), ModeKind::Mixolydian);

        let mode = Mode::parse("A aeolian").unwrap();
        assert_eq!(mode.root(), A);
        assert_eq!(mode.kind(), ModeKind::Aeolian);

        let mode = Mode::parse("B locrian").unwrap();
        assert_eq!(mode.root(), B);
        assert_eq!(mode.kind(), ModeKind::Locrian);

        // Test with accidentals
        let mode = Mode::parse("F# dorian").unwrap();
        assert_eq!(mode.root(), FSharp);
        assert_eq!(mode.kind(), ModeKind::Dorian);

        let mode = Mode::parse("Bb lydian").unwrap();
        assert_eq!(mode.root(), BFlat);
        assert_eq!(mode.kind(), ModeKind::Lydian);
    }

    #[test]
    fn test_harmonic_minor_modes_parse() {
        // Test harmonic minor modes
        let mode = Mode::parse("B locrian nat6").unwrap();
        assert_eq!(mode.kind(), ModeKind::LocrianNatural6);

        let mode = Mode::parse("C ionian #5").unwrap();
        assert_eq!(mode.kind(), ModeKind::IonianSharp5);

        let mode = Mode::parse("D dorian sharp 4").unwrap();
        assert_eq!(mode.kind(), ModeKind::DorianSharp4);

        let mode = Mode::parse("E phrygian dominant").unwrap();
        assert_eq!(mode.kind(), ModeKind::PhrygianDominant);

        let mode = Mode::parse("F lydian #2").unwrap();
        assert_eq!(mode.kind(), ModeKind::LydianSharp2);

        let mode = Mode::parse("G# ultralocrian").unwrap();
        assert_eq!(mode.kind(), ModeKind::Ultralocrian);
    }

    #[test]
    fn test_melodic_minor_modes_parse() {
        // Test melodic minor modes
        let mode = Mode::parse("B dorian b2").unwrap();
        assert_eq!(mode.kind(), ModeKind::DorianFlat2);

        let mode = Mode::parse("C lydian augmented").unwrap();
        assert_eq!(mode.kind(), ModeKind::LydianAugmented);

        let mode = Mode::parse("D lydian dominant").unwrap();
        assert_eq!(mode.kind(), ModeKind::LydianDominant);

        let mode = Mode::parse("E mixolydian b6").unwrap();
        assert_eq!(mode.kind(), ModeKind::MixolydianFlat6);

        let mode = Mode::parse("F# locrian nat2").unwrap();
        assert_eq!(mode.kind(), ModeKind::LocrianNatural2);

        let mode = Mode::parse("G# altered").unwrap();
        assert_eq!(mode.kind(), ModeKind::Altered);
    }

    #[test]
    fn test_enharmonic_spelling_diatonic_modes() {
        // Test all diatonic modes with various root notes to ensure correct enharmonic spelling
        // Each 7-note mode should use each letter A-G exactly once

        // C Ionian - all natural notes
        let mode = Mode::new(C, ModeKind::Ionian);
        mode.validate_spelling().unwrap();

        // F# Dorian - should use sharps, not flats
        let mode = Mode::new(FSharp, ModeKind::Dorian);
        mode.validate_spelling().unwrap();
        let notes = mode.notes();
        // F# Dorian: F# G# A B C# D E#
        assert_eq!(notes[0].named_pitch().letter(), "F");
        assert_eq!(notes[1].named_pitch().letter(), "G");
        assert_eq!(notes[2].named_pitch().letter(), "A");
        assert_eq!(notes[3].named_pitch().letter(), "B");
        assert_eq!(notes[4].named_pitch().letter(), "C");
        assert_eq!(notes[5].named_pitch().letter(), "D");
        assert_eq!(notes[6].named_pitch().letter(), "E");

        // Db Lydian - should use flats
        let mode = Mode::new(DFlat, ModeKind::Lydian);
        mode.validate_spelling().unwrap();
        let notes = mode.notes();
        // Db Lydian: Db Eb F G Ab Bb C
        assert_eq!(notes[0].named_pitch().letter(), "D");
        assert_eq!(notes[1].named_pitch().letter(), "E");
        assert_eq!(notes[2].named_pitch().letter(), "F");
        assert_eq!(notes[3].named_pitch().letter(), "G");
        assert_eq!(notes[4].named_pitch().letter(), "A");
        assert_eq!(notes[5].named_pitch().letter(), "B");
        assert_eq!(notes[6].named_pitch().letter(), "C");

        // Bb Mixolydian
        let mode = Mode::new(BFlat, ModeKind::Mixolydian);
        mode.validate_spelling().unwrap();

        // E Locrian
        let mode = Mode::new(E, ModeKind::Locrian);
        mode.validate_spelling().unwrap();
    }

    #[test]
    fn test_enharmonic_spelling_harmonic_minor_modes() {
        // Test harmonic minor modes

        // F# Locrian Natural 6 - should spell with F# G A B C# D E#
        let mode = Mode::new(FSharp, ModeKind::LocrianNatural6);
        mode.validate_spelling().unwrap();

        // C Ionian #5
        let mode = Mode::new(C, ModeKind::IonianSharp5);
        mode.validate_spelling().unwrap();

        // D Dorian #4
        let mode = Mode::new(D, ModeKind::DorianSharp4);
        mode.validate_spelling().unwrap();

        // E Phrygian Dominant
        let mode = Mode::new(E, ModeKind::PhrygianDominant);
        mode.validate_spelling().unwrap();

        // F Lydian #2
        let mode = Mode::new(F, ModeKind::LydianSharp2);
        mode.validate_spelling().unwrap();

        // G# Ultralocrian
        let mode = Mode::new(GSharp, ModeKind::Ultralocrian);
        mode.validate_spelling().unwrap();
    }

    #[test]
    fn test_enharmonic_spelling_melodic_minor_modes() {
        // Test melodic minor modes

        // B Dorian b2
        let mode = Mode::new(B, ModeKind::DorianFlat2);
        mode.validate_spelling().unwrap();

        // C Lydian Augmented
        let mode = Mode::new(C, ModeKind::LydianAugmented);
        mode.validate_spelling().unwrap();

        // D Lydian Dominant
        let mode = Mode::new(D, ModeKind::LydianDominant);
        mode.validate_spelling().unwrap();

        // E Mixolydian b6
        let mode = Mode::new(E, ModeKind::MixolydianFlat6);
        mode.validate_spelling().unwrap();

        // F# Locrian natural 2
        let mode = Mode::new(FSharp, ModeKind::LocrianNatural2);
        mode.validate_spelling().unwrap();

        // G# Altered
        let mode = Mode::new(GSharp, ModeKind::Altered);
        mode.validate_spelling().unwrap();
    }

    #[test]
    fn test_enharmonic_spelling_all_roots() {
        // Test a few modes with all 12 root notes to ensure consistency
        for root in [C, CSharp, D, DFlat, DSharp, E, EFlat, F, FSharp, G, GFlat, GSharp, A, AFlat, ASharp, B, BFlat] {
            // Ionian (Major)
            let mode = Mode::new(root, ModeKind::Ionian);
            mode.validate_spelling().unwrap_or_else(|e| panic!("Ionian spelling failed for {}: {}", root.static_name(), e));

            // Dorian
            let mode = Mode::new(root, ModeKind::Dorian);
            mode.validate_spelling().unwrap_or_else(|e| panic!("Dorian spelling failed for {}: {}", root.static_name(), e));

            // Lydian
            let mode = Mode::new(root, ModeKind::Lydian);
            mode.validate_spelling().unwrap_or_else(|e| panic!("Lydian spelling failed for {}: {}", root.static_name(), e));
        }
    }

    #[test]
    fn test_mode_spelling() {
        let mode = Mode::new(E, ModeKind::PhrygianDominant);
        assert_eq!(mode.notes(), vec![E, F, GSharp, A, B, CFive, DFive], "E phrygian dominant spelling incorrect");
        mode.validate_spelling().unwrap();

        let mode = Mode::new(B, ModeKind::LocrianNatural6);
        assert_eq!(mode.notes(), vec![B, CFive, DFive, EFive, FFive, GSharpFive, AFive], "B locrian nat6 spelling incorrect");
        mode.validate_spelling().unwrap();

        let mode = Mode::new(D, ModeKind::LydianDominant);
        assert_eq!(mode.notes(), vec![D, E, FSharp, GSharp, A, B, CFive], "D lydian dominant spelling incorrect");
        mode.validate_spelling().unwrap();

        assert_eq!(
            mode.intervals(),
            &[
                Interval::PerfectUnison,
                Interval::MajorSecond,
                Interval::MajorThird,
                Interval::AugmentedFourth,
                Interval::PerfectFifth,
                Interval::MajorSixth,
                Interval::MinorSeventh,
            ]
        );
    }
}