-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_test.go
183 lines (141 loc) · 3.34 KB
/
midi_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package midi
import (
"os"
"testing"
)
func TestReadVariableLengthInteger(t *testing.T) {
// Test valid input
bs := make([]byte, 2)
bs[0] = 0xFF
bs[1] = 0x7F
v, n, err := readVariableLengthInteger(bs)
if err != nil {
t.Errorf("unexpected error %v", err)
}
if v != 16383 {
t.Errorf("expected value to be 16383, value returned is %v", v)
}
if n != 2 {
t.Errorf("expected num bytes read to be 2, num bytes returned is %v", n)
}
bs = make([]byte, 2)
bs[0] = 0x87
bs[1] = 0x68
v, n, err = readVariableLengthInteger(bs)
if err != nil {
t.Errorf("unexpected error %v", err)
}
if v != 1000 {
t.Errorf("expected value to be 1000, value returned is %v", v)
}
if n != 2 {
t.Errorf("expected num bytes read to be 2, num bytes returned is %v", n)
}
bs = make([]byte, 3)
bs[0] = 0xBD
bs[1] = 0x84
bs[2] = 0x40
v, n, err = readVariableLengthInteger(bs)
if err != nil {
t.Errorf("unexpected error %v", err)
}
if v != 1000000 {
t.Errorf("expected value to be 1000000, value returned is %v", v)
}
if n != 3 {
t.Errorf("expected num bytes read to be 3, num bytes returned is %v", n)
}
// Test invalid input
bs = make([]byte, 2)
bs[0] = 0xFF
bs[1] = 0xFF
v, n, err = readVariableLengthInteger(bs)
if err == nil {
t.Errorf("expected ReadVariableLengthInteger to return an error")
}
}
func TestWriteVariableLengthVariable(t *testing.T) {
bs := make([]byte, 1)
bs[0] = 0x0
data := writeVariableLengthInteger(0)
if len(data) != len(bs) {
t.Fatalf("0: inequal length of bytes %d - %d", len(data), len(bs))
}
for index, b := range data {
if bs[index] != b {
t.Errorf("0: byte %d is not equal", index)
}
}
t.Log("0 passed")
bs = make([]byte, 2)
bs[0] = 0xFF
bs[1] = 0x7F
data = writeVariableLengthInteger(16383)
if len(data) != len(bs) {
t.Fatalf("16383: inequal length of bytes %d - %d", len(data), len(bs))
}
for index, b := range data {
if bs[index] != b {
t.Errorf("16383: byte %d is not equal", index)
}
}
t.Log("16383 passed")
bs = make([]byte, 2)
bs[0] = 0x87
bs[1] = 0x68
data = writeVariableLengthInteger(1000)
if len(data) != len(bs) {
t.Fatalf("1000: inequal length of bytes %d - %d", len(data), len(bs))
}
for index, b := range data {
if bs[index] != b {
t.Errorf("1000: byte %d is not equal", index)
}
}
t.Log("1000 passed")
bs = make([]byte, 3)
bs[0] = 0xBD
bs[1] = 0x84
bs[2] = 0x40
data = writeVariableLengthInteger(1000000)
if len(data) != len(bs) {
t.Fatalf("1000000: inequal length of bytes %d - %d", len(data), len(bs))
}
for index, b := range data {
if bs[index] != b {
t.Errorf("1000000: byte %d is not equal", index)
}
}
t.Log("1000000 passed")
}
// TestMidi test midi
func TestMidi(t *testing.T) {
fo, err := os.Open("data/teddybear.mid")
if err != nil {
t.Fatalf("err %v", err)
}
defer fo.Close()
mf := &File{}
_, err = mf.ReadFrom(fo)
if err != nil {
t.Fatalf("err %v", err)
}
t.Logf("%v", *mf.Header)
t.Logf("chunks %v\n", mf.Chunks)
if len(mf.Tracks) > 0 {
track := mf.Tracks[1]
for index, event := range track.Events {
if index < 10 {
t.Logf("%v", event)
}
}
}
// mft := &File{}
// mft.Chunks = []*Chunk{mf.Chunks[0]}
// for _, track := range mf.Tracks {
// mft.Chunks = append(mft.Chunks, track.Chunk())
// }
// f, err := os.Create("data/teddybear_test.mid")
// defer f.Close()
// mft.WriteTo(f)
}