Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #40

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions MusicKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
377091D51E06340B00DFFEA8 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 377091D41E06340B00DFFEA8 /* Extensions.swift */; };
377091D61E06340B00DFFEA8 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 377091D41E06340B00DFFEA8 /* Extensions.swift */; };
960F015E1AFFA6AA006974AE /* MIDIMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960F015D1AFFA6AA006974AE /* MIDIMessage.swift */; };
960F015F1AFFA6AA006974AE /* MIDIMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960F015D1AFFA6AA006974AE /* MIDIMessage.swift */; };
960F21F81B08FF320088C194 /* ChordExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 960F21F71B08FF320088C194 /* ChordExtension.swift */; };
Expand Down Expand Up @@ -111,6 +113,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
377091D41E06340B00DFFEA8 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
960F015D1AFFA6AA006974AE /* MIDIMessage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MIDIMessage.swift; sourceTree = "<group>"; };
960F21F61B08FEC70088C194 /* UnalteredExtendedChords.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; name = UnalteredExtendedChords.playground; path = ../Playgrounds/UnalteredExtendedChords.playground; sourceTree = "<group>"; };
960F21F71B08FF320088C194 /* ChordExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChordExtension.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -313,6 +316,7 @@
isa = PBXGroup;
children = (
965AF8C11AF7A1DA00D31E83 /* Protocols.swift */,
377091D41E06340B00DFFEA8 /* Extensions.swift */,
96DFB6331AF6C2F0005EEBD6 /* MKUtil.swift */,
);
name = Internal;
Expand Down Expand Up @@ -501,17 +505,19 @@
TargetAttributes = {
9691BB231A57C75500421B56 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0820;
};
9693F6521A57C403006270F4 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0800;
LastSwiftMigration = 0820;
};
96F299211A5663E500D7B006 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0820;
};
96F2992C1A5663E500D7B006 = {
CreatedOnToolsVersion = 6.1.1;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -592,6 +598,7 @@
buildActionMask = 2147483647;
files = (
960F21F91B08FF320088C194 /* ChordExtension.swift in Sources */,
377091D61E06340B00DFFEA8 /* Extensions.swift in Sources */,
96A06D551AFF281400029013 /* MKMIDIProc.m in Sources */,
96DFB6351AF6C2F0005EEBD6 /* MKUtil.swift in Sources */,
9693F6761A57C44A006270F4 /* MusicKit.swift in Sources */,
Expand Down Expand Up @@ -625,6 +632,7 @@
buildActionMask = 2147483647;
files = (
960F21F81B08FF320088C194 /* ChordExtension.swift in Sources */,
377091D51E06340B00DFFEA8 /* Extensions.swift in Sources */,
96A06D541AFF281400029013 /* MKMIDIProc.m in Sources */,
96DFB6341AF6C2F0005EEBD6 /* MKUtil.swift in Sources */,
96F299411A5665CA00D7B006 /* Pitch.swift in Sources */,
Expand Down Expand Up @@ -877,6 +885,7 @@
SKIP_INSTALL = YES;
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -897,6 +906,7 @@
PRODUCT_NAME = MusicKit;
SKIP_INSTALL = YES;
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -916,6 +926,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "benzguo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -931,6 +942,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "benzguo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
7 changes: 7 additions & 0 deletions MusicKit/ChordDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public struct ChordDescriptor: CustomStringConvertible {
self.bass = bass
}

public var name: String {
if root == bass {
return "\(root)\(quality)"
}
return "\(root)\(quality)/\(bass)"
}

public var description: String {
return "root: \(root), quality: \(quality), bass: \(bass)"
}
Expand Down
2 changes: 1 addition & 1 deletion MusicKit/ChordExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension Chord {
}

// sort and convert to zero-indexed indices
let sortedIndices = (indices.map { $0 - 1 }).sorted()
let sortedIndices = indices.map { $0 - 1 }.sorted()
let maxIndex = Int(sortedIndices.last!)
var scalePitches = harmonizer(Chroma.c*0)

Expand Down
46 changes: 13 additions & 33 deletions MusicKit/ChordName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ extension PitchSet {
extension Chord {
/// Returns the name of the chord if found.
public static func name(_ pitchSet: PitchSet) -> String? {
if let desc = descriptor(pitchSet) {
let rootName = desc.root.description
let quality = desc.quality.description
if desc.root == desc.bass {
return "\(rootName)\(quality)"
}
else {
let bassName = desc.bass.description
return "\(rootName)\(quality)/\(bassName)"
}
}
return nil
return descriptor(pitchSet)?.name
}

/// Returns an optional `ChordDescriptor`.
Expand All @@ -47,15 +36,10 @@ extension Chord {
_ = bassRemoved.remove(bass)

// dyads
if count == 2 {
return _descriptor(pitchSet, qualities: ChordQuality.Dyads)
}
// triads
else if count == 3 {
return _descriptor(pitchSet, qualities: ChordQuality.Triads)
}
// Tetrads, Pentads, Hexads, Heptads
else if count > 3 && count < 8 {
switch count {
case 2: return _descriptor(pitchSet, qualities: ChordQuality.Dyads)
case 3: return _descriptor(pitchSet, qualities: ChordQuality.Triads)
case 4..<8:
var fullQs = [ChordQuality]()
var fullUnalteredQs = [ChordQuality]()
var slashQs = [ChordQuality]()
Expand Down Expand Up @@ -94,22 +78,18 @@ extension Chord {
}
}
return slashNoBassOpt ?? fullOpt
default: return nil
}
return nil

}

/// Returns an optional `ChordDescriptor`.
static func _descriptor(_ pitchSet: PitchSet,
qualities: [ChordQuality]) -> ChordDescriptor?
qualities: [ChordQuality]) -> ChordDescriptor?
{
let count = pitchSet.count
if count < 1 {
return nil
}

let bass = pitchSet.first()!
guard let bass = pitchSet.first else { return nil }
let bassChromaOpt = bass.chroma

let indices = pitchSet.semitoneIndices()
// check root position chords
for quality in qualities {
Expand All @@ -123,10 +103,10 @@ extension Chord {
// check inversions
for quality in qualities {
let _indices = MKUtil.collapse(MKUtil.semitoneIndices(quality.intervals))
for i in 1..<count {
for i in 1..<pitchSet.endIndex {
let inversion = MKUtil.zero(MKUtil.invert(_indices, n: UInt(i)))
if inversion == indices {
if let rootChroma = pitchSet[count - i].chroma {
if let rootChroma = pitchSet[pitchSet.count - i].chroma {
return bassChromaOpt.map {
ChordDescriptor(root: rootChroma, quality: quality, bass: $0)
}
Expand Down
Loading