-
Notifications
You must be signed in to change notification settings - Fork 13
/
note_name.go
72 lines (64 loc) · 1007 Bytes
/
note_name.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
package theory
import (
"fmt"
"strings"
)
type NoteName = int
const (
C NoteName = 0
D NoteName = 2
E NoteName = 4
F NoteName = 5
G NoteName = 7
A NoteName = 9
B NoteName = 11
)
var NoteNames = []NoteName{C, D, E, F, G, A, B}
func NoteNameToString(n NoteName) string {
switch n {
case C:
return "C"
case D:
return "D"
case E:
return "E"
case F:
return "F"
case G:
return "G"
case A:
return "A"
case B:
return "B"
}
return ""
}
func NewNoteNameFromString(s string) (NoteName, error) {
if len(s) == 0 {
return 0, fmt.Errorf("Missing note name")
}
switch strings.ToUpper(s)[0:1] {
case "C":
return C, nil
case "D":
return D, nil
case "E":
return E, nil
case "F":
return F, nil
case "G":
return G, nil
case "A":
return A, nil
case "B":
return B, nil
}
return 0, fmt.Errorf("Invalid note name '%s'", s[0:1])
}
func MustNewNoteNameFromString(s string) NoteName {
n, err := NewNoteNameFromString(s)
if err != nil {
panic(err)
}
return n
}