Mercurial > hg > dmlib
annotate minijss/jssmod.c @ 1129:e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
instead of plain boolean value. Change some of the relevant places to use
this return value.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 04 Mar 2015 06:31:54 +0200 |
parents | e22d4ceb6414 |
children | aa3738b121d1 |
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 */ | |
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++) | |
312 { | |
743 | 313 if (module->patterns[i] != NULL) |
0 | 314 { |
315 JSSPattern *pat = module->patterns[i]; | |
316 dmFree(pat->data); | |
317 dmFree(pat); | |
318 module->patterns[i] = NULL; | |
319 } | |
320 } | |
321 | |
286
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
322 // 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
|
323 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
|
324 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
|
325 { |
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->data); |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
327 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
|
328 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
|
329 } |
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
330 |
0 | 331 // Free instruments |
332 for (i = 0; i < module->ninstruments; i++) | |
333 { | |
743 | 334 if (module->instruments[i] != NULL) |
0 | 335 { |
336 JSSInstrument *inst = module->instruments[i]; | |
337 #ifndef JSS_LIGHT | |
338 dmFree(inst->desc); | |
339 #endif | |
340 dmFree(inst->data); | |
341 dmFree(inst); | |
342 module->instruments[i] = NULL; | |
343 } | |
344 } | |
345 | |
346 // Free extended instruments | |
347 for (i = 0; i < module->nextInstruments; i++) | |
348 { | |
743 | 349 if (module->extInstruments[i] != NULL) |
0 | 350 { |
351 JSSExtInstrument *ext = module->extInstruments[i]; | |
352 #ifndef JSS_LIGHT | |
353 dmFree(ext->desc); | |
354 #endif | |
355 dmFree(ext); | |
356 module->extInstruments[i] = NULL; | |
357 } | |
358 } | |
359 | |
360 // Free mutex | |
361 #ifdef JSS_SUP_THREADS | |
362 dmDestroyMutex(module->mutex); | |
363 #endif | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
364 |
0 | 365 // Free the module structure |
366 memset(module, 0, sizeof(JSSModule)); | |
367 dmFree(module); | |
368 | |
369 return DMERR_OK; | |
370 } | |
371 | |
372 | |
373 /* Allocates and initializes a internal pattern structure. | |
374 */ | |
743 | 375 JSSPattern *jssAllocatePattern(const int nrows, const int nchannels) |
0 | 376 { |
743 | 377 int row, chn; |
0 | 378 JSSPattern *res; |
379 JSSNote *pnote; | |
380 | |
381 // Check arguments | |
382 if (nrows <= 0 || nchannels <= 0) | |
383 JSSERROR(DMERR_INVALID_ARGS, NULL, "Invalid nrows=%i or nchannels=%i.\n", nrows, nchannels); | |
384 | |
385 // Allocate a pattern structure | |
386 res = dmMalloc0(sizeof(JSSPattern)); | |
387 if (res == NULL) | |
388 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); | |
389 | |
390 // Allocate notedata | |
391 res->data = dmCalloc(nrows * nchannels, sizeof(JSSNote)); | |
392 if (res->data == NULL) | |
393 { | |
394 dmFree(res); | |
395 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, | |
396 nchannels); | |
397 } | |
398 | |
399 // Initialize | |
400 res->nrows = nrows; | |
401 res->nchannels = nchannels; | |
402 | |
403 pnote = res->data; | |
743 | 404 for (row = 0; row < nrows; row++) |
0 | 405 { |
743 | 406 for (chn = 0; chn < nchannels; chn++) |
0 | 407 { |
408 pnote->note = pnote->instrument = pnote->volume = | |
409 pnote->effect = pnote->param = jsetNotSet; | |
410 | |
411 pnote++; | |
412 } | |
413 } | |
414 | |
415 // OK, return pointer to struct | |
416 return res; | |
417 } | |
418 | |
419 | |
420 /* Allocates and initializes internal "normal" instrument structure. | |
421 */ | |
422 JSSInstrument *jssAllocateInstrument(void) | |
423 { | |
424 JSSInstrument *res; | |
425 | |
426 // Allocate a instrument structure | |
427 res = dmMalloc0(sizeof(JSSInstrument)); | |
428 if (res == NULL) | |
429 return NULL; | |
430 | |
431 return res; | |
432 } | |
433 | |
434 | |
435 /* Allocates and initializes "extended" instrument structure. | |
436 */ | |
437 JSSExtInstrument *jssAllocateExtInstrument(void) | |
438 { | |
439 int i; | |
440 JSSExtInstrument *res; | |
441 | |
442 // Allocate a instrument structure | |
443 res = dmMalloc0(sizeof(JSSExtInstrument)); | |
444 if (res == NULL) | |
445 return NULL; | |
446 | |
447 for (i = 0; i < jsetNNotes; i++) | |
448 { | |
449 res->sNumForNotes[i] = jsetNotSet; | |
450 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
451 |
0 | 452 return res; |
453 } |