Struct tune::note::Note

source ·
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

source

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

Creates a Note instance from the given MIDI number.

source

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);
source

pub fn from_piano_key(piano_key: PianoKey) -> Self

Creates a Note instance from a PianoKey assuming standard 12-EDO tuning.

source

pub fn midi_number(self) -> i32

Returns the MIDI number of this Note.

source

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);
source

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)));
source

pub fn as_piano_key(self) -> PianoKey

Retrieves the associated PianoKey assuming standard 12-EDO tuning.

source

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);
source

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));
source

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

Iterates over all Notes 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());
source

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);
source

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 Clone for Note

source§

fn clone(&self) -> Note

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 Note

source§

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

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

impl Display for Note

Notes 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§

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

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

impl Hash for Note

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 Ord for Note

source§

fn cmp(&self, other: &Note) -> 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 Note

source§

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

source§

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 · 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 Pitched for Note

source§

fn pitch(&self) -> Pitch

Retrieves the Pitch of the Pitched object. Read more
source§

fn find_in_tuning<K, T: Tuning<K>>(&self, tuning: T) -> Approximation<K>

Finds a key or note for any Pitched object in the given tuning. Read more
source§

impl PitchedNote for Note

source§

fn note(&self) -> Note

Retrieves the Note part of self. Read more
source§

fn alter_pitch_by(&self, delta: Ratio) -> NoteAtConcertPitch

Returns a new PitchedNote with the same Note part but a Pitch altered by delta. Read more
source§

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 pitch_of(&self, note: Note) -> 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<Note>

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 Tuning<Note> for ConcertPitch

A ConcertPitch maps Notes to Pitches 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 pitch_of(&self, note: Note) -> 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<Note>

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 Note

source§

impl Eq for Note

source§

impl StructuralEq for Note

source§

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§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.