Mercurial > hg > dmlib
annotate jssmod.c @ 286:a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 11 Oct 2012 10:24:00 +0300 |
parents | 8725853609db |
children |
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); | |
102 } else | |
103 t = *sdata; | |
104 | |
105 if (ops & jsampDelta) | |
106 { | |
107 int n = t - value; | |
108 value = t; | |
109 t = n; | |
110 } | |
111 | |
112 if (ops & jsampFlipSign) | |
113 t ^= 0x8000; | |
114 | |
115 *(sdata++) = t; | |
116 } | |
117 } | |
118 return TRUE; | |
119 } | |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
120 |
0 | 121 #endif |
122 | |
123 | |
124 /* Decodes a given 8-bit sample | |
125 */ | |
43 | 126 BOOL jssDecodeSample8(Uint8 * data, const size_t len, const int ops) |
0 | 127 { |
43 | 128 size_t count = len; |
0 | 129 Sint8 t, value = 0; |
130 | |
43 | 131 while (count--) |
0 | 132 { |
43 | 133 t = *data; |
0 | 134 |
135 if (ops & jsampDelta) | |
136 t = value = t + value; | |
137 | |
138 if (ops & jsampFlipSign) | |
139 t ^= 0x80; | |
140 | |
43 | 141 *(data++) = t; |
0 | 142 } |
143 return TRUE; | |
144 } | |
145 | |
146 | |
147 /* Decodes a given 16-bit sample | |
148 */ | |
43 | 149 BOOL jssDecodeSample16(Uint16 * data, const size_t len, const int ops) |
0 | 150 { |
151 if (ops & jsampSplit) | |
152 { | |
43 | 153 size_t count, bufSize = len * sizeof(Uint16); |
0 | 154 Uint8 *bp1, *bp2; |
155 Sint16 *tmpBuf, *sdata; | |
156 | |
43 | 157 if (!jssDecodeSample8((Uint8 *) data, bufSize, ops)) |
0 | 158 return FALSE; |
159 | |
160 tmpBuf = dmMalloc(bufSize); | |
161 if (tmpBuf == NULL) return FALSE; | |
43 | 162 memcpy(tmpBuf, data, bufSize); |
0 | 163 |
43 | 164 sdata = (Sint16 *) data; |
0 | 165 bp1 = (Uint8 *) tmpBuf; |
43 | 166 bp2 = bp1 + len; |
167 count = len; | |
168 while (count--) | |
0 | 169 { |
170 *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff); | |
171 } | |
172 | |
173 dmFree(tmpBuf); | |
174 } | |
175 else | |
176 { | |
43 | 177 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
178 size_t count = len; | |
179 while (count--) | |
0 | 180 { |
181 if (ops & jsampSwapEndianess) | |
182 { | |
183 p = *sdata; | |
184 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
185 } else | |
186 t = *sdata; | |
187 | |
188 if (ops & jsampDelta) | |
189 t = value = t + value; | |
190 | |
191 if (ops & jsampFlipSign) | |
192 t ^= 0x8000; | |
193 | |
194 *(sdata++) = t; | |
195 } | |
196 } | |
197 return TRUE; | |
198 } | |
199 | |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
200 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
201 /* 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
|
202 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
203 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
|
204 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
205 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
|
206 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
|
207 Sint16 *out; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
208 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
209 *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
|
210 if (out == NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
211 return DMERR_MALLOC; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
212 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
213 while (count--) |
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 *(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
|
216 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
217 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
218 return DMERR_OK; |
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 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
221 /* 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
|
222 * 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
|
223 * |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
224 * 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
|
225 * 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
|
226 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
227 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
|
228 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
229 int i; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
230 if (module == NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
231 return DMERR_NULLPTR; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
232 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
233 // Convert instruments |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
234 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
|
235 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
236 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
|
237 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
|
238 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
239 int res; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
240 void *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 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
|
243 continue; |
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 ((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
|
246 return res; |
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 inst->flags |= jsf16bit; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
249 dmFree(inst->data); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
250 inst->data = data; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
251 } |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
252 } |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
253 |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
254 return DMERR_OK; |
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 |
0 | 258 /* Allocates a new module structure or returns errorvalue if failed. |
259 * Memory is allocated only for the basic structure. Sample- and pattern | |
260 * areas must be allocated separately with appropriate routines. | |
261 */ | |
262 JSSModule *jssAllocateModule(void) | |
263 { | |
264 int i; | |
265 JSSModule *module; | |
266 | |
267 // Allocate module structure | |
268 module = dmMalloc0(sizeof(JSSModule)); | |
269 if (module == NULL) | |
270 return NULL; | |
271 | |
272 // Initialize structure | |
273 for (i = 0; i < jsetNChannels; i++) | |
274 module->defPanning[i] = jchPanMiddle; | |
275 | |
276 for (i = 0; i < jsetMaxOrders; i++) | |
277 module->orderList[i] = jsetOrderEnd; | |
278 | |
279 // Allocate mutex | |
280 #ifdef JSS_SUP_THREADS | |
281 module->mutex = dmCreateMutex(); | |
282 #endif | |
283 | |
284 return module; | |
285 } | |
286 | |
287 | |
288 /* Frees a given module structure, freeing all memory areas | |
289 * that were allocated for it (including patterns, samples, etc.) | |
290 */ | |
291 int jssFreeModule(JSSModule * module) | |
292 { | |
293 int i; | |
294 | |
295 if (module == NULL) | |
296 return DMERR_NULLPTR; | |
297 | |
298 // Free strings | |
299 #ifndef JSS_LIGHT | |
300 dmFree(module->moduleName); | |
301 dmFree(module->trackerName); | |
302 #endif | |
303 | |
304 // Free patterns | |
305 for (i = 0; i < module->npatterns; i++) | |
306 { | |
307 if (module->patterns[i]) | |
308 { | |
309 JSSPattern *pat = module->patterns[i]; | |
310 dmFree(pat->data); | |
311 dmFree(pat); | |
312 module->patterns[i] = NULL; | |
313 } | |
314 } | |
315 | |
286
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
316 // 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
|
317 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
|
318 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
|
319 { |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
320 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
|
321 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
|
322 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
|
323 } |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
324 |
0 | 325 // Free instruments |
326 for (i = 0; i < module->ninstruments; i++) | |
327 { | |
328 if (module->instruments[i]) | |
329 { | |
330 JSSInstrument *inst = module->instruments[i]; | |
331 #ifndef JSS_LIGHT | |
332 dmFree(inst->desc); | |
333 #endif | |
334 dmFree(inst->data); | |
335 dmFree(inst); | |
336 module->instruments[i] = NULL; | |
337 } | |
338 } | |
339 | |
340 // Free extended instruments | |
341 for (i = 0; i < module->nextInstruments; i++) | |
342 { | |
343 if (module->extInstruments[i]) | |
344 { | |
345 JSSExtInstrument *ext = module->extInstruments[i]; | |
346 #ifndef JSS_LIGHT | |
347 dmFree(ext->desc); | |
348 #endif | |
349 dmFree(ext); | |
350 module->extInstruments[i] = NULL; | |
351 } | |
352 } | |
353 | |
354 // Free mutex | |
355 #ifdef JSS_SUP_THREADS | |
356 dmDestroyMutex(module->mutex); | |
357 #endif | |
358 | |
359 // Free the module structure | |
360 memset(module, 0, sizeof(JSSModule)); | |
361 dmFree(module); | |
362 | |
363 return DMERR_OK; | |
364 } | |
365 | |
366 | |
367 /* Allocates and initializes a internal pattern structure. | |
368 */ | |
369 JSSPattern *jssAllocatePattern(int nrows, int nchannels) | |
370 { | |
371 int i, j; | |
372 JSSPattern *res; | |
373 JSSNote *pnote; | |
374 | |
375 // Check arguments | |
376 if (nrows <= 0 || nchannels <= 0) | |
377 JSSERROR(DMERR_INVALID_ARGS, NULL, "Invalid nrows=%i or nchannels=%i.\n", nrows, nchannels); | |
378 | |
379 // Allocate a pattern structure | |
380 res = dmMalloc0(sizeof(JSSPattern)); | |
381 if (res == NULL) | |
382 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); | |
383 | |
384 // Allocate notedata | |
385 res->data = dmCalloc(nrows * nchannels, sizeof(JSSNote)); | |
386 if (res->data == NULL) | |
387 { | |
388 dmFree(res); | |
389 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, | |
390 nchannels); | |
391 } | |
392 | |
393 // Initialize | |
394 res->nrows = nrows; | |
395 res->nchannels = nchannels; | |
396 | |
397 pnote = res->data; | |
398 for (j = 0; j < nrows; j++) | |
399 { | |
400 for (i = 0; i < nchannels; i++) | |
401 { | |
402 pnote->note = pnote->instrument = pnote->volume = | |
403 pnote->effect = pnote->param = jsetNotSet; | |
404 | |
405 pnote++; | |
406 } | |
407 } | |
408 | |
409 // OK, return pointer to struct | |
410 return res; | |
411 } | |
412 | |
413 | |
414 /* Allocates and initializes internal "normal" instrument structure. | |
415 */ | |
416 JSSInstrument *jssAllocateInstrument(void) | |
417 { | |
418 JSSInstrument *res; | |
419 | |
420 // Allocate a instrument structure | |
421 res = dmMalloc0(sizeof(JSSInstrument)); | |
422 if (res == NULL) | |
423 return NULL; | |
424 | |
425 return res; | |
426 } | |
427 | |
428 | |
429 /* Allocates and initializes "extended" instrument structure. | |
430 */ | |
431 JSSExtInstrument *jssAllocateExtInstrument(void) | |
432 { | |
433 int i; | |
434 JSSExtInstrument *res; | |
435 | |
436 // Allocate a instrument structure | |
437 res = dmMalloc0(sizeof(JSSExtInstrument)); | |
438 if (res == NULL) | |
439 return NULL; | |
440 | |
441 for (i = 0; i < jsetNNotes; i++) | |
442 { | |
443 res->sNumForNotes[i] = jsetNotSet; | |
444 } | |
445 | |
446 return res; | |
447 } |