-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitch.go
67 lines (57 loc) · 1.27 KB
/
pitch.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
package musicgo
import (
"fmt"
"math"
)
type Octave int
type Pitch float64
const nilPitch Pitch = 0
func (p Pitch) Interval(i Interval) Pitch {
return p + Pitch(i)
}
func (p Pitch) Index() NoteIndex {
m := NoteIndex(math.Floor(float64(p))) % 12
if m < 0 {
return 12 + m
}
return m
}
func (p Pitch) Cents() Cents {
n64 := float64(p)
return Cents(math.Floor(0.5 + (n64-math.Floor(n64))*100))
}
func (p Pitch) Octave() Octave {
return Octave(math.Floor(float64(p / 12)))
}
func (p Pitch) String() string {
noteName := noteNames[Note(p.Index())]
o := p.Octave()
c := p.Cents()
var octaveName string
if o < 0 {
octaveName = fmt.Sprintf("[%v]", o)
} else {
octaveName = fmt.Sprintf("%v", o)
}
if c != 0 {
return fmt.Sprintf("%v%v (%v cents)", noteName, octaveName, c)
}
return noteName + octaveName
}
func (p Pitch) Locations(fl *FretboardLayout) []FretboardCoordinate {
var coords []FretboardCoordinate
for i := 0; i < fl.NumString(); i++ {
index, err := fl.FretboardString(StringIndex(i)).Fret(p)
if err == nil {
coords = append(coords,
FretboardCoordinate{
fretboardString: StringIndex(i),
fret: FretboardOffset(index),
fretboardLayout: fl})
}
}
return coords
}
func (p Pitch) Note() Note {
return normalize(Note(p))
}