Struct tune::scala::Scl[][src]

pub struct Scl { /* fields omitted */ }
Expand description

Scale format according to http://www.huygens-fokker.org/scala/scl_format.html.

The Scl format describes a periodic scale in relative pitches. You can access those pitches using Scl::relative_pitch_of. To retrieve absolute Pitch information, you need to pair the Scl struct with a Kbm or KbmRoot struct (see implementations of the Tuning or KeyboardMapping trait for more info).

Examples

use tune::tuning::Tuning;

let scl = scala::create_harmonics_scale(None, 8, 8, false).unwrap();
let kbm = KbmRoot::from(Note::from_midi_number(43).at_pitch(Pitch::from_hz(100.0)));
let tuning = (scl, kbm);

assert_approx_eq!(tuning.pitch_of(PianoKey::from_midi_number(43)).as_hz(), 100.0);
assert_approx_eq!(tuning.pitch_of(PianoKey::from_midi_number(44)).as_hz(), 112.5);
assert_approx_eq!(tuning.pitch_of(PianoKey::from_midi_number(45)).as_hz(), 125.0);

Implementations

Retrieves relative pitches without requiring any Kbm reference.

Examples
let scl = Scl::builder()
    .push_cents(100.0)
    .push_cents(50.0)
    .push_cents(150.0)
    .build().unwrap();

assert_approx_eq!(scl.relative_pitch_of(0).as_cents(), 0.0);
assert_approx_eq!(scl.relative_pitch_of(1).as_cents(), 100.0);
assert_approx_eq!(scl.relative_pitch_of(2).as_cents(), 50.0);
assert_approx_eq!(scl.relative_pitch_of(3).as_cents(), 150.0);
assert_approx_eq!(scl.relative_pitch_of(4).as_cents(), 250.0);

Retrieves relative pitches in ascending order without requiring any Kbm reference.

Examples
let scl = Scl::builder()
    .push_cents(100.0)
    .push_cents(50.0)
    .push_cents(150.0)
    .build().unwrap();

assert_approx_eq!(scl.sorted_relative_pitch_of(0).as_cents(), 0.0);
assert_approx_eq!(scl.sorted_relative_pitch_of(1).as_cents(), 50.0);
assert_approx_eq!(scl.sorted_relative_pitch_of(2).as_cents(), 100.0);
assert_approx_eq!(scl.sorted_relative_pitch_of(3).as_cents(), 150.0);
assert_approx_eq!(scl.sorted_relative_pitch_of(4).as_cents(), 200.0);

Finds the approximate degree of a relative pitch without requiring any Kbm reference.

Examples
let scl = Scl::builder()
    .push_cents(100.0)
    .push_cents(50.0)
    .push_cents(150.0)
    .build().unwrap();

assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(0.0)).approx_value, 0);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(10.0)).approx_value, 0);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(40.0)).approx_value, 2);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(50.0)).approx_value, 2);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(100.0)).approx_value, 1);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(150.0)).approx_value, 3);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(200.0)).approx_value, 5);
assert_eq!(scl.find_by_relative_pitch(Ratio::from_cents(250.0)).approx_value, 4);

Finds the approximate degree of a relative pitch in ascending order without requiring any Kbm reference.

Examples
let scl = Scl::builder()
    .push_cents(100.0)
    .push_cents(50.0)
    .push_cents(150.0)
    .build().unwrap();

assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(0.0)).approx_value, 0);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(10.0)).approx_value, 0);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(40.0)).approx_value, 1);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(50.0)).approx_value, 1);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(100.0)).approx_value, 2);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(150.0)).approx_value, 3);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(200.0)).approx_value, 4);
assert_eq!(scl.find_by_relative_pitch_sorted(Ratio::from_cents(250.0)).approx_value, 5);

Imports the given file in SCL format.

let scl_file = &[
    "!A comment",
    "  !An indented comment",
    "  Example scale  ",
    "7",
    "100.",
    "150.0 ignore text after first whitespace",
    "  ", // ignore blank line
    "!175.0 ignore whole line",
    "200.0 .ignore additional dots",
    "  6/5  ",
    "5/4 (ignore parentheses)",
    "3/2 /ignore additional slashes",
    "2",
];

let scl = Scl::import(scl_file.join("\n").as_bytes()).unwrap();

assert_eq!(scl.description(), "Example scale");
assert_eq!(scl.num_items(), 7);
assert_approx_eq!(scl.relative_pitch_of(0).as_cents(), 0.0);
assert_approx_eq!(scl.relative_pitch_of(1).as_cents(), 100.0);
assert_approx_eq!(scl.relative_pitch_of(2).as_cents(), 150.0);
assert_approx_eq!(scl.relative_pitch_of(3).as_cents(), 200.0);
assert_approx_eq!(scl.relative_pitch_of(4).as_float(), 6.0 / 5.0);
assert_approx_eq!(scl.relative_pitch_of(5).as_float(), 5.0 / 4.0);
assert_approx_eq!(scl.relative_pitch_of(6).as_float(), 3.0 / 2.0);
assert_approx_eq!(scl.relative_pitch_of(7).as_float(), 2.0);
assert_approx_eq!(scl.period().as_float(), 2.0);

Exports the current scale in SCL file format.

Examples
let scl = Scl::builder()
    .push_cents(100.0)
    .push_ratio("1:13:3".parse().unwrap())
    .push_fraction(4, 3)
    .push_int(2)
    .build_with_description("Example scale")
    .unwrap();

assert_eq!(
    format!("{}", scl.export()).lines().collect::<Vec<_>>(),
    ["Example scale", "4", "100.000", "146.304", "4/3", "2"]
);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.