137 lines
4.8 KiB
Swift
137 lines
4.8 KiB
Swift
// ScaleTheoryTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Scale — modes, symmetric scales, spelling, degree queries")
|
|
struct ScaleTheoryTests {
|
|
|
|
// MARK: - Diatonic mode spelling
|
|
|
|
@Test func dMajorUsesSevenDistinctLetters() throws {
|
|
let scale: Scale = try Scale.major(.d)
|
|
#expect(scale.degrees == [.d, .e, .fSharp, .g, .a, .b, .cSharp])
|
|
}
|
|
|
|
@Test func bFlatMajorSpelling() throws {
|
|
let scale: Scale = try Scale.major(.bFlat)
|
|
#expect(scale.degrees == [.bFlat, .c, .d, .eFlat, .f, .g, .a])
|
|
}
|
|
|
|
@Test func aHarmonicMinorRaisesSeventh() throws {
|
|
let scale: Scale = try Scale.harmonicMinor(.a)
|
|
#expect(scale.degrees == [.a, .b, .c, .d, .e, .f, .gSharp])
|
|
}
|
|
|
|
@Test func eFlatDorian() throws {
|
|
let scale: Scale = try Scale.dorian(.eFlat)
|
|
// Eb F Gb Ab Bb C Db
|
|
#expect(scale.degrees == [.eFlat, .f, .gFlat, .aFlat, .bFlat, .c, .dFlat])
|
|
}
|
|
|
|
// MARK: - Symmetric scale canonical spelling
|
|
|
|
@Test func wholeToneCSpelling() throws {
|
|
let scale: Scale = try Scale.wholeTone(.c)
|
|
#expect(scale.degrees == [.c, .d, .e, .fSharp, .gSharp, .aSharp])
|
|
}
|
|
|
|
@Test func chromaticCSpelling() throws {
|
|
let scale: Scale = try Scale.chromatic(.c)
|
|
#expect(scale.degrees == [
|
|
.c, .cSharp, .d, .dSharp, .e, .f,
|
|
.fSharp, .g, .gSharp, .a, .aSharp, .b,
|
|
])
|
|
}
|
|
|
|
@Test func octatonicHalfWholeCSpelling() throws {
|
|
let scale: Scale = try Scale.octatonicHalfWhole(.c)
|
|
#expect(scale.degrees == [.c, .dFlat, .eFlat, .e, .fSharp, .g, .a, .bFlat])
|
|
}
|
|
|
|
@Test func majorPentatonicCSpelling() throws {
|
|
let scale: Scale = try Scale.majorPentatonic(.c)
|
|
#expect(scale.degrees == [.c, .d, .e, .g, .a])
|
|
}
|
|
|
|
@Test func bluesCSpelling() throws {
|
|
let scale: Scale = try Scale.blues(.c)
|
|
#expect(scale.degrees == [.c, .eFlat, .f, .gFlat, .g, .bFlat])
|
|
}
|
|
|
|
// MARK: - Membership
|
|
|
|
@Test func containsByPitchClass() throws {
|
|
let cMajor: Scale = try Scale.major(.c)
|
|
#expect(cMajor.contains(.e))
|
|
#expect(cMajor.contains(.f))
|
|
#expect(!cMajor.contains(.fSharp))
|
|
// Enharmonic equivalent of B (Cb) is in C major by pitch-class.
|
|
#expect(cMajor.contains(.cFlat))
|
|
}
|
|
|
|
// MARK: - Degree lookup
|
|
|
|
@Test func degreeExactAndAltered() throws {
|
|
let cMajor: Scale = try Scale.major(.c)
|
|
#expect(cMajor.degree(of: .c)?.index == 0)
|
|
#expect(cMajor.degree(of: .g)?.index == 4)
|
|
#expect(cMajor.degree(of: .g)?.alteration == 0)
|
|
// F# is the raised fourth degree.
|
|
let raisedFourth: ScaleDegree? = cMajor.degree(of: .fSharp)
|
|
#expect(raisedFourth?.index == 3)
|
|
#expect(raisedFourth?.alteration == 1)
|
|
// Eb is the lowered third degree.
|
|
let loweredThird: ScaleDegree? = cMajor.degree(of: .eFlat)
|
|
#expect(loweredThird?.index == 2)
|
|
#expect(loweredThird?.alteration == -1)
|
|
}
|
|
|
|
// MARK: - noteName / pitch at degree
|
|
|
|
@Test func noteNameAtDegreeWraps() throws {
|
|
let cMajor: Scale = try Scale.major(.c)
|
|
#expect(cMajor.noteName(atDegree: 0) == .c)
|
|
#expect(cMajor.noteName(atDegree: 7) == .c) // wraps
|
|
#expect(cMajor.noteName(atDegree: 9) == .e) // 9 mod 7 = 2
|
|
#expect(cMajor.noteName(atDegree: -1) == .b) // wraps backward
|
|
}
|
|
|
|
@Test func pitchAtDegreeOctaveBookkeeping() throws {
|
|
let cMajor: Scale = try Scale.major(.c)
|
|
#expect(cMajor.pitch(atDegree: 0, octave: 4) == .named(.c, 4))
|
|
#expect(cMajor.pitch(atDegree: 2, octave: 4) == .named(.e, 4))
|
|
#expect(cMajor.pitch(atDegree: 7, octave: 4) == .named(.c, 5)) // octave up
|
|
}
|
|
|
|
@Test func pitchAtDegreeWithHighTonicBumpsOctave() throws {
|
|
// A major: third degree C# sits above A, so A4's third is C#5.
|
|
let aMajor: Scale = try Scale.major(.a)
|
|
#expect(aMajor.pitch(atDegree: 0, octave: 4) == .named(.a, 4))
|
|
#expect(aMajor.pitch(atDegree: 2, octave: 4) == .named(.cSharp, 5))
|
|
#expect(aMajor.pitch(atDegree: 5, octave: 4) == .named(.fSharp, 5))
|
|
#expect(aMajor.pitch(atDegree: 7, octave: 4) == .named(.a, 5))
|
|
}
|
|
|
|
// MARK: - Arbitrary pitch-set scale
|
|
|
|
@Test func scaleFromNamedPitches() throws {
|
|
let scale: Scale = try Scale(tonic: .c, pitches: [
|
|
.named(.c, 4), .named(.e, 4), .named(.g, 4),
|
|
])
|
|
#expect(scale.degrees == [.c, .e, .g])
|
|
#expect(scale.degree(of: .e)?.index == 1)
|
|
}
|
|
|
|
@Test func tonicMismatchThrows() {
|
|
#expect(throws: ScaleError.self) {
|
|
_ = try Scale(tonic: .c, degrees: [.d, .e, .f])
|
|
}
|
|
}
|
|
}
|