-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinterval.go
301 lines (254 loc) · 6.74 KB
/
interval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package musictheory
import (
"fmt"
"math"
)
// Quality types
const (
PerfectType QualityType = iota
MajorType
MinorType
AugmentedType
DiminishedType
)
// IntervalFunc creates an interval at as specific step/degree
type IntervalFunc func(int) Interval
// Perfect interval
func Perfect(step int) Interval {
return qualityInterval(step, Quality{PerfectType, 0})
}
// Major interval
func Major(step int) Interval {
return qualityInterval(step, Quality{MajorType, 0})
}
// Minor interval
func Minor(step int) Interval {
return qualityInterval(step, Quality{MinorType, 0})
}
// Augmented interval
func Augmented(step int) Interval {
return qualityInterval(step, Quality{AugmentedType, 1})
}
// DoublyAugmented interval
func DoublyAugmented(step int) Interval {
return qualityInterval(step, Quality{AugmentedType, 2})
}
// Diminished interval
func Diminished(step int) Interval {
return qualityInterval(step, Quality{DiminishedType, 1})
}
// DoublyDiminished interval
func DoublyDiminished(step int) Interval {
return qualityInterval(step, Quality{DiminishedType, 2})
}
// Octave interval
func Octave(step int) Interval {
return Interval{step, 0, 0}
}
// Semitones is an interval using direct semitones
func Semitones(step int) Interval {
normal := normalizeChromatic(step)
return Interval{chromaticOctaves(step), chromaticToDiatonic(normal), normal}
}
func qualityInterval(step int, quality Quality) Interval {
var (
absStep = int(math.Abs(float64(step)))
diatonic = normalizeDiatonic(absStep - 1)
diff = qualityDiff(quality, canBePerfect(diatonic))
octaves = diatonicOctaves(absStep - 1)
)
i := NewInterval(absStep, octaves, diff)
if step > 0 {
return i
}
return i.Negate()
}
// NewInterval builds a new Interval
func NewInterval(step, octaves, offset int) Interval {
diatonic := normalizeDiatonic(step - 1)
chromatic := diatonicToChromatic(diatonic) + offset
return Interval{octaves, diatonic, chromatic}
}
// Interval represents an interval in 12-tone equal temperament
type Interval struct {
Octaves int
Diatonic int
Chromatic int
}
// Semitones returns the total number of semitones that make up the interval
func (i Interval) Semitones() int {
return i.Octaves*12 + i.Chromatic
}
// Quality returns the Quality
func (i Interval) Quality() Quality {
quality := diffQuality(i.Chromatic-diatonicToChromatic(i.Diatonic), canBePerfect(i.Diatonic))
if i.Octaves < 0 {
return quality.Invert()
}
return quality
}
// Ratio returns the interval ratio
func (i Interval) Ratio() float64 {
return math.Exp2(float64(i.Semitones()) / 12.0)
}
// Transpose returns a new Interval that has been transposed by the given Interval
func (i Interval) Transpose(o Interval) Interval {
var diatonic int
// TODO: Accomodate weird behavior of sequential minor second transpositions. We don't need to advance the diatonic
// every transposition. We're currently modeling things as integers, but maybe we need to model as floats and
// accumulate over time; whole numbers trigger a move.
if o.Diatonic == o.Chromatic {
if diatonicToChromatic(i.Diatonic) == i.Chromatic {
diatonic = i.Diatonic + o.Diatonic
} else {
diatonic = i.Diatonic
}
} else {
diatonic = i.Diatonic + o.Diatonic
}
var (
chromatic = i.Chromatic + o.Chromatic
diatonicRemainder = normalizeDiatonic(diatonic)
octaves = i.Octaves + o.Octaves + chromaticOctaves(chromatic)
finalChromatic = normalizeChromatic(i.Chromatic + o.Chromatic)
)
return Interval{octaves, diatonicRemainder, finalChromatic}
}
// Negate returns a new, negated Interval
func (i Interval) Negate() Interval {
if i.Diatonic == 0 && i.Chromatic == 0 {
return Interval{-i.Octaves, i.Diatonic, i.Chromatic}
}
return Interval{-(i.Octaves + 1), inverseDiatonic(i.Diatonic), inverseChromatic(i.Chromatic)}
}
// Eq determines if another interval is the same
func (i Interval) Eq(o Interval) bool {
return i.Semitones() == o.Semitones()
}
func (i Interval) String() string {
mag := 1
if i.Octaves < 0 {
i = i.Negate()
mag = -1
}
var (
quality = i.Quality()
diatonic = (i.Octaves * 7) + i.Diatonic + 1
)
return fmt.Sprintf("%s %d", quality, mag*diatonic)
}
// QualityType represents the type a Quality can take
type QualityType int
func (q QualityType) String() string {
switch q {
case PerfectType:
return "perfect"
case MajorType:
return "major"
case MinorType:
return "minor"
case AugmentedType:
return "augmented"
case DiminishedType:
return "diminished"
default:
return "unknown"
}
}
// Quality describes the quality of an interval
type Quality struct {
Type QualityType
Size int
}
// Invert returns a new, inverted Quality
func (q Quality) Invert() Quality {
switch q.Type {
case PerfectType:
return q
case MajorType:
return Quality{MinorType, q.Size}
case MinorType:
return Quality{MajorType, q.Size}
case AugmentedType:
return Quality{DiminishedType, q.Size}
case DiminishedType:
return Quality{AugmentedType, q.Size}
default:
panic(fmt.Sprintf("invalid type: %d", q.Type))
}
}
// Eq checks two Qualities for equality
func (q Quality) Eq(o Quality) bool {
return q.Type == o.Type && q.Size == o.Size
}
func (q Quality) String() string {
switch q.Type {
case PerfectType, MajorType, MinorType:
return fmt.Sprintf("%s", q.Type)
case AugmentedType, DiminishedType:
prefix := ""
if q.Size == 2 || q.Size == -2 {
prefix = "doubly "
}
return fmt.Sprintf("%s%s", prefix, q.Type)
default:
return "unknown"
}
}
func diatonicToChromatic(interval int) int {
if interval >= len(diatonicToChromaticLookup) {
panic(fmt.Sprintf("interval out of range: %d", interval))
}
return diatonicToChromaticLookup[interval]
}
var diatonicToChromaticLookup = []int{0, 2, 4, 5, 7, 9, 11}
func chromaticToDiatonic(v int) int {
mag := 1
if v < 0 {
mag = -1
v = -v
}
v = normalizeChromatic(v)
for i, c := range diatonicToChromaticLookup {
if v == c || v < c {
return i * mag
}
}
return 6 * mag
}
func qualityDiff(q Quality, perfect bool) int {
if q.Type == PerfectType || q.Type == MajorType {
return 0
} else if q.Type == MinorType {
return -1
} else if q.Type == AugmentedType {
return q.Size
} else if q.Type == DiminishedType {
if perfect {
return -q.Size
}
return -(q.Size + 1)
}
panic("invalid quality")
}
func diffQuality(diff int, perfect bool) Quality {
if perfect {
if diff == 0 {
return Quality{PerfectType, 0}
} else if diff > 0 {
return Quality{AugmentedType, diff}
}
return Quality{DiminishedType, -diff}
}
if diff == 0 {
return Quality{MajorType, 0}
} else if diff == -1 {
return Quality{MinorType, 0}
} else if diff > 0 {
return Quality{AugmentedType, diff}
}
return Quality{DiminishedType, -(diff + 1)}
}
func canBePerfect(interval int) bool {
return interval == 0 || interval == 3 || interval == 4
}