Mercurial > hg > dmlib
annotate minijss/jssmod.c @ 1402:b47109fce375
Make JSSExtInstrument.instConvSamples dynamically allocated and add check
for XM_MaxInstSamples in XM loader.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 30 Oct 2017 18:12:38 +0200 |
parents | aaed8fa9a11f |
children | 6aa0897265e8 |
rev | line source |
---|---|
0 | 1 /* |
2 * miniJSS - Module structure and handling routines | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
1163 | 4 * (C) Copyright 2006-2015 Tecnic Software productions (TNSP) |
0 | 5 */ |
6 #include "jssmod.h" | |
7 | |
8 | |
9 #ifndef JSS_LIGHT | |
948 | 10 |
11 /* Take given data until maxlen reached, make a string. | |
12 * Basically a bit like strndup(), except end marker byte | |
13 * can be specified. | |
0 | 14 */ |
948 | 15 char *jssASCIItoStr(const char * src, const char endByte, const size_t len) |
0 | 16 { |
17 size_t i, k; | |
18 char *res; | |
19 | |
1401
aaed8fa9a11f
Fix jssASCIItoStr() bounds check.
Matti Hamalainen <ccr@tnsp.org>
parents:
1238
diff
changeset
|
20 for (i = 0; src[i] != endByte && i < len; ) |
aaed8fa9a11f
Fix jssASCIItoStr() bounds check.
Matti Hamalainen <ccr@tnsp.org>
parents:
1238
diff
changeset
|
21 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 */ | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
129 int 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 } |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
146 return DMERR_OK; |
0 | 147 } |
148 | |
149 | |
150 /* Decodes a given 16-bit sample | |
151 */ | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
152 int 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; | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
159 int ret; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
160 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
161 if ((ret = jssDecodeSample8((Uint8 *) data, bufSize, ops)) != DMERR_OK) |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
162 return ret; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
163 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
164 if ((tmpBuf = dmMalloc(bufSize)) == NULL) |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
165 return DMERR_MALLOC; |
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
166 |
43 | 167 memcpy(tmpBuf, data, bufSize); |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
168 |
43 | 169 sdata = (Sint16 *) data; |
0 | 170 bp1 = (Uint8 *) tmpBuf; |
43 | 171 bp2 = bp1 + len; |
172 count = len; | |
173 while (count--) | |
0 | 174 { |
175 *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff); | |
176 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
177 |
0 | 178 dmFree(tmpBuf); |
179 } | |
180 else | |
181 { | |
43 | 182 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
183 size_t count = len; | |
184 while (count--) | |
0 | 185 { |
186 if (ops & jsampSwapEndianess) | |
187 { | |
188 p = *sdata; | |
189 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
743 | 190 } |
191 else | |
0 | 192 t = *sdata; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
193 |
0 | 194 if (ops & jsampDelta) |
195 t = value = t + value; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
196 |
0 | 197 if (ops & jsampFlipSign) |
198 t ^= 0x8000; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
199 |
0 | 200 *(sdata++) = t; |
201 } | |
202 } | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
203 return DMERR_OK; |
0 | 204 } |
205 | |
49
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 /* 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
|
208 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
209 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
|
210 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
211 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
|
212 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
|
213 Sint16 *out; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
214 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
215 if ((*dst = out = dmMalloc(sizeof(Sint16) * len)) == NULL) |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
216 return DMERR_MALLOC; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
217 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
218 while (count--) |
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 *(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
|
221 } |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
222 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
223 return DMERR_OK; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
224 } |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
225 |
743 | 226 |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
227 /* 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
|
228 * 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
|
229 * |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
230 * 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
|
231 * 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
|
232 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
233 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
|
234 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
235 int i; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
236 if (module == NULL) |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
237 return DMERR_NULLPTR; |
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 // Convert instruments |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
240 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
|
241 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
242 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
|
243 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
|
244 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
245 int res; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
246 void *data = NULL; |
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 (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 continue; |
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 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
|
252 return res; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
253 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
254 inst->flags |= jsf16bit; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
255 dmFree(inst->data); |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
256 inst->data = data; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
257 } |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
258 } |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
259 |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
260 return DMERR_OK; |
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 |
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
263 |
0 | 264 /* Allocates a new module structure or returns errorvalue if failed. |
265 * Memory is allocated only for the basic structure. Sample- and pattern | |
266 * areas must be allocated separately with appropriate routines. | |
267 */ | |
268 JSSModule *jssAllocateModule(void) | |
269 { | |
270 int i; | |
271 JSSModule *module; | |
272 | |
273 // Allocate module structure | |
274 module = dmMalloc0(sizeof(JSSModule)); | |
275 if (module == NULL) | |
276 return NULL; | |
277 | |
278 // Initialize structure | |
279 for (i = 0; i < jsetNChannels; i++) | |
280 module->defPanning[i] = jchPanMiddle; | |
281 | |
282 for (i = 0; i < jsetMaxOrders; i++) | |
283 module->orderList[i] = jsetOrderEnd; | |
284 | |
285 // Allocate mutex | |
286 #ifdef JSS_SUP_THREADS | |
287 module->mutex = dmCreateMutex(); | |
288 #endif | |
289 | |
290 return module; | |
291 } | |
292 | |
293 | |
294 /* Frees a given module structure, freeing all memory areas | |
295 * that were allocated for it (including patterns, samples, etc.) | |
296 */ | |
297 int jssFreeModule(JSSModule * module) | |
298 { | |
299 int i; | |
300 | |
301 if (module == NULL) | |
302 return DMERR_NULLPTR; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
303 |
0 | 304 // Free strings |
305 #ifndef JSS_LIGHT | |
306 dmFree(module->moduleName); | |
307 dmFree(module->trackerName); | |
308 #endif | |
309 | |
310 // Free patterns | |
311 for (i = 0; i < module->npatterns; i++) | |
1227
a4a9ce298cdd
Simplify jssFreeModule() by using jssFree*() functions for freeing parts of
Matti Hamalainen <ccr@tnsp.org>
parents:
1224
diff
changeset
|
312 jssFreePattern(module->patterns[i]); |
0 | 313 |
286
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
314 // Free the "empty" pattern |
1227
a4a9ce298cdd
Simplify jssFreeModule() by using jssFree*() functions for freeing parts of
Matti Hamalainen <ccr@tnsp.org>
parents:
1224
diff
changeset
|
315 jssFreePattern(module->patterns[jsetMaxPatterns]); |
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 |
0 | 317 // Free instruments |
318 for (i = 0; i < module->ninstruments; i++) | |
1227
a4a9ce298cdd
Simplify jssFreeModule() by using jssFree*() functions for freeing parts of
Matti Hamalainen <ccr@tnsp.org>
parents:
1224
diff
changeset
|
319 jssFreeInstrument(module->instruments[i]); |
0 | 320 |
321 // Free extended instruments | |
322 for (i = 0; i < module->nextInstruments; i++) | |
1227
a4a9ce298cdd
Simplify jssFreeModule() by using jssFree*() functions for freeing parts of
Matti Hamalainen <ccr@tnsp.org>
parents:
1224
diff
changeset
|
323 jssFreeExtInstrument(module->extInstruments[i]); |
0 | 324 |
325 // Free mutex | |
326 #ifdef JSS_SUP_THREADS | |
327 dmDestroyMutex(module->mutex); | |
328 #endif | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
329 |
0 | 330 // Free the module structure |
1167 | 331 dmMemset(module, 0, sizeof(JSSModule)); |
0 | 332 dmFree(module); |
333 | |
334 return DMERR_OK; | |
335 } | |
336 | |
337 | |
338 /* Allocates and initializes a internal pattern structure. | |
339 */ | |
743 | 340 JSSPattern *jssAllocatePattern(const int nrows, const int nchannels) |
0 | 341 { |
743 | 342 int row, chn; |
1222 | 343 JSSPattern *pattern; |
0 | 344 JSSNote *pnote; |
345 | |
346 // Check arguments | |
347 if (nrows <= 0 || nchannels <= 0) | |
348 JSSERROR(DMERR_INVALID_ARGS, NULL, "Invalid nrows=%i or nchannels=%i.\n", nrows, nchannels); | |
349 | |
350 // Allocate a pattern structure | |
1222 | 351 if ((pattern = dmMalloc0(sizeof(JSSPattern))) == NULL) |
0 | 352 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); |
353 | |
354 // Allocate notedata | |
1238 | 355 pattern->data = dmMalloc(nrows * nchannels * sizeof(JSSNote)); |
1222 | 356 if (pattern->data == NULL) |
0 | 357 { |
1222 | 358 dmFree(pattern); |
0 | 359 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, |
360 nchannels); | |
361 } | |
362 | |
1238 | 363 // Initialize structure |
364 pattern->nrows = nrows; | |
1222 | 365 pattern->nchannels = nchannels; |
0 | 366 |
1222 | 367 pnote = pattern->data; |
743 | 368 for (row = 0; row < nrows; row++) |
0 | 369 { |
743 | 370 for (chn = 0; chn < nchannels; chn++) |
0 | 371 { |
372 pnote->note = pnote->instrument = pnote->volume = | |
373 pnote->effect = pnote->param = jsetNotSet; | |
374 | |
375 pnote++; | |
376 } | |
377 } | |
378 | |
1222 | 379 return pattern; |
380 } | |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
381 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
382 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
383 void jssFreePattern(JSSPattern *pattern) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
384 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
385 if (pattern != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
386 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
387 dmFree(pattern->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
388 dmFree(pattern); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
389 } |
0 | 390 } |
391 | |
392 | |
393 /* Allocates and initializes internal "normal" instrument structure. | |
394 */ | |
395 JSSInstrument *jssAllocateInstrument(void) | |
396 { | |
1222 | 397 JSSInstrument *inst; |
0 | 398 |
399 // Allocate a instrument structure | |
1222 | 400 if ((inst = dmMalloc0(sizeof(JSSInstrument))) == NULL) |
0 | 401 return NULL; |
402 | |
1222 | 403 return inst; |
0 | 404 } |
405 | |
406 | |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
407 void jssFreeInstrument(JSSInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
408 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
409 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
410 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
411 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
412 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
413 #endif |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
414 dmFree(inst->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
415 dmFree(inst); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
416 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
417 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
418 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
419 |
0 | 420 /* Allocates and initializes "extended" instrument structure. |
421 */ | |
422 JSSExtInstrument *jssAllocateExtInstrument(void) | |
423 { | |
424 int i; | |
1222 | 425 JSSExtInstrument *inst; |
0 | 426 |
427 // Allocate a instrument structure | |
1222 | 428 if ((inst = dmMalloc0(sizeof(JSSExtInstrument))) == NULL) |
0 | 429 return NULL; |
430 | |
1222 | 431 // Initialize the requisite fields |
0 | 432 for (i = 0; i < jsetNNotes; i++) |
1222 | 433 inst->sNumForNotes[i] = jsetNotSet; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
434 |
1222 | 435 return inst; |
0 | 436 } |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
437 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
438 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
439 void jssFreeExtInstrument(JSSExtInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
440 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
441 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
442 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
443 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
444 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
445 #endif |
1402
b47109fce375
Make JSSExtInstrument.instConvSamples dynamically allocated and add check
Matti Hamalainen <ccr@tnsp.org>
parents:
1401
diff
changeset
|
446 dmFree(inst->instConvTable); |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
447 dmFree(inst); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
448 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
449 } |