-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexclusive.go
74 lines (57 loc) · 1.38 KB
/
exclusive.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
package midi
import (
"errors"
"io"
)
// SystemExclusiveEvent representation
type SystemExclusiveEvent struct {
coreEvent
Data []byte
}
// WriteTo writer
func (e *SystemExclusiveEvent) WriteTo(w io.Writer) (int64, error) {
var totalBytesWritten int64
n, err := w.Write(writeVariableLengthInteger(e.deltaTime))
if err != nil {
return 0, err
}
totalBytesWritten += int64(n)
n, err = w.Write([]byte{0xF0})
if err != nil {
return 0, err
}
totalBytesWritten += int64(n)
n, err = w.Write(writeVariableLengthInteger(uint32(len(e.Data))))
if err != nil {
return 0, err
}
totalBytesWritten += int64(n)
n, err = w.Write(e.Data)
if err != nil {
return 0, err
}
return totalBytesWritten + int64(n), nil
}
// parseSystemExclusive parses a system exclusive event
func parseSystemExclusive(statusByte uint8, deltaTime uint32, data []byte) (event Event, bytesRead uint32, err error) {
numBytes, bytesRead, err := readVariableLengthInteger(data)
if err != nil {
return
}
data = data[bytesRead:]
if uint32(len(data)) < numBytes {
err = errors.New("given system exclusive event length exceeds available data length")
return
}
bytesRead += numBytes
exclusiveData := make([]byte, numBytes)
copy(exclusiveData, data)
event = &SystemExclusiveEvent{
coreEvent: coreEvent{
deltaTime: deltaTime,
eventType: SystemExclusive,
},
Data: exclusiveData,
}
return
}