Skip to content

Commit 074d080

Browse files
Added every missing interval up to two octaves (#45)
1 parent cc8c546 commit 074d080

File tree

2 files changed

+163
-3
lines changed

2 files changed

+163
-3
lines changed

Sources/Tonic/Interval.swift

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Foundation
55
/// The distance between two notes.
66
///
77
/// An interval distance is measured in degree (number of letters away) and
8-
/// quality of the interval (essentialy number of semitones away).
8+
/// quality of the interval (essentially number of semitones away).
99
/// Some Intervals refer to the same difference in pitch.
1010
public enum Interval: Int, CaseIterable, Codable {
1111
/// Perfect Unison
@@ -14,15 +14,24 @@ public enum Interval: Int, CaseIterable, Codable {
1414
/// Augmented Unison
1515
case A1
1616

17+
/// Diminished Second
18+
case d2
19+
1720
/// Minor Second
1821
case m2
1922

23+
/// Diminished Third
24+
case d3
25+
2026
/// Major Second
2127
case M2
2228

2329
/// Minor Third
2430
case m3
2531

32+
/// Augmented Second
33+
case A2
34+
2635
/// Major Third
2736
case M3
2837

@@ -32,6 +41,9 @@ public enum Interval: Int, CaseIterable, Codable {
3241
/// Perfect Fourth
3342
case P4
3443

44+
/// Augmented Third
45+
case A3
46+
3547
/// Augmented Fourth
3648
case A4
3749

@@ -44,6 +56,9 @@ public enum Interval: Int, CaseIterable, Codable {
4456
/// Augmented Fifth
4557
case A5
4658

59+
/// Diminished Sixth
60+
case d6
61+
4762
/// Minor Sixth
4863
case m6
4964

@@ -62,13 +77,19 @@ public enum Interval: Int, CaseIterable, Codable {
6277
/// Major Seventh
6378
case M7
6479

80+
/// Augmented Seventh
81+
case A7
82+
6583
/// Diminished Octave
6684
case d8
6785

6886
/// Perfect Octave
6987
case P8
7088

71-
/// AugmentedOctave
89+
/// Diminished Ninth
90+
case d9
91+
92+
/// Augmented Octave
7293
case A8
7394

7495
/// Minor Ninth
@@ -80,6 +101,18 @@ public enum Interval: Int, CaseIterable, Codable {
80101
/// Augmented Ninth
81102
case A9
82103

104+
/// Diminished Tenth
105+
case d10
106+
107+
/// Minor Tenth
108+
case m10
109+
110+
/// Major Tenth
111+
case M10
112+
113+
/// Augmented Tenth
114+
case A10
115+
83116
/// Diminished Eleventh
84117
case d11
85118

@@ -89,6 +122,18 @@ public enum Interval: Int, CaseIterable, Codable {
89122
/// Augmented Eleventh
90123
case A11
91124

125+
/// Diminished Twelfth
126+
case d12
127+
128+
/// Perfect Twelfth
129+
case P12
130+
131+
/// Augmented Twelfth
132+
case A12
133+
134+
/// Diminished Thirteenth
135+
case d13
136+
92137
/// Minor Thirteenth
93138
case m13
94139

@@ -98,39 +143,82 @@ public enum Interval: Int, CaseIterable, Codable {
98143
/// Augmented Thirteenth
99144
case A13
100145

146+
/// Diminished Fourteenth
147+
case d14
148+
149+
/// Minor Fourteenth
150+
case m14
151+
152+
/// Major Fourteenth
153+
case M14
154+
155+
/// Augmented Fourteenth
156+
case A14
157+
158+
/// Diminished Fifteenth
159+
case d15
160+
161+
/// Perfect Fifteenth
162+
case P15
163+
164+
/// Augmented Fifteenth
165+
case A15
166+
101167
/// Number of semitones the interval spans
102168
public var semitones: Int {
103169
switch self {
104170
case .P1: return 0
171+
case .d2: return 0
105172
case .A1: return 1
106173
case .m2: return 1
107174
case .M2: return 2
175+
case .d3: return 2
108176
case .m3: return 3
177+
case .A2: return 3
109178
case .M3: return 4
110179
case .d4: return 4
111180
case .P4: return 5
181+
case .A3: return 5
112182
case .A4: return 6
113183
case .d5: return 6
114184
case .P5: return 7
115-
case .A5: return 8
185+
case .d6: return 7
116186
case .m6: return 8
187+
case .A5: return 8
117188
case .d7: return 9
118189
case .M6: return 9
119190
case .A6: return 10
120191
case .m7: return 10
121192
case .M7: return 11
122193
case .d8: return 11
123194
case .P8: return 12
195+
case .A7: return 12
196+
case .d9: return 12
124197
case .A8: return 13
125198
case .m9: return 13
126199
case .M9: return 14
200+
case .d10: return 14
127201
case .A9: return 15
202+
case .m10: return 15
128203
case .d11: return 16
204+
case .M10: return 16
129205
case .P11: return 17
206+
case .A10: return 17
207+
case .d12: return 18
130208
case .A11: return 18
209+
case .P12: return 19
210+
case .d13: return 19
131211
case .m13: return 20
212+
case .A12: return 20
213+
case .d14: return 21
132214
case .M13: return 21
215+
case .m14: return 22
133216
case .A13: return 22
217+
case .d15: return 23
218+
case .M14: return 23
219+
case .P15: return 24
220+
case .A14: return 24
221+
case .A15: return 25
134222
}
135223
}
136224

@@ -139,34 +227,56 @@ public enum Interval: Int, CaseIterable, Codable {
139227
switch self {
140228
case .P1: return 1
141229
case .A1: return 1
230+
case .d2: return 2
142231
case .m2: return 2
143232
case .M2: return 2
233+
case .A2: return 2
234+
case .d3: return 3
144235
case .m3: return 3
145236
case .M3: return 3
237+
case .A3: return 3
146238
case .d4: return 4
147239
case .P4: return 4
148240
case .A4: return 4
149241
case .d5: return 5
150242
case .P5: return 5
151243
case .A5: return 5
244+
case .d6: return 6
152245
case .m6: return 6
153246
case .M6: return 6
154247
case .A6: return 6
155248
case .d7: return 7
156249
case .m7: return 7
157250
case .M7: return 7
251+
case .A7: return 7
158252
case .d8: return 8
159253
case .P8: return 8
160254
case .A8: return 8
255+
case .d9: return 9
161256
case .m9: return 9
162257
case .M9: return 9
163258
case .A9: return 9
259+
case .m10: return 10
260+
case .d10: return 10
261+
case .M10: return 10
262+
case .A10: return 10
164263
case .d11: return 11
165264
case .P11: return 11
166265
case .A11: return 11
266+
case .d12: return 12
267+
case .P12: return 12
268+
case .A12: return 12
269+
case .d13: return 13
167270
case .m13: return 13
168271
case .M13: return 13
169272
case .A13: return 13
273+
case .d14: return 14
274+
case .m14: return 14
275+
case .M14: return 14
276+
case .A14: return 14
277+
case .d15: return 15
278+
case .P15: return 15
279+
case .A15: return 15
170280
}
171281
}
172282

@@ -202,34 +312,56 @@ extension Interval: CustomStringConvertible {
202312
switch self {
203313
case .P1: return "P1"
204314
case .A1: return "A1"
315+
case .d2: return "d2"
205316
case .m2: return "m2"
317+
case .A2: return "A2"
206318
case .M2: return "M2"
319+
case .d3: return "d3"
207320
case .m3: return "m3"
208321
case .M3: return "M3"
322+
case .A3: return "A3"
209323
case .d4: return "d4"
210324
case .P4: return "P4"
211325
case .A4: return "A4"
212326
case .d5: return "d5"
213327
case .P5: return "P5"
214328
case .A5: return "A5"
329+
case .d6: return "d6"
215330
case .m6: return "m6"
216331
case .M6: return "M6"
217332
case .A6: return "A6"
218333
case .d7: return "d7"
219334
case .m7: return "m7"
220335
case .M7: return "M7"
336+
case .A7: return "A7"
221337
case .d8: return "d8"
222338
case .P8: return "P8"
223339
case .A8: return "A8"
340+
case .d9: return "d9"
224341
case .m9: return "m9"
225342
case .M9: return "M9"
226343
case .A9: return "A9"
344+
case .d10: return "d10"
345+
case .m10: return "m10"
346+
case .M10: return "M10"
347+
case .A10: return "A10"
227348
case .d11: return "d11"
349+
case .d12: return "d12"
228350
case .P11: return "P11"
229351
case .A11: return "A11"
352+
case .P12: return "P12"
353+
case .A12: return "A12"
354+
case .d13: return "D13"
230355
case .m13: return "m13"
231356
case .M13: return "M13"
232357
case .A13: return "A13"
358+
case .d14: return "d14"
359+
case .m14: return "m14"
360+
case .M14: return "M14"
361+
case .A14: return "A14"
362+
case .d15: return "d15"
363+
case .P15: return "P15"
364+
case .A15: return "A15"
233365
}
234366
}
235367

@@ -238,34 +370,56 @@ extension Interval: CustomStringConvertible {
238370
switch self {
239371
case .P1: return "Perfect Unison"
240372
case .A1: return "Augmented Unison"
373+
case .d2: return "Diminished Second"
241374
case .m2: return "Minor Second"
242375
case .M2: return "Major Second"
376+
case .A2: return "Augmented Second"
377+
case .d3: return "Diminished Third"
243378
case .m3: return "Minor Third"
244379
case .M3: return "Major Third"
380+
case .A3: return "Augmented Third"
245381
case .d4: return "Diminished Fourth"
246382
case .P4: return "Perfect Fourth"
247383
case .A4: return "Augmented Fourth"
248384
case .d5: return "Diminished Fifth"
249385
case .P5: return "Perfect Fifth"
250386
case .A5: return "Augmented Fifth"
387+
case .d6: return "Diminished Sixth"
251388
case .m6: return "Minor Sixth"
252389
case .M6: return "Major Sixth"
253390
case .A6: return "Augmented Sixth"
254391
case .d7: return "Diminished Seventh"
255392
case .m7: return "Minor Seventh"
256393
case .M7: return "Major Seventh"
394+
case .A7: return "Augmented Seventh"
257395
case .d8: return "Diminished Octave"
258396
case .P8: return "Perfect Octave"
259397
case .A8: return "Augmented Octave"
398+
case .d9: return "Diminished Ninth"
260399
case .m9: return "Minor Ninth"
261400
case .M9: return "Major Ninth"
262401
case .A9: return "Augmented Ninth"
402+
case .d10: return "Diminished Tenth"
403+
case .m10: return "Minor Tenth"
404+
case .M10: return "Major Tenth"
405+
case .A10: return "Augmented Tenth"
263406
case .d11: return "Diminished Eleventh"
264407
case .P11: return "Perfect Eleventh"
265408
case .A11: return "Augmented Eleventh"
409+
case .d12: return "Diminished Twelfth"
410+
case .P12: return "Perfect Twelfth"
411+
case .A12: return "Augmented Twelfth"
412+
case .d13: return "Diminished Thirteenth"
266413
case .m13: return "Minor Thirteenth"
267414
case .M13: return "Major Thirteenth"
268415
case .A13: return "Augmented Thirteenth"
416+
case .d14: return "Diminished Fourteenth"
417+
case .m14: return "Minor Fourteenth"
418+
case .M14: return "Major Fourteenth"
419+
case .A14: return "Augmented Fourteenth"
420+
case .d15: return "Diminished Fifteenth"
421+
case .P15: return "Perfect Fifteenth"
422+
case .A15: return "Augmented Fifteenth"
269423
}
270424
}
271425
}

Tests/TonicTests/IntervalTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import XCTest
44
final class IntervalTests: XCTestCase {
55
func testIntervalBetween() {
66
XCTAssertEqual(Interval.betweenNotes(.C, .D), .M2)
7+
XCTAssertEqual(Interval.betweenNotes(.C, .Ds), .A2)
78
XCTAssertEqual(Interval.betweenNotes(.C, .G), .P5)
89
XCTAssertEqual(Interval.betweenNotes(.G, .C), .P5)
910
XCTAssertEqual(Interval.betweenNotes(.C, .Fs), .A4)
1011
XCTAssertEqual(Interval.betweenNotes(.C, .Gb), .d5)
12+
XCTAssertEqual(Interval.betweenNotes(.C, Note(.D, accidental: .doubleFlat)), .d2)
13+
XCTAssertEqual(Interval.betweenNotes(.D, Note(.F, accidental: .flat)), .d3)
1114
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.D, octave: 2)), .M9)
1215
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, octave: 2)), .P8)
1316
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, accidental: .flat, octave: 0)), .A1)
1417
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, accidental: .flat, octave: 1)), .d8)
1518
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, accidental: .sharp, octave: 2)), .A8)
19+
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.A, accidental: .doubleFlat, octave: 2)), .d13)
20+
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, accidental: .sharp, octave: 3)), .A15)
21+
XCTAssertEqual(Interval.betweenNotes(Note(.C, octave: 1), Note(.C, octave: 3)), .P15)
1622
}
1723
}

0 commit comments

Comments
 (0)