Mercurial > hg > dmlib
annotate minijss/jloadjss.c @ 1239:2be42ba9fc39
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Mar 2015 18:45:13 +0200 |
parents | b8c9af10b7fd |
children | 5ae2ce21c7ae |
rev | line source |
---|---|
0 | 1 /* |
2 * miniJSS - JSSMOD module loader | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
1163 | 4 * (C) Copyright 2007-2015 Tecnic Software productions (TNSP) |
0 | 5 */ |
6 #include "jssmod.h" | |
7 | |
8 | |
1196 | 9 // If all pattern modes are required, enable them here |
0 | 10 #ifdef JM_SUP_PATMODE_ALL |
282 | 11 # define JM_SUP_PATMODE_1 1 |
12 # define JM_SUP_PATMODE_2 1 | |
13 # define JM_SUP_PATMODE_3 1 | |
14 # define JM_SUP_PATMODE_4 1 | |
15 # define JM_SUP_PATMODE_5 1 | |
0 | 16 #endif |
17 | |
18 | |
1196 | 19 // Short helper macros for reading data |
20 #define JSGETBUF(XV, XT) \ | |
21 if (!dmf_read_str(inFile, XV, sizeof(XT))) \ | |
22 return DMERR_OUT_OF_DATA | |
23 | |
24 #define JSGETBYTE(XV) \ | |
25 if (!dmf_read_byte(inFile, XV)) \ | |
26 return DMERR_OUT_OF_DATA | |
0 | 27 |
28 | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
29 static int jssDoGetConvertedNote(DMResource *inFile, JSSNote *pnote, Uint8 note) |
0 | 30 { |
31 Uint8 tmp; | |
32 | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
33 if (note == 127) |
1197 | 34 pnote->note = jsetNoteOff; |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
35 else |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
36 if (note == 0) |
1197 | 37 pnote->note = jsetNotSet; |
0 | 38 else |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
39 pnote->note = note - 1; |
0 | 40 |
41 JSGETBYTE(&tmp); | |
1197 | 42 pnote->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 43 |
44 JSGETBYTE(&tmp); | |
1197 | 45 pnote->volume = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 46 |
47 JSGETBYTE(&tmp); | |
1197 | 48 pnote->effect = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 49 |
50 JSGETBYTE(&tmp); | |
1197 | 51 pnote->param = (tmp == 0 && pnote->effect == jsetNotSet) ? jsetNotSet : tmp; |
0 | 52 |
53 return DMERR_OK; | |
54 } | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
55 |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
56 |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
57 static inline int jssGetConvertedNote(DMResource *inFile, JSSNote *pnote) |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
58 { |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
59 Uint8 tmp; |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
60 JSGETBYTE(&tmp); |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
61 return jssDoGetConvertedNote(inFile, pnote, tmp); |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
62 } |
0 | 63 |
64 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
65 #if defined(JM_SUP_PATMODE_2) || defined(JM_SUP_PATMODE_4) |
1197 | 66 static int jssGetCompressedNote(DMResource *inFile, JSSNote *pnote) |
0 | 67 { |
68 Uint8 packb, tmp; | |
69 | |
70 JSGETBYTE(&packb); | |
71 if (packb & 0x80) | |
72 { | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
73 if (packb & JM_COMP_NOTE) |
0 | 74 { |
75 JSGETBYTE(&tmp); | |
76 if (tmp == 127) | |
1197 | 77 pnote->note = jsetNoteOff; |
0 | 78 else |
1197 | 79 pnote->note = tmp; |
0 | 80 } |
81 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
82 if (packb & JM_COMP_INSTRUMENT) |
0 | 83 { |
84 JSGETBYTE(&tmp); | |
1197 | 85 pnote->instrument = tmp; |
0 | 86 } |
87 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
88 if (packb & JM_COMP_VOLUME) |
0 | 89 { |
90 JSGETBYTE(&tmp); | |
1197 | 91 pnote->volume = tmp; |
0 | 92 } |
93 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
94 if (packb & JM_COMP_EFFECT) |
0 | 95 { |
96 JSGETBYTE(&tmp); | |
1197 | 97 pnote->effect = tmp; |
98 pnote->param = 0; | |
0 | 99 } |
100 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
101 if (packb & JM_COMP_PARAM) |
0 | 102 { |
103 JSGETBYTE(&tmp); | |
1197 | 104 pnote->param = tmp; |
0 | 105 } |
106 } | |
107 else | |
108 { | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
109 int ret; |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
110 if ((ret = jssDoGetConvertedNote(inFile, pnote, packb)) != DMERR_OK) |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
111 return ret; |
0 | 112 } |
113 | |
114 return DMERR_OK; | |
115 } | |
116 #endif | |
117 | |
118 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
119 #ifdef JM_SUP_PATMODE_2 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
120 static int jssGetPatternCompHoriz(DMResource *inFile, JSSPattern *pattern) |
0 | 121 { |
122 int row, channel; | |
123 | |
124 assert(buf != NULL); | |
125 assert(pattern != NULL); | |
126 | |
127 for (row = 0; row < pattern->nrows; row++) | |
128 for (channel = 0; channel < pattern->nchannels; channel++) | |
129 { | |
1197 | 130 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
131 int res = jssGetCompressedNote(inFile, pnote); | |
0 | 132 if (res != DMERR_OK) |
1197 | 133 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", |
134 row, channel); | |
0 | 135 } |
136 | |
137 return DMERR_OK; | |
138 } | |
139 #endif | |
140 | |
141 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
142 #ifdef JM_SUP_PATMODE_4 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
143 static int jssGetPatternCompVert(DMResource *inFile, JSSPattern *pattern) |
0 | 144 { |
145 int row, channel; | |
146 | |
147 assert(buf != NULL); | |
148 assert(pattern != NULL); | |
149 | |
150 for (channel = 0; channel < pattern->nchannels; channel++) | |
151 for (row = 0; row < pattern->nrows; row++) | |
152 { | |
1197 | 153 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
154 int res = jssGetCompressedNote(inFile, pnote); | |
0 | 155 if (res != DMERR_OK) |
1197 | 156 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", |
157 row, channel); | |
0 | 158 } |
159 | |
160 return DMERR_OK; | |
161 } | |
162 #endif | |
163 | |
164 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
165 #ifdef JM_SUP_PATMODE_1 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
166 static int jssGetPatternRawHoriz(DMResource *inFile, JSSPattern *pattern) |
0 | 167 { |
168 int row, channel; | |
169 | |
170 assert(buf != NULL); | |
171 assert(pattern != NULL); | |
172 | |
173 for (row = 0; row < pattern->nrows; row++) | |
174 for (channel = 0; channel < pattern->nchannels; channel++) | |
175 { | |
1197 | 176 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
177 int res = jssGetConvertedNote(inFile, pnote); | |
0 | 178 if (res != DMERR_OK) |
1197 | 179 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", |
180 row, channel); | |
0 | 181 } |
182 | |
183 return DMERR_OK; | |
184 } | |
185 #endif | |
186 | |
187 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
188 #ifdef JM_SUP_PATMODE_3 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
189 static int jssGetPatternRawVert(DMResource *inFile, JSSPattern *pattern) |
0 | 190 { |
191 int row, channel; | |
192 | |
193 assert(buf != NULL); | |
194 assert(pattern != NULL); | |
195 | |
196 for (channel = 0; channel < pattern->nchannels; channel++) | |
197 for (row = 0; row < pattern->nrows; row++) | |
198 { | |
1197 | 199 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
200 int res = jssGetConvertedNote(inFile, pnote); | |
0 | 201 if (res != DMERR_OK) |
1197 | 202 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", |
203 row, channel); | |
0 | 204 } |
205 | |
206 return DMERR_OK; | |
207 } | |
208 #endif | |
209 | |
210 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
211 #ifdef JM_SUP_PATMODE_5 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
212 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
213 #undef JSGETBYTE |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
214 #define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
215 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
216 #define JSFOREACHNOTE1 \ |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
217 for (channel = 0; channel < pattern->nchannels; channel++) \ |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
218 for (row = 0; row < pattern->nrows; row++) { \ |
1197 | 219 JSSNote *pnote = pattern->data + (pattern->nchannels * row) + channel; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
220 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
221 #define JSFOREACHNOTE2 } |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
222 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
223 static int jssGetPatternRawVertElem(DMResource *inFile, JSSPattern *pattern) |
0 | 224 { |
225 int row, channel; | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
226 Uint8 tmp; |
0 | 227 |
228 assert(buf != NULL); | |
229 assert(pattern != NULL); | |
230 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
231 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
232 JSGETBYTE(&tmp); |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
233 if (tmp == 0) |
1197 | 234 pnote->note = jsetNotSet; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
235 else if (tmp == 127) |
1197 | 236 pnote->note = jsetNoteOff; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
237 else |
1197 | 238 pnote->note = tmp - 1; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
239 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
240 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
241 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
242 JSGETBYTE(&tmp); |
1197 | 243 pnote->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
244 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
245 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
246 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
247 JSGETBYTE(&tmp); |
1197 | 248 pnote->volume = (tmp > 0) ? tmp - 1 : jsetNotSet; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
249 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
250 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
251 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
252 JSGETBYTE(&tmp); |
1197 | 253 pnote->effect = (tmp > 0) ? tmp - 1 : jsetNotSet; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
254 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
255 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
256 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
257 JSGETBYTE(&tmp); |
1197 | 258 pnote->param = (tmp == 0 && pnote->effect == jsetNotSet) ? jsetNotSet : tmp; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
259 JSFOREACHNOTE2 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
260 |
0 | 261 return DMERR_OK; |
262 } | |
263 #endif | |
264 | |
265 | |
266 #undef JSGETBUF | |
267 #undef JSGETBYTE | |
268 #define JSGETBUF(XV, XT) do { \ | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
269 if (!dmf_read_str(inFile, XV, sizeof(XT))) \ |
0 | 270 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ |
271 "Out of data at getting " # XT " (%d bytes)\n", sizeof(XT)); \ | |
272 } while (0) | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
273 #define JSGETBYTE(XV) if (!dmf_read_byte(inFile, XV)) return DMERR_OUT_OF_DATA |
0 | 274 |
275 | |
276 #ifdef JM_SUP_EXT_INSTR | |
277 static void jssCopyEnvelope(JSSEnvelope *e, JSSMODEnvelope *je) | |
278 { | |
279 int i; | |
280 | |
281 e->flags = je->flags; | |
282 e->npoints = je->npoints; | |
283 e->sustain = je->sustain; | |
284 e->loopS = je->loopS; | |
285 e->loopE = je->loopE; | |
286 | |
287 for (i = 0; i < je->npoints; i++) | |
288 { | |
289 e->points[i].frame = je->points[i].frame; | |
290 e->points[i].value = je->points[i].value; | |
291 } | |
292 } | |
293 #endif | |
294 | |
295 | |
796 | 296 int jssLoadJSSMOD(DMResource *inFile, JSSModule **ppModule, BOOL probe) |
0 | 297 { |
298 JSSModule *module; | |
299 JSSMODHeader jssH; | |
300 int index; | |
301 | |
302 *ppModule = NULL; | |
303 | |
304 // Check the JSSMOD header | |
1167 | 305 dmMemset(&jssH, 0, sizeof(jssH)); |
0 | 306 JSGETBUF(&jssH, JSSMODHeader); |
307 | |
308 if (memcmp(jssH.idMagic, "JM", 2) != 0) | |
309 { | |
796 | 310 if (probe) |
311 return DMERR_NOT_SUPPORTED; | |
312 else | |
313 JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, | |
314 "Not a JSSMOD file, header signature mismatch!\n"); | |
0 | 315 } |
316 | |
317 if (jssH.idVersion != JSSMOD_VERSION) | |
318 { | |
1198 | 319 if (probe) |
320 return DMERR_VERSION; | |
321 else | |
0 | 322 JSSERROR(DMERR_VERSION, DMERR_VERSION, |
1212 | 323 "Unsupported version of JSSMOD 0x%2x, this version only supports 0x%2x!\n", |
0 | 324 jssH.idVersion, JSSMOD_VERSION); |
325 } | |
326 | |
1198 | 327 if (probe) |
328 return DMERR_OK; | |
329 | |
1218
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
330 // Check some of the things for sanity |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
331 if (jssH.nchannels > jsetMaxChannels || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
332 jssH.nchannels == 0 || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
333 jssH.norders > jsetMaxOrders || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
334 jssH.npatterns > jsetMaxPatterns || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
335 jssH.nextInstruments > jsetMaxInstruments || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
336 jssH.ninstruments > jsetMaxInstruments) |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
337 { |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
338 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
339 "Invalid values in JSSMOD header.\n"); |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
340 } |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
341 |
0 | 342 // Allocate the module |
1239 | 343 if ((*ppModule = module = jssAllocateModule()) == NULL) |
0 | 344 { |
345 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
346 "Could not allocate memory for module structure.\n"); | |
347 } | |
348 | |
349 // Copy header information | |
1216 | 350 module->defFlags = jssH.defFlags; |
351 module->intVersion = jssH.intVersion; | |
352 | |
0 | 353 module->norders = jssH.norders; |
354 module->npatterns = jssH.npatterns; | |
355 module->nchannels = jssH.nchannels; | |
356 module->nextInstruments = jssH.nextInstruments; | |
357 module->ninstruments = jssH.ninstruments; | |
358 module->defRestartPos = jssH.defRestartPos; | |
359 module->defSpeed = jssH.defSpeed; | |
360 module->defTempo = jssH.defTempo; | |
361 | |
362 // Get the orders list | |
363 for (index = 0; index < module->norders; index++) | |
364 { | |
365 Sint16 order; | |
366 JSGETBUF(&order, Sint16); | |
367 module->orderList[index] = order; | |
368 } | |
369 | |
370 // Parse the patterns | |
371 for (index = 0; index < module->npatterns; index++) | |
372 { | |
373 JSSMODPattern jssP; | |
374 int result = DMERR_INVALID_DATA; | |
375 | |
376 // Get header and check size | |
1167 | 377 dmMemset(&jssP, 0, sizeof(jssP)); |
0 | 378 JSGETBUF(&jssP, JSSMODPattern); |
379 | |
380 // Allocate pattern | |
381 module->patterns[index] = jssAllocatePattern(jssP.nrows, module->nchannels); | |
382 if (module->patterns[index] == NULL) | |
383 { | |
384 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1212 | 385 "Could not allocate memory for pattern #%d.\n", |
386 index); | |
0 | 387 } |
388 | |
389 // Get pattern data | |
390 switch (jssH.patMode) | |
391 { | |
392 #ifdef JM_SUP_PATMODE_1 | |
393 case PATMODE_RAW_HORIZ: | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
394 result = jssGetPatternRawHoriz(inFile, module->patterns[index]); |
0 | 395 break; |
396 #endif | |
397 #ifdef JM_SUP_PATMODE_2 | |
398 case PATMODE_COMP_HORIZ: | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
399 result = jssGetPatternCompHoriz(inFile, module->patterns[index]); |
0 | 400 break; |
401 #endif | |
402 #ifdef JM_SUP_PATMODE_3 | |
403 case PATMODE_RAW_VERT: | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
404 result = jssGetPatternRawVert(inFile, module->patterns[index]); |
0 | 405 break; |
406 #endif | |
407 #ifdef JM_SUP_PATMODE_4 | |
408 case PATMODE_COMP_VERT: | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
409 result = jssGetPatternCompVert(inFile, module->patterns[index]); |
0 | 410 break; |
411 #endif | |
412 #ifdef JM_SUP_PATMODE_5 | |
413 case PATMODE_RAW_ELEM: | |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
414 result = jssGetPatternRawVertElem(inFile, module->patterns[index]); |
0 | 415 break; |
416 #endif | |
417 default: | |
418 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
1212 | 419 "Unsupported pattern mode %d. Check compilation options.", |
420 jssH.patMode); | |
0 | 421 break; |
422 } | |
423 | |
424 if (result != DMERR_OK) | |
425 { | |
1212 | 426 JSSERROR(result, result, |
427 "Error in unpacking pattern #%d data.\n", | |
428 index); | |
0 | 429 } |
430 } | |
431 | |
432 #ifdef JM_SUP_EXT_INSTR | |
433 // Read extended instruments | |
434 for (index = 0; index < module->nextInstruments; index++) | |
435 { | |
436 JSSMODExtInstrument jssE; | |
437 JSSExtInstrument *einst; | |
438 int i; | |
439 | |
1167 | 440 dmMemset(&jssE, 0, sizeof(jssE)); |
0 | 441 JSGETBUF(&jssE, JSSMODExtInstrument); |
442 | |
443 if ((einst = jssAllocateExtInstrument()) == NULL) | |
444 { | |
445 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1214 | 446 "Could not allocate extended instrument structure #%d\n", index); |
0 | 447 } |
448 | |
449 module->extInstruments[index] = einst; | |
450 | |
451 einst->nsamples = jssE.nsamples; | |
452 einst->vibratoType = jssE.vibratoType; | |
453 einst->vibratoSweep = jssE.vibratoSweep; | |
454 einst->vibratoDepth = jssE.vibratoDepth; | |
455 einst->vibratoRate = jssE.vibratoRate; | |
456 einst->fadeOut = jssE.fadeOut; | |
457 | |
458 for (i = 0; i < jsetNNotes; i++) | |
459 { | |
460 int snum = jssE.sNumForNotes[i]; | |
1118
de25e0f2b856
Fix sNumForNotes conversion/handling in JSSMOD loader and xm2jss.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
461 einst->sNumForNotes[i] = (snum > 0) ? snum - 1 : jsetNotSet; |
0 | 462 } |
463 | |
464 jssCopyEnvelope(&(einst->volumeEnv), &jssE.volumeEnv); | |
465 jssCopyEnvelope(&(einst->panningEnv), &jssE.panningEnv); | |
466 } | |
467 | |
468 #ifdef JM_SUP_INSTR | |
469 // Read sample instrument headers | |
470 for (index = 0; index < module->ninstruments; index++) | |
471 { | |
472 JSSMODInstrument jssI; | |
473 JSSInstrument *inst; | |
474 | |
1167 | 475 dmMemset(&jssI, 0, sizeof(jssI)); |
0 | 476 JSGETBUF(&jssI, JSSMODInstrument); |
477 | |
478 if ((inst = jssAllocateInstrument()) == NULL) | |
479 { | |
480 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1214 | 481 "Could not allocate sample instrument structure #%d\n", index); |
0 | 482 } |
483 | |
484 module->instruments[index] = inst; | |
485 | |
1231 | 486 // Copy data |
0 | 487 inst->size = jssI.size; |
488 inst->loopS = jssI.loopS; | |
489 inst->loopE = jssI.loopE; | |
490 inst->volume = jssI.volume; | |
491 inst->flags = jssI.flags; | |
492 inst->C4BaseSpeed = jssI.C4BaseSpeed; | |
493 inst->ERelNote = jssI.ERelNote; | |
494 inst->EFineTune = jssI.EFineTune; | |
495 inst->EPanning = jssI.EPanning; | |
496 inst->convFlags = jssI.convFlags; | |
497 } | |
498 | |
499 #ifdef JM_SUP_SAMPLES | |
500 // Read sample data | |
501 for (index = 0; index < module->ninstruments; index++) | |
502 { | |
503 JSSInstrument *inst = module->instruments[index]; | |
1236 | 504 if (inst != NULL && inst->convFlags & jsampHasData) |
0 | 505 { |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
506 int ret; |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
507 size_t bsize = (inst->flags & jsf16bit) ? sizeof(Uint16) : sizeof(Uint8); |
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
508 bsize *= inst->size; |
0 | 509 |
510 // Allocate | |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
511 if ((inst->data = dmMalloc(bsize)) == NULL) |
0 | 512 { |
513 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
514 "Could not allocate %d bytes of sample data #%d\n", |
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
515 bsize, index); |
0 | 516 } |
517 | |
518 // Copy data | |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
519 if (!dmf_read_str(inFile, inst->data, bsize)) |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
520 { |
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
521 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
522 "Could not read %d bytes of sample data for #%d\n", |
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
523 bsize, index); |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
524 } |
0 | 525 |
526 // Convert, if needed | |
527 if (inst->flags & jsf16bit) | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
528 ret = jssDecodeSample16(inst->data, inst->size, inst->convFlags); |
0 | 529 else |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
530 ret = jssDecodeSample8(inst->data, inst->size, inst->convFlags); |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
531 |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
532 if (ret != DMERR_OK) |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
533 { |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
534 JSSERROR(ret, ret, |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
535 "Failed to decode sample data for #%d\n", index); |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
536 } |
0 | 537 } |
538 } | |
539 #else | |
282 | 540 # warning Not including JSSMOD sample loading! |
0 | 541 #endif // JM_SUP_SAMPLES |
542 #else | |
282 | 543 # warning Not including JSSMOD instrument loading! |
0 | 544 #endif // JM_SUP_INSTR |
545 #else | |
282 | 546 # warning Not including JSSMOD ext.instrument loading! |
0 | 547 #endif // JM_SUP_EXT_INSTR |
548 | |
549 return DMERR_OK; | |
550 } |