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
impl PianoKey
sourcepub fn from_midi_number(midi_number: impl Into<i32>) -> Self
pub fn from_midi_number(midi_number: impl Into<i32>) -> Self
Creates a PianoKey
instance from the given MIDI number.
sourcepub fn midi_number(self) -> i32
pub fn midi_number(self) -> i32
Returns the MIDI number of this PianoKey
.
sourcepub fn checked_midi_number(self) -> Option<u8>
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);
sourcepub fn keys_before(
self,
upper_bound: PianoKey
) -> impl DoubleEndedIterator<Item = PianoKey> + ExactSizeIterator<Item = PianoKey> + 'static
pub fn keys_before( self, upper_bound: PianoKey ) -> impl DoubleEndedIterator<Item = PianoKey> + ExactSizeIterator<Item = PianoKey> + 'static
Iterates over all PianoKey
s 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());
sourcepub fn num_keys_before(self, other: PianoKey) -> i32
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);
sourcepub fn plus_steps(self, num_steps: i32) -> PianoKey
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<S: Borrow<Scl>, K: Borrow<Kbm>> KeyboardMapping<PianoKey> for (S, K)
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§impl Ord for PianoKey
impl Ord for PianoKey
source§impl PartialEq for PianoKey
impl PartialEq for PianoKey
source§impl PartialOrd for PianoKey
impl PartialOrd for PianoKey
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moresource§impl<S: Borrow<Scl>, K: Borrow<KbmRoot>> Tuning<PianoKey> for (S, K)
impl<S: Borrow<Scl>, K: Borrow<KbmRoot>> Tuning<PianoKey> for (S, K)
source§fn find_by_pitch(&self, pitch: Pitch) -> Approximation<PianoKey>
fn find_by_pitch(&self, pitch: Pitch) -> Approximation<PianoKey>
source§fn as_linear_mapping(self) -> LinearMapping<Self>where
Self: Sized,
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>
.impl Copy for PianoKey
impl Eq for PianoKey
impl StructuralEq for PianoKey
impl StructuralPartialEq for PianoKey
Auto Trait Implementations§
impl RefUnwindSafe for PianoKey
impl Send for PianoKey
impl Sync for PianoKey
impl Unpin for PianoKey
impl UnwindSafe for PianoKey
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more