Skip to content

Commit

Permalink
Spell individual pitches (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbean committed Nov 2, 2018
1 parent 5c463f0 commit ce6cd41
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct WeightCarrying<G: WeightedGraphProtocol> {

func weight (from start: G.Node, to end: G.Node) -> G.Weight? {
return weight(G.Edge(start, end))
}
}

func pullback <H: WeightedGraphProtocol> (_ f: @escaping (H.Node) -> G.Node) -> WeightCarrying<H>
where H.Weight == G.Weight
Expand Down
17 changes: 16 additions & 1 deletion Sources/SpelledPitch/PitchSpeller/Wetherfield/Wetherfield.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ protocol PitchSpellingNode: Hashable {

struct PitchSpeller {

// FIXME: Flesh out for all tendencies
static let tendencyGraph: WeightedGraph<Tendency,Double> = WeightedGraph(
[
.up,
.down
],
[
UnorderedPair(.up,.up): 1,
UnorderedPair(.down, .down): 1
]
)

static let weightCarrying = WeightCarrying.build(from: PitchSpeller.tendencyGraph)
static let tendencyMask: WeightCarrying<WeightedGraph<Cross<Int,Tendency>,Double>> = weightCarrying.pullback { $0.b }

struct UnassignedNode: PitchSpellingNode {
let index: Index
}
Expand Down Expand Up @@ -75,6 +90,7 @@ struct PitchSpeller {
sink: PitchSpellingNode.Index(-1, .up),
internalNodes: pitchNodes
)
flowNetwork.mask(PitchSpeller.tendencyMask)
}

/// - Returns: An array of `SpelledPitch` values with the same indices as the original
Expand All @@ -89,7 +105,6 @@ struct PitchSpeller {
let upNodes = sinkSide.map { index in AssignedNode(index, .up) }
return downNodes + upNodes
}

return assignedNodes
.reduce(into: [Int: (AssignedNode, AssignedNode)]()) { pairs, node in
if !pairs.keys.contains(node.index.a) {
Expand Down
78 changes: 78 additions & 0 deletions Tests/SpelledPitchTests/PitchSpellerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import XCTest
import DataStructures
import Pitch
@testable import SpelledPitch

class PitchSpellerTests: XCTestCase {
Expand All @@ -19,6 +20,83 @@ class PitchSpellerTests: XCTestCase {
XCTAssertLessThan(a,b)
}

func testSpellZeroOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 60], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.c))]
XCTAssertEqual(result, expected)
}

func testSpellOneOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 61], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.c, .sharp))]
XCTAssertEqual(result, expected)
}

func testSpellTwoOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 62], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.d))]
XCTAssertEqual(result, expected)
}

func testSpellThreeOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 63], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.e, .flat))]
XCTAssertEqual(result, expected)
}

func testSpellFourOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 64], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.e))]
XCTAssertEqual(result, expected)
}

func testSpellFiveOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 65], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.f))]
XCTAssertEqual(result, expected)
}

func testSpellSixOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 66], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.f, .sharp))]
XCTAssertEqual(result, expected)
}

func testSpellSevenOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 67], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.g))]
XCTAssertEqual(result, expected)
}

func testSpellNineOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 69], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.a))]
XCTAssertEqual(result, expected)
}

func testSpellTenOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 70], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.b, .flat))]
XCTAssertEqual(result, expected)
}

func testSpellElevenOverDNatural() {
let pitchSpeller = PitchSpeller(pitches: [0: 71], parsimonyPivot: Pitch.Spelling(.d))
let result = pitchSpeller.spell()
let expected: [Int: SpelledPitch] = [0: SpelledPitch(Pitch.Spelling(.b))]
XCTAssertEqual(result, expected)
}

func testSpellCF() {
let pitchSpeller = PitchSpeller(pitches: [0:60,1:65])
let _ = pitchSpeller.spell()
Expand Down

0 comments on commit ce6cd41

Please sign in to comment.