Skip to content

Commit

Permalink
Add Chord.init(lowest:descriptor) (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbean committed Aug 24, 2019
1 parent 051fbaa commit f3fd487
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/Pitch/Chord/Chord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ extension Chord {
// MARK: - Initializers

/// Creates a `Chord` with the given `first` pitch and the given `intervals`.
init(_ lowest: Pitch, _ intervals: IntervalPattern) {
public init(_ lowest: Pitch, _ intervals: IntervalPattern) {
// FIXME: Use `intervals.accumulatingSum` when https://bugs.swift.org/browse/SR-11048 if fixed.
self.pitches = [lowest] + intervals.intervals.accumulatingSum.map { $0 + lowest }
}

/// Creates a `Chord` with the pitches in the given `sequence`.
init <S> (_ sequence: S) where S: Sequence, S.Element == Pitch {
/// Creates a `Chord` with the intervals in the given `sequence`.
public init <S> (_ sequence: S) where S: Sequence, S.Element == Pitch {
let sorted = sequence.sorted()
precondition(!sorted.isEmpty, "Cannot create a 'Chord' with an empty sequence of pitches")
self.init(sorted.first!, IntervalPattern(sorted.pairs.map { $1 - $0 }))
}

/// Creates a `Chord` with the given `lowest` pitch and the given `ChordDescriptor`.
public init(lowest: Pitch, descriptor: ChordDescriptor) {
self.pitches = [lowest] + descriptor.base.map { lowest + Pitch($0.steps) }
}
}

// FIXME: Reinstate Chord: CollectionWrapping when https://bugs.swift.org/browse/SR-11048 if fixed."
Expand Down
5 changes: 5 additions & 0 deletions Sources/Pitch/NoteNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public struct NoteNumber:
public init(value: Double) {
self.value = value
}

/// Create a `NoteNumber` with the given `int` value.
public init(_ int: Int) {
self.value = Double(int)
}
}

extension NoteNumber {
Expand Down
5 changes: 5 additions & 0 deletions Sources/Pitch/Pitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public struct Pitch: NoteNumberRepresentable {
public init(_ value: NoteNumber) {
self.value = value
}

/// Creates a `Pitch` with the given `int` value.
public init(_ int: Int) {
self.value = NoteNumber(int)
}
}

extension Pitch {
Expand Down

0 comments on commit f3fd487

Please sign in to comment.