Mercurial > hg > dmlib
annotate minijss/jssmod.c @ 1403:6aa0897265e8
Modernization cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 30 Oct 2017 18:15:11 +0200 |
parents | b47109fce375 |
children | cedb5ca1533b |
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 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 |
1403 | 239 for (int i = 0; i < module->ninstruments; i++) |
49
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 JSSModule *module; | |
270 | |
271 // Allocate module structure | |
272 module = dmMalloc0(sizeof(JSSModule)); | |
273 if (module == NULL) | |
274 return NULL; | |
275 | |
276 // Initialize structure | |
1403 | 277 for (int i = 0; i < jsetNChannels; i++) |
0 | 278 module->defPanning[i] = jchPanMiddle; |
279 | |
1403 | 280 for (int i = 0; i < jsetMaxOrders; i++) |
0 | 281 module->orderList[i] = jsetOrderEnd; |
282 | |
283 // Allocate mutex | |
284 #ifdef JSS_SUP_THREADS | |
285 module->mutex = dmCreateMutex(); | |
286 #endif | |
287 | |
288 return module; | |
289 } | |
290 | |
291 | |
292 /* Frees a given module structure, freeing all memory areas | |
293 * that were allocated for it (including patterns, samples, etc.) | |
294 */ | |
295 int jssFreeModule(JSSModule * module) | |
296 { | |
297 if (module == NULL) | |
298 return DMERR_NULLPTR; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
299 |
0 | 300 // Free strings |
301 #ifndef JSS_LIGHT | |
302 dmFree(module->moduleName); | |
303 dmFree(module->trackerName); | |
304 #endif | |
305 | |
306 // Free patterns | |
1403 | 307 for (int 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
|
308 jssFreePattern(module->patterns[i]); |
0 | 309 |
286
a17e54015bd9
Fix a small memory leak, the special "empty" pattern was not being freed.
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
310 // 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
|
311 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
|
312 |
0 | 313 // Free instruments |
1403 | 314 for (int 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
|
315 jssFreeInstrument(module->instruments[i]); |
0 | 316 |
317 // Free extended instruments | |
1403 | 318 for (int 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
|
319 jssFreeExtInstrument(module->extInstruments[i]); |
0 | 320 |
321 // Free mutex | |
322 #ifdef JSS_SUP_THREADS | |
323 dmDestroyMutex(module->mutex); | |
324 #endif | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
325 |
0 | 326 // Free the module structure |
1167 | 327 dmMemset(module, 0, sizeof(JSSModule)); |
0 | 328 dmFree(module); |
329 | |
330 return DMERR_OK; | |
331 } | |
332 | |
333 | |
334 /* Allocates and initializes a internal pattern structure. | |
335 */ | |
743 | 336 JSSPattern *jssAllocatePattern(const int nrows, const int nchannels) |
0 | 337 { |
1222 | 338 JSSPattern *pattern; |
0 | 339 JSSNote *pnote; |
340 | |
341 // Check arguments | |
342 if (nrows <= 0 || nchannels <= 0) | |
343 JSSERROR(DMERR_INVALID_ARGS, NULL, "Invalid nrows=%i or nchannels=%i.\n", nrows, nchannels); | |
344 | |
345 // Allocate a pattern structure | |
1222 | 346 if ((pattern = dmMalloc0(sizeof(JSSPattern))) == NULL) |
0 | 347 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); |
348 | |
349 // Allocate notedata | |
1238 | 350 pattern->data = dmMalloc(nrows * nchannels * sizeof(JSSNote)); |
1222 | 351 if (pattern->data == NULL) |
0 | 352 { |
1222 | 353 dmFree(pattern); |
0 | 354 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, |
355 nchannels); | |
356 } | |
357 | |
1238 | 358 // Initialize structure |
359 pattern->nrows = nrows; | |
1222 | 360 pattern->nchannels = nchannels; |
0 | 361 |
1222 | 362 pnote = pattern->data; |
1403 | 363 for (int row = 0; row < nrows; row++) |
364 for (int chn = 0; chn < nchannels; chn++) | |
0 | 365 { |
1403 | 366 pnote->note = pnote->instrument = pnote->volume = |
367 pnote->effect = pnote->param = jsetNotSet; | |
0 | 368 |
1403 | 369 pnote++; |
0 | 370 } |
371 | |
1222 | 372 return pattern; |
373 } | |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
374 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
375 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
376 void jssFreePattern(JSSPattern *pattern) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
377 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
378 if (pattern != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
379 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
380 dmFree(pattern->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
381 dmFree(pattern); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
382 } |
0 | 383 } |
384 | |
385 | |
386 /* Allocates and initializes internal "normal" instrument structure. | |
387 */ | |
388 JSSInstrument *jssAllocateInstrument(void) | |
389 { | |
1222 | 390 JSSInstrument *inst; |
0 | 391 |
392 // Allocate a instrument structure | |
1222 | 393 if ((inst = dmMalloc0(sizeof(JSSInstrument))) == NULL) |
0 | 394 return NULL; |
395 | |
1222 | 396 return inst; |
0 | 397 } |
398 | |
399 | |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
400 void jssFreeInstrument(JSSInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
401 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
402 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
403 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
404 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
405 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
406 #endif |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
407 dmFree(inst->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
408 dmFree(inst); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
409 } |
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 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
412 |
0 | 413 /* Allocates and initializes "extended" instrument structure. |
414 */ | |
415 JSSExtInstrument *jssAllocateExtInstrument(void) | |
416 { | |
1222 | 417 JSSExtInstrument *inst; |
0 | 418 |
419 // Allocate a instrument structure | |
1222 | 420 if ((inst = dmMalloc0(sizeof(JSSExtInstrument))) == NULL) |
0 | 421 return NULL; |
422 | |
1222 | 423 // Initialize the requisite fields |
1403 | 424 for (int i = 0; i < jsetNNotes; i++) |
1222 | 425 inst->sNumForNotes[i] = jsetNotSet; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
426 |
1222 | 427 return inst; |
0 | 428 } |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
429 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
430 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
431 void jssFreeExtInstrument(JSSExtInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
432 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
433 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
434 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
435 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
436 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
437 #endif |
1402
b47109fce375
Make JSSExtInstrument.instConvSamples dynamically allocated and add check
Matti Hamalainen <ccr@tnsp.org>
parents:
1401
diff
changeset
|
438 dmFree(inst->instConvTable); |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
439 dmFree(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 } |