MusicTheory

Music theory facade — constants, conversions, interval generation, and shruti alignment.

What is MusicTheory?

The shared utility layer for pitch ↔ MIDI ↔ note-label ↔ cents conversions, note-name constants (Western, Carnatic, Hindustani), and interval generation (12-TET and Just Intonation). Also provides alignShruti for computing a student's practice tonic relative to a lesson key.

Used as a building block across the SDK: Accura for intonation analysis, PitchAnalysis for histogram computation, and demo apps for pitch visualization.

When to Use

ScenarioMethod(s)Why
Convert Hz ↔ MIDI ↔ note labelhzToMidi, midiToHz, hzToNoteLabel, etc.Core pitch-space conversions
Get cents deviation from nearest notecentsDeviationTuner-style display
Generate target intervals for a scalegenerateEqTemperedIntervals, generateJustIntonationIntervalsFeed to PitchAnalysis.quantize or Accura.analyzePitching
Compute a student's practice shrutialignShrutiShruti picker UI + CalibraLiveEval.setStudentKeyHz

Quick Start

Kotlin

val midi = MusicTheory.hzToMidi(440f)            // 69.0
val hz = MusicTheory.midiToHz(69f) // 440.0
val label = MusicTheory.hzToNoteLabel(440f) // "A4"
val cents = MusicTheory.hzToCents(466.16f, tonicHz = 440f) // ~100

val intervals = MusicTheory.EQ_TEMPERED_INTERVALS_CENTS_BASE // [0, 100, ..., 1100]

Swift

let midi = MusicTheory.hzToMidi(440.0)            // 69.0
let hz = MusicTheory.midiToHz(69.0) // 440.0
let label = MusicTheory.hzToNoteLabel(440.0) // "A4"
let cents = MusicTheory.hzToCents(466.16, tonicHz: 440.0) // ~100

let names = MusicTheory.noteNames // ["C", "C#", "D", ...]

Common Pitfalls

  1. hzToCents default tonic is A4 (440 Hz): If you're working relative to a different tonic (e.g., the singer's shruti), pass it explicitly.

  2. Note labels use sharps only: midiToNoteLabel returns "C#4", never "Db4". noteLabelToMidi accepts both sharps and flats.

  3. centsDeviation range is -50 to +50: It reports deviation from the nearest 12-TET note, not from a custom tonic.

See also

Uses MusicTheory for intonation analysis

Uses MusicTheory for histogram computation

Properties

Link copied to clipboard
const val A4_FREQUENCY: Float

Reference pitch for A4 (Hz).

Link copied to clipboard
const val A4_MIDI_NUMBER: Int

MIDI note number for A4.

Link copied to clipboard

Number of cents in an octave.

Link copied to clipboard

Number of cents in a semitone (in 12-TET).

Link copied to clipboard

Default maximum cents for interval lists.

Link copied to clipboard

Default minimum cents for interval lists.

Link copied to clipboard

12-TET intervals spanning multiple octaves.

Link copied to clipboard

Base 12-TET intervals in cents within one octave (0 to 1100).

Link copied to clipboard

JI intervals spanning multiple octaves.

Link copied to clipboard

Base JI intervals in cents within one octave.

Link copied to clipboard

5-limit Just Intonation ratios for common intervals.

Link copied to clipboard

Standard chromatic note names using sharps for accidentals.

Link copied to clipboard

Standard Carnatic swarasthana names (12 pitch classes).

Link copied to clipboard

Standard Hindustani sargam note names (12 notes per octave).

Link copied to clipboard

Maps note names (e.g., "C", "C#", "Db") to their base MIDI number within an octave (0-11).

Link copied to clipboard

Number of semitones in an octave (12-tone equal temperament).

Functions

Link copied to clipboard
fun alignShruti(userShrutiHz: Float, refKeyHz: Float, maxFineTuneSemitones: Float = 2.0f): ShrutiAlignmentResult

Compute the practice shruti (tonic) for a student given their natural shruti and the reference lesson key.

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 deriveUserShruti(nspHz: Float?, rangeLowHz: Float? = null, rangeHighHz: Float? = null, rangeThresholdSemitones: Float = 18.0f): UserShrutiDerivation

Compute the user's practice shruti from their natural speaking pitch (NSP) and most-recent vocal range, applying the policy from Musicmuni research synthesis (§B7).

Link copied to clipboard
fun generateEqTemperedIntervals(minCents: Int, maxCents: Int, step: Int = 100): List<Int>

Generate equal-tempered intervals in cents within a range.

Link copied to clipboard
fun generateJustIntonationIntervals(baseRatios: Map<String, Pair<Int, Int>>, minCents: Float, maxCents: Float): List<Float>

Generate Just Intonation intervals in cents within a range.

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.

Link copied to clipboard
fun ratioToCents(numerator: Int, denominator: Int): Float

Convert a frequency ratio to cents.