100 lines
3.4 KiB
Swift
100 lines
3.4 KiB
Swift
// PitchMapIntoRangeTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Pitch.mapIntoRange — octave-fold into a register, spelling preserved")
|
|
struct PitchMapIntoRangeTests {
|
|
|
|
// MARK: - Named: octave fold preserving spelling
|
|
|
|
@Test func foldsUpIntoRange() {
|
|
let result: Pitch = Pitch.named(.c, 3)
|
|
.mapIntoRange(low: .named(.a, 4), high: .named(.a, 5))
|
|
#expect(result == .named(.c, 5))
|
|
}
|
|
|
|
@Test func foldsDownIntoRange() {
|
|
// C7 folded down into [A4, A5] → C5.
|
|
let result: Pitch = Pitch.named(.c, 7)
|
|
.mapIntoRange(low: .named(.a, 4), high: .named(.a, 5))
|
|
#expect(result == .named(.c, 5))
|
|
}
|
|
|
|
@Test func wideRangeFoldsUpToNearestRepresentative() {
|
|
// [A4, A6] spans two octaves; C3 below range → first C going up = C5.
|
|
let result: Pitch = Pitch.named(.c, 3)
|
|
.mapIntoRange(low: .named(.a, 4), high: .named(.a, 6))
|
|
#expect(result == .named(.c, 5))
|
|
}
|
|
|
|
@Test func wideRangeFoldsDownToNearestRepresentative() {
|
|
// C7 above range → first C going down = C6 (not C5).
|
|
let result: Pitch = Pitch.named(.c, 7)
|
|
.mapIntoRange(low: .named(.a, 4), high: .named(.a, 6))
|
|
#expect(result == .named(.c, 6))
|
|
}
|
|
|
|
@Test func alreadyInRangeReturnsUnchanged() {
|
|
let p: Pitch = Pitch.named(.c, 5)
|
|
#expect(p.mapIntoRange(low: .named(.a, 4), high: .named(.a, 5)) == p)
|
|
}
|
|
|
|
@Test func flatStaysFlatNeverRespelled() {
|
|
// A B♭ must come back a B♭, not an enharmonic A♯.
|
|
let result: Pitch = Pitch.named(.bFlat, 2)
|
|
.mapIntoRange(low: .named(.c, 4), high: .named(.c, 5))
|
|
#expect(result == .named(.bFlat, 4))
|
|
}
|
|
|
|
// MARK: - No representative → unchanged
|
|
|
|
@Test func noRepresentativeInSubOctaveRangeReturnsOriginal() {
|
|
// [C4, E4] spans < an octave; pitch class G has no member there.
|
|
let p: Pitch = Pitch.named(.g, 2)
|
|
#expect(p.mapIntoRange(low: .named(.c, 4), high: .named(.e, 4)) == p)
|
|
}
|
|
|
|
@Test func representativeExistsInSubOctaveRange() {
|
|
// [C4, E4]; D2 → D4 sits inside.
|
|
let result: Pitch = Pitch.named(.d, 2)
|
|
.mapIntoRange(low: .named(.c, 4), high: .named(.e, 4))
|
|
#expect(result == .named(.d, 4))
|
|
}
|
|
|
|
// MARK: - midi / freq cases
|
|
|
|
@Test func midiFoldsNumerically() {
|
|
// C3 = 48, into [A4=69, A5=81] → C5 = 72.
|
|
#expect(Pitch.midi(48).mapIntoRange(low: .midi(69), high: .midi(81)) == .midi(72))
|
|
}
|
|
|
|
@Test func freqFoldsByOctaveKeepingCents() {
|
|
// 110 Hz (A2) into [A4=440, A5=880] → 440 Hz.
|
|
if case .freq(let hz) = Pitch.freq(110.0)
|
|
.mapIntoRange(low: .freq(440.0), high: .freq(880.0)) {
|
|
#expect(abs(hz - 440.0) < 1e-9)
|
|
} else {
|
|
Issue.record("expected a .freq result")
|
|
}
|
|
}
|
|
|
|
// MARK: - Degenerate inputs → unchanged
|
|
|
|
@Test func restReturnsUnchanged() {
|
|
let r: Pitch = .rest
|
|
#expect(r.mapIntoRange(low: .named(.a, 4), high: .named(.a, 5)) == r)
|
|
}
|
|
|
|
@Test func invertedBoundsReturnOriginal() {
|
|
let p: Pitch = Pitch.named(.c, 3)
|
|
// high < low → no-op.
|
|
#expect(p.mapIntoRange(low: .named(.a, 5), high: .named(.a, 4)) == p)
|
|
}
|
|
}
|