80 lines
2.7 KiB
Swift
80 lines
2.7 KiB
Swift
// PitchTransposeTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Pitch transposition — spelled octave bookkeeping + numeric shift")
|
|
struct PitchTransposeTests {
|
|
|
|
// MARK: - Named: spelling + octave
|
|
|
|
@Test func majorThirdUpStaysInOctave() throws {
|
|
let result: Pitch = try Pitch.named(.c, 4).transposed(by: .majorThird)
|
|
#expect(result == .named(.e, 4))
|
|
}
|
|
|
|
@Test func perfectFourthUpFromBCrossesOctave() throws {
|
|
// B4 + P4 = E5 (letter wraps past C).
|
|
let result: Pitch = try Pitch.named(.b, 4).transposed(by: .perfectFourth)
|
|
#expect(result == .named(.e, 5))
|
|
}
|
|
|
|
@Test func octaveUpAddsOneOctave() throws {
|
|
#expect(try Pitch.named(.c, 4).transposed(by: .octave) == .named(.c, 5))
|
|
}
|
|
|
|
@Test func minorThirdDownStaysInOctave() throws {
|
|
let result: Pitch = try Pitch.named(.fSharp, 4).transposed(by: .minorThird, .down)
|
|
#expect(result == .named(.dSharp, 4))
|
|
}
|
|
|
|
@Test func cFlatMajorSecondUpKeepsOctave() throws {
|
|
// C♭4 lives in octave 4; +M2 → D♭4.
|
|
let result: Pitch = try Pitch.named(.cFlat, 4).transposed(by: .majorSecond)
|
|
#expect(result == .named(.dFlat, 4))
|
|
}
|
|
|
|
@Test func bSharpFromANamedKeepsMidiConsistency() throws {
|
|
// A4 +aug2 = B♯4; MIDI must match A4 + 3 semitones.
|
|
let a4: Pitch = .named(.a, 4)
|
|
let result: Pitch = try a4.transposed(by: .augmentedSecond)
|
|
#expect(result == .named(.bSharp, 4))
|
|
#expect(result.midiNote == (a4.midiNote.map { $0 + 3 }))
|
|
}
|
|
|
|
// MARK: - Compound interval octave bookkeeping
|
|
|
|
@Test func majorNinthUpCrossesOctave() throws {
|
|
let majorNinth: Interval = try Interval(number: 9, quality: .major)
|
|
let result: Pitch = try Pitch.named(.c, 4).transposed(by: majorNinth)
|
|
#expect(result == .named(.d, 5))
|
|
}
|
|
|
|
// MARK: - MIDI / freq numeric shift (spelling lost)
|
|
|
|
@Test func midiShiftsBySemitones() throws {
|
|
#expect(try Pitch.midi(60).transposed(by: .majorThird) == .midi(64))
|
|
#expect(try Pitch.midi(60).transposed(by: .perfectFifth, .down) == .midi(53))
|
|
}
|
|
|
|
@Test func freqShiftsByRatio() throws {
|
|
let result: Pitch = try Pitch.freq(440.0).transposed(by: .octave)
|
|
guard case .freq(let hz) = result else {
|
|
Issue.record("expected .freq")
|
|
return
|
|
}
|
|
let expectedHz: Double = 880.0
|
|
let tolerance: Double = 1e-9
|
|
#expect(abs(hz - expectedHz) < tolerance)
|
|
}
|
|
|
|
@Test func restUnchanged() throws {
|
|
#expect(try Pitch.rest.transposed(by: .majorThird).isRest)
|
|
}
|
|
}
|