forked from vampirefrog/mdxtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDX.h
286 lines (258 loc) · 9 KB
/
MDX.h
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
#ifndef MDX_H_
#define MDX_H_
#include <stdint.h>
#include "exceptionf.h"
#include "Stream.h"
#include "FS.h"
struct MDXVoiceOsc {
uint8_t dt1_mul;
uint8_t tl;
uint8_t ks_ar;
uint8_t ame_d1r;
uint8_t dt2_d2r;
uint8_t d1l_rr;
void dump() {
printf("dt1_mul=0x%02x DT1=%d MUL=%d ", dt1_mul, getDT1(), getMUL());
printf("tl=0x%02x TL=%d\n", tl, getTL());
printf("ks_ar=0x%02x KS=%d AR=%d ", ks_ar, getKS(), getAR());
printf("ame_d1r=0x%02x AME=%d D1R=%d\n", ame_d1r, getAME(), getD1R());
printf("dt2_d2r=0x%02x DT2=%d D2R=%d ", dt2_d2r, getDT2(), getD2R());
printf("d1l_rr=0x%02x D1L=%d RR=%d\n", d1l_rr, getD1L(), getRR());
}
inline uint8_t getDT1() { return (dt1_mul >> 4) & 0x07; } // DeTune 1
inline uint8_t getMUL() { return dt1_mul & 0x0f; } // detune MULtiplier
inline uint8_t getTL() { return tl & 0x7f; } // Total Level (envelope)
inline uint8_t getKS() { return ks_ar >> 6; } // Key Scaling
inline uint8_t getAR() { return ks_ar & 0x1f; } // Attack Rate (envelope)
inline uint8_t getAME() { return ame_d1r >> 7; } // AMS Enable
inline uint8_t getD1R() { return ame_d1r & 0x1f; } // Decay Rate 1
inline uint8_t getDT2() { return dt2_d2r >> 6; } // DeTune 2
inline uint8_t getD2R() { return dt2_d2r & 0x1f; } // Decay Rate 2
inline uint8_t getD1L() { return d1l_rr >> 4; } // Decay Level 1
inline uint8_t getRR() { return d1l_rr & 0x0f; } // Release Rate
};
struct MDXVoice {
uint8_t number, fl_con, slot_mask;
MDXVoiceOsc osc[4];
bool load(ReadStream &s) {
uint8_t buf[27];
if((unsigned long)s.read(buf, sizeof(buf)) < sizeof(buf)) return false;
uint8_t *b = buf;
number = *b++;
fl_con = *b++;
slot_mask = *b++;
for(int i = 0; i < 4; i++, b++) osc[i].dt1_mul = *b;
for(int i = 0; i < 4; i++, b++) osc[i].tl = *b;
for(int i = 0; i < 4; i++, b++) osc[i].ks_ar = *b;
for(int i = 0; i < 4; i++, b++) osc[i].ame_d1r = *b;
for(int i = 0; i < 4; i++, b++) osc[i].dt2_d2r = *b;
for(int i = 0; i < 4; i++, b++) osc[i].d1l_rr = *b;
return true;
}
static const char *oscName(uint8_t n) {
const char *oscNames[] = { "M1", "C1", "M2", "C2" };
return oscNames[n & 0x03];
}
void dump() {
printf("Voice %d: fl_con=0x%02x fl=%d con=%d slot_mask=0x%02x\n", number, fl_con, getFL(), getCON(), slot_mask);
for(int i = 0; i < 4; i++) {
printf("%s: ", oscName(i));
osc[i].dump();
}
}
uint8_t getFL() { return (fl_con >> 3) & 0x07; }
uint8_t getCON() { return fl_con & 0x07; }
};
struct MDXChannelPos {
uint16_t offset, length;
};
struct MDXHeader {
const char *title, *pcmFile;
uint16_t fileBase;
MDXChannelPos channels[16];
uint8_t numChannels;
uint16_t voiceOffset;
MDXVoice *voices[256];
bool reorderVoices;
MDXHeader(): title(0), pcmFile(0), fileBase(0), numChannels(0), voiceOffset(0), reorderVoices(false) {
memset(channels, 0, sizeof(channels));
memset(voices, 0, sizeof(voices));
}
~MDXHeader() {
if(title) delete title;
if(pcmFile) delete pcmFile;
for(size_t i = 0; i < 256; i++) {
if(voices[i]) delete voices[i];
}
}
void read(FileReadStream &s);
void dump();
};
class MDXParser {
public:
int channel;
protected:
int pos;
uint8_t nn, n2, n3, n4, n5;
int16_t w, v;
enum {
None = 0, NoteDuration, TempoVal,
OPMRegisterNum, OPMRegisterVal, VoiceNum,
PanVal, VolumeVal, SoundLen,
RepeatStartCount, RepeatStartZero, RepeatEndOffsetMSB,
RepeatEndOffsetLSB, RepeatEscapeMSB, RepeatEscapeLSB,
DetuneMSB, DetuneLSB, PortamentoMSB,
PortamentoLSB, DataEndMSB, DataEndLSB,
KeyOnDelayVal, SyncSendChannel, ADPCMNoiseFreqVal,
LFODelayVal, LFOPitchWave, LFOPitchPeriodMSB,
LFOPitchPeriodLSB, LFOPitchChangeMSB, LFOPitchChangeLSB,
LFOVolumeWave, LFOVolumePeriodMSB, LFOVolumePeriodLSB,
LFOVolumeChangeMSB, LFOVolumeChangeLSB, OPMLFOSyncWave,
OPMLFOLFRQ, OPMLFOPMD, OPMLFOAMD,
OPMLFOPMSAMS, ExtendedCmd, FadeOutValue,
FlagValue,
} state;
public:
MDXParser(): pos(0), nn(0), n2(0), n3(0), n4(0), n5(0), w(0), v(0), state(None) {}
~MDXParser() {}
bool eat(uint8_t b);
static const char *commandName(uint8_t c) {
const char *cmdNames[] = {
"Informal", // 0xe6
"Extended MML", // 0xe7
"PCM4/8 enable", // 0xe8
"LFO delay", // 0xe9
"OPM LFO", // 0xea
"Amplitude LFO", // 0xeb
"Pitch LFO", // 0xec
"ADPCM/noise freq", // 0xed
"Sync wait", // 0xee
"Sync send", // 0xef
"Key on delay", // 0xf0
"Data end", // 0xf1
"Portamento", // 0xf2
"Detune", // 0xf3
"Repeat escape", // 0xf4
"Repeat end", // 0xf5
"Repeat start", // 0xf6
"Disable key-off", // 0xf7
"Sound length", // 0xf8
"Volume dec", // 0xf9
"Volume inc", // 0xfa
"Set volume", // 0xfb
"Output phase", // 0xfc
"Set voice #", // 0xfd
"Set OPM reg", // 0xfe
"Set tempo", // 0xff
};
if(c >= 0xe6 && c <= 0xff) return cmdNames[c - 0xe6];
return "Unknown";
}
static const char *noteName(int note) {
const char *noteNames[] = { "c", "c+", "d", "d+" , "e", "f", "f+", "g", "g+", "a", "a+", "b", };
return noteNames[(note + 3) % 12];
}
static int noteOctave(int note) {
return (note + 3) / 12;
}
static char channelName(uint8_t chan) {
if(chan < 8) return 'A' + chan;
if(chan < 16) return 'P' + chan - 8;
return '!';
}
static int volumeVal(uint8_t v) {
int vol_conv[] = {
85, 87, 90, 93, 95, 98, 101, 103,
106, 109, 111, 114, 117, 119, 122, 125
};
if(v < 16) return vol_conv[v];
return 255 - v;
}
// Callbacks
virtual void handleByte(uint8_t b) {}
virtual void handleRest(uint8_t duration) {}
virtual void handleNote(uint8_t note, uint8_t duration) {}
virtual void handleCommand(uint8_t c, ...) {}
virtual void handleVolumeInc() {}
virtual void handleVolumeDec() {}
virtual void handleDisableKeyOff() {}
virtual void handleSyncWait() {}
virtual void handlePCM8Enable() {}
virtual void handleSetTempo(uint8_t t) {}
virtual void handleSetVoiceNum(uint8_t t) {}
virtual void handlePan(uint8_t p) {}
virtual void handleSetVolume(uint8_t v) {}
virtual void handleSoundLength(uint8_t l) {}
virtual void handleKeyOnDelay(uint8_t d) {}
virtual void handleSyncSend(uint8_t s) {}
virtual void handleADPCMNoiseFreq(uint8_t f) {}
virtual void handleLFODelaySetting(uint8_t d) {}
virtual void handleRepeatStart(uint8_t r) {}
virtual void handleRepeatEnd(int16_t r) {}
virtual void handleRepeatEscape(int16_t r) {}
virtual void handleDetune(int16_t d) {}
virtual void handlePortamento(int16_t t) {}
virtual void handleSetOPMRegister(uint8_t reg, uint8_t val) {}
virtual void handleDataEnd() {}
virtual void handleDataEnd(int16_t end) {}
virtual void handleLFOPitch(uint8_t b, uint16_t period, uint16_t change) {}
virtual void handleLFOPitchMPON() {}
virtual void handleLFOPitchMPOF() {}
virtual void handleLFOVolume(uint8_t b, uint16_t period, uint16_t change) {}
virtual void handleLFOVolumeMAON() {}
virtual void handleLFOVolumeMAOF() {}
virtual void handleOPMLFO(uint8_t sync_wave, uint8_t lfrq, uint8_t pmd, uint8_t amd, uint8_t pms_ams) {}
virtual void handleOPMLFOMHON() {}
virtual void handleOPMLFOMHOF() {}
virtual void handleFadeOut(uint8_t f) {}
virtual void handlePCM8ExpansionShift() {}
virtual void handleUndefinedCommand(uint8_t b) {}
virtual void handleChannelStart(int chan) {}
virtual void handleChannelEnd(int chan) {}
virtual void handleKill() {}
virtual void handleFlag(uint8_t f) {}
};
class MDXMemParser: public MDXParser {
public:
uint8_t *data;
bool ended;
int dataLen;
int loopIterations;
protected:
int dataPos;
private:
int repeatStack[5];
int repeatStackPos;
public:
MDXMemParser(): MDXParser(), data(0), ended(false), dataLen(0), loopIterations(0), dataPos(0), repeatStackPos(0) {}
void feed() {
eat(data[dataPos++]);
if(dataPos >= dataLen) ended = true;
}
virtual void handleDataEnd() {
ended = true;
}
virtual void handleDataEnd(int16_t ofs) {
if(loopIterations-- > 0) dataPos -= ofs + 3;
else ended = true;
}
virtual void handleRepeatStart(uint8_t iterations) {
repeatStack[repeatStackPos++] = iterations - 1;
if(repeatStackPos >= 5) repeatStackPos = 4;
}
virtual void handleRepeatEnd(int16_t ofs) {
if(repeatStack[repeatStackPos - 1]-- > 0) {
dataPos += ofs;
if(dataPos < 0) dataPos = 0;
} else repeatStackPos--;
if(repeatStackPos < 0) repeatStackPos = 0;
}
virtual void handleRepeatEscape(int16_t ofs) {
if(repeatStack[repeatStackPos - 1] == 0) {
dataPos += ofs + 2;
repeatStackPos--;
if(repeatStackPos < 0) repeatStackPos = 0;
}
}
};
#endif /* MDX_H_ */