Struct Tuning

Source
pub struct Tuning {
    pub scale: Scale,
    pub keyboard_mapping: KeyboardMapping,
    /* private fields */
}
Expand description

The Tuning struct is the primary place where you will interact with this library.

It is constrcted for a scale and mapping and then gives you the ability to determine frequencies across and beyond the MIDI keyboard. Since modulation can force key number well outside the [0, 127] range, we support a MIDI note range from -256 to +256 spanning more than the entire frequency space reasonable.

To use this struct, you construct a fresh instance every time you want to use a different Scale and KeyboardMapping. If you want to tune to a different scale or mapping, just construct a new instance.

let s = Scale::even_temperament_12_note_scale(); // or any other function constructing a Scale
let k = KeyboardMapping::tune_a69_to(432.0); // or any other function constructing a KeyboardMapping

let t1 = Tuning::from_scale(s.clone());
let t2 = Tuning::from_keyboard_mapping(k.clone());
let t3 = Tuning::from_scale_and_keyboard_mapping(s.clone(), k.clone(), AllowTuningOnUnmapped(false));

Fields§

§scale: Scale

Scale of the tuning.

§keyboard_mapping: KeyboardMapping

Keyboard mapping of the tuning.

Implementations§

Source§

impl Tuning

Source

pub fn new() -> Self

Constucts a Tuning with 12-EDO scale and standard mapping.

Source

pub fn from_scale(scale: Scale) -> Result<Self, TuningError>

Constructs a Tuning with given scale and standard mapping.

Source

pub fn from_keyboard_mapping( keyboard_mapping: KeyboardMapping, ) -> Result<Self, TuningError>

Constructs a Tuning with 12-EDO scale and given keyboard_mapping.

Source

pub fn from_scale_and_keyboard_mapping( scale: Scale, keyboard_mapping: KeyboardMapping, allow_tuning_center_on_unmapped: AllowTuningOnUnmapped, ) -> Result<Self, TuningError>

Constructs a Tuning with given scale and keyboard_mapping.

Source

pub fn with_skipped_notes_interpolated(&self) -> Self

Fills unmapped notes with interpolated vaules.

Source

pub fn frequency_for_midi_note(&self, midi_note: i32) -> f64

Retunrs the frequency in Hz for a given MIDI note.

let t = Tuning::new();
assert!((t.frequency_for_midi_note(69) - 440.0).abs() < 1e-4); // A
assert!((t.frequency_for_midi_note(60) - 261.6256).abs() < 1e-4); // middle C
Source

pub fn frequency_for_midi_note_scaled_by_midi0(&self, midi_note: i32) -> f64

Returns the frequency but with the standard frequency of MIDI note 0 divided out.

let t = Tuning::new();
assert_eq!(t.frequency_for_midi_note_scaled_by_midi0(0), 1.0);
assert_eq!(t.frequency_for_midi_note_scaled_by_midi0(60), 32.0);
Source

pub fn log_scaled_frequency_for_midi_note(&self, midi_note: i32) -> f64

Returns the log base 2 of the scaled frequency.

let t = Tuning::new();
assert_eq!(t.log_scaled_frequency_for_midi_note(0), 0.0);
assert_eq!(t.log_scaled_frequency_for_midi_note(60), 5.0);

The value increases by one per frequency double.

Source

pub fn scale_position_for_midi_note(&self, midi_note: i32) -> i32

Returns the space in the logical scale. Note 0 is the root. It has a maximum value of count-1. Note that SCL files omit the root internally and so this logical scale position is off by 1 from the index in the tones vector of the Scale data.

Source

pub fn is_midi_note_mapped(&self, midi_note: i32) -> bool

Returns whether a given midi_note is mapped in the Tuning’s KeyboardMapping.

Trait Implementations§

Source§

impl Clone for Tuning

Source§

fn clone(&self) -> Tuning

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tuning

Source§

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

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

impl Default for Tuning

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Tuning

§

impl RefUnwindSafe for Tuning

§

impl Send for Tuning

§

impl Sync for Tuning

§

impl Unpin for Tuning

§

impl UnwindSafe for Tuning

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.