// PitchSpellingTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("Pitch spelling — Letter + Accidental + NoteName preserve enharmonic distinctions") struct PitchSpellingTests { // MARK: - 1. Letter root semitones @Test func letterRootSemitones() { #expect(Letter.c.rootSemitone == 0) #expect(Letter.d.rootSemitone == 2) #expect(Letter.e.rootSemitone == 4) #expect(Letter.f.rootSemitone == 5) #expect(Letter.g.rootSemitone == 7) #expect(Letter.a.rootSemitone == 9) #expect(Letter.b.rootSemitone == 11) } // MARK: - 2. Accidental semitone offsets @Test func accidentalSemitones() { #expect(Accidental.doubleFlat.semitones == -2.0) #expect(Accidental.threeQuarterFlat.semitones == -1.5) #expect(Accidental.flat.semitones == -1.0) #expect(Accidental.quarterFlat.semitones == -0.5) #expect(Accidental.natural.semitones == 0.0) #expect(Accidental.quarterSharp.semitones == 0.5) #expect(Accidental.sharp.semitones == 1.0) #expect(Accidental.threeQuarterSharp.semitones == 1.5) #expect(Accidental.doubleSharp.semitones == 2.0) } @Test func accidentalCents() { #expect(Accidental.flat.cents == -100) #expect(Accidental.quarterSharp.cents == 50) #expect(Accidental.doubleSharp.cents == 200) #expect(Accidental.threeQuarterFlat.cents == -150) } @Test func accidentalIsQuarterTone() { #expect(Accidental.quarterSharp.isQuarterTone) #expect(Accidental.threeQuarterFlat.isQuarterTone) #expect(!Accidental.sharp.isQuarterTone) #expect(!Accidental.natural.isQuarterTone) } // MARK: - 3. NoteName composition @Test func noteNameLetterAndAccidentalAccessible() { #expect(NoteName.cSharp.letter == .c) #expect(NoteName.cSharp.accidental == .sharp) #expect(NoteName.dFlat.letter == .d) #expect(NoteName.dFlat.accidental == .flat) #expect(NoteName.cFlat.letter == .c) #expect(NoteName.cFlat.accidental == .flat) #expect(NoteName.bSharp.letter == .b) #expect(NoteName.bSharp.accidental == .sharp) #expect(NoteName.cDoubleSharp.accidental == .doubleSharp) #expect(NoteName.dDoubleFlat.accidental == .doubleFlat) } @Test func noteNameSemitones() { // C natural = 0 #expect(NoteName.c.semitones == 0.0) // C♯ = 1; D♭ = 1 (same chromatic step, different spelling) #expect(NoteName.cSharp.semitones == 1.0) #expect(NoteName.dFlat.semitones == 1.0) // C♭ = -1; B♯ = 12 (chromatic step matches B in same octave plus 1) #expect(NoteName.cFlat.semitones == -1.0) #expect(NoteName.bSharp.semitones == 12.0) // Quarter tones #expect(NoteName.cQuarterSharp.semitones == 0.5) #expect(NoteName.dQuarterFlat.semitones == 1.5) } @Test func noteNameSemitoneIntForQuarterToneIsNil() { #expect(NoteName.c.semitoneInt == 0) #expect(NoteName.cSharp.semitoneInt == 1) #expect(NoteName.cQuarterSharp.semitoneInt == nil) #expect(NoteName.dThreeQuarterFlat.semitoneInt == nil) } // MARK: - 4. Spelling preservation: enharmonics are NOT equal @Test func cSharpNotEqualToDFlatAsNoteName() { #expect(NoteName.cSharp != NoteName.dFlat) #expect(NoteName.cFlat != NoteName.b) #expect(NoteName.bSharp != NoteName.c) #expect(NoteName.eSharp != NoteName.f) #expect(NoteName.fFlat != NoteName.e) } @Test func enharmonicPitchesAreNotEqual() { // Pitch values themselves carry the spelling. #expect(Pitch.named(.cFlat, 4) != Pitch.named(.b, 3)) #expect(Pitch.named(.bSharp, 3) != Pitch.named(.c, 4)) #expect(Pitch.named(.cSharp, 4) != Pitch.named(.dFlat, 4)) } // MARK: - 5. Octave is letter-based — Cb4 stays in octave 4 @Test func cFlatStaysInWrittenOctave() { // C♭4 sounds at MIDI 59 (B3 territory) but the LETTER octave is 4. // The MIDI number reflects the SOUNDING pitch. let cFlat4: Pitch = Pitch.named(.cFlat, 4) #expect(cFlat4.midiNote == 59) // B♯3 sounds at MIDI 60 (middle C territory) but the letter octave is 3. let bSharp3: Pitch = Pitch.named(.bSharp, 3) #expect(bSharp3.midiNote == 60) } @Test func doubleFlatAndDoubleSharp() { // Cbb4 = MIDI 58 #expect(Pitch.named(.cDoubleFlat, 4).midiNote == 58) // Bx3 (Bdouble-sharp) = MIDI 61 (= C♯4 territory) #expect(Pitch.named(.bDoubleSharp, 3).midiNote == 61) } // MARK: - 6. Enharmonics share MIDI number @Test func enharmonicPitchesShareMIDINumber() { #expect(Pitch.named(.cFlat, 4).midiNote == Pitch.named(.b, 3).midiNote) #expect(Pitch.named(.bSharp, 3).midiNote == Pitch.named(.c, 4).midiNote) #expect(Pitch.named(.cSharp, 4).midiNote == Pitch.named(.dFlat, 4).midiNote) #expect(Pitch.named(.eSharp, 4).midiNote == Pitch.named(.f, 4).midiNote) #expect(Pitch.named(.fFlat, 4).midiNote == Pitch.named(.e, 4).midiNote) } // MARK: - 7. Quarter-tone pitches: midiNoteFractional preserves precision @Test func quarterToneFractionalMIDI() { let cqs4: Pitch = Pitch.named(.cQuarterSharp, 4) #expect(cqs4.midiNoteFractional == 60.5) // Swift's Double.rounded() uses .toNearestOrAwayFromZero, so 60.5 → 61. #expect(cqs4.midiNote == 61) // D-three-quarter-flat 4: (4+1)*12 + (2 - 1.5) = 60.5 let dthqf4: Pitch = Pitch.named(.dThreeQuarterFlat, 4) #expect(dthqf4.midiNoteFractional == 60.5) } @Test func quarterToneFrequencyOffset() { // C4 (60) vs C-quarter-sharp 4 (60.5): ratio = 2^(0.5/12) ≈ 1.02930223664 let cFreq: Double = Pitch.named(.c, 4).frequency ?? 0 let cqsFreq: Double = Pitch.named(.cQuarterSharp, 4).frequency ?? 0 let ratio: Double = cqsFreq / cFreq let expectedRatio: Double = pow(2.0, 0.5 / 12.0) #expect(abs(ratio - expectedRatio) < 1e-9, "Quarter-sharp should be ~+50¢ above natural; ratio=\(ratio) vs expected=\(expectedRatio)") } // MARK: - 8. NoteName glyphs @Test func noteNameGlyphCombinesLetterAndAccidental() { #expect(NoteName.c.glyph == "C") #expect(NoteName.cSharp.glyph == "C♯") #expect(NoteName.cFlat.glyph == "C♭") #expect(NoteName.cDoubleSharp.glyph == "C𝄪") #expect(NoteName.bDoubleFlat.glyph == "B𝄫") } // MARK: - 9. Hashable distinguishes spellings @Test func setOfSpellingsHasDistinctEntries() { let set: Set = [.cSharp, .dFlat, .c, .cFlat, .bSharp] #expect(set.count == 5) } // MARK: - 10. Default accidental is natural @Test func defaultAccidentalIsNatural() { let n: NoteName = NoteName(letter: .e) #expect(n.accidental == .natural) #expect(n == NoteName.e) } // MARK: - 11. Out-of-range guard @Test func bSharp8IsInRangeAtBoundary() { // (8 + 1) * 12 + 11 + 1 = 120 — top end of MIDI but valid. #expect(Pitch.named(.bSharp, 8).midiNote == 120) } @Test func bSharp9MidiNoteOutOfRange() { // (9 + 1) * 12 + 11 + 1 = 132 — out of MIDI range. #expect(Pitch.named(.bSharp, 9).midiNote == nil) } @Test func cFlatMinusOneMidiNoteIsNegativeOutOfRange() { // (-1 + 1) * 12 + 0 - 1 = -1 — out of MIDI range. #expect(Pitch.named(.cFlat, -1).midiNote == nil) } }