-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpitch_test.go
168 lines (149 loc) · 3 KB
/
pitch_test.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
package musictheory
import (
"math"
"testing"
)
const tolerance = 0.000001
func TestSharpPitchNames(test *testing.T) {
data := []struct {
pitch int
expected string
}{
{C, "C#4"},
{D, "D#4"},
{E, "F4"},
{F, "F#4"},
{G, "G#4"},
{A, "A#4"},
{B, "C4"},
}
for i, t := range data {
actual := NewPitch(t.pitch, Sharp, 4).Name(AscNames)
if actual != t.expected {
test.Errorf("index=%d actual=%s expected=%s", i, actual, t.expected)
}
}
}
func TestFlatPitchNames(test *testing.T) {
data := []struct {
pitch int
expected string
}{
{C, "B4"},
{D, "Db4"},
{E, "Eb4"},
{F, "E4"},
{G, "Gb4"},
{A, "Ab4"},
{B, "Bb4"},
}
for i, t := range data {
actual := NewPitch(t.pitch, Flat, 4).Name(DescNames)
if actual != t.expected {
test.Errorf("index=%d actual=%s expected=%s", i, actual, t.expected)
}
}
}
func TestDoubleSharpPitchNames(test *testing.T) {
data := []struct {
pitch int
expected string
}{
{C, "D4"},
{D, "E4"},
{E, "F#4"},
{F, "G4"},
{G, "A4"},
{A, "B4"},
{B, "C#4"},
}
for i, t := range data {
actual := NewPitch(t.pitch, DoubleSharp, 4).Name(AscNames)
if actual != t.expected {
test.Errorf("index=%d actual=%s expected=%s", i, actual, t.expected)
}
}
}
func TestDoubleFlatPitchNames(test *testing.T) {
data := []struct {
pitch int
expected string
}{
{C, "Bb4"},
{D, "C4"},
{E, "D4"},
{F, "Eb4"},
{G, "F4"},
{A, "G4"},
{B, "A4"},
}
for i, t := range data {
actual := NewPitch(t.pitch, DoubleFlat, 4).Name(DescNames)
if actual != t.expected {
test.Errorf("index=%d actual=%s expected=%s", i, actual, t.expected)
}
}
}
func TestFrequency(test *testing.T) {
data := []struct {
input Pitch
expected float64
}{
{NewPitch(C, Natural, 4), 261.625565},
{NewPitch(A, Natural, 4), 440.0},
}
for _, t := range data {
actual := t.input.Freq()
if closeEqualFloat64(actual, t.expected) {
test.Errorf("input=%s output=%f, expected=%f",
t.input,
actual,
t.expected)
}
}
}
func TestMIDI(test *testing.T) {
data := []struct {
input Pitch
expected int
}{
{NewPitch(C, Natural, 3), 60},
{NewPitch(C, Natural, 4), 72},
{NewPitch(A, Natural, 4), 81},
}
for _, t := range data {
actual := t.input.MIDI()
if actual != t.expected {
test.Errorf("input=%s output=%v, expected=%v",
t.input,
actual,
t.expected)
}
}
}
func closeEqualFloat64(actual, expected float64) bool {
return math.Abs(actual-expected) >= tolerance
}
func TestNearestPitch(test *testing.T) {
data := []struct {
input float64
expected Pitch
}{
{74, NewPitch(D, Natural, 2)},
{190, NewPitch(F, Sharp, 3)},
{400, NewPitch(G, Natural, 4)},
{350, NewPitch(F, Natural, 4)},
{concertFrequency, NewPitch(A, Natural, 4)},
{800, NewPitch(G, Natural, 5)},
{32000, NewPitch(B, Natural, 10)},
}
for _, t := range data {
actual := NearestPitch(t.input)
if !actual.Eq(t.expected) {
test.Errorf("input=%f output=%v, expected=%v",
t.input,
actual,
t.expected)
}
}
}