Skip to content

Commit cbf66b7

Browse files
authored
Merge pull request #21 from glaurent/swift3
Swift 3 conversion
2 parents 512828a + 6a774fa commit cbf66b7

File tree

10 files changed

+81
-62
lines changed

10 files changed

+81
-62
lines changed

Pitchy.xcodeproj/project.pbxproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,19 @@
366366
isa = PBXProject;
367367
attributes = {
368368
LastSwiftUpdateCheck = 0720;
369-
LastUpgradeCheck = 0720;
369+
LastUpgradeCheck = 0800;
370370
ORGANIZATIONNAME = "Vadym Markov";
371371
TargetAttributes = {
372372
D5DF49D11C2EE72E00343F13 = {
373373
CreatedOnToolsVersion = 7.2;
374+
LastSwiftMigration = 0800;
374375
};
375376
D5DF49DE1C2EE77A00343F13 = {
376377
CreatedOnToolsVersion = 7.2;
377378
};
378379
D5DF4A0A1C2EE95600343F13 = {
379380
CreatedOnToolsVersion = 7.2;
381+
LastSwiftMigration = 0800;
380382
};
381383
D5DF4A191C2EE96D00343F13 = {
382384
CreatedOnToolsVersion = 7.2;
@@ -555,8 +557,10 @@
555557
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
556558
CLANG_WARN_EMPTY_BODY = YES;
557559
CLANG_WARN_ENUM_CONVERSION = YES;
560+
CLANG_WARN_INFINITE_RECURSION = YES;
558561
CLANG_WARN_INT_CONVERSION = YES;
559562
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
563+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
560564
CLANG_WARN_UNREACHABLE_CODE = YES;
561565
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
562566
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -603,8 +607,10 @@
603607
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
604608
CLANG_WARN_EMPTY_BODY = YES;
605609
CLANG_WARN_ENUM_CONVERSION = YES;
610+
CLANG_WARN_INFINITE_RECURSION = YES;
606611
CLANG_WARN_INT_CONVERSION = YES;
607612
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
613+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
608614
CLANG_WARN_UNREACHABLE_CODE = YES;
609615
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
610616
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -645,6 +651,7 @@
645651
PRODUCT_BUNDLE_IDENTIFIER = "com.vadymmarkov.Pitchy-iOS";
646652
PRODUCT_NAME = Pitchy;
647653
SKIP_INSTALL = YES;
654+
SWIFT_VERSION = 3.0;
648655
};
649656
name = Debug;
650657
};
@@ -662,6 +669,8 @@
662669
PRODUCT_BUNDLE_IDENTIFIER = "com.vadymmarkov.Pitchy-iOS";
663670
PRODUCT_NAME = Pitchy;
664671
SKIP_INSTALL = YES;
672+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
673+
SWIFT_VERSION = 3.0;
665674
};
666675
name = Release;
667676
};
@@ -683,6 +692,7 @@
683692
PRODUCT_NAME = Pitchy;
684693
SDKROOT = macosx;
685694
SKIP_INSTALL = YES;
695+
SWIFT_VERSION = 3.0;
686696
};
687697
name = Debug;
688698
};
@@ -704,6 +714,8 @@
704714
PRODUCT_NAME = Pitchy;
705715
SDKROOT = macosx;
706716
SKIP_INSTALL = YES;
717+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
718+
SWIFT_VERSION = 3.0;
707719
};
708720
name = Release;
709721
};
@@ -718,6 +730,7 @@
718730
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
719731
PRODUCT_BUNDLE_IDENTIFIER = "com.vadymmarkov.Pitchy-iOS-Tests";
720732
PRODUCT_NAME = "$(TARGET_NAME)";
733+
SWIFT_VERSION = 3.0;
721734
};
722735
name = Debug;
723736
};
@@ -732,6 +745,8 @@
732745
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
733746
PRODUCT_BUNDLE_IDENTIFIER = "com.vadymmarkov.Pitchy-iOS-Tests";
734747
PRODUCT_NAME = "$(TARGET_NAME)";
748+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
749+
SWIFT_VERSION = 3.0;
735750
};
736751
name = Release;
737752
};
@@ -768,6 +783,7 @@
768783
PRODUCT_BUNDLE_IDENTIFIER = "com.vadymmarkov.Pitchy-Mac-Tests";
769784
PRODUCT_NAME = "$(TARGET_NAME)";
770785
SDKROOT = macosx;
786+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
771787
};
772788
name = Release;
773789
};

Pitchy.xcodeproj/xcshareddata/xcschemes/Pitchy-Mac.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Pitchy.xcodeproj/xcshareddata/xcschemes/Pitchy-iOS.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Source/Calculators/NoteCalculator.swift

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ public struct NoteCalculator {
1717
// MARK: - Bounds
1818

1919
public static var indexBounds: (minimum: Int, maximum: Int) {
20-
let minimum = try! index(frequency: Config.minimumFrequency)
21-
let maximum = try! index(frequency: Config.maximumFrequency)
20+
let minimum = try! index(Config.minimumFrequency)
21+
let maximum = try! index(Config.maximumFrequency)
2222

2323
return (minimum: minimum, maximum: maximum)
2424
}
2525

2626
public static var octaveBounds: (minimum: Int, maximum: Int) {
2727
let bounds = indexBounds
28-
let minimum = try! octave(index: bounds.minimum)
29-
let maximum = try! octave(index: bounds.maximum)
28+
let minimum = try! octave(bounds.minimum)
29+
let maximum = try! octave(bounds.maximum)
3030

3131
return (minimum: minimum, maximum: maximum)
3232
}
3333

3434
// MARK: - Validators
3535

36-
public static func isValidIndex(index: Int) -> Bool {
36+
public static func isValidIndex(_ index: Int) -> Bool {
3737
let bounds = indexBounds
3838

3939
return index >= bounds.minimum
4040
&& index <= bounds.maximum
4141
}
4242

43-
public static func isValidOctave(octave: Int) -> Bool {
43+
public static func isValidOctave(_ octave: Int) -> Bool {
4444
let bounds = octaveBounds
4545

4646
return octave >= bounds.minimum
@@ -49,9 +49,9 @@ public struct NoteCalculator {
4949

5050
// MARK: - Pitch Notations
5151

52-
public static func frequency(index index: Int) throws -> Double {
52+
public static func frequency(_ index: Int) throws -> Double {
5353
guard isValidIndex(index) else {
54-
throw Error.InvalidPitchIndex
54+
throw PitchError.invalidPitchIndex
5555
}
5656

5757
let count = letters.count
@@ -60,9 +60,9 @@ public struct NoteCalculator {
6060
return pow(2, power) * Standard.frequency
6161
}
6262

63-
public static func letter(index index: Int) throws -> Note.Letter {
63+
public static func letter(_ index: Int) throws -> Note.Letter {
6464
guard isValidIndex(index) else {
65-
throw Error.InvalidPitchIndex
65+
throw PitchError.invalidPitchIndex
6666
}
6767

6868
let count = letters.count
@@ -75,43 +75,46 @@ public struct NoteCalculator {
7575
}
7676

7777
guard lettersIndex >= 0 && lettersIndex < letters.count else {
78-
throw Error.InvalidPitchIndex
78+
throw PitchError.invalidPitchIndex
7979
}
8080

8181
return letters[lettersIndex]
8282
}
8383

84-
public static func octave(index index: Int) throws -> Int {
84+
public static func octave(_ index: Int) throws -> Int {
8585
guard isValidIndex(index) else {
86-
throw Error.InvalidPitchIndex
86+
throw PitchError.invalidPitchIndex
8787
}
8888

8989
let count = letters.count
9090

91+
let resNegativeIndex = Standard.octave - (abs(index) + 2) / count
92+
let resPositiveIndex = Standard.octave + (index + 9) / count
93+
9194
return index < 0
92-
? Standard.octave - (abs(index) + 2) / count
93-
: Standard.octave + (index + 9) / count
95+
? resNegativeIndex
96+
: resPositiveIndex
9497
}
9598

9699
// MARK: - Pitch Index
97100

98-
public static func index(frequency frequency: Double) throws -> Int {
101+
public static func index(_ frequency: Double) throws -> Int {
99102
guard PitchCalculator.isValidFrequency(frequency) else {
100-
throw Error.InvalidFrequency
103+
throw PitchError.invalidFrequency
101104
}
102105

103106
let count = Double(letters.count)
104107

105108
return Int(round(count * log2(frequency / Standard.frequency)))
106109
}
107110

108-
public static func index(letter letter: Note.Letter, octave: Int) throws -> Int {
111+
public static func index(_ letter: Note.Letter, octave: Int) throws -> Int {
109112
guard isValidOctave(octave) else {
110-
throw Error.InvalidOctave
113+
throw PitchError.invalidOctave
111114
}
112115

113116
let count = letters.count
114-
let letterIndex = letters.indexOf(letter) ?? 0
117+
let letterIndex = letters.index(of: letter) ?? 0
115118
let offset = letterIndex < 3 ? 0 : count
116119

117120
return letterIndex + count * (octave - Standard.octave) - offset

Source/Calculators/PitchCalculator.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ public struct PitchCalculator {
44

55
// MARK: - Validators
66

7-
public static func isValidFrequency(frequency: Double) -> Bool {
7+
public static func isValidFrequency(_ frequency: Double) -> Bool {
88
return frequency > 0.0
99
&& frequency >= Config.minimumFrequency
1010
&& frequency <= Config.maximumFrequency
1111
}
1212

1313
// MARK: - Offsets
1414

15-
public static func offsets(frequency: Double) throws -> Pitch.Offsets {
15+
public static func offsets(_ frequency: Double) throws -> Pitch.Offsets {
1616
let note = try Note(frequency: frequency)
1717
let higherNote = try note.higher()
1818
let lowerNote = try note.lower()
@@ -27,24 +27,24 @@ public struct PitchCalculator {
2727
frequency: frequency - note.frequency,
2828
percentage: (frequency - note.frequency) * 100
2929
/ abs(note.frequency - closestNote.frequency),
30-
cents: try cents(frequency1: note.frequency, frequency2: frequency)
30+
cents: try cents(note.frequency, frequency2: frequency)
3131
)
3232

3333
let secondOffset = Pitch.Offset(
3434
note: closestNote,
3535
frequency: frequency - closestNote.frequency,
3636
percentage: (frequency - closestNote.frequency) * 100
3737
/ abs(note.frequency - closestNote.frequency),
38-
cents: try cents(frequency1: closestNote.frequency, frequency2: frequency)
38+
cents: try cents(closestNote.frequency, frequency2: frequency)
3939
)
4040

4141
return Pitch.Offsets(firstOffset, secondOffset)
4242
}
4343

44-
public static func cents(frequency1 frequency1: Double, frequency2: Double) throws -> Double {
44+
public static func cents(_ frequency1: Double, frequency2: Double) throws -> Double {
4545
guard isValidFrequency(frequency1)
4646
&& isValidFrequency(frequency2)
47-
else { throw Error.InvalidFrequency }
47+
else { throw PitchError.invalidFrequency }
4848

4949
return 1200.0 * log2(frequency2 / frequency1)
5050
}

Source/Calculators/WaveCalculator.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public struct WaveCalculator {
1111

1212
public static var periodBounds: (minimum: Double, maximum: Double) {
1313
let bounds = wavelengthBounds
14-
let minimum = try! period(wavelength: bounds.minimum)
15-
let maximum = try! period(wavelength: bounds.maximum)
14+
let minimum = try! period(bounds.minimum)
15+
let maximum = try! period(bounds.maximum)
1616

1717
return (minimum: minimum, maximum: maximum)
1818
}
1919

2020
// MARK: - Validators
2121

22-
public static func isValidWavelength(wavelength: Double) -> Bool {
22+
public static func isValidWavelength(_ wavelength: Double) -> Bool {
2323
let bounds = wavelengthBounds
2424

2525
return wavelength > 0.0
2626
&& wavelength >= bounds.minimum
2727
&& wavelength <= bounds.maximum
2828
}
2929

30-
public static func isValidPeriod(period: Double) -> Bool {
30+
public static func isValidPeriod(_ period: Double) -> Bool {
3131
let bounds = periodBounds
3232

3333
return period > 0.0
@@ -37,33 +37,33 @@ public struct WaveCalculator {
3737

3838
// MARK: - Conversions
3939

40-
public static func frequency(wavelength wavelength: Double) throws -> Double {
40+
public static func frequency(_ wavelength: Double) throws -> Double {
4141
guard isValidWavelength(wavelength) else {
42-
throw Error.InvalidWavelength
42+
throw PitchError.invalidWavelength
4343
}
4444

4545
return AcousticWave.speed / wavelength
4646
}
4747

4848
public static func wavelength(frequency frequency: Double) throws -> Double {
4949
guard PitchCalculator.isValidFrequency(frequency) else {
50-
throw Error.InvalidFrequency
50+
throw PitchError.invalidFrequency
5151
}
5252

5353
return AcousticWave.speed / frequency
5454
}
5555

5656
public static func wavelength(period period: Double) throws -> Double {
5757
guard isValidPeriod(period) else {
58-
throw Error.InvalidPeriod
58+
throw PitchError.invalidPeriod
5959
}
6060

6161
return period * AcousticWave.speed
6262
}
6363

64-
public static func period(wavelength wavelength: Double) throws -> Double {
64+
public static func period(_ wavelength: Double) throws -> Double {
6565
guard isValidWavelength(wavelength) else {
66-
throw Error.InvalidWavelength
66+
throw PitchError.invalidWavelength
6767
}
6868

6969
return wavelength / AcousticWave.speed

Source/Data/AcousticWave.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ public struct AcousticWave {
2222

2323
public init(frequency: Double) throws {
2424
guard PitchCalculator.isValidFrequency(frequency) else {
25-
throw Error.InvalidFrequency
25+
throw PitchError.invalidFrequency
2626
}
2727

2828
self.frequency = frequency
2929
wavelength = try WaveCalculator.wavelength(frequency: frequency)
30-
period = try WaveCalculator.period(wavelength: wavelength)
30+
period = try WaveCalculator.period(wavelength)
3131
}
3232

3333
public init(wavelength: Double) throws {
3434
guard WaveCalculator.isValidWavelength(wavelength) else {
35-
throw Error.InvalidWavelength
35+
throw PitchError.invalidWavelength
3636
}
3737

3838
self.wavelength = wavelength
39-
frequency = try WaveCalculator.frequency(wavelength: wavelength)
40-
period = try WaveCalculator.period(wavelength: wavelength)
39+
frequency = try WaveCalculator.frequency(wavelength)
40+
period = try WaveCalculator.period(wavelength)
4141
}
4242

4343
public init(period: Double) throws {
4444
guard WaveCalculator.isValidPeriod(period) else {
45-
throw Error.InvalidPeriod
45+
throw PitchError.invalidPeriod
4646
}
4747

4848
self.period = period
4949
wavelength = try WaveCalculator.wavelength(period: period)
50-
frequency = try WaveCalculator.frequency(wavelength: wavelength)
50+
frequency = try WaveCalculator.frequency(wavelength)
5151
}
5252
}

0 commit comments

Comments
 (0)