CalibraMusic

Music theory utilities for pitch and note conversions.

What is CalibraMusic?

CalibraMusic provides conversion functions between different musical pitch representations:

  • Hz (Hertz): Physical frequency (e.g., 440.0 Hz)

  • MIDI note numbers: Integer scale (60 = middle C, 69 = A4)

  • Note labels: Human-readable (e.g., "C4", "A#5", "Bb3")

  • Cents: Fine pitch deviation (100 cents = 1 semitone)

Use it when you need to:

  • Display detected pitch as a note name

  • Calculate pitch deviation from a target note

  • Convert between different pitch units for analysis

When to Use

ScenarioUse This?Why
Convert Hz to note nameYeshzToNoteLabel(440f) → "A4"
Show pitch deviationYescentsDeviation(442f) → +7.8 cents
Create note frequenciesYesnoteLabelToHz("C4") → 261.63 Hz
Detect pitch from audioNoUse CalibraPitch

Quick Start

Kotlin

// Convert frequency to note name
val noteLabel = CalibraMusic.hzToNoteLabel(440f) // "A4"

// Convert note name to frequency
val frequency = CalibraMusic.noteLabelToHz("C4") // 261.63 Hz

// Get cents deviation from nearest note
val deviation = CalibraMusic.centsDeviation(442f) // +7.8 cents (sharp)

// Convert between MIDI and Hz
val midi = CalibraMusic.hzToMidi(440f) // 69.0
val hz = CalibraMusic.midiToHz(60f) // 261.63 Hz (middle C)

Swift

// Convert frequency to note name
let noteLabel = CalibraMusic.hzToNoteLabel(440) // "A4"

// Convert note name to frequency
let frequency = CalibraMusic.noteLabelToHz("C4") // 261.63 Hz

// Get cents deviation from nearest note
let deviation = CalibraMusic.centsDeviation(442) // +7.8 cents

// Convert between MIDI and Hz
let midi = CalibraMusic.hzToMidi(440) // 69.0
let hz = CalibraMusic.midiToHz(60) // 261.63 Hz

Pitch Reference

NoteMIDIHz
C4 (middle C)60261.63
A4 (concert pitch)69440.00
C572523.25

Common Pitfalls

  1. Invalid frequencies: Functions return Float.NaN or "-" for invalid input

  2. Flat notation: Use lowercase 'b' for flats (e.g., "Bb4", not "BB4")

  3. Octave numbering: Middle C is C4 (scientific pitch notation)

  4. Cents interpretation: Positive = sharp, negative = flat

See also

For detecting pitch from audio

For scoring pitch accuracy

Properties

Link copied to clipboard
const val A4_FREQUENCY: Float = 440.0f

Reference pitch for A4 (Hz)

Link copied to clipboard
const val A4_MIDI_NUMBER: Int = 69

MIDI note number for A4

Functions

Link copied to clipboard
fun centsDeviation(frequency: Float): Float

Get cents deviation from nearest note (-50 to +50).

Link copied to clipboard
fun centsToHz(cents: Float, tonicHz: Float = A4_FREQUENCY): Float

Convert cents to frequency in Hz (relative to tonic).

Link copied to clipboard
fun centsToMidi(cents: Float, referenceMidi: Int = A4_MIDI_NUMBER): Float

Convert cents to MIDI note number.

Link copied to clipboard
fun centsToNoteLabel(cents: Float, tonicHz: Float): String

Convert cents to note label (relative to tonic).

Link copied to clipboard
fun hzToCents(frequency: Float, tonicHz: Float = A4_FREQUENCY): Float

Convert frequency in Hz to cents (relative to tonic).

Link copied to clipboard
fun hzToMidi(frequency: Float): Float

Convert frequency in Hz to MIDI note number.

Link copied to clipboard
fun hzToNoteLabel(frequency: Float): String

Convert frequency in Hz to note label.

Link copied to clipboard
fun midiToCents(midiNote: Float, referenceMidi: Int = A4_MIDI_NUMBER): Float

Convert MIDI note number to cents.

Link copied to clipboard
fun midiToHz(midiNote: Float): Float

Convert MIDI note number to frequency in Hz.

Link copied to clipboard
fun midiToNoteLabel(midiNote: Float): String

Convert MIDI note number to note label (e.g., "A4", "C#5").

Link copied to clipboard
fun noteLabelToCents(noteLabel: String, tonicHz: Float): Float

Convert note label to cents (relative to tonic).

Link copied to clipboard
fun noteLabelToHz(noteLabel: String): Float

Convert note label to frequency in Hz.

Link copied to clipboard
fun noteLabelToMidi(noteLabel: String): Float

Convert note label to MIDI note number. Supports sharps (#), flats (b).