Skip to main content

music21_rs/scale/
mod.rs

1use std::{cmp::Ordering, collections::HashMap};
2
3use crate::{defaults::IntegerType, stepname::StepName};
4
5pub(crate) mod concretescale;
6/// Diatonic scale construction and harmonization helpers.
7pub mod diatonicscale;
8pub mod hexatonicblues;
9/// The named scales music21 exposes, realized from a tonic.
10pub(crate) mod realized;
11pub mod scaletype;
12pub mod stepscale;
13
14pub use diatonicscale::DiatonicScale;
15pub use hexatonicblues::{BluesForm, WeightedHexatonicBlues};
16pub use scaletype::{
17    DegreeComparison, HUMDRUM_SOLFEG_SYLLABLES, SOLFEG_SYLLABLES, Scale, ScaleType, SolfegVariant,
18};
19pub use stepscale::StepScale;
20
21pub(crate) const FIFTHS_ORDER_SHARP: [StepName; 7] = [
22    StepName::F,
23    StepName::C,
24    StepName::G,
25    StepName::D,
26    StepName::A,
27    StepName::E,
28    StepName::B,
29];
30pub(crate) const FIFTHS_ORDER_FLAT: [StepName; 7] = [
31    StepName::B,
32    StepName::E,
33    StepName::A,
34    StepName::D,
35    StepName::G,
36    StepName::C,
37    StepName::F,
38];
39
40pub(crate) fn altered_steps_from_sharps(sharps: IntegerType) -> HashMap<StepName, IntegerType> {
41    let mut map = HashMap::new();
42    // Past seven the circle comes round again and every step takes another
43    // accidental: A double-flat major has eleven flats, four of them double.
44    match sharps.cmp(&0) {
45        Ordering::Greater => {
46            for step in FIFTHS_ORDER_SHARP.iter().cycle().take(sharps as usize) {
47                *map.entry(*step).or_insert(0) += 1;
48            }
49        }
50        Ordering::Less => {
51            for step in FIFTHS_ORDER_FLAT.iter().cycle().take((-sharps) as usize) {
52                *map.entry(*step).or_insert(0) -= 1;
53            }
54        }
55        Ordering::Equal => {}
56    }
57    map
58}