@@ -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
0 commit comments