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§
source§impl 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§
source§impl 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");
source§impl Ord for Note
impl Ord for Note
source§impl PartialEq for Note
impl PartialEq for Note
source§impl PartialOrd for Note
impl PartialOrd for Note
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PitchedNote for Note
impl PitchedNote for Note
source§impl 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));
source§fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
source§fn as_linear_mapping(self) -> LinearMapping<Self>where
Self: Sized,
fn as_linear_mapping(self) -> LinearMapping<Self>where
Self: Sized,
self
in a type adapter s.t. it can be used in functions that are generic over KeyboardMapping<K>
.source§impl 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);
source§fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
fn find_by_pitch(&self, pitch: Pitch) -> Approximation<Note>
source§fn as_linear_mapping(self) -> LinearMapping<Self>where
Self: Sized,
fn as_linear_mapping(self) -> LinearMapping<Self>where
Self: Sized,
self
in a type adapter s.t. it can be used in functions that are generic over KeyboardMapping<K>
.