Minimal, no_std-friendly Rust library for working with musical pitches, frequencies, MIDI numbers, and symbolic note representations.
- Convert between musical frequencies (Hz) and MIDI note numbers
- Transpose notes by semitones or octaves
- Convert ASCII note names like "C#4" or "Db3" into pitch (frequency-based) representations
- Reconstruct symbolic notes (e.g., "C#4") from pitch via default sharp-based spelling
- Extract musical components like note letter, accidental, and octave
- Lightweight and
no_stdcompatible (via feature flag)
Add to your Cargo.toml:
[dependencies]
pitchy = "0.2"Or, to use in no_std mode:
[dependencies.pitchy]
version = "0.2"
default-features = false
features = ["libm"]use pitchy::Pitch;
use core::str::FromStr;
let pitch = Pitch::from_str("A4").unwrap();
assert_eq!(pitch.try_midi_number().unwrap(), 69);
assert_eq!(pitch.frequency(), 440.0);let pitch = Pitch::from_str("C4").unwrap();
let up = pitch.transpose(4.0);
assert_eq!(up.try_midi_number().unwrap(), 64); // E4let pitch = Pitch::try_from_midi_number(60).unwrap();
let actual = pitch.frequency(); // 261.6255653005986
let expected = 261.625565; // C4
let epsilon = 1e-6; // 0.000001
assert!((actual - expected).abs() < epsilon);use pitchy::{Pitch, Note};
let pitch = Pitch::new(277.183); // C#4
let note = Note::try_from(pitch).unwrap();
#[cfg(feature = "std")]
assert_eq!(note.name(), "C#4");
assert_eq!(note.letter(), pitchy::NoteLetter::C);
assert_eq!(note.accidental(), pitchy::Accidental::Sharp);
assert_eq!(note.octave(), 4);std(enabled by default): enables note name formattinglibm: enables thelibmmath backend used inno_stdmode
To build without std, use:
cargo build --no-default-features --features libmMIT © paramako