Mercurial > hg > dmlib
annotate minijss/jssmod.c @ 1224:e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 06 Mar 2015 09:56:21 +0200 |
parents | 823f8e06ff6a |
children | a4a9ce298cdd |
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 | |
948 | 20 for (i = 0; src[i] && i < len; i++); |
0 | 21 |
948 | 22 if ((res = dmMalloc(i + 1)) == NULL) |
0 | 23 return NULL; |
24 | |
948 | 25 for (k = 0; src[k] != endByte && k < i; k++) |
26 res[k] = src[k]; | |
0 | 27 |
28 res[k] = 0; | |
29 | |
30 return res; | |
31 } | |
32 | |
33 | |
34 /* Encodes a given 8-bit sample | |
35 */ | |
43 | 36 BOOL jssEncodeSample8(Uint8 * data, const size_t len, const int ops) |
0 | 37 { |
43 | 38 size_t count = len; |
0 | 39 Sint8 t, value = 0; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
40 |
43 | 41 while (count--) |
0 | 42 { |
43 | 43 t = *data; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
44 |
0 | 45 if (ops & jsampFlipSign) |
46 t ^= 0x80; | |
47 | |
48 if (ops & jsampDelta) | |
49 { | |
50 int n = t - value; | |
51 value = t; | |
52 t = n; | |
53 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
54 |
43 | 55 *(data++) = t; |
0 | 56 } |
57 | |
58 return TRUE; | |
59 } | |
60 | |
61 | |
62 /* Decodes a given 16-bit sample | |
63 */ | |
43 | 64 BOOL jssEncodeSample16(Uint16 * data, const size_t len, const int ops) |
0 | 65 { |
66 // "Split" the 16-bit samples into 8-bit halves | |
67 if (ops & jsampSplit) | |
68 { | |
69 // Allocate temporary processing buffer | |
43 | 70 size_t count, bufSize = len * sizeof(Sint16); |
0 | 71 Uint8 *bp1, *bp2; |
43 | 72 Sint16 *sdata, *tmpBuf = dmMalloc(bufSize); |
0 | 73 if (tmpBuf == NULL) return FALSE; |
74 | |
75 sdata = tmpBuf; | |
43 | 76 bp1 = (Uint8 *) data; |
77 bp2 = bp1 + len; | |
78 count = len; | |
0 | 79 |
43 | 80 while (count--) |
0 | 81 { |
43 | 82 Sint16 t = (*sdata++); |
0 | 83 *bp1++ = t >> 8; |
84 *bp2++ = t & 0xff; | |
85 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
86 |
43 | 87 memcpy(data, tmpBuf, bufSize); |
0 | 88 dmFree(tmpBuf); |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
89 |
43 | 90 return jssEncodeSample8((Uint8 *) data, bufSize, ops); |
0 | 91 } |
92 else | |
93 { | |
43 | 94 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
95 size_t count = len; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
96 |
43 | 97 while (count--) |
0 | 98 { |
99 if (ops & jsampSwapEndianess) | |
100 { | |
101 p = *sdata; | |
102 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
743 | 103 } |
104 else | |
0 | 105 t = *sdata; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
106 |
0 | 107 if (ops & jsampDelta) |
108 { | |
109 int n = t - value; | |
110 value = t; | |
111 t = n; | |
112 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
113 |
0 | 114 if (ops & jsampFlipSign) |
115 t ^= 0x8000; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
116 |
0 | 117 *(sdata++) = t; |
118 } | |
119 } | |
120 return TRUE; | |
121 } | |
44
064d1d1d5b0f
Add new functions, jssConvertSampleFromFP() and jssConvertSampleToFP().
Matti Hamalainen <ccr@tnsp.org>
parents:
43
diff
changeset
|
122 |
0 | 123 #endif |
124 | |
125 | |
126 /* Decodes a given 8-bit sample | |
127 */ | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
128 int jssDecodeSample8(Uint8 * data, const size_t len, const int ops) |
0 | 129 { |
43 | 130 size_t count = len; |
0 | 131 Sint8 t, value = 0; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
132 |
43 | 133 while (count--) |
0 | 134 { |
43 | 135 t = *data; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
136 |
0 | 137 if (ops & jsampDelta) |
138 t = value = t + value; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
139 |
0 | 140 if (ops & jsampFlipSign) |
141 t ^= 0x80; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
142 |
43 | 143 *(data++) = t; |
0 | 144 } |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
145 return DMERR_OK; |
0 | 146 } |
147 | |
148 | |
149 /* Decodes a given 16-bit sample | |
150 */ | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
151 int jssDecodeSample16(Uint16 * data, const size_t len, const int ops) |
0 | 152 { |
153 if (ops & jsampSplit) | |
154 { | |
43 | 155 size_t count, bufSize = len * sizeof(Uint16); |
0 | 156 Uint8 *bp1, *bp2; |
157 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
|
158 int ret; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
159 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
160 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
|
161 return ret; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
162 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
163 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
|
164 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
|
165 |
43 | 166 memcpy(tmpBuf, data, bufSize); |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
167 |
43 | 168 sdata = (Sint16 *) data; |
0 | 169 bp1 = (Uint8 *) tmpBuf; |
43 | 170 bp2 = bp1 + len; |
171 count = len; | |
172 while (count--) | |
0 | 173 { |
174 *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff); | |
175 } | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
176 |
0 | 177 dmFree(tmpBuf); |
178 } | |
179 else | |
180 { | |
43 | 181 Sint16 t, p, value = 0, *sdata = (Sint16 *) data; |
182 size_t count = len; | |
183 while (count--) | |
0 | 184 { |
185 if (ops & jsampSwapEndianess) | |
186 { | |
187 p = *sdata; | |
188 t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); | |
743 | 189 } |
190 else | |
0 | 191 t = *sdata; |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
192 |
0 | 193 if (ops & jsampDelta) |
194 t = value = t + value; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
195 |
0 | 196 if (ops & jsampFlipSign) |
197 t ^= 0x8000; | |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
198 |
0 | 199 *(sdata++) = t; |
200 } | |
201 } | |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
202 return DMERR_OK; |
0 | 203 } |
204 | |
49
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
205 |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
206 /* 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
|
207 */ |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
208 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
|
209 { |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
210 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
|
211 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
|
212 Sint16 *out; |
033c660c25f5
Restructure module playing, removing 8bit sample mixing (output can still be
Matti Hamalainen <ccr@tnsp.org>
parents:
44
diff
changeset
|
213 |
1129
e466d10dae6d
Change API of jssDecodeSample{8,16}() functions to return dmlib error codes
Matti Hamalainen <ccr@tnsp.org>
parents:
1127
diff
changeset
|
214 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
|
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 |
1167 | 365 dmMemset(module, 0, sizeof(JSSModule)); |
0 | 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; |
1222 | 377 JSSPattern *pattern; |
0 | 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 | |
1222 | 385 if ((pattern = dmMalloc0(sizeof(JSSPattern))) == NULL) |
0 | 386 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern structure.\n"); |
387 | |
388 // Allocate notedata | |
1222 | 389 pattern->data = dmCalloc(nrows * nchannels, sizeof(JSSNote)); |
390 if (pattern->data == NULL) | |
0 | 391 { |
1222 | 392 dmFree(pattern); |
0 | 393 JSSERROR(DMERR_MALLOC, NULL, "Could not allocate pattern data (nrows=%i, nchannels=%i).\n", nrows, |
394 nchannels); | |
395 } | |
396 | |
397 // Initialize | |
1222 | 398 pattern->nrows = nrows; |
399 pattern->nchannels = nchannels; | |
0 | 400 |
1222 | 401 pnote = pattern->data; |
743 | 402 for (row = 0; row < nrows; row++) |
0 | 403 { |
743 | 404 for (chn = 0; chn < nchannels; chn++) |
0 | 405 { |
406 pnote->note = pnote->instrument = pnote->volume = | |
407 pnote->effect = pnote->param = jsetNotSet; | |
408 | |
409 pnote++; | |
410 } | |
411 } | |
412 | |
413 // OK, return pointer to struct | |
1222 | 414 return pattern; |
415 } | |
1224
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 void jssFreePattern(JSSPattern *pattern) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
419 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
420 if (pattern != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
421 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
422 dmFree(pattern->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
423 dmFree(pattern); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
424 } |
0 | 425 } |
426 | |
427 | |
428 /* Allocates and initializes internal "normal" instrument structure. | |
429 */ | |
430 JSSInstrument *jssAllocateInstrument(void) | |
431 { | |
1222 | 432 JSSInstrument *inst; |
0 | 433 |
434 // Allocate a instrument structure | |
1222 | 435 if ((inst = dmMalloc0(sizeof(JSSInstrument))) == NULL) |
0 | 436 return NULL; |
437 | |
1222 | 438 return inst; |
0 | 439 } |
440 | |
441 | |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
442 void jssFreeInstrument(JSSInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
443 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
444 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
445 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
446 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
447 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
448 #endif |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
449 dmFree(inst->data); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
450 dmFree(inst); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
451 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
452 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
453 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
454 |
0 | 455 /* Allocates and initializes "extended" instrument structure. |
456 */ | |
457 JSSExtInstrument *jssAllocateExtInstrument(void) | |
458 { | |
459 int i; | |
1222 | 460 JSSExtInstrument *inst; |
0 | 461 |
462 // Allocate a instrument structure | |
1222 | 463 if ((inst = dmMalloc0(sizeof(JSSExtInstrument))) == NULL) |
0 | 464 return NULL; |
465 | |
1222 | 466 // Initialize the requisite fields |
0 | 467 for (i = 0; i < jsetNNotes; i++) |
468 { | |
1222 | 469 inst->sNumForNotes[i] = jsetNotSet; |
470 inst->instConvTable[i] = jsetNotSet; | |
0 | 471 } |
1127
e22d4ceb6414
Cosmetics pass, remove excess and trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
948
diff
changeset
|
472 |
1222 | 473 return inst; |
0 | 474 } |
1224
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
475 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
476 |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
477 void jssFreeExtInstrument(JSSExtInstrument *inst) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
478 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
479 if (inst != NULL) |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
480 { |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
481 #ifndef JSS_LIGHT |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
482 dmFree(inst->desc); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
483 #endif |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
484 dmFree(inst); |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
485 } |
e667710aee8e
Add functions jssFreeInstrument(), jssFreeExtInstrument() and jssFreePattern().
Matti Hamalainen <ccr@tnsp.org>
parents:
1222
diff
changeset
|
486 } |