-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_midi_file_with_running_status.ksy
182 lines (179 loc) · 4.29 KB
/
standard_midi_file_with_running_status.ksy
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
meta:
id: standard_midi_file_with_running_status
title: Standard MIDI file with running status
file-extension:
- mid
- midi
- smf
xref:
justsolve: MIDI
loc:
- fdd000102 # MIDI Sequence Data
- fdd000119 # Standard MIDI File
mime: audio/midi
pronom: x-fmt/230
wikidata: Q10610388
license: CC0-1.0
imports:
- /common/vlq_base128_be
endian: be
doc: |
Standard MIDI file, typically known just as "MID", is a standard way
to serialize series of MIDI events, which is a protocol used in many
music synthesizers to transfer music data: notes being played,
effects being applied, etc.
Internally, file consists of a header and series of tracks, every
track listing MIDI events with certain header designating time these
events are happening.
NOTE: Rarely, MIDI files employ certain stateful compression scheme
to avoid storing certain elements of further elements, instead
reusing them from events which happened earlier in the
stream. Kaitai Struct (as of v0.9) is currently unable to parse
these, but files employing this mechanism are relatively rare.
seq:
- id: hdr
type: header
- id: tracks
type: track
repeat: expr
repeat-expr: hdr.num_tracks
types:
header:
seq:
- id: magic
contents: "MThd"
- id: len_header
type: u4
- id: format
type: u2
- id: num_tracks
type: u2
- id: division
type: s2
track:
seq:
- id: magic
contents: "MTrk"
- id: len_events
type: u4
- id: events
type: track_events
size: len_events
track_events:
seq:
- id: event
type: 'track_event(_index != 0 ? event[_index - 1].event_type : 0xFF)'
repeat: eos
track_event:
seq:
- id: v_time
type: vlq_base128_be
- id: event_header
type: u1
if: not using_running_status
- id: meta_event_body
type: meta_event_body
if: event_header == 0xff
- id: sysex_body
type: sysex_event_body
if: event_header == 0xf0
- id: event_body
type:
switch-on: event_type
cases:
0x80: note_off_event
0x90: note_on_event
0xa0: polyphonic_pressure_event
0xb0: controller_event
0xc0: program_change_event
0xd0: channel_pressure_event
0xe0: pitch_bend_event
params:
- id: previous_event_type
type: u1
instances:
using_running_status:
value: status_byte_lookahead & 0x80 == 0
status_byte_lookahead:
pos: _io.pos
type: u1
event_type:
value: using_running_status ? previous_event_type : event_header & 0xf0
channel:
value: event_header & 0xf
if: event_type != 0xf0
meta_event_body:
seq:
- id: meta_type
type: u1
enum: meta_type_enum
- id: len
type: vlq_base128_be
- id: body
size: len.value
enums:
meta_type_enum:
0x00: sequence_number
0x01: text_event
0x02: copyright
0x03: sequence_track_name
0x04: instrument_name
0x05: lyric_text
0x06: marker_text
0x07: cue_point
0x20: midi_channel_prefix_assignment
0x2f: end_of_track
0x51: tempo
0x54: smpte_offset
0x58: time_signature
0x59: key_signature
0x7f: sequencer_specific_event
note_off_event:
seq:
- id: note
type: u1
- id: velocity
type: u1
note_on_event:
seq:
- id: note
type: u1
- id: velocity
type: u1
polyphonic_pressure_event:
seq:
- id: note
type: u1
- id: pressure
type: u1
controller_event:
seq:
- id: controller
type: u1
- id: value
type: u1
program_change_event:
seq:
- id: program
type: u1
channel_pressure_event:
seq:
- id: pressure
type: u1
pitch_bend_event:
seq:
- id: b1
type: u1
- id: b2
type: u1
instances:
bend_value:
value: (b2 << 7) + b1 - 0x4000
adj_bend_value:
value: bend_value - 0x4000
sysex_event_body:
seq:
- id: len
type: vlq_base128_be
- id: data
size: len.value