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