Mercurial > hg > dmlib
annotate minijss/jssmod.c @ 743:b136ddc4070b
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 04 May 2013 02:12:33 +0300 |
parents | c430112449a7 |
children | 698ee83bac98 |
rev | line source |
---|---|
0 | 1 /* |
2 * miniJSS - Module structure and handling routines | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2006-2007 Tecnic Software productions (TNSP) | |
5 */ | |
6 #include "jssmod.h" | |
7 #include <string.h> | |
8 | |
9 | |
10 #ifndef JSS_LIGHT | |
11 /* Take given data until maxlen reached, make a string | |
12 */ | |
13 char *jssASCIItoStr(char * sdata, const char endByte, const size_t maxLen) | |
14 { | |
15 size_t i, k; | |
16 char *res; | |
17 | |
18 for (i = 0; sdata[i] && i < maxLen; i++); | |
19 | |
20 res = (char *) dmMalloc(i + 1); | |
21 if (res == NULL) | |
22 return NULL; | |
23 | |
24 for (k = 0; sdata[k] != endByte && k < i; k++) | |
25 res[k] = sdata[k]; | |
26 | |
27 res[k] = 0; | |
28 | |
29 return res; | |
30 } | |
31 | |
32 | |
33 /* Encodes a given 8-bit sample | |
34 */ | |
43 | 35 BOOL jssEncodeSample8(Uint8 * data, const size_t len, const int ops) |
0 | 36 { |
43 | 37 size_t count = len; |
0 | 38 Sint8 t, value = 0; |
39 | |
43 | 40 while (count--) |
0 | 41 { |
43 | 42 t = *data; |
0 | 43 |
44 if (ops & jsampFlipSign) | |
45 t ^= 0x80; | |
46 | |
47 if (ops & jsampDelta) | |
48 { | |
49 int n = t - value; | |
50 value = t; | |
51 t = n; | |
52 } | |
53 | |
43 | 54 *(data++) = t; |
0 | 55 } |
56 | |
57 return TRUE; | |
58 } | |
59 | |
60 | |
61 /* Decodes a given 16-bit sample | |
62 */ | |
43 | 63 BOOL jssEncodeSample16(Uint16 * data, const size_t len, const int ops) |
0 | 64 { |
65 // "Split" the 16-bit samples into 8-bit halves | |
66 if (ops & jsampSplit) | |
67 { | |
68 // Allocate temporary processing buffer | |
43 | 69 size_t count, bufSize = len * sizeof(Sint16); |
0 | 70 Uint8 *bp1, *bp2; |
43 | 71 Sint16 *sdata, *tmpBuf = dmMalloc(bufSize); |
0 | 72 if (tmpBuf == NULL) return FALSE; |
73 | |
74 sdata = tmpBuf; | |
43 | 75 bp1 = (Uint8 *) data; |
76 bp2 = bp1 + len; | |
77 count = len; | |
0 | 78 |
43 | 79 while (count--) |
0 | 80 { |
43 | 81 Sint16 t = (*sdata++); |
0 | 82 *bp1++ = t >> 8; |
83 *bp2++ = t & 0xff; | |
84 } | |
85 | |
43 | 86 memcpy(data, tmpBuf, bufSize); |
0 | 87 dmFree(tmpBuf); |
88 | |
43 | 89 return jssEncodeSample8((Uint8 *) data, bufSize, ops); |
0 | 90 } |
91 else | |
92 { | |
43 | 93 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
94 size_t count = len; | |
0 | 95 |
43 | 96 while (count--) |
0 | 97 { |
98 if (ops & jsampSwapEndianess) | |
99 { | |
100 p = *sdata; | |
101 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
743 | 102 } |
103 else | |
0 | 104 t = *sdata; |
105 | |
106 if (ops & jsampDelta) | |
107 { | |
108 int n = t - value; | |
109 value = t; | |
110 t = n; | |
111 } | |
112 | |
113 if (ops & jsampFlipSign) | |
114 t ^= 0x8000; | |
115 | |
116 *(sdata++) = t; | |
117 } | |
118 } | |
119 return TRUE; | |
120 } | |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
121 |
0 | 122 #endif |
123 | |
124 | |
125 /* Decodes a given 8-bit sample | |
126 */ | |
43 | 127 BOOL jssDecodeSample8(Uint8 * data, const size_t len, const int ops) |
0 | 128 { |
43 | 129 size_t count = len; |
0 | 130 Sint8 t, value = 0; |
131 | |
43 | 132 while (count--) |
0 | 133 { |
43 | 134 t = *data; |
0 | 135 |
136 if (ops & jsampDelta) | |
137 t = value = t + value; | |
138 | |
139 if (ops & jsampFlipSign) | |
140 t ^= 0x80; | |
141 | |
43 | 142 *(data++) = t; |
0 | 143 } |
144 return TRUE; | |
145 } | |
146 | |
147 | |
148 /* Decodes a given 16-bit sample | |
149 */ | |
43 | 150 BOOL jssDecodeSample16(Uint16 * data, const size_t len, const int ops) |
0 | 151 { |
152 if (ops & jsampSplit) | |
153 { | |
43 | 154 size_t count, bufSize = len * sizeof(Uint16); |
0 | 155 Uint8 *bp1, *bp2; |
156 Sint16 *tmpBuf, *sdata; | |
157 | |
43 | 158 if (!jssDecodeSample8((Uint8 *) data, bufSize, ops)) |
0 | 159 return FALSE; |
160 | |
161 tmpBuf = dmMalloc(bufSize); | |
162 if (tmpBuf == NULL) return FALSE; | |
43 | 163 memcpy(tmpBuf, data, bufSize); |
0 | 164 |
43 | 165 sdata = (Sint16 *) data; |
0 | 166 bp1 = (Uint8 *) tmpBuf; |
43 | 167 bp2 = bp1 + len; |
168 count = len; | |
169 while (count--) | |
0 | 170 { |
171 *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff); | |
172 } | |
173 | |
174 dmFree(tmpBuf); | |
175 } | |
176 else | |
177 { | |
43 | 178 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
179 size_t count = len; | |
180 while (count--) | |
0 | 181 { |
182 if (ops & jsampSwapEndianess) | |
183 { | |
184 p = *sdata; | |
185 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
743 | 186 } |
187 else | |
0 | 188 t = *sdata; |
189 | |
190 if (ops & jsampDelta) | |
191 t = value = t + value; | |
192 | |
193 if (ops & jsampFlipSign) | |
194 t ^= 0x8000; | |
195 | |
196 *(sdata++) = t; | |
197 } | |
198 } | |
199 return TRUE; | |
200 } | |
201 | |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
202 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
203 /* Convert sample data from U8 to S16 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
204 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
205 int jssConvertSampleTo16(void **dst, void * src, const size_t len) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
206 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
207 size_t count = len; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
208 Uint8 *in = (Uint8 *) src; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
209 Sint16 *out; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
210 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
211 *dst = out = dmMalloc(sizeof(Sint16) * len); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
212 if (out == NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
213 return DMERR_MALLOC; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
214 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
215 while (count--) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
216 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
217 *(out++) = (*(in++) * 256) - 32768; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
218 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
219 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
220 return DMERR_OK; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
221 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
222 |
743 | 223 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
224 /* Converts the given module in preparation for playing it. |
56
8725853609db
Remove the floating point mixing .. it wasn't so good idea after all.
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
225 * This involves sample format conversion (8 to 16 bit, etc.) |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
226 * |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
227 * NOTICE! The converted module can only be saved in JSSMOD |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
228 * format, but this is not recommended. |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
229 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
230 int jssConvertModuleForPlaying(JSSModule *module) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
231 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
232 int i; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
233 if (module == NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
234 return DMERR_NULLPTR; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
235 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
236 // Convert instruments |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
237 for (i = 0; i < module->ninstruments; i++) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
238 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
239 JSSInstrument *inst = module->instruments[i]; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
240 if (inst != NULL && inst->data != NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
241 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
242 int res; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
243 void *data = NULL; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
244 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
245 if (inst->flags & jsf16bit) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
246 continue; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
247 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
248 if ((res = jssConvertSampleTo16(&data, inst->data, inst->size)) != DMERR_OK) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
249 return res; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
250 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
251 inst->flags |= jsf16bit; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
252 dmFree(inst->data); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
253 inst->data = data; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
254 } |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
255 } |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
256 |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
257 return DMERR_OK; |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
258 } |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
259 |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
260 |
0 | 261 /* Allocates a new module structure or returns errorvalue if failed. |
262 * Memory is allocated only for the basic structure. Sample- and pattern | |
263 * areas must be allocated separately with appropriate routines. | |
264 */ | |
265 JSSModule *jssAllocateModule(void) | |
266 { | |
267 int i; | |
268 JSSModule *module; | |
269 | |
270 // Allocate module structure | |
271 module = dmMalloc0(sizeof(JSSModule)); | |
272 if (module == NULL) | |
273 return NULL; | |
274 | |
275 // Initialize structure | |
276 for (i = 0; i < jsetNChannels; i++) | |
277 module->defPanning[i] = jchPanMiddle; | |
278 | |
279 for (i = 0; i < jsetMaxOrders; i++) | |
280 module->orderList[i] = jsetOrderEnd; | |
281 | |
282 // Allocate mutex | |
283 #ifdef JSS_SUP_THREADS | |
284 module->mutex = dmCreateMutex(); | |
285 #endif | |
286 | |
287 return module; | |
288 } | |
289 | |
290 | |
291 /* Frees a given module structure, freeing all memory areas | |
292 * that were allocated for it (including patterns, samples, etc.) | |
293 */ | |
294 int jssFreeModule(JSSModule * module) | |
295 { | |
296 int i; | |
297 | |
298 if (module == NULL) | |
299 return DMERR_NULLPTR; | |
300 | |
301 // Free strings | |
302 #ifndef JSS_LIGHT | |
303 dmFree(module->moduleName); | |
304 dmFree(module->trackerName); | |
305 #endif | |
306 | |
307 // Free patterns | |
308 for (i = 0; i < module->npatterns; i++) | |
309 { | |
743 | 310 if (module->patterns[i] != NULL) |
0 | 311 { |
312 JSSPattern *pat = module->patterns[i]; | |
313 dmFree(pat->data); | |
314 dmFree(pat); | |
315 module->patterns[i] = NULL; | |
316 } | |
317 } | |
318 | |
286
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
319 // Free the "empty" pattern |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
320 JSSPattern *pat = module->patterns[jsetMaxPatterns]; |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
321 if (pat != NULL) |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
322 { |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
323 dmFree(pat->data); |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
324 dmFree(pat); |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
325 module->patterns[i] = NULL; |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
326 } |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
327 |
0 | 328 // Free instruments |
329 for (i = 0; i < module->ninstruments; i++) | |
330 { | |
743 | 331 if (module->instruments[i] != NULL) |
0 | 332 { |
333 JSSInstrument *inst = module->instruments[i]; | |
334 #ifndef JSS_LIGHT | |
335 dmFree(inst->desc); | |
336 #endif | |
337 dmFree(inst->data); | |
338 dmFree(inst); | |
339 module->instruments[i] = NULL; | |
340 } | |
341 } | |
342 | |
343 // Free extended instruments | |
344 for (i = 0; i < module->nextInstruments; i++) | |
345 { | |
743 | 346 if (module->extInstruments[i] != NULL) |
0 | 347 { |
348 JSSExtInstrument *ext = module->extInstruments[i]; | |
349 #ifndef JSS_LIGHT | |
350 dmFree(ext->desc); | |
351 #endif | |
352 dmFree(ext); | |
353 module->extInstruments[i] = NULL; | |
354 } | |
355 } | |
356 | |
357 // Free mutex | |
358 #ifdef JSS_SUP_THREADS | |
359 dmDestroyMutex(module->mutex); | |
360 #endif | |
361 | |
362 // Free the module structure | |
363 memset(module, 0, sizeof(JSSModule)); | |
364 dmFree(module); | |
365 | |
366 return DMERR_OK; | |
367 } | |
368 | |
369 | |
370 /* Allocates and initializes a internal pattern structure. | |
371 */ | |
743 | 372 JSSPattern *jssAllocatePattern(const int nrows, const int nchannels) |
0 | 373 { |
743 | 374 int row, chn; |
0 | 375 JSSPattern *res; |
376 JSSNote *pnote; | |
377 | |
378 // Check arguments | |
379 if (nrows <= 0 || nchannels <= 0) | |
380 JSSERROR(DMERR_INVALID_ARGS, NULL, "Invalid nrows=%i or nchannels=%i.\n", nrows, nchannels); | |
381 | |
382 // Allocate a pattern structure | |
383 res = dmMalloc0(sizeof(JSSPattern)); | |
384 if (res == NULL) | |
385 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); | |
386 | |
387 // Allocate notedata | |
388 res->data = dmCalloc(nrows * nchannels, sizeof(JSSNote)); | |
389 if (res->data == NULL) | |
390 { | |
391 dmFree(res); | |
392 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, | |
393 nchannels); | |
394 } | |
395 | |
396 // Initialize | |
397 res->nrows = nrows; | |
398 res->nchannels = nchannels; | |
399 | |
400 pnote = res->data; | |
743 | 401 for (row = 0; row < nrows; row++) |
0 | 402 { |
743 | 403 for (chn = 0; chn < nchannels; chn++) |
0 | 404 { |
405 pnote->note = pnote->instrument = pnote->volume = | |
406 pnote->effect = pnote->param = jsetNotSet; | |
407 | |
408 pnote++; | |
409 } | |
410 } | |
411 | |
412 // OK, return pointer to struct | |
413 return res; | |
414 } | |
415 | |
416 | |
417 /* Allocates and initializes internal "normal" instrument structure. | |
418 */ | |
419 JSSInstrument *jssAllocateInstrument(void) | |
420 { | |
421 JSSInstrument *res; | |
422 | |
423 // Allocate a instrument structure | |
424 res = dmMalloc0(sizeof(JSSInstrument)); | |
425 if (res == NULL) | |
426 return NULL; | |
427 | |
428 return res; | |
429 } | |
430 | |
431 | |
432 /* Allocates and initializes "extended" instrument structure. | |
433 */ | |
434 JSSExtInstrument *jssAllocateExtInstrument(void) | |
435 { | |
436 int i; | |
437 JSSExtInstrument *res; | |
438 | |
439 // Allocate a instrument structure | |
440 res = dmMalloc0(sizeof(JSSExtInstrument)); | |
441 if (res == NULL) | |
442 return NULL; | |
443 | |
444 for (i = 0; i < jsetNNotes; i++) | |
445 { | |
446 res->sNumForNotes[i] = jsetNotSet; | |
447 } | |
448 | |
449 return res; | |
450 } |