133 lines
5.2 KiB
Swift
133 lines
5.2 KiB
Swift
// PitchCollectionTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("PitchCollection — spelled cell ops + 12-tone row forms")
|
|
struct PitchCollectionTests {
|
|
|
|
// MARK: - Cell transformations
|
|
|
|
@Test func retrogradeReverses() {
|
|
let cell: PitchCollection = PitchCollection(pitches: [
|
|
.named(.c, 4), .named(.d, 4), .named(.e, 4),
|
|
])
|
|
#expect(cell.retrograde.pitches == [.named(.e, 4), .named(.d, 4), .named(.c, 4)])
|
|
}
|
|
|
|
@Test func transposedChromaticSpellsCorrectly() throws {
|
|
let cell: PitchCollection = PitchCollection(pitches: [
|
|
.named(.c, 4), .named(.d, 4), .named(.e, 4),
|
|
])
|
|
let up: PitchCollection = try cell.transposedChromatic(by: .majorThird)
|
|
#expect(up.pitches == [.named(.e, 4), .named(.fSharp, 4), .named(.gSharp, 4)])
|
|
}
|
|
|
|
@Test func invertedAroundAxisPreservesSpelling() throws {
|
|
let cell: PitchCollection = PitchCollection(pitches: [
|
|
.named(.c, 4), .named(.e, 4), .named(.g, 4),
|
|
])
|
|
let inv: PitchCollection = try cell.inverted(around: .named(.c, 4))
|
|
// C stays; E (M3 up) → Ab below; G (P5 up) → F below.
|
|
#expect(inv.pitches == [.named(.c, 4), .named(.aFlat, 3), .named(.f, 3)])
|
|
}
|
|
|
|
@Test func octaveReducedFoldsToPitchClasses() {
|
|
let cell: PitchCollection = PitchCollection(pitches: [
|
|
.named(.c, 2), .named(.fSharp, 6), .named(.bFlat, 3),
|
|
])
|
|
let reduced: PitchCollection = cell.octaveReduced
|
|
#expect(reduced.pitches == [.named(.c, 4), .named(.fSharp, 4), .named(.bFlat, 4)])
|
|
}
|
|
|
|
// MARK: - 12-tone row validation
|
|
|
|
private var chromaticRow: PitchCollection {
|
|
PitchCollection(pitches: [
|
|
.named(.c, 4), .named(.cSharp, 4), .named(.d, 4), .named(.dSharp, 4),
|
|
.named(.e, 4), .named(.f, 4), .named(.fSharp, 4), .named(.g, 4),
|
|
.named(.gSharp, 4), .named(.a, 4), .named(.aSharp, 4), .named(.b, 4),
|
|
])
|
|
}
|
|
|
|
@Test func isToneRowDetection() {
|
|
#expect(chromaticRow.isToneRow)
|
|
let short: PitchCollection = PitchCollection(pitches: [.named(.c, 4), .named(.d, 4)])
|
|
#expect(!short.isToneRow)
|
|
}
|
|
|
|
@Test func notToneRowThrows() {
|
|
let short: PitchCollection = PitchCollection(pitches: [.named(.c, 4), .named(.d, 4)])
|
|
#expect(throws: ToneRowError.self) {
|
|
_ = try short.toneRowMatrix()
|
|
}
|
|
}
|
|
|
|
@Test func primeIsRowAtPitchClassOctave() throws {
|
|
let prime: PitchCollection = try chromaticRow.toneRowPrime()
|
|
#expect(prime.pitches == chromaticRow.pitches)
|
|
}
|
|
|
|
@Test func inversionMirrorsAboutFirst() throws {
|
|
let inversion: PitchCollection = try chromaticRow.toneRowInversion()
|
|
// I0 of the ascending chromatic from C: C B A# A G# G F# F E D# D C#.
|
|
let expected: [Pitch] = [
|
|
.named(.c, 4), .named(.b, 4), .named(.aSharp, 4), .named(.a, 4),
|
|
.named(.gSharp, 4), .named(.g, 4), .named(.fSharp, 4), .named(.f, 4),
|
|
.named(.e, 4), .named(.dSharp, 4), .named(.d, 4), .named(.cSharp, 4),
|
|
]
|
|
#expect(inversion.pitches == expected)
|
|
}
|
|
|
|
@Test func transposedShiftsPitchClasses() throws {
|
|
let t1: PitchCollection = try chromaticRow.toneRowTransposed(1)
|
|
#expect(t1.pitches.first == .named(.cSharp, 4))
|
|
#expect(t1.pitches.last == .named(.c, 4))
|
|
}
|
|
|
|
@Test func retrogradeRowReverses() throws {
|
|
let retro: PitchCollection = try chromaticRow.toneRowRetrograde()
|
|
#expect(retro.pitches.first == .named(.b, 4))
|
|
#expect(retro.pitches.last == .named(.c, 4))
|
|
}
|
|
|
|
@Test func matrixStructure() throws {
|
|
let matrix: [[NoteName]] = try chromaticRow.toneRowMatrix()
|
|
#expect(matrix.count == 12)
|
|
#expect(matrix.allSatisfy { $0.count == 12 })
|
|
// Top row = prime.
|
|
#expect(matrix[0] == [
|
|
.c, .cSharp, .d, .dSharp, .e, .f, .fSharp, .g, .gSharp, .a, .aSharp, .b,
|
|
])
|
|
// Left column = inversion.
|
|
let leftColumn: [NoteName] = matrix.map { $0[0] }
|
|
#expect(leftColumn == [
|
|
.c, .b, .aSharp, .a, .gSharp, .g, .fSharp, .f, .e, .dSharp, .d, .cSharp,
|
|
])
|
|
// Main diagonal is all the tonic pitch-class (cell[i][i] returns to P0[0]).
|
|
for i: Int in 0..<12 {
|
|
#expect(matrix[i][i] == .c)
|
|
}
|
|
}
|
|
|
|
// MARK: - Row spelling preservation (flats survive)
|
|
|
|
@Test func rowSpellingPreservedAcrossForms() throws {
|
|
// A row spelling pc 6 as Gb (not F#) and pc 3 as Eb (not D#).
|
|
let flatRow: PitchCollection = PitchCollection(pitches: [
|
|
.named(.c, 4), .named(.dFlat, 4), .named(.d, 4), .named(.eFlat, 4),
|
|
.named(.e, 4), .named(.f, 4), .named(.gFlat, 4), .named(.g, 4),
|
|
.named(.aFlat, 4), .named(.a, 4), .named(.bFlat, 4), .named(.b, 4),
|
|
])
|
|
let t1: PitchCollection = try flatRow.toneRowTransposed(1)
|
|
// Every derived pitch-class is spelled via the row's own map — pc 6 = Gb.
|
|
#expect(t1.pitches.contains(.named(.gFlat, 4)))
|
|
#expect(!t1.pitches.contains(.named(.fSharp, 4)))
|
|
}
|
|
}
|