forked from afandian/go-midi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_functions_test.go
478 lines (380 loc) · 10.5 KB
/
midi_functions_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
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
// Copyright 2012 Joe Wass. All rights reserved.
// Use of this source code is governed by the MIT license
// which can be found in the LICENSE file.
// MIDI package
// A package for reading Standard Midi Files, written in Go.
// Joe Wass 2012
// joe@afandian.com
/*
* Tests for test_functions.
* Make sure that each midi function works.
*/
package midi
import (
"io"
"testing"
)
// Test that parseVarLength does what it should.
// Example data taken from http://www.music.mcgill.ca/~ich/classes/mumt306/midiformat.pdf
func TestVarLengthParser(t *testing.T) {
expected := []uint32{
0x00000000,
0x00000040,
0x0000007F,
0x00000080,
0x00002000,
0x00003FFF,
0x00004000,
0x00100000,
0x001FFFFF,
0x00200000,
0x08000000,
0x0FFFFFFF}
input := [][]byte{
[]byte{0x00},
[]byte{0x40},
[]byte{0x7F},
[]byte{0x81, 0x00},
[]byte{0xC0, 0x00},
[]byte{0xFF, 0x7F},
[]byte{0x81, 0x80, 0x00},
[]byte{0xC0, 0x80, 0x00},
[]byte{0xFF, 0xFF, 0x7F},
[]byte{0x81, 0x80, 0x80, 0x00},
[]byte{0xC0, 0x80, 0x80, 0x00},
[]byte{0xFF, 0xFF, 0xFF, 0x7F}}
var numItems = len(input)
for i := 0; i < numItems; i++ {
var reader = NewMockReadSeeker(&input[i])
var result, err = parseVarLength(reader)
if result != expected[i] {
t.Fatal("Expected ", expected[i], " got ", result)
}
if err != nil {
t.Fatal("Expected no error got ", err)
}
}
// Now we want to read past the end of a var length file.
// We should get an UnexpectedEndOfFile error.
// First read OK.
var reader = NewMockReadSeeker(&input[0])
var _, err = parseVarLength(reader)
if err != nil {
t.Fatal("Expected no error got ", err)
}
// Second read not OK.
_, err = parseVarLength(reader)
if err != UnexpectedEndOfFile {
t.Fatal("Expected End of file ")
}
}
// Test for parseUint32
func TestParse32Bit(t *testing.T) {
expected := []uint32{
0,
1,
4,
42,
429,
4294,
42949,
429496,
4294967,
42949672,
429496729,
4294967295,
}
input := [][]byte{
[]byte{0x00, 0x00, 0x00, 0x00},
[]byte{0x00, 0x00, 0x00, 0x01},
[]byte{0x00, 0x00, 0x00, 0x04},
[]byte{0x00, 0x00, 0x00, 0x2A},
[]byte{0x00, 0x00, 0x01, 0xAD},
[]byte{0x00, 0x00, 0x10, 0xC6},
[]byte{0x00, 0x00, 0xA7, 0xC5},
[]byte{0x00, 0x06, 0x8D, 0xB8},
[]byte{0x00, 0x41, 0x89, 0x37},
[]byte{0x02, 0x8F, 0x5C, 0x28},
[]byte{0x19, 0x99, 0x99, 0x99},
[]byte{0xFF, 0xFF, 0xFF, 0xFF},
}
var numItems = len(input)
for i := 0; i < numItems; i++ {
var reader = NewMockReadSeeker(&input[i])
var result, err = parseUint32(reader)
if result != expected[i] {
t.Fatal("Expected ", expected[i], " got ", result)
}
if err != nil {
t.Fatal("Expected no error got ", err)
}
}
// Now we want to read past the end of a file.
// We should get an UnexpectedEndOfFile error.
// First read OK.
var reader = NewMockReadSeeker(&input[0])
var _, err = parseUint32(reader)
if err != nil {
t.Fatal("Expected no error got ", err)
}
// Second read not OK.
_, err = parseUint32(reader)
if err != UnexpectedEndOfFile {
t.Fatal("Expected End of file ")
}
}
// Test for parseUint8
func TestParse8Bit(t *testing.T) {
expected := []uint8{
0,
1,
4,
42,
}
input := [][]byte{
[]byte{0x00},
[]byte{0x01},
[]byte{0x04},
[]byte{0x2A},
}
var numItems = len(input)
for i := 0; i < numItems; i++ {
var reader = NewMockReadSeeker(&input[i])
var result, err = parseUint8(reader)
if result != expected[i] {
t.Fatal("Expected ", expected[i], " got ", result)
}
if err != nil {
t.Fatal("Expected no error got ", err)
}
}
// Now we want to read past the end of a file.
// We should get an UnexpectedEndOfFile error.
// First read OK.
var reader = NewMockReadSeeker(&input[0])
var _, err = parseUint8(reader)
if err != nil {
t.Fatal("Expected no error got ", err)
}
// Second read not OK.
_, err = parseUint8(reader)
if err != UnexpectedEndOfFile {
t.Fatal("Expected End of file ")
}
}
// Test for parseUint16
func TestParse16Bit(t *testing.T) {
expected := []uint16{
0,
1,
4,
42,
429,
4294,
42949,
}
input := [][]byte{
[]byte{0x00, 0x00},
[]byte{0x00, 0x01},
[]byte{0x00, 0x04},
[]byte{0x00, 0x2A},
[]byte{0x01, 0xAD},
[]byte{0x10, 0xC6},
[]byte{0xA7, 0xC5},
}
var numItems = len(input)
for i := 0; i < numItems; i++ {
var reader = NewMockReadSeeker(&input[i])
var result, err = parseUint16(reader)
if result != expected[i] {
t.Fatal("Expected ", expected[i], " got ", result)
}
if err != nil {
t.Fatal("Expected no error got ", err)
}
}
// Now we want to read past the end of a file.
// We should get an UnexpectedEndOfFile error.
// First read OK.
var reader = NewMockReadSeeker(&input[0])
var _, err = parseUint16(reader)
if err != nil {
t.Fatal("Expected no error got ", err)
}
// Second read not OK.
_, err = parseUint16(reader)
if err != UnexpectedEndOfFile {
t.Fatal("Expected End of file ")
}
}
// Test for parseChunkHeader.
func TestParseChunkHeader(t *testing.T) {
// Headers for two popular chunk types.
// Length 6, as all MThds shoudl be 6 long.
var MThd = []byte{0x4D, 0x54, 0x68, 0x64, 0x00, 0x00, 0x00, 0x06}
// Arbitrary length 4294967 (parseUint32 is tested separately).
var MTrk = []byte{0x4D, 0x54, 0x72, 0x6b, 0x00, 0x41, 0x89, 0x37}
// Too short in the type word.
var tooShort1 = []byte{0x4D, 0x54, 0x72}
// To short in the length word.
var tooShort2 = []byte{0x4D, 0x54, 0x72, 0x6b, 0x00, 0x41, 0x89}
var header ChunkHeader
var err error
var reader io.ReadSeeker
// Try for typical MIDI file header
reader = NewMockReadSeeker(&MThd)
header, err = parseChunkHeader(reader)
if header.ChunkType != "MThd" {
t.Fatal("Got ", header, " expected MThd")
}
if header.Length != 6 {
t.Fatal("Got ", header, " expected 6")
}
if err != nil {
t.Fatal("Got error ", err)
}
// Try for typical track header
reader = NewMockReadSeeker(&MTrk)
header, err = parseChunkHeader(reader)
if header.ChunkType != "MTrk" {
t.Fatal("Got ", header, " expected MTrk")
}
if header.Length != 4294967 {
t.Fatal("Got ", header, " expected 4294967")
}
if err != nil {
t.Fatal("Got error ", err)
}
// Now two incomplete headers.
// Too short to parse the type
reader = NewMockReadSeeker(&tooShort1)
header, err = parseChunkHeader(reader)
if err == nil {
t.Fatal("Expected error for tooshort1")
}
// Too short to parse the length
reader = NewMockReadSeeker(&tooShort2)
header, err = parseChunkHeader(reader)
if err == nil {
t.Fatal("Expected error for tooshort 2")
}
}
// Test for parseHeaderData.
func TestParseHeaderData(t *testing.T) {
var err error
var data, expected HeaderData
// Format: 1
// Tracks: 2
// Division: metrical 5
var headerMetrical = NewMockReadSeeker(&[]byte{0x00, 0x01, 0x00, 0x02, 0x00, 0x05})
expected = HeaderData{
Format: 1,
NumTracks: 2,
TimeFormat: MetricalTimeFormat,
TimeFormatData: 0x00,
TicksPerQuarterNote: 5}
data, err = parseHeaderData(headerMetrical)
if err != nil {
t.Fatal(err)
}
if data != expected {
t.Fatal(data, " != ", expected)
}
// Format: 2
// Tracks: 1
// Division: timecode (actual data ignored for now)
var headerTimecode = NewMockReadSeeker(&[]byte{0x00, 0x02, 0x00, 0x01, 0xFF, 0x05})
expected = HeaderData{
Format: 2,
NumTracks: 1,
TimeFormat: TimeCodeTimeFormat,
TimeFormatData: 0x7F05, // Removed the top timecode type bit flag.
TicksPerQuarterNote: 0}
data, err = parseHeaderData(headerTimecode)
if err != nil {
t.Fatal(err)
}
if data != expected {
t.Fatal(data, " != ", expected)
}
// Format: 3, which doesn't exist.
var badFormat = NewMockReadSeeker(&[]byte{0x00, 0x03, 0x00, 0x01, 0xFF, 0x05})
data, err = parseHeaderData(badFormat)
if err != UnsupportedSmfFormat {
t.Fatal("Expected exception but got none")
}
// Too short in each field
var tooShort1 = NewMockReadSeeker(&[]byte{0x00, 0x02, 0x00, 0x01, 0xFF})
data, err = parseHeaderData(tooShort1)
if err != UnexpectedEndOfFile {
t.Fatal("Expected exception but got ", err)
}
var tooShort2 = NewMockReadSeeker(&[]byte{0x00, 0x02, 0x00})
data, err = parseHeaderData(tooShort2)
if err != UnexpectedEndOfFile {
t.Fatal("Expected exception but got none")
}
var tooShort3 = NewMockReadSeeker(&[]byte{0x00})
data, err = parseHeaderData(tooShort3)
if err != UnexpectedEndOfFile {
t.Fatal("Expected exception but got none")
}
}
// readStatusByte should read the status byte and return type and channel correctly.
func TestReadStatusByte(t *testing.T) {
expectedType := []uint8{1, 2, 3, 4}
expectedChannel := []uint8{5, 4, 3, 2}
input := [][]byte{
[]byte{0x15},
[]byte{0x24},
[]byte{0x33},
[]byte{0x42},
}
var numItems = len(input)
for i := 0; i < numItems; i++ {
var reader = NewMockReadSeeker(&input[i])
var messageType, messageChannel, err = readStatusByte(reader)
if messageType != expectedType[i] {
t.Fatal("Expected type", expectedType[i], " got ", messageType)
}
if messageChannel != expectedChannel[i] {
t.Fatal("Expected channel", expectedChannel[i], " got ", messageChannel)
}
if err != nil {
t.Fatal("Expected no error got ", err)
}
}
}
// Test that parsePitchWheelValue works for a number of values.
// It returns both unsigned absolute and signed relative values.
func TestParsePitchWheelValue(t *testing.T) {
// 0x200 should be centre, i.e. 0 relative.
mockReadSeeker := NewMockReadSeeker(&[]byte{
0x00, 0x40})
relative, absolute, err := parsePitchWheelValue(mockReadSeeker)
assertNoError(err, t)
assertInt16sEqual(relative, 0x00, t)
assertUint16Equal(absolute, 0x2000, t)
// 0x1234 encoded
mockReadSeeker = NewMockReadSeeker(&[]byte{
0x34, 0x24, // 0x1234 encoded
})
relative, absolute, err = parsePitchWheelValue(mockReadSeeker)
assertNoError(err, t)
assertInt16sEqual(relative, -0xDCC, t)
assertUint16Equal(absolute, 0x1234, t)
}
// Test that the parseString function works under normal circumstances.
func TestParseString(t *testing.T) {
var reader = NewMockReadSeeker(&[]uint8{0x10, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
expectedResult := "joe@afandian.com"
result, err := parseText(reader)
assertNoError(err, t)
assertStringsEqual(result, expectedResult, t)
}
// Test the parseString signals unexpected early end of file
func TestParseBadString(t *testing.T) {
var reader = NewMockReadSeeker(&[]uint8{0x11, 0x6A, 0x6F, 0x65, 0x40, 0x61, 0x66, 0x61, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x2E, 0x63, 0x6F, 0x6D})
_, err := parseText(reader)
assertError(err, UnexpectedEndOfFile, t)
}