-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRolandArrangerTypes.bt
279 lines (252 loc) · 6.66 KB
/
RolandArrangerTypes.bt
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
//------------------------------------------------
//--- 010 Editor v12.0 Binary Template
//
// File: RolandArrangerTypes.bt
// Authors: Daniel Stien <[email protected]>
// Version:
// Purpose: Common types used in Roland arranger ROM images.
// Category: Misc
// File Mask:
// ID Bytes:
// History:
//------------------------------------------------
#define TICKS_PER_BEAT 24
#define PHRASES 6
#define CHORD_MODES 3
#define END_OF_TRACK 0x3F
typedef struct (int length)
{
if (length == -1) {
ubyte SpecifiedLength;
length = SpecifiedLength;
}
char Value[length];
} Label <optimize=false,read=Str("%s", Value)>;
typedef struct
{
char Name[12];
char Data0[23];
ubyte Sample;
char Data[180];
} Instrument <read=Str("%s (%i)", Name, Sample)>;
typedef struct
{
char Name[12];
char Data[48];
} Sample <read=Str("%s", Name)>;
typedef struct
{
char Name[12];
char Data[204];
} DrumSample <read=Str("%s", Name)>;
typedef struct
{
char Data[1152];
char Name[12];
} Drumset <read=Str("%s", Name)>;
typedef struct
{
char Header[25];
byte TitleLength;
char Title[TitleLength];
} Smf <read=Str("%s", Title)>;
// GS Init?
typedef struct
{
uint16 Unknown0[4];
char Frustration[16];
uchar Unknown1[48];
struct
{
uint32 Data0;
uchar Channel;
uchar Data1[107];
} Channel[16];
char Version[32];
} GsInit;
typedef enum
{
// E-20 gen, TN-SC1
EventVersion0,
// GS, TN-SC2
EventVersion1,
} EventVersion;
typedef enum
{
EventNote,
EventProgramChange,
EventUnknown,
EventEnd
} EventType;
string ReadNoteValue(byte &n)
{
local string name;
switch (n % 12) {
case 0: name = "C"; break;
case 1: name = "C#"; break;
case 2: name = "D"; break;
case 3: name = "D#"; break;
case 4: name = "E"; break;
case 5: name = "F"; break;
case 6: name = "F#"; break;
case 7: name = "G"; break;
case 8: name = "G#"; break;
case 10: name = "A"; break;
case 11: name = "A#"; break;
case 12: name = "B"; break;
}
// TODO: Range offset varies by part type
local int octave = (n / 12) + 3;
return Str("%s%d (%d)", name, octave, n);
}
string ReadVelocityValue(byte &v)
{
if (v > 0) {
return Str("%d (%d)", (v + 1 << 3) - 1, v);
}
else {
return "0";
}
}
// New hypothesis for E-20/TN-SC1-nn - oct 2024
// 1 bit: is program change
// 1: 7-bit program
// 0: 1 bit is controller or delta?
// 1: 6-bit value
// Multiple bytes or other event types?
// 0: 6-bit value:
// 0x3F (all bits set): end of track
// Else:
// Note
// 4-bit velocity
// 4-bit delta
typedef struct
{
local EventType Type;
byte IsProgramChange : 1;
if (IsProgramChange) {
Type = EventProgramChange;
byte Program : 7;
}
else {
byte IsElse : 1;
local byte Value = ReadByte() & 0x3F;
if (IsElse) {
Type = EventUnknown;
byte unknown0 : 6;
//byte unknown1;
//byte unknown2;
//byte unknown3;
}
else if (Value == END_OF_TRACK) {
Type = EventEnd;
byte EndOfTrack : 6;
}
else {
Type = EventNote;
byte Note : 6 <read=ReadNoteValue>;
byte Velocity : 4 <read=ReadVelocityValue>;
byte Delta : 4;
}
}
} EventV0 <read=ReadEventV0,optimize=false>;
string ReadEventV0(EventV0 &e)
{
local string type = EnumToString(e.Type);
switch (e.Type) {
case EventProgramChange:
return Str("%s: %d", type, e.Program);
case EventUnknown:
return Str("%s: %d ...", type, e.unknown0);
case EventNote:
return Str("%s: %s vel: %s del: %d", type, ReadNoteValue(e.Note), ReadVelocityValue(e.Velocity), e.Length);
case EventEnd:
return type;
}
}
// Chunk of custom coded MIDI messages
typedef struct (EventVersion ver)
{
switch (ver) {
case EventVersion0:
local int i = 0;
while (true) {
EventV0 Events;
if (Events.Type == EventEnd || i++ > 50) break;
}
break;
case EventVersion1:
EventV0 Events[5];
break;
}
} Chunk <read=ReadChunk,optimize=false>;
string ReadChunk(Chunk &c)
{
return "...";
}
typedef struct (int count)
{
BitfieldDisablePadding();
int Unknown : 24;
Chunk Chunks(EventVersion0)[count];
} ChunkCollection <comment=Str("%d", Unknown),optimize=false>;
typedef struct (byte segment, int64 styleSegments[])
{
local byte Segment = segment;
ushort Offset;
local int64 Full = styleSegments[segment - 1] + Offset;
} Ptr <read=Str("%X:%04X (%06X)", Segment, Offset, Full)>;
// TODO: Figure out how the segments are mapped. Endianess change? Interleaving?
local int segmap[18];
segmap[ 0] = 1;
segmap[ 1] = 0;
segmap[ 2] = 3;
//segmap[ 3] = 4; // !!
segmap[ 3] = 2;
segmap[ 4] = 5;
//segmap[ 5] = 2; // !!
segmap[ 5] = 4;
segmap[ 6] = 7;
segmap[ 7] = 6;
segmap[ 8] = 9;
segmap[ 9] = 8;
segmap[10] = 11;
segmap[11] = 10;
segmap[12] = 13;
segmap[13] = 12;
segmap[14] = 15;
segmap[15] = 17; // !!
segmap[16] = 16; // !!
segmap[17] = 14;
typedef struct (int count, int64 styleSegments[], EventVersion ver)
{
struct (int count, int64 styleSegments[]) {
local int i;
for (i = 0; i < count; i++) {
byte Segments : 4;
}
for (i = 0; i < count; i++) {
Ptr Offsets(Segments[segmap[i]], styleSegments);
}
} Ptrs(count, styleSegments);
local int64 pos <hidden=true> = FTell();
local int i;
for (i = 0; i < count; i++) {
FSeek(Ptrs.Offsets[i].Full);
Chunk Data(ver);
}
FSeek(pos);
} ChunkRefs <optimize=false>;
typedef struct (char name[], int64 styleSegments[], EventVersion ver)
{
local string Name = name;
ChunkRefs DrumChunks(PHRASES, styleSegments, ver);
ubyte TicksPerBar;
ubyte Tempo;
// Current hypothesis:
// 4 (Bass, Accomp1, Accomp2, Accomp3) * 18 (3 chord variants (major, minor, seventh) * 6 phrases (Intro, Original, Variation, ToOriginal, ToVariation, Ending))
ChunkRefs BassChunks(PHRASES * CHORD_MODES, styleSegments, ver);
ChunkRefs Part2Chunks(PHRASES * CHORD_MODES, styleSegments, ver);
ChunkRefs Part3Chunks(PHRASES * CHORD_MODES, styleSegments, ver);
ChunkRefs Part4Chunks(PHRASES * CHORD_MODES, styleSegments, ver);
} StyleHeader <read=Str("%s %d/4 @ %d BPM", Name, TicksPerBar / TICKS_PER_BEAT, Tempo)>;