Mercurial > hg > dmlib
annotate minijss/jloadjss.c @ 1331:c0479e984242
Fix a envelope "sanity check" in jmod loader.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 22 Aug 2017 13:52:39 +0300 |
parents | 817a4d0c70b5 |
children | 5adf67d1dea4 |
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 JSGETBYTE(XV) \ | |
21 if (!dmf_read_byte(inFile, XV)) \ | |
22 return DMERR_OUT_OF_DATA | |
0 | 23 |
24 | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
25 static int jssDoGetConvertedNote(DMResource *inFile, JSSNote *pnote, Uint8 note) |
0 | 26 { |
27 Uint8 tmp; | |
28 | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
29 if (note == 127) |
1197 | 30 pnote->note = jsetNoteOff; |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
31 else |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
32 if (note == 0) |
1197 | 33 pnote->note = jsetNotSet; |
0 | 34 else |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
35 pnote->note = note - 1; |
0 | 36 |
37 JSGETBYTE(&tmp); | |
1197 | 38 pnote->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 39 |
40 JSGETBYTE(&tmp); | |
1197 | 41 pnote->volume = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 42 |
43 JSGETBYTE(&tmp); | |
1197 | 44 pnote->effect = (tmp > 0) ? tmp - 1 : jsetNotSet; |
0 | 45 |
46 JSGETBYTE(&tmp); | |
1197 | 47 pnote->param = (tmp == 0 && pnote->effect == jsetNotSet) ? jsetNotSet : tmp; |
0 | 48 |
49 return DMERR_OK; | |
50 } | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
51 |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
52 |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
53 static inline int jssGetConvertedNote(DMResource *inFile, JSSNote *pnote) |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
54 { |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
55 Uint8 tmp; |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
56 JSGETBYTE(&tmp); |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
57 return jssDoGetConvertedNote(inFile, pnote, tmp); |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
58 } |
0 | 59 |
60 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
61 #if defined(JM_SUP_PATMODE_2) || defined(JM_SUP_PATMODE_4) |
1197 | 62 static int jssGetCompressedNote(DMResource *inFile, JSSNote *pnote) |
0 | 63 { |
64 Uint8 packb, tmp; | |
65 | |
66 JSGETBYTE(&packb); | |
67 if (packb & 0x80) | |
68 { | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
69 if (packb & JM_COMP_NOTE) |
0 | 70 { |
71 JSGETBYTE(&tmp); | |
72 if (tmp == 127) | |
1197 | 73 pnote->note = jsetNoteOff; |
0 | 74 else |
1197 | 75 pnote->note = tmp; |
0 | 76 } |
77 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
78 if (packb & JM_COMP_INSTRUMENT) |
0 | 79 { |
80 JSGETBYTE(&tmp); | |
1197 | 81 pnote->instrument = tmp; |
0 | 82 } |
83 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
84 if (packb & JM_COMP_VOLUME) |
0 | 85 { |
86 JSGETBYTE(&tmp); | |
1197 | 87 pnote->volume = tmp; |
0 | 88 } |
89 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
90 if (packb & JM_COMP_EFFECT) |
0 | 91 { |
92 JSGETBYTE(&tmp); | |
1197 | 93 pnote->effect = tmp; |
94 pnote->param = 0; | |
0 | 95 } |
96 | |
1210
6b48bad13399
Rename pattern note packing flag COMP_* constants to JM_COMP_*.
Matti Hamalainen <ccr@tnsp.org>
parents:
1208
diff
changeset
|
97 if (packb & JM_COMP_PARAM) |
0 | 98 { |
99 JSGETBYTE(&tmp); | |
1197 | 100 pnote->param = tmp; |
0 | 101 } |
102 } | |
103 else | |
104 { | |
1208
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
105 int ret; |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
106 if ((ret = jssDoGetConvertedNote(inFile, pnote, packb)) != DMERR_OK) |
fa758951720f
Remove some code duplication.
Matti Hamalainen <ccr@tnsp.org>
parents:
1198
diff
changeset
|
107 return ret; |
0 | 108 } |
109 | |
110 return DMERR_OK; | |
111 } | |
112 #endif | |
113 | |
114 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
115 #ifdef JM_SUP_PATMODE_2 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
116 static int jssGetPatternCompHoriz(DMResource *inFile, JSSPattern *pattern) |
0 | 117 { |
118 int row, channel; | |
119 | |
120 assert(buf != NULL); | |
121 assert(pattern != NULL); | |
122 | |
123 for (row = 0; row < pattern->nrows; row++) | |
124 for (channel = 0; channel < pattern->nchannels; channel++) | |
125 { | |
1197 | 126 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
127 int res = jssGetCompressedNote(inFile, pnote); | |
0 | 128 if (res != DMERR_OK) |
1197 | 129 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", |
130 row, channel); | |
0 | 131 } |
132 | |
133 return DMERR_OK; | |
134 } | |
135 #endif | |
136 | |
137 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
138 #ifdef JM_SUP_PATMODE_4 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
139 static int jssGetPatternCompVert(DMResource *inFile, JSSPattern *pattern) |
0 | 140 { |
141 int row, channel; | |
142 | |
143 assert(buf != NULL); | |
144 assert(pattern != NULL); | |
145 | |
146 for (channel = 0; channel < pattern->nchannels; channel++) | |
147 for (row = 0; row < pattern->nrows; row++) | |
148 { | |
1197 | 149 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
150 int res = jssGetCompressedNote(inFile, pnote); | |
0 | 151 if (res != DMERR_OK) |
1197 | 152 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", |
153 row, channel); | |
0 | 154 } |
155 | |
156 return DMERR_OK; | |
157 } | |
158 #endif | |
159 | |
160 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
161 #ifdef JM_SUP_PATMODE_1 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
162 static int jssGetPatternRawHoriz(DMResource *inFile, JSSPattern *pattern) |
0 | 163 { |
164 int row, channel; | |
165 | |
166 assert(buf != NULL); | |
167 assert(pattern != NULL); | |
168 | |
169 for (row = 0; row < pattern->nrows; row++) | |
170 for (channel = 0; channel < pattern->nchannels; channel++) | |
171 { | |
1197 | 172 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
173 int res = jssGetConvertedNote(inFile, pnote); | |
0 | 174 if (res != DMERR_OK) |
1197 | 175 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", |
176 row, channel); | |
0 | 177 } |
178 | |
179 return DMERR_OK; | |
180 } | |
181 #endif | |
182 | |
183 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
184 #ifdef JM_SUP_PATMODE_3 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
185 static int jssGetPatternRawVert(DMResource *inFile, JSSPattern *pattern) |
0 | 186 { |
187 int row, channel; | |
188 | |
189 assert(buf != NULL); | |
190 assert(pattern != NULL); | |
191 | |
192 for (channel = 0; channel < pattern->nchannels; channel++) | |
193 for (row = 0; row < pattern->nrows; row++) | |
194 { | |
1197 | 195 JSSNote *pnote = &pattern->data[(pattern->nchannels * row) + channel]; |
196 int res = jssGetConvertedNote(inFile, pnote); | |
0 | 197 if (res != DMERR_OK) |
1197 | 198 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", |
199 row, channel); | |
0 | 200 } |
201 | |
202 return DMERR_OK; | |
203 } | |
204 #endif | |
205 | |
206 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
207 #ifdef JM_SUP_PATMODE_5 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
208 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
209 #undef JSGETBYTE |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
210 #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
|
211 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
212 #define JSFOREACHNOTE1 \ |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
213 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
|
214 for (row = 0; row < pattern->nrows; row++) { \ |
1197 | 215 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
|
216 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
217 #define JSFOREACHNOTE2 } |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
218 |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
219 static int jssGetPatternRawVertElem(DMResource *inFile, JSSPattern *pattern) |
0 | 220 { |
221 int row, channel; | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
222 Uint8 tmp; |
0 | 223 |
224 assert(buf != NULL); | |
225 assert(pattern != NULL); | |
226 | |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
227 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
228 JSGETBYTE(&tmp); |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
229 if (tmp == 0) |
1197 | 230 pnote->note = jsetNotSet; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
231 else if (tmp == 127) |
1197 | 232 pnote->note = jsetNoteOff; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
233 else |
1197 | 234 pnote->note = tmp - 1; |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
235 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
236 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
237 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
238 JSGETBYTE(&tmp); |
1197 | 239 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
|
240 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
241 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
242 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
243 JSGETBYTE(&tmp); |
1197 | 244 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
|
245 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
246 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
247 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
248 JSGETBYTE(&tmp); |
1197 | 249 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
|
250 JSFOREACHNOTE2 |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1118
diff
changeset
|
251 |
204
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
252 JSFOREACHNOTE1 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
253 JSGETBYTE(&tmp); |
1197 | 254 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
|
255 JSFOREACHNOTE2 |
d3a9a3804079
Implement pattern mode 5 in the JMOD jloader.
Matti Hamalainen <ccr@tnsp.org>
parents:
96
diff
changeset
|
256 |
0 | 257 return DMERR_OK; |
258 } | |
259 #endif | |
260 | |
261 | |
262 #ifdef JM_SUP_EXT_INSTR | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
263 static int jssMODLoadEnvelope(DMResource *inFile, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
264 JSSEnvelope *env, const char *name, const int ninst) |
0 | 265 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
266 JSSMODEnvelope jssEnv; |
0 | 267 |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
268 // Read envelope data |
1250
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
269 if (!dmf_read_byte(inFile, &jssEnv.flags) || |
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
270 !dmf_read_byte(inFile, &jssEnv.npoints) || |
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
271 !dmf_read_byte(inFile, &jssEnv.sustain) || |
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
272 !dmf_read_byte(inFile, &jssEnv.loopS) || |
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
273 !dmf_read_byte(inFile, &jssEnv.loopE)) |
1314 | 274 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
275 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
276 "Failed to read %s-envelope data for instrument #%d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
277 name, ninst); |
1314 | 278 } |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
279 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
280 // Do some sanity checking |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
281 if (jssEnv.npoints > jsetMaxEnvPoints || |
1331
c0479e984242
Fix a envelope "sanity check" in jmod loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1316
diff
changeset
|
282 jssEnv.loopS > jssEnv.loopE || |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
283 jssEnv.loopS > jssEnv.npoints || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
284 jssEnv.loopE > jssEnv.npoints) |
1314 | 285 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
286 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
287 "Invalid values in %s-envelope for instrument #%d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
288 name, ninst); |
1314 | 289 } |
0 | 290 |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
291 // Copy data |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
292 env->flags = jssEnv.flags; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
293 env->npoints = jssEnv.npoints; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
294 env->sustain = jssEnv.sustain; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
295 env->loopS = jssEnv.loopS; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
296 env->loopE = jssEnv.loopE; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
297 |
1314 | 298 for (int i = 0; i < jssEnv.npoints; i++) |
0 | 299 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
300 Uint16 frame, value; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
301 if (!dmf_read_le16(inFile, &frame) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
302 !dmf_read_le16(inFile, &value)) |
1314 | 303 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
304 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
305 "Failed to read %s-envelope values (%d) for instrument #%d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
306 name, i, ninst); |
1314 | 307 } |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
308 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
309 env->points[i].frame = frame; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
310 env->points[i].value = value; |
0 | 311 } |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
312 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
313 return DMERR_OK; |
0 | 314 } |
315 #endif | |
316 | |
317 | |
1256 | 318 int jssLoadJSSMOD(DMResource *inFile, JSSModule **pmodule, BOOL probe) |
0 | 319 { |
320 JSSModule *module; | |
321 JSSMODHeader jssH; | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
322 int index, ret = DMERR_OK; |
0 | 323 |
1256 | 324 *pmodule = NULL; |
0 | 325 |
326 // Check the JSSMOD header | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
327 if (!dmf_read_str(inFile, &jssH.idMagic, sizeof(jssH.idMagic)) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
328 !dmf_read_byte(inFile, &jssH.idVersion)) |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
329 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
330 "Failed to read JSSMOD header #1.\n"); |
0 | 331 |
332 if (memcmp(jssH.idMagic, "JM", 2) != 0) | |
333 { | |
796 | 334 if (probe) |
335 return DMERR_NOT_SUPPORTED; | |
336 else | |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
337 { |
796 | 338 JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, |
339 "Not a JSSMOD file, header signature mismatch!\n"); | |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
340 } |
0 | 341 } |
342 | |
343 if (jssH.idVersion != JSSMOD_VERSION) | |
344 { | |
1198 | 345 if (probe) |
346 return DMERR_VERSION; | |
347 else | |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
348 { |
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
349 JSSERROR(DMERR_VERSION, DMERR_VERSION, |
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
350 "Unsupported version of JSSMOD 0x%2x, this version only supports 0x%2x!\n", |
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
351 jssH.idVersion, JSSMOD_VERSION); |
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
352 } |
0 | 353 } |
354 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
355 // Read rest of the header |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
356 if (!dmf_read_le16(inFile, &jssH.defFlags) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
357 !dmf_read_le16(inFile, &jssH.intVersion) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
358 !dmf_read_le16(inFile, &jssH.norders) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
359 !dmf_read_le16(inFile, &jssH.npatterns) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
360 !dmf_read_le16(inFile, &jssH.nextInstruments) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
361 !dmf_read_le16(inFile, &jssH.ninstruments) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
362 !dmf_read_le16(inFile, &jssH.defRestartPos) || |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
363 |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
364 !dmf_read_byte(inFile, &jssH.nchannels) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
365 !dmf_read_byte(inFile, &jssH.defSpeed) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
366 !dmf_read_byte(inFile, &jssH.defTempo) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
367 !dmf_read_byte(inFile, &jssH.patMode)) |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
368 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
369 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
370 "Failed to read JSSMOD header #2.\n"); |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
371 } |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
372 |
1198 | 373 if (probe) |
374 return DMERR_OK; | |
375 | |
1218
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
376 // 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
|
377 if (jssH.nchannels > jsetMaxChannels || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
378 jssH.nchannels == 0 || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
379 jssH.norders > jsetMaxOrders || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
380 jssH.npatterns > jsetMaxPatterns || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
381 jssH.nextInstruments > jsetMaxInstruments || |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
382 jssH.ninstruments > jsetMaxInstruments) |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
383 { |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
384 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
|
385 "Invalid values in JSSMOD header.\n"); |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
386 } |
30c2dbf05841
Add a sanity check for JSSMOD header values.
Matti Hamalainen <ccr@tnsp.org>
parents:
1214
diff
changeset
|
387 |
0 | 388 // Allocate the module |
1256 | 389 if ((*pmodule = module = jssAllocateModule()) == NULL) |
0 | 390 { |
391 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
392 "Could not allocate memory for module structure.\n"); | |
393 } | |
394 | |
395 // Copy header information | |
1216 | 396 module->defFlags = jssH.defFlags; |
397 module->intVersion = jssH.intVersion; | |
398 | |
0 | 399 module->norders = jssH.norders; |
400 module->npatterns = jssH.npatterns; | |
401 module->nchannels = jssH.nchannels; | |
402 module->nextInstruments = jssH.nextInstruments; | |
403 module->ninstruments = jssH.ninstruments; | |
404 module->defRestartPos = jssH.defRestartPos; | |
405 module->defSpeed = jssH.defSpeed; | |
406 module->defTempo = jssH.defTempo; | |
407 | |
408 // Get the orders list | |
409 for (index = 0; index < module->norders; index++) | |
410 { | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
411 Uint16 tmp; |
1250
99852310dc02
Some more work on the loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1247
diff
changeset
|
412 if (!dmf_read_le16(inFile, &tmp)) |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
413 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
414 "Failed to read orders list entry #%d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
415 index); |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
416 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
417 if (tmp != 0xffff && tmp > jssH.npatterns) |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
418 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
419 "Invalid orders list entry #%d value %d > %d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
420 index, tmp, jssH.npatterns); |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
421 |
1311 | 422 module->orderList[index] = (tmp == 0xffff) ? jsetNotSet : tmp; |
0 | 423 } |
424 | |
425 // Parse the patterns | |
426 for (index = 0; index < module->npatterns; index++) | |
427 { | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
428 JSSPattern *pattern; |
0 | 429 JSSMODPattern jssP; |
430 | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
431 // Read pattern header |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
432 if (!dmf_read_le32(inFile, &jssP.size) || |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
433 !dmf_read_le16(inFile, &jssP.nrows)) |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
434 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
435 "Failed to read JSSMOD pattern header #%d.\n", |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
436 index); |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
437 |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
438 // Validate |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
439 if (jssP.nrows == 0 || jssP.nrows > jsetMaxRows) |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
440 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
441 "Invalid number of rows in pattern #%d: %d.\n", |
1257 | 442 index, jssP.nrows); |
0 | 443 |
444 // Allocate pattern | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
445 pattern = module->patterns[index] = jssAllocatePattern(jssP.nrows, module->nchannels); |
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
446 if (pattern == NULL) |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
447 { |
0 | 448 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, |
1212 | 449 "Could not allocate memory for pattern #%d.\n", |
450 index); | |
1310
dce4730372c7
Add some blocks, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
1271
diff
changeset
|
451 } |
0 | 452 |
453 // Get pattern data | |
454 switch (jssH.patMode) | |
455 { | |
456 #ifdef JM_SUP_PATMODE_1 | |
457 case PATMODE_RAW_HORIZ: | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
458 ret = jssGetPatternRawHoriz(inFile, pattern); |
0 | 459 break; |
460 #endif | |
461 #ifdef JM_SUP_PATMODE_2 | |
462 case PATMODE_COMP_HORIZ: | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
463 ret = jssGetPatternCompHoriz(inFile, pattern); |
0 | 464 break; |
465 #endif | |
466 #ifdef JM_SUP_PATMODE_3 | |
467 case PATMODE_RAW_VERT: | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
468 ret = jssGetPatternRawVert(inFile, pattern); |
0 | 469 break; |
470 #endif | |
471 #ifdef JM_SUP_PATMODE_4 | |
472 case PATMODE_COMP_VERT: | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
473 ret = jssGetPatternCompVert(inFile, pattern); |
0 | 474 break; |
475 #endif | |
476 #ifdef JM_SUP_PATMODE_5 | |
477 case PATMODE_RAW_ELEM: | |
1255
f9bf71071f0b
Some work on JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1250
diff
changeset
|
478 ret = jssGetPatternRawVertElem(inFile, pattern); |
0 | 479 break; |
480 #endif | |
481 default: | |
482 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
1212 | 483 "Unsupported pattern mode %d. Check compilation options.", |
484 jssH.patMode); | |
0 | 485 break; |
486 } | |
487 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
488 if (ret != DMERR_OK) |
0 | 489 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
490 JSSERROR(ret, ret, |
1212 | 491 "Error in unpacking pattern #%d data.\n", |
492 index); | |
0 | 493 } |
494 } | |
495 | |
496 #ifdef JM_SUP_EXT_INSTR | |
497 // Read extended instruments | |
498 for (index = 0; index < module->nextInstruments; index++) | |
499 { | |
500 JSSMODExtInstrument jssE; | |
501 JSSExtInstrument *einst; | |
502 int i; | |
503 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
504 // Read header data |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
505 if (!dmf_read_byte(inFile, &jssE.nsamples) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
506 !dmf_read_byte(inFile, &jssE.vibratoType) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
507 !dmf_read_le16(inFile, &jssE.vibratoSweep) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
508 !dmf_read_le16(inFile, &jssE.vibratoDepth) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
509 !dmf_read_le16(inFile, &jssE.vibratoRate) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
510 !dmf_read_le16(inFile, &jssE.fadeOut)) |
1271 | 511 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
512 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
513 "Failed to read ext.instrument #%d header.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
514 index); |
1271 | 515 } |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
516 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
517 // Allocate instrument |
1311 | 518 einst = module->extInstruments[index] = jssAllocateExtInstrument(); |
519 if (einst == NULL) | |
0 | 520 { |
521 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1214 | 522 "Could not allocate extended instrument structure #%d\n", index); |
0 | 523 } |
524 | |
525 einst->nsamples = jssE.nsamples; | |
526 einst->vibratoType = jssE.vibratoType; | |
527 einst->vibratoSweep = jssE.vibratoSweep; | |
528 einst->vibratoDepth = jssE.vibratoDepth; | |
529 einst->vibratoRate = jssE.vibratoRate; | |
530 einst->fadeOut = jssE.fadeOut; | |
531 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
532 // Read and somewhat validate sNumForNotes |
0 | 533 for (i = 0; i < jsetNNotes; i++) |
534 { | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
535 int snum; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
536 Uint32 tmp; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
537 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
538 if (!dmf_read_le32(inFile, &tmp)) |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
539 { |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
540 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
541 "Failed to read ext.instrument #%d sNumForNotes[%d].\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
542 index, i); |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
543 } |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
544 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
545 einst->sNumForNotes[i] = snum = (tmp > 0) ? ((int) tmp - 1) : jsetNotSet; |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
546 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
547 if (snum != jsetNotSet && snum > module->ninstruments) |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
548 { |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
549 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
550 "Ext.instrument #%d has invalid sNumForNotes[%d] = %d > %d max.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
551 index, i, snum, module->ninstruments); |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
552 } |
0 | 553 } |
554 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
555 // Read and validate envelopes |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
556 if ((ret = jssMODLoadEnvelope(inFile, &einst->volumeEnv, "volume", index)) != DMERR_OK || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
557 (ret = jssMODLoadEnvelope(inFile, &einst->panningEnv, "panning", index)) != DMERR_OK) |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
558 return ret; |
0 | 559 } |
560 | |
561 #ifdef JM_SUP_INSTR | |
562 // Read sample instrument headers | |
563 for (index = 0; index < module->ninstruments; index++) | |
564 { | |
565 JSSMODInstrument jssI; | |
566 JSSInstrument *inst; | |
567 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
568 // Read header data |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
569 if (!dmf_read_le32(inFile, &jssI.size) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
570 !dmf_read_le32(inFile, &jssI.loopS) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
571 !dmf_read_le32(inFile, &jssI.loopE) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
572 !dmf_read_le16(inFile, &jssI.flags) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
573 !dmf_read_le16(inFile, &jssI.C4BaseSpeed) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
574 !dmf_read_le16(inFile, &jssI.ERelNote) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
575 !dmf_read_le16(inFile, &jssI.EFineTune) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
576 !dmf_read_le16(inFile, &jssI.EPanning) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
577 !dmf_read_byte(inFile, &jssI.volume) || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
578 !dmf_read_byte(inFile, &jssI.convFlags)) |
0 | 579 { |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
580 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
581 "Failed to read sample instrument #%d header.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
582 index); |
0 | 583 } |
584 | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
585 // Validate what we can |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
586 if (jssI.loopS > jssI.size || |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
587 jssI.loopE > jssI.size || |
1311 | 588 jssI.loopS > jssI.loopE) |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
589 { |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
590 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
591 "Invalid or corrupted sample instrument #%d.\n", |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
592 index); |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
593 } |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
594 |
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
595 // Allocate instrument |
1311 | 596 inst = module->instruments[index] = jssAllocateInstrument(); |
597 if (inst == NULL) | |
598 { | |
599 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
600 "Could not allocate sample instrument structure #%d\n", | |
601 index); | |
602 } | |
0 | 603 |
1231 | 604 // Copy data |
0 | 605 inst->size = jssI.size; |
606 inst->loopS = jssI.loopS; | |
607 inst->loopE = jssI.loopE; | |
608 inst->flags = jssI.flags; | |
609 inst->C4BaseSpeed = jssI.C4BaseSpeed; | |
610 inst->ERelNote = jssI.ERelNote; | |
611 inst->EFineTune = jssI.EFineTune; | |
612 inst->EPanning = jssI.EPanning; | |
1247
5ae2ce21c7ae
Some work on refactoring the JSSMOD loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
1239
diff
changeset
|
613 inst->volume = jssI.volume; |
0 | 614 inst->convFlags = jssI.convFlags; |
615 } | |
616 | |
617 #ifdef JM_SUP_SAMPLES | |
618 // Read sample data | |
619 for (index = 0; index < module->ninstruments; index++) | |
620 { | |
621 JSSInstrument *inst = module->instruments[index]; | |
1312 | 622 if (inst != NULL && (inst->convFlags & jsampHasData) != 0) |
0 | 623 { |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
624 int ret; |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
625 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
|
626 bsize *= inst->size; |
0 | 627 |
1316 | 628 // Allocate sample data memory |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
629 if ((inst->data = dmMalloc(bsize)) == NULL) |
0 | 630 { |
631 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
632 "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
|
633 bsize, index); |
0 | 634 } |
635 | |
1316 | 636 // Read data |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
637 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
|
638 { |
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
639 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
1213
505317227ab4
Rename variable, improve two error messages.
Matti Hamalainen <ccr@tnsp.org>
parents:
1212
diff
changeset
|
640 "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
|
641 bsize, index); |
777
ed60a7ee3ebb
Change JSSMOD loader to use DMResources.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
642 } |
0 | 643 |
644 // Convert, if needed | |
645 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
|
646 ret = jssDecodeSample16(inst->data, inst->size, inst->convFlags); |
0 | 647 else |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
648 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
|
649 |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
650 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
|
651 { |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
652 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
|
653 "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
|
654 } |
0 | 655 } |
656 } | |
657 #else | |
282 | 658 # warning Not including JSSMOD sample loading! |
0 | 659 #endif // JM_SUP_SAMPLES |
660 #else | |
282 | 661 # warning Not including JSSMOD instrument loading! |
0 | 662 #endif // JM_SUP_INSTR |
663 #else | |
282 | 664 # warning Not including JSSMOD ext.instrument loading! |
0 | 665 #endif // JM_SUP_EXT_INSTR |
666 | |
667 return DMERR_OK; | |
668 } |