-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.v
129 lines (126 loc) · 2.19 KB
/
event.v
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
module vmidi
struct NoteOff {
pub:
delta_time u64
channel byte
note byte
velocity byte
}
struct NoteOn {
pub:
delta_time u64
channel byte
note byte
velocity byte
}
struct NoteAftertouch {
pub:
delta_time u64
channel byte
note byte
amount byte
}
struct Controller {
pub:
delta_time u64
channel byte
controller_type byte
value byte
}
struct ProgramChange {
pub:
delta_time u64
channel byte
program_number byte
}
struct ChannelAftertouch {
pub:
delta_time u64
channel byte
amount byte
}
struct PitchBend {
pub:
delta_time u64
channel byte
lsb byte
msb byte
}
fn read_midi_event(file []byte, mut index_track &int, delta_time u64, mut last_status &byte) ?TrkData {
mut index := (*index_track)
use_curr_status := byte(file[index] & 0xf0) >> 4 >= 0x08
status_byte := if use_curr_status { file[index] } else { *last_status }
event_type := byte(status_byte & 0xf0) >> 4
midi_channel := byte(status_byte & 0x0f)
if use_curr_status {
(*last_status) = file[index]
index++
}
mut event := TrkData{}
match event_type {
0x08 {
event = NoteOff {
delta_time: delta_time
channel: midi_channel
note: file[index]
velocity: file[index + 1]
}
}
0x09 {
event = NoteOn {
delta_time: delta_time
channel: midi_channel
note: file[index]
velocity: file[index + 1]
}
}
0x0a {
event = NoteAftertouch {
delta_time: delta_time
channel: midi_channel
note: file[index]
amount: file[index + 1]
}
}
0x0b {
event = Controller {
delta_time: delta_time
channel: midi_channel
controller_type: file[index]
value: file[index + 1]
}
}
0x0c {
event = ProgramChange {
delta_time: delta_time
channel: midi_channel
program_number: file[index]
}
}
0x0d {
event = ChannelAftertouch {
delta_time: delta_time
channel: midi_channel
amount: file[index]
}
}
0x0e {
event = PitchBend {
delta_time: delta_time
channel: midi_channel
lsb: file[index]
msb: file[index + 1]
}
}
else {
println('UNKNOWN EVENT: ${int(event_type).hex()} ${file[index - 1]}')
return none
}
}
index++
if event_type != 0x0c && event_type != 0x0d {
index++
}
(*index_track) = index
return event
}