Struct tune::scala::KbmRoot

source ·
pub struct KbmRoot {
    pub ref_key: PianoKey,
    pub ref_pitch: Pitch,
    pub root_offset: i32,
}
Expand description

Defines an absolute horizontal and vertical location of a scale.

KbmRoot is intended to be used in combination with Scl to form a Tuning. The interesting thing about a Tuning is that it offers a bidirectional key-to-pitch mapping. This means it is possible to find the best matching PianoKey for a given Pitch input. The pitch input can be a continuous value, e.g. the location of a mouse pointer.

In order to enable invertibility the mapping described by KbmRoot is linear. In other words, the keyboard mapping degree and the scale degree are the same number. If the mapping is required to be non-linear KbmRoot needs to be surrounded by the more general Kbm struct.

Fields§

§ref_key: PianoKey

The PianoKey that maps to degree 0 of the keyboard mapping. If a Kbm surrounding is used with the first entry being n, ref_key maps to scale degree n.

§ref_pitch: Pitch

A Pitch that is guaranteed to be present in a Tuning but which might be skipped in the KeyboardMapping spanned by the Kbm surrounding.

§root_offset: i32

The amount by which the scale’s root is displaced wrt. to ref_key.

Implementations§

source§

impl KbmRoot

source

pub fn shift_ref_key_by(self, num_degrees: i32) -> Self

Shifts the ref_key of a scale by num_degrees correcting the scale’s vertical location.

Examples
let kbm_root =  KbmRoot {
    ref_key: PianoKey::from_midi_number(67),
    ref_pitch: Pitch::from_hz(432.0),
    root_offset: -2,
};

let shifted = kbm_root.shift_ref_key_by(-7);

assert_eq!(shifted.ref_key, PianoKey::from_midi_number(60));
assert_approx_eq!(shifted.ref_pitch.as_hz(), 288.325409);
assert_eq!(shifted.root_offset, -2);
source

pub fn to_kbm(self) -> Kbm

Creates a quasi-equivalent Kbm surrounding which can be exported.

Examples
let kbm_root = KbmRoot::from(Note::from_midi_number(62));
let kbm = kbm_root.to_kbm();

assert_eq!(kbm.kbm_root(), kbm_root);
assert_eq!(kbm.range(), PianoKey::from_midi_number(0)..PianoKey::from_midi_number(128));
assert_eq!(kbm.formal_octave(), 1);
assert_eq!(kbm.num_items(), 1);
assert_eq!(kbm.scale_degree_of(PianoKey::from_midi_number(61)), Some(-1));
assert_eq!(kbm.scale_degree_of(PianoKey::from_midi_number(62)), Some(0));
assert_eq!(kbm.scale_degree_of(PianoKey::from_midi_number(63)), Some(1));

let exported = kbm.export();

Trait Implementations§

source§

impl Clone for KbmRoot

source§

fn clone(&self) -> KbmRoot

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for KbmRoot

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: PitchedNote> From<N> for KbmRoot

source§

fn from(note: N) -> Self

Converts to this type from the input type.
source§

impl FromStr for KbmRoot

§

type Err = String

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for KbmRoot

source§

fn eq(&self, other: &KbmRoot) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for KbmRoot

source§

impl StructuralPartialEq for KbmRoot

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.