-
Notifications
You must be signed in to change notification settings - Fork 1
/
note.go
527 lines (440 loc) · 12.4 KB
/
note.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package largo
import (
"fmt"
"github.com/marcel/largo/chord"
)
const (
ConcertPitch = 440.0
SemiToneCents = 1200
SemiTonesPerOctave = 12
)
type SemiTone uint8
const (
// Intervals of extended meantone temperament
// Name Interval Pitch (from C) Roman No.
Unison = SemiTone(0) // C I
DiminishedSecond = Unison // D♭♭
ChromaticSemiTone = SemiTone(1) // C♯
MinorSecond = ChromaticSemiTone // D♭
HalfTone = MinorSecond
WholeTone = SemiTone(2) // D II
DiminishedThird = WholeTone // E♭♭
AugmentedSecond = SemiTone(3) // D♯
MinorThird = AugmentedSecond // E♭
MajorThird = SemiTone(4) // E III
DiminishedFourth = MajorThird // F♭
AugmentedThird = SemiTone(5) // E♯
PerfectFourth = AugmentedThird // F IV
AugmentedFourth = SemiTone(6) // F♯
DiminishedFifth = AugmentedFourth // G♭
PerfectFifth = SemiTone(7) // G V
DiminishedSixth = PerfectFifth // A♭♭
AugmentedFifth = SemiTone(8) // G♯
MinorSixth = AugmentedFifth // A♭
MajorSixth = SemiTone(9) // A VI
DiminishedSeventh = MajorSixth // B♭♭
AugmentedSixth = SemiTone(10) // A♯
MinorSeventh = AugmentedSixth // B♭
MajorSeventh = SemiTone(11) // B VII
DiminishedOctave = MajorSeventh // C♭
AugmentedSeventh = SemiTone(12) // B♯
OctaveTone = AugmentedSeventh // C VIII
)
type PitchInterval interface {
FromNote(Note) Notes
}
type StackedInterval []SemiTone
func (s StackedInterval) FromNote(note Note) Notes {
// intervals.foldLeft(Seq(note)) { case (notes, interval) =>
// notes :+ (notes.last + interval)
// }
panic("not implemented")
}
type AbsoluteInterval []SemiTone
func (a AbsoluteInterval) FromNote(note Note) Notes {
// intervals.foldLeft(Seq(note)) { case (notes, interval) =>
// notes :+ (note + interval)
// }
panic("not implemented")
}
// TODO implement sort.Interface
type Note struct {
Pitch Pitch
Octave Octave
}
func NewNote(pitch Pitch, octave Octave) Note {
return Note{
Pitch: pitch,
Octave: octave,
}
}
func (n Note) Frequency() {
// val intervalFromConcertPitch = Note.ConcertPitch.interval(this)
// Pitch.Frequency.ConcertPitch * Math.pow(Octave.TwelfthRootOf2, intervalFromConcertPitch.interval)
panic("not implemented")
}
func (n Note) Index() int {
// CircularPitchClass.indexOf(pitch) + (octave.value * SemiTone.perOctave)
panic("not implemented")
}
// override func (n Note) equals(obj: Any) = {
// obj match {
// case o: Note => o.index.equals(index)
// case _ => false
// }
// }
func (n Note) Interval(otherNote Note) SemiTone {
// val octaveInterval = octave.interval(otherNote.octave) * SemiTone.perOctave
// SemiTone(pitch.interval(otherNote.pitch).interval + octaveInterval)
panic("not implemented")
}
// func (n Note) compare(otherNote: Note) = {
// 0.compare(interval(otherNote).interval)
// }
func (n Note) Raise(interval SemiTone) Note {
return PitchSpace[n.Index()+int(interval)]
}
// TODO this was called ++ in Scala, so I'm not sure if the name I chose is
// correct
func (n Note) RaiseAccordingTo(intervals PitchInterval) Notes {
return intervals.FromNote(n)
}
func (n Note) MajorScale() Notes {
return n.RaiseAccordingTo(MajorScale)
}
func (n Note) MinorScale() Notes {
return n.RaiseAccordingTo(MinorScale)
}
func (n Note) String() string {
return fmt.Sprintf("%s%d", n.Pitch.String(), n.Octave)
}
type Notes []Note
func (s SemiTone) IntervalBetweenFrequencies() {
panic("not implemented yet")
}
type Accidental uint8
const (
Natural Accidental = iota + 1
Sharp
Flat
DoubleSharp
DoubleFlat
)
type Degree string
const (
Tonic = Degree("I")
Supertonic = Degree("ii")
Mediant = Degree("iii")
Subdominant = Degree("IV")
Dominant = Degree("V")
Submediant = Degree("vi")
Subtonic = Degree("vii")
)
var (
MajorScale = AbsoluteInterval{
WholeTone, // Supertonic
WholeTone, // Mediant
HalfTone, // Subdominant
WholeTone, // Dominant
WholeTone, // Submediant
WholeTone, // Leading tone
HalfTone, // Tonic / Octave
}
// TODO Natural minor scale vs harmonic minor scale (https://en.wikipedia.org/wiki/Minor_scale)
MinorScale = AbsoluteInterval{
WholeTone,
HalfTone,
WholeTone,
WholeTone,
HalfTone,
WholeTone,
WholeTone,
}
)
type Scale struct {
TonicKeyNote Note
IntervalsFromTonicKeyNote PitchInterval
}
func (s Scale) Degrees() Notes { return s.TonicKeyNote.RaiseAccordingTo(s.IntervalsFromTonicKeyNote) }
func (s Scale) Supertonic() Note { return s.Degrees()[1] }
func (s Scale) Mediant() Note { return s.Degrees()[2] }
func (s Scale) Subdominant() Note { return s.Degrees()[3] }
func (s Scale) Dominant() Note { return s.Degrees()[4] }
func (s Scale) Submediant() Note { return s.Degrees()[5] }
func (s Scale) LeadingTone() Note { return s.Degrees()[6] }
func (s Scale) Octave() Note { return s.Degrees()[7] }
type Letter rune
func (l Letter) Pitch(optionalAccidental ...Accidental) Pitch {
accidental := Natural
if optionalAccidental != nil {
accidental = optionalAccidental[0]
}
return Pitch{
Letter: l,
Accidental: accidental,
}
}
type Pitch struct {
Letter Letter
Accidental Accidental
}
func (p Pitch) AtOctave(octave Octave) Note {
return NewNote(p, octave)
}
func (p Pitch) Sharp() Pitch {
return p.Letter.Pitch(Sharp)
}
func (p Pitch) Flat() Pitch {
return p.Letter.Pitch(Flat)
}
func (p Pitch) Natural() Pitch {
return p.Letter.Pitch(Natural)
}
func (p Pitch) Note(octave Octave) Note {
// return NewNote(p, octave)
panic("not implemented")
}
func (p Pitch) RaiseBy(number int) Pitch {
// this + SemiTone(number)
panic("not implemented")
}
func (p Pitch) MoveUpFrom(semiTone SemiTone) Pitch {
// CircularPitchClass.moveUpFrom(this, semiTone)
panic("not implemented")
}
func (p Pitch) LowerBy(number int) Pitch {
// this - SemiTone(number)
panic("not implemented")
}
func (p Pitch) MoveDownFrom(semiTone SemiTone) Pitch {
// CircularPitchClass.moveDownFrom(this, semiTone)
panic("not implemented")
}
func (p Pitch) Interval(pitch Pitch) {
// SemiTone(CircularPitchClass.indexOf(pitch) - CircularPitchClass.indexOf(this))
panic("not implemented, figure out return type")
}
func (p Pitch) String() string {
display := func(s string) string {
return string(p.Letter) + string(p.Accidental)
}
switch p.Accidental {
case Sharp:
return display("♯")
case Flat:
return display("♭")
case Natural:
return display("")
case DoubleFlat:
return display("♭♭") // N.B. Double flat unicode work?
case DoubleSharp:
return display("♯♯") // N.B. Double sharp unicode work?
}
return "<unknown pitch>"
}
// TODO replace with whatever resolves the TODO for Key
type Pitches []Pitch
// TODO These should be chords rather than pitches
// (in other words, the perfect fifth interval should be calculated from the tonic of each chord)
type Key Pitch
func (k Key) Pitch() Pitch { return Pitch(k) }
func (k Key) IV() Pitch { return k.Pitch().MoveDownFrom(PerfectFifth) }
func (k Key) I() Pitch { return Pitch(k) }
func (k Key) V() Pitch { return k.Pitch().MoveUpFrom(PerfectFifth) }
// N.B. II - VII should be lower case in accordance with proper musical
// notation but we want these methods to be exported to we have capitalized
// them
func (k Key) II() Pitch { return k.V().MoveUpFrom(PerfectFifth) }
func (k Key) VI() Pitch { return k.II().MoveUpFrom(PerfectFifth) }
func (k Key) III() Pitch { return k.VI().MoveUpFrom(PerfectFifth) }
func (k Key) VII() Pitch { return k.III().MoveUpFrom(PerfectFifth) }
func (k Key) Major() Pitches { return Pitches{k.IV(), k.I(), k.V()} }
func (k Key) Minor() Pitches { return Pitches{k.II(), k.VI(), k.III()} }
func (k Key) Diminished() Pitches { return Pitches{k.VII()} }
func (k Key) Chords() Pitches { return append(k.Major(), append(k.Minor(), k.Diminished()...)...) }
var PitchSpace = func() []Note {
// Octave.Min.to(Octave.Max).flatMap { octave =>
// CircularPitchClass.pitches.map { pitch =>
// Note(pitch, octave)
// }
// }
panic("not implemented")
}()
var (
C = Letter('C').Pitch()
D = Letter('D').Pitch()
E = Letter('E').Pitch()
F = Letter('F').Pitch()
G = Letter('G').Pitch()
A = Letter('A').Pitch()
B = Letter('B').Pitch()
)
// https://en.wikipedia.org/wiki/Seventh_chord
//
// Tertian:
// Cmaj⁷ — Major seventh (major/major)
// Cmin⁷ — Minor seventh (minor/minor)
// C⁷ — Dominant seventh (major/minor)
// C°⁷ — Diminished seventh
// Cm⁷ — Half-diminished seventh (diminished/minor)
// Cm maj⁷ - Minor major seventh
// Cmaj⁷⁽⁵⁾ – Augmented major seventh
//
// Non-tertian:
// Caug⁷ — Augmented seventh (augmented/minor)
// CmM7♭5 — Diminished major seventh
// C7♭5 — Dominant seventh flat five
type Seventh struct {
Triad Triad
Note Note
}
var (
DominantSeventh = AbsoluteInterval{
MajorThird,
PerfectFifth,
MinorSeventh,
}
MajorSeventhSeventh = AbsoluteInterval{
MajorThird,
PerfectFifth,
MajorSeventh,
}
MinorMajorSeventh = AbsoluteInterval{
MinorThird,
PerfectFifth,
MajorSeventh,
}
MinorSeventhSeventh = AbsoluteInterval{
MinorThird,
PerfectFifth,
MinorSeventh,
}
AugmentedMajorSeventh = AbsoluteInterval{
MajorThird,
AugmentedFifth,
MajorSeventh,
}
AugmentedSeventhSeventh = AbsoluteInterval{
MajorThird,
AugmentedFifth,
MinorSeventh,
}
HalfDiminishedSeventh = AbsoluteInterval{
MinorThird,
DiminishedFifth,
MinorSeventh,
}
DiminishedSeventhSeventh = AbsoluteInterval{
MinorThird,
DiminishedFifth,
DiminishedSeventh,
}
DominantFlatFiveSeventh = AbsoluteInterval{
MajorThird,
DiminishedFifth,
MinorSeventh,
}
)
type Triad [3]Note
func (t Triad) Root() Note { return t[0] }
func (t Triad) Third() Note { return t[1] }
func (t Triad) Fifth() Note { return t[2] }
func (t Triad) Inversion(number int) {
panic("not implemented")
}
var (
MajorTriad = StackedInterval{
MajorThird, // 4 semitones from root
MinorThird, // perfect fifth, 7 semitones from root
}
MinorTriad = StackedInterval{
MinorThird, // 3 semitones from root
MajorThird, // perfect fifth, 7 semitones from root
}
DiminishedTriad = StackedInterval{
MinorThird, // 3 semitones from root
MinorThird, // diminished fifth, 6 semitones from root
}
AugmentedTriad = StackedInterval{
MajorThird, // 4 semitones from root
MajorThird, // augmented fifth, 8 semitones from root
}
)
const (
TwelfthRootOf2 = 1.0594630943592953 // 2^(1/12)
LowestOctave = 0
HighestOctave = 8
)
// Octave is two notes that have a frequency ratio of 2:1. An octave spans 12
// semitones or 1200 cents.
type Octave uint8
func (o Octave) Interval(otherOctave Octave) Octave {
return otherOctave - o
}
func (o Octave) Note(pitch Pitch) Note {
return NewNote(pitch, o)
}
// TODO Altered chords (https://en.wikipedia.org/wiki/Chord_(music)#Altered_chords)
// TODO Suspended chords (https://en.wikipedia.org/wiki/Chord_(music)#Suspended_chords)
// a Chord is a combination of three or more tones sounded simultaneously
type Chord struct {
Root Note
Quality chord.Quality
Number chord.IntervalNumber
}
var CircularPitchClass = Pitches{
C,
C.Sharp(),
D,
D.Sharp(),
E,
F,
F.Sharp(),
G,
G.Sharp(),
A,
A.Sharp(),
B,
}
func (p Pitches) Indexes() map[Pitch]int {
// p.zipWithIndex.toMap
panic("not implemented")
}
func (p Pitches) MoveUpFrom(pitch Pitch, semiTone SemiTone) Pitch {
index := p.indexOf(pitch)
indexToMoveTo := (index + int(semiTone)) % 12
return p[indexToMoveTo]
}
func (p Pitches) MoveDownFrom(pitch Pitch, semiTone SemiTone) Pitch {
index := p.indexOf(pitch)
indexToMoveTo := (index - int(semiTone)) % 12
destination := indexToMoveTo
if indexToMoveTo < 0 {
destination = len(p) + indexToMoveTo
}
return p[destination]
}
func (p Pitches) MoveFrom(pitch Pitch, semiTone SemiTone) Pitch {
if semiTone == 0 {
return pitch
}
if semiTone > 0 {
return p.MoveUpFrom(pitch, semiTone)
}
return p.MoveDownFrom(pitch, SemiTone(-semiTone))
}
func (p Pitches) indexOf(pitch Pitch) int {
// switch pitch.Accidental {
// case Flat :
// val natural = pitch.♮
// val index = indexes(natural)
// if (index != 0) {
// indexes(natural) - 1
// } else {
// indexOf(B)
// }
// default:
// return indexes(pitch)
// }
panic("not implmeented")
}