Struct tune::key::PianoKey

source ·
pub struct PianoKey { /* private fields */ }
Expand description

A physical or logical key on a real or virtual instrument without any notion of a pitch.

This struct does not represent a musical key, like in “F minor”, which is why its name is PianoKey.

Implementations§

source§

impl PianoKey

source

pub fn from_midi_number(midi_number: impl Into<i32>) -> Self

Creates a PianoKey instance from the given MIDI number.

source

pub fn midi_number(self) -> i32

Returns the MIDI number of this PianoKey.

source

pub fn checked_midi_number(self) -> Option<u8>

Returns the MIDI number of this PianoKey if it is in the valid MIDI range [0..128).

Examples
assert_eq!(PianoKey::from_midi_number(-1).checked_midi_number(), None);
assert_eq!(PianoKey::from_midi_number(0).checked_midi_number(), Some(0));
assert_eq!(PianoKey::from_midi_number(64).checked_midi_number(), Some(64));
assert_eq!(PianoKey::from_midi_number(127).checked_midi_number(), Some(127));
assert_eq!(PianoKey::from_midi_number(128).checked_midi_number(), None);
source

pub fn keys_before( self, upper_bound: PianoKey ) -> impl DoubleEndedIterator<Item = PianoKey> + ExactSizeIterator<Item = PianoKey> + 'static

Iterates over all PianoKeys in the range [self..upper_bound).

Examples
let midi_key_62 = PianoKey::from_midi_number(62);
let midi_key_67 = PianoKey::from_midi_number(67);

assert_eq!(
    midi_key_62.keys_before(midi_key_67).collect::<Vec<_>>(),
    (62..67).map(PianoKey::from_midi_number).collect::<Vec<_>>()
);
assert!(midi_key_67.keys_before(midi_key_62).collect::<Vec<_>>().is_empty());
source

pub fn num_keys_before(self, other: PianoKey) -> i32

Counts the number of keys [left inclusive, right exclusive) between self and other.

Examples
let midi_key_62 = PianoKey::from_midi_number(62);
let midi_key_67 = PianoKey::from_midi_number(67);

assert_eq!(midi_key_62.num_keys_before(midi_key_67), 5);
assert_eq!(midi_key_67.num_keys_before(midi_key_62), -5);
source

pub fn plus_steps(self, num_steps: i32) -> PianoKey

Returns the key num_steps steps after self.

Examples
let midi_key_62 = PianoKey::from_midi_number(62);
let midi_key_67 = PianoKey::from_midi_number(67);

assert_eq!(midi_key_62.plus_steps(5), midi_key_67);
assert_eq!(midi_key_67.plus_steps(-5), midi_key_62);

Trait Implementations§

source§

impl Clone for PianoKey

source§

fn clone(&self) -> PianoKey

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 PianoKey

source§

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

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

impl Hash for PianoKey

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<S: Borrow<Scl>, K: Borrow<Kbm>> KeyboardMapping<PianoKey> for (S, K)

An (Scl, Kbm) pair has the complete information to define a KeyboardMapping.

Examples

use tune::tuning::KeyboardMapping;

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

let kbm = Kbm::builder(Note::from_midi_number(62))
   .push_mapped_key(0)
   .push_mapped_key(4)
   .push_unmapped_key()
   .push_mapped_key(4)
   .formal_octave(12)
   .build()
   .unwrap();

let f = |midi_number| (&scl, &kbm).maybe_pitch_of(PianoKey::from_midi_number(midi_number));
assert_approx_eq!(f(62).unwrap().as_hz(), 293.664768);
assert_approx_eq!(f(63).unwrap().as_hz(), 369.994423);
assert!(f(64).is_none());
assert_approx_eq!(f(65).unwrap().as_hz(), 369.994423);
assert_approx_eq!(f(66).unwrap().as_hz(), 587.329536);
source§

fn maybe_pitch_of(&self, key: PianoKey) -> Option<Pitch>

Returns the Pitch of the provided key or note.
source§

impl Ord for PianoKey

source§

fn cmp(&self, other: &PianoKey) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for PianoKey

source§

fn eq(&self, other: &PianoKey) -> 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 PartialOrd for PianoKey

source§

fn partial_cmp(&self, other: &PianoKey) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<S: Borrow<Scl>, K: Borrow<KbmRoot>> Tuning<PianoKey> for (S, K)

source§

fn pitch_of(&self, key: PianoKey) -> Pitch

Returns the Pitch of the given key or note K in the current Tuning. Read more
source§

fn find_by_pitch(&self, pitch: Pitch) -> Approximation<PianoKey>

Finds a closest key or note Approximation for the given Pitch in the current Tuning. Read more
source§

fn as_linear_mapping(self) -> LinearMapping<Self>
where Self: Sized,

Wraps self in a type adapter s.t. it can be used in functions that are generic over KeyboardMapping<K>.
source§

impl Copy for PianoKey

source§

impl Eq for PianoKey

source§

impl StructuralEq for PianoKey

source§

impl StructuralPartialEq for PianoKey

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.