pub struct Note { /* private fields */ }
Expand description
A musical note encapsulating a clearly defined pitch.
The pitch can be derived using the Pitched
impl on the Note
type itself, assuming
standard 440 Hz tuning, or on NoteAtConcertPitch
, given a specific concert pitch.
Implementations
sourceimpl Note
impl Note
sourcepub fn from_midi_number(midi_number: impl Into<i32>) -> Self
pub fn from_midi_number(midi_number: impl Into<i32>) -> Self
Creates a Note
instance from the given MIDI number.
sourcepub fn from_letter_and_octave(
note_letter: NoteLetter,
octave: impl Into<Octave>
) -> Self
pub fn from_letter_and_octave(
note_letter: NoteLetter,
octave: impl Into<Octave>
) -> Self
Creates a Note
instance given a NoteLetter
and an octave.
Examples
let a4 = Note::from_midi_number(69);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, 4), a4);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, Octave::from_octave_number(4)), a4);
assert_eq!(Note::from_letter_and_octave(NoteLetter::A, HelmholtzOctave::OneLined), a4);
sourcepub fn from_piano_key(piano_key: PianoKey) -> Self
pub fn from_piano_key(piano_key: PianoKey) -> Self
sourcepub fn midi_number(self) -> i32
pub fn midi_number(self) -> i32
Returns the MIDI number of this Note
.
sourcepub fn checked_midi_number(self) -> Option<u8>
pub fn checked_midi_number(self) -> Option<u8>
Returns the MIDI number of this Note
if it is in the valid MIDI range [0..128).
Examples
assert_eq!(Note::from_midi_number(-1).checked_midi_number(), None);
assert_eq!(Note::from_midi_number(0).checked_midi_number(), Some(0));
assert_eq!(Note::from_midi_number(64).checked_midi_number(), Some(64));
assert_eq!(Note::from_midi_number(127).checked_midi_number(), Some(127));
assert_eq!(Note::from_midi_number(128).checked_midi_number(), None);
sourcepub fn letter_and_octave(self) -> (NoteLetter, Octave)
pub fn letter_and_octave(self) -> (NoteLetter, Octave)
Splits the current note into a NoteLetter
and an Octave
part.
Examples
let a4 = Note::from_midi_number(69);
assert_eq!(a4.letter_and_octave(), (NoteLetter::A, Octave::from_octave_number(4)));
let midi_root = Note::from_midi_number(0);
assert_eq!(midi_root.letter_and_octave(), (NoteLetter::C, Octave::from_octave_number(-1)));
sourcepub fn as_piano_key(self) -> PianoKey
pub fn as_piano_key(self) -> PianoKey
Retrieves the associated PianoKey
assuming standard 12-EDO tuning.
sourcepub fn at_pitch(self, pitched: impl Pitched) -> NoteAtConcertPitch
pub fn at_pitch(self, pitched: impl Pitched) -> NoteAtConcertPitch
Creates a NoteAtConcertPitch
instance with self
sounding at a different pitch.
Examples
use tune::pitch::Pitched;
let c4_at_260_hz = NoteLetter::C.in_octave(4).at_pitch(Pitch::from_hz(260.0));
assert_approx_eq!(c4_at_260_hz.pitch().as_hz(), 260.0);
let (_note, concert_pitch) = c4_at_260_hz;
assert_approx_eq!(concert_pitch.a4_pitch().as_hz(), 437.266136);
sourcepub fn at_concert_pitch(self, concert_pitch: ConcertPitch) -> NoteAtConcertPitch
pub fn at_concert_pitch(self, concert_pitch: ConcertPitch) -> NoteAtConcertPitch
Convenience function creating a NoteAtConcertPitch
instance.
Examples
let a4 = NoteLetter::A.in_octave(4);
let concert_pitch = ConcertPitch::from_a4_pitch(Pitch::from_hz(435.0));
assert_eq!(a4.at_concert_pitch(concert_pitch), (a4, concert_pitch));
sourcepub fn notes_before(
self,
upper_bound: Note
) -> impl DoubleEndedIterator<Item = Note> + ExactSizeIterator<Item = Note> + 'static
pub fn notes_before(
self,
upper_bound: Note
) -> impl DoubleEndedIterator<Item = Note> + ExactSizeIterator<Item = Note> + 'static
Iterates over all Note
s in the range [self
..upper_bound
).
Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);
assert_eq!(
midi_note_62.notes_before(midi_note_67).collect::<Vec<_>>(),
(62..67).map(Note::from_midi_number).collect::<Vec<_>>()
);
assert!(midi_note_67.notes_before(midi_note_62).collect::<Vec<_>>().is_empty());
sourcepub fn num_semitones_before(self, other: Note) -> i32
pub fn num_semitones_before(self, other: Note) -> i32
Counts the number of semitones [left inclusive, right exclusive) between self
and other
.
Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);
assert_eq!(midi_note_62.num_semitones_before(midi_note_67), 5);
assert_eq!(midi_note_67.num_semitones_before(midi_note_62), -5);
sourcepub fn plus_semitones(self, num_semitones: i32) -> Note
pub fn plus_semitones(self, num_semitones: i32) -> Note
Returns the note num_semitones
semitones above self
.
Examples
let midi_note_62 = Note::from_midi_number(62);
let midi_note_67 = Note::from_midi_number(67);
assert_eq!(midi_note_62.plus_semitones(5), midi_note_67);
assert_eq!(midi_note_67.plus_semitones(-5), midi_note_62);
Trait Implementations
sourceimpl Display for Note
impl Display for Note
Note
s are rendered in scientific pitch notation.
Examples
assert_eq!(Note::from_midi_number(0).to_string(), "C -1");
assert_eq!(Note::from_midi_number(69).to_string(), "A 4");
assert_eq!(Note::from_midi_number(70).to_string(), "A#/Bb 4");
assert_eq!(Note::from_midi_number(71).to_string(), "B 4");
assert_eq!(Note::from_midi_number(72).to_string(), "C 5");
assert_eq!(Note::from_midi_number(127).to_string(), "G 9");
// Format flags
assert_eq!(format!("{:+}", Note::from_midi_number(70)), "A# 4");
assert_eq!(format!("{:-}", Note::from_midi_number(70)), "Bb 4");
assert_eq!(format!("{:10}", Note::from_midi_number(70)), "A#/Bb 4 ");
assert_eq!(format!("{:<10}", Note::from_midi_number(70)), "A#/Bb 4 ");
assert_eq!(format!("{:>10}", Note::from_midi_number(70)), " A#/Bb 4");
sourceimpl Ord for Note
impl Ord for Note
sourceimpl PartialOrd<Note> for Note
impl PartialOrd<Note> for Note
sourcefn partial_cmp(&self, other: &Note) -> Option<Ordering>
fn partial_cmp(&self, other: &Note) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn 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 more
sourceimpl PitchedNote for Note
impl PitchedNote for Note
sourceimpl Tuning<Note> for ConcertPitch
impl Tuning<Note> for ConcertPitch
A ConcertPitch
maps Note
s to Pitch
es and is considered a Tuning
.
Examples
use tune::tuning::Tuning;
let c4 = NoteLetter::C.in_octave(4);
let a4 = NoteLetter::A.in_octave(4);
let standard_tuning = ConcertPitch::default();
assert_approx_eq!(standard_tuning.pitch_of(c4).as_hz(), 261.625565);
assert_approx_eq!(standard_tuning.pitch_of(a4).as_hz(), 440.0);
let healing_tuning = ConcertPitch::from_a4_pitch(Pitch::from_hz(432.0));
assert_approx_eq!(healing_tuning.pitch_of(c4).as_hz(), 256.868737);
assert_approx_eq!(healing_tuning.pitch_of(a4).as_hz(), 432.0);
sourcefn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
Finds a closest key or note Approximation
for the given Pitch
in the current Tuning
. Read more
sourcefn 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>
.
sourceimpl Tuning<Note> for ()
impl Tuning<Note> for ()
Convenience implementation enabling to write ()
instead of ConcertPitch::default()
.
Examples
use tune::pitch::Pitched;
assert_eq!(Pitch::from_hz(880.0).find_in_tuning(()).approx_value, Note::from_midi_number(81));
sourcefn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
Finds a closest key or note Approximation
for the given Pitch
in the current Tuning
. Read more
sourcefn 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 Note
impl Eq for Note
impl StructuralEq for Note
impl StructuralPartialEq for Note
Auto Trait Implementations
impl RefUnwindSafe for Note
impl Send for Note
impl Sync for Note
impl Unpin for Note
impl UnwindSafe for Note
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more