Files
Lyrebird/Tests/LyrebirdTests/PitchCollectionMapTests.swift
T

85 lines
3.4 KiB
Swift

// PitchCollectionMapTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("PitchCollection scale mapping — re-assign by degree, correct spelling")
struct PitchCollectionMapTests {
private func cell(_ pitches: Pitch...) -> PitchCollection {
PitchCollection(pitches: pitches)
}
// MARK: - Plan worked examples
@Test func cMajorTriadToDMajor() throws {
let cMajor: Scale = try Scale.major(.c)
let dMajor: Scale = try Scale.major(.d)
let triad: PitchCollection = cell(.named(.c, 4), .named(.e, 4), .named(.g, 4))
let mapped: PitchCollection = try triad.mapped(from: cMajor, to: dMajor)
#expect(mapped.pitches == [.named(.d, 4), .named(.fSharp, 4), .named(.a, 4)])
}
@Test func cMajorTriadToEFlatMajor() throws {
let cMajor: Scale = try Scale.major(.c)
let eFlatMajor: Scale = try Scale.major(.eFlat)
let triad: PitchCollection = cell(.named(.c, 4), .named(.e, 4), .named(.g, 4))
let mapped: PitchCollection = try triad.mapped(from: cMajor, to: eFlatMajor)
#expect(mapped.pitches == [.named(.eFlat, 4), .named(.g, 4), .named(.bFlat, 4)])
}
// MARK: - referenceScale carried
@Test func mappedSetsTargetReferenceScale() throws {
let cMajor: Scale = try Scale.major(.c)
let gMajor: Scale = try Scale.major(.g)
let mapped: PitchCollection = try cell(.named(.c, 4)).mapped(from: cMajor, to: gMajor)
#expect(mapped.referenceScale == gMajor)
}
// MARK: - Mode change (major -> minor) keeps degree, changes colour
@Test func majorToMinorChangesThirdAndSixth() throws {
let cMajor: Scale = try Scale.major(.c)
let cMinor: Scale = try Scale.aeolian(.c)
let line: PitchCollection = cell(.named(.c, 4), .named(.e, 4), .named(.a, 4))
let mapped: PitchCollection = try line.mapped(from: cMajor, to: cMinor)
// Degree 0/2/5 of C natural minor = C / Eb / Ab.
#expect(mapped.pitches == [.named(.c, 4), .named(.eFlat, 4), .named(.aFlat, 4)])
}
// MARK: - Octave register survives across the octave boundary
@Test func mappingPreservesRegister() throws {
let cMajor: Scale = try Scale.major(.c)
let dMajor: Scale = try Scale.major(.d)
// B4 is degree 6 of C major; in D major degree 6 is C#5.
let mapped: PitchCollection = try cell(.named(.b, 4)).mapped(from: cMajor, to: dMajor)
#expect(mapped.pitches == [.named(.cSharp, 5)])
}
// MARK: - Degree-count mismatch: by-index with wrap
@Test func pentatonicToHeptatonicByIndex() throws {
let cPent: Scale = try Scale.majorPentatonic(.c) // C D E G A (5)
let cMajor: Scale = try Scale.major(.c) // C D E F G A B (7)
// Pentatonic degrees 0-1-2 (C-D-E) map to major degrees 0-1-2 (C-D-E).
let mapped: PitchCollection = try cell(.named(.c, 4), .named(.d, 4), .named(.e, 4))
.mapped(from: cPent, to: cMajor)
#expect(mapped.pitches == [.named(.c, 4), .named(.d, 4), .named(.e, 4)])
}
@Test func nonNamedPitchThrows() throws {
let cMajor: Scale = try Scale.major(.c)
let dMajor: Scale = try Scale.major(.d)
#expect(throws: ScaleError.self) {
_ = try cell(.midi(60)).mapped(from: cMajor, to: dMajor)
}
}
}