// ScaleDiatonicTransposeTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("Scale diatonic transposition — move by scale degrees, stay in scale") struct ScaleDiatonicTransposeTests { private func cell(_ pitches: Pitch...) -> PitchCollection { PitchCollection(pitches: pitches) } // MARK: - Plan worked example @Test func upAThirdInCMajor() throws { let cMajor: Scale = try Scale.major(.c) let input: PitchCollection = cell(.named(.c, 4), .named(.d, 4), .named(.e, 4)) let result: PitchCollection = try cMajor.transposeDiatonic(input, byDegrees: 2) // E-F-G, NOT E-F#-G#. #expect(result.pitches == [.named(.e, 4), .named(.f, 4), .named(.g, 4)]) } // MARK: - Direction + octave wrap @Test func downASecondWrapsOctave() throws { let cMajor: Scale = try Scale.major(.c) let input: PitchCollection = cell(.named(.c, 4), .named(.d, 4)) let result: PitchCollection = try cMajor.transposeDiatonic(input, byDegrees: -1) #expect(result.pitches == [.named(.b, 3), .named(.c, 4)]) } @Test func upAnOctaveOfDegrees() throws { let cMajor: Scale = try Scale.major(.c) let input: PitchCollection = cell(.named(.e, 4), .named(.g, 4)) let result: PitchCollection = try cMajor.transposeDiatonic(input, byDegrees: 7) #expect(result.pitches == [.named(.e, 5), .named(.g, 5)]) } // MARK: - Non-C tonic register handling @Test func diatonicInAMajorRegisters() throws { let aMajor: Scale = try Scale.major(.a) let input: PitchCollection = cell(.named(.a, 4), .named(.gSharp, 4)) let result: PitchCollection = try aMajor.transposeDiatonic(input, byDegrees: 1) // A4 -> B4; G#4 is the leading tone just below A4, so +1 degree -> A4. #expect(result.pitches == [.named(.b, 4), .named(.a, 4)]) } // MARK: - Modal-vs-chromatic contrast (locks the distinction) @Test func diatonicVersusChromaticThird() throws { let cMajor: Scale = try Scale.major(.c) let input: PitchCollection = cell(.named(.c, 4), .named(.d, 4), .named(.e, 4)) let diatonic: PitchCollection = try cMajor.transposeDiatonic(input, byDegrees: 2) let chromatic: PitchCollection = try input.transposedChromatic(by: .majorThird) #expect(diatonic.pitches == [.named(.e, 4), .named(.f, 4), .named(.g, 4)]) #expect(chromatic.pitches == [.named(.e, 4), .named(.fSharp, 4), .named(.gSharp, 4)]) #expect(diatonic.pitches != chromatic.pitches) } // MARK: - Chromatic (non-scale) pitch carries its alteration @Test func nonScalePitchCarriesAlteration() throws { let cMajor: Scale = try Scale.major(.c) // F# is the raised 4th; up two degrees -> raised 6th = A#. let input: PitchCollection = cell(.named(.fSharp, 4)) let result: PitchCollection = try cMajor.transposeDiatonic(input, byDegrees: 2) #expect(result.pitches == [.named(.aSharp, 4)]) } @Test func nonNamedPitchThrows() throws { let cMajor: Scale = try Scale.major(.c) let input: PitchCollection = cell(.midi(60)) #expect(throws: ScaleError.self) { _ = try cMajor.transposeDiatonic(input, byDegrees: 1) } } }