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