Skip to content

Minimalistic Rust library for working with musical notes, frequencies (Hz), MIDI numbers, and pitch operations like transposition and octave shifts.

License

Notifications You must be signed in to change notification settings

paramako/pitchy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pitchy

Crates.io Docs.rs License: MIT Changelog

Minimal, no_std-friendly Rust library for working with musical pitches, frequencies, MIDI numbers, and symbolic note representations.


✨ Features

  • 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_std compatible (via feature flag)

🚀 Usage

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"]

🔧 Example Usage

Parse and Analyze a Note

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

Transpose a Note

let pitch = Pitch::from_str("C4").unwrap();
let up = pitch.transpose(4.0);
assert_eq!(up.try_midi_number().unwrap(), 64); // E4

Convert from MIDI Number

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

Convert Pitch to Note

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

⚙️ Optional Features

  • std (enabled by default): enables note name formatting
  • libm: enables the libm math backend used in no_std mode

To build without std, use:

cargo build --no-default-features --features libm

📄 License

MIT © paramako

About

Minimalistic Rust library for working with musical notes, frequencies (Hz), MIDI numbers, and pitch operations like transposition and octave shifts.

Topics

Resources

License

Stars

Watchers

Forks

Languages