-
Notifications
You must be signed in to change notification settings - Fork 3
/
JUDASSMP.C
378 lines (349 loc) · 12.2 KB
/
JUDASSMP.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* JUDAS sample handling
*/
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>
#include <math.h>
#include "judas.h"
#include "judascfg.h"
/* A "volume profile" for each sample will be precalculated.
*
* VUPROFILESAMPLES defines how many samplepoints are used in the estimation of
* one volume value. VUPROFILERATIO defines how many samplepoints there are
* between the volume estimations. The first sample in the sampledata will be
* in the middle of the first chunk to investigate.
*/
#define VUPROFILERATIO 512
/*
* This is something interesting...the sample pointer might go this many bytes
* over the sample end while in the mixing inner loop. Here's the formula:
*
* maximum_playspeed / minimum_mixspeed (round upwards for safety)
* bytes = ---------------------------------------------------------------
* repeats_in_mixing_inner_loop
*
* Maximum playspeed is 535232 (highest speed in linear freq. table), minimum
* mixspeed is 5000 and there are 16 repeats.
* And, of course, because there can be 16bit samples the number of bytes must
* be multiplied with 2.
*/
#define ENDSAFETYAREA ((3456 > 4*VUPROFILERATIO)? 3456:4*VUPROFILERATIO)
#define STARTSAFETYAREA 4
SAMPLE *judas_allocsample(int length);
void judas_freesample(SAMPLE *smp);
void judas_playsample(SAMPLE *smp, unsigned chnum, unsigned frequency, unsigned short volume, unsigned char panning);
void judas_ipcorrect(SAMPLE *smp);
void judas_stopsample(unsigned chnum);
void judas_preventdistortion(unsigned channels);
void judas_setmastervolume(unsigned chnum, unsigned char mastervol);
void judas_setmusicmastervolume(unsigned musicchannels, unsigned char mastervol);
void judas_setsfxmastervolume(unsigned musicchannels, unsigned char mastervol);
void judas_calcvuprofile(SAMPLE *smp);
float judas_getvumeter(unsigned chnum);
SAMPLE *judas_allocsample(int length)
{
SAMPLE *smp;
int vuprofilelen = 1+length/VUPROFILERATIO;
judas_error = JUDAS_OUT_OF_MEMORY;
length += STARTSAFETYAREA + ENDSAFETYAREA; // ***
smp = (SAMPLE *)locked_malloc(sizeof (SAMPLE));
if (!smp) return NULL;
smp->start = locked_malloc(length);
if (!smp->start)
{
judas_freesample(smp);
return NULL;
}
// vuprofile allocation:
smp->vuprofile = locked_malloc(vuprofilelen+1); // Some extra, because of interpolation
if (!smp->vuprofile)
{
judas_freesample(smp);
return NULL;
}
smp->start += STARTSAFETYAREA; // ***
smp->repeat = NULL;
smp->end = NULL;
judas_error = JUDAS_OK;
return smp;
}
void judas_freesample(SAMPLE *smp)
{
int c;
CHANNEL *chptr;
if (!smp) return;
if (smp == &fakesample) return;
/*
* Go thru all channels; if that sample is playing then stop the
* channel; we don't want a crash or audible shit!
*/
chptr = &judas_channel[0];
for (c = CHANNELS; c > 0; c--)
{
if (chptr->smp == smp)
{
chptr->smp = NULL;
chptr->voicemode = VM_OFF;
}
chptr++;
}
/*
* Free the sample data and then the sample structure itself
*/
if (smp->vuprofile) locked_free(smp->vuprofile);
if (smp->start) locked_free(smp->start-STARTSAFETYAREA); // ***
locked_free(smp);
}
void judas_ipcorrect(SAMPLE *smp)
{
if (!smp) return;
if (smp == &fakesample) return;
{
int count;
for (count = -STARTSAFETYAREA ; count < 0 ; count++)
*(smp->start + count) = 0;
}
if (smp->voicemode & VM_LOOP)
{
int count, lcount = smp->end - smp->repeat;
unsigned char *src = &smp->repeat[0], *dest = &smp->end[0];
if (!src) return;
if (!dest) return;
if (smp->voicemode & VM_16BIT) count = ENDSAFETYAREA;
else count = ENDSAFETYAREA / 2;
while (count)
{
if (lcount <= 0)
{
lcount = smp->end - smp->repeat;
src = &smp->repeat[0];
}
*dest++ = *src++;
lcount--;
count--;
}
}
else
{
/* Since V2.04 the data beyond sample end is presumed to be
* what it was at sample end, not zero! (Because of q-mixer)
*/
if (!smp->end) return;
if (smp->voicemode & VM_16BIT) {
int count = ENDSAFETYAREA/2;
char *dest = &smp->end[0];
while (count)
{
*dest = smp->end[-2];
*(dest+1) = smp->end[-1];
dest += 2;
count--;
}
} else {
int count = ENDSAFETYAREA;
char *dest = &smp->end[0];
while (count)
{
*dest++ = smp->end[-1]; /* Toivottavasti samplen pituus > 0 */
count--;
}
}
}
judas_calcvuprofile(smp);
}
void judas_calcvuprofile(SAMPLE *smp)
{
if (smp->voicemode & VM_16BIT)
{
if (smp->voicemode & VM_LOOP)
{
unsigned char *vup = smp->vuprofile;
int samples = (smp->end - smp->start)/2;
int chunks = 1+samples/VUPROFILERATIO+1; // +1 for interpolation
int chunk;
for (chunk = 0; chunk < chunks; chunk++)
{
float thisvol = 0;
int chunksample;
signed short *firstsmpinchunk = (short *) smp->start - VUPROFILERATIO/2 + chunk*VUPROFILERATIO;
if (firstsmpinchunk < (short *) smp->start) firstsmpinchunk = (short *) smp->start;
for (chunksample = 0; (chunksample < VUPROFILERATIO); chunksample++)
{
thisvol += ((float) firstsmpinchunk[chunksample]*firstsmpinchunk[chunksample])/16384;
}
thisvol = sqrt(255*sqrt((float)thisvol/VUPROFILERATIO));
if (thisvol > 255) thisvol = 255;
*vup = thisvol;
vup++;
}
} else
{
unsigned char *vup = smp->vuprofile;
int samples = (smp->end - smp->start)/2;
int chunks = 1+samples/VUPROFILERATIO+1; // +1 for interpolation
int chunk;
for (chunk = 0; chunk < chunks; chunk++)
{
float thisvol = 0;
int chunksample;
signed short *firstsmpinchunk = (short *) smp->start - VUPROFILERATIO/2 + chunk*VUPROFILERATIO;
if (firstsmpinchunk < (short *) smp->start) firstsmpinchunk = (short *) smp->start;
for (chunksample = 0; (chunksample < VUPROFILERATIO); chunksample++)
{
if (&firstsmpinchunk[chunksample] >= (short *) smp->end) break;
thisvol += ((float) firstsmpinchunk[chunksample]*firstsmpinchunk[chunksample])/16384;
}
thisvol = sqrt(255*sqrt((float)thisvol/VUPROFILERATIO));
if (thisvol > 255) thisvol = 255;
*vup = thisvol;
vup++;
}
}
} else
{
if (smp->voicemode & VM_LOOP)
{
unsigned char *vup = smp->vuprofile;
int samples = (smp->end - smp->start);
int chunks = 1+samples/VUPROFILERATIO+1; // +1 for interpolation
int chunk;
for (chunk = 0; chunk < chunks; chunk++)
{
float thisvol = 0;
int chunksample;
signed char *firstsmpinchunk = (signed char *) smp->start - VUPROFILERATIO/2 + chunk*VUPROFILERATIO;
if (firstsmpinchunk < (signed char *) smp->start) firstsmpinchunk = (signed char *) smp->start;
for (chunksample = 0; (chunksample < VUPROFILERATIO); chunksample++)
{
thisvol += ((float) firstsmpinchunk[chunksample]*firstsmpinchunk[chunksample]);
}
thisvol = sqrt(255*sqrt(4*(float)thisvol/VUPROFILERATIO));
if (thisvol > 255) thisvol = 255;
*vup = thisvol;
vup++;
}
} else
{
unsigned char *vup = smp->vuprofile;
int samples = (smp->end - smp->start);
int chunks = 1+samples/VUPROFILERATIO+1; // +1 for interpolation
int chunk;
for (chunk = 0; chunk < chunks; chunk++)
{
float thisvol = 0;
int chunksample;
signed char *firstsmpinchunk = (signed char *) smp->start - VUPROFILERATIO/2 + chunk*VUPROFILERATIO;
if (firstsmpinchunk < (signed char *) smp->start) firstsmpinchunk = (signed char *) smp->start;
for (chunksample = 0; (chunksample < VUPROFILERATIO); chunksample++)
{
if (&firstsmpinchunk[chunksample] >= (signed char *) smp->end) break;
thisvol += ((float) firstsmpinchunk[chunksample]*firstsmpinchunk[chunksample]);
}
thisvol = sqrt(255*sqrt(4*(float)thisvol/VUPROFILERATIO));
if (thisvol > 255) thisvol = 255;
*vup = thisvol;
vup++;
}
}
}
}
float judas_getvumeter(unsigned chnum) {
if (
(chnum >= CHANNELS) ||
((judas_channel[chnum].voicemode & VM_ON) == VM_OFF) ||
(!judas_channel[chnum].smp)
) return 0;
{
SAMPLE *smp = judas_channel[chnum].smp;
float vu = judas_channel[chnum].vol;
char *pos = judas_channel[chnum].pos;
int chunk;
float fractpos, vuprofile0, vuprofile1;
if (pos >= smp->end || pos < smp->start) return 0; // Might have changed!!!
if (smp->voicemode & VM_16BIT)
{
chunk = (pos - smp->start)/(2*VUPROFILERATIO);
fractpos = (float)(pos - smp->start)/(2*VUPROFILERATIO)-chunk;
} else {
chunk = (pos - smp->start)/(VUPROFILERATIO);
fractpos = (float)(pos - smp->start)/(VUPROFILERATIO)-chunk;
}
// Interpolation:
vuprofile0 = smp->vuprofile[chunk];
vuprofile1 = smp->vuprofile[chunk+1];
vuprofile0 *= vuprofile0/255;
vuprofile1 *= vuprofile1/255;
vu *= fractpos*(vuprofile1-vuprofile0)+vuprofile0;
// max now 255*64*256
vu /= (float)64*255*256; // Shrink to 0..1 but not 1
return (vu<1)? vu:0.99999999;
}
}
void judas_playsample(SAMPLE *smp, unsigned chnum, unsigned frequency, unsigned short volume, unsigned char panning)
{
CHANNEL *chptr;
if (!smp) return;
if (smp == &fakesample) return;
if (chnum >= CHANNELS) return;
chptr = &judas_channel[chnum];
chptr->voicemode = VM_OFF;
chptr->pos = smp->start;
chptr->repeat = smp->repeat;
chptr->end = smp->end;
chptr->smp = smp;
chptr->fractpos = 0;
chptr->freq = frequency;
chptr->vol = volume;
chptr->panning = panning;
chptr->voicemode = smp->voicemode;
}
void judas_stopsample(unsigned chnum)
{
CHANNEL *chptr;
if (chnum >= CHANNELS) return;
chptr = &judas_channel[chnum];
chptr->voicemode = VM_OFF;
chptr->smp = NULL;
}
void judas_preventdistortion(unsigned active_channels)
{
int count;
unsigned char mastervol;
if (active_channels < 2) mastervol = 255;
else mastervol = 256 / active_channels;
for (count = 0; count < CHANNELS; count++)
{
judas_setmastervolume(count, mastervol);
}
}
void judas_setmastervolume(unsigned chnum, unsigned char mastervol)
{
CHANNEL *chptr;
if (chnum >= CHANNELS) return;
chptr = &judas_channel[chnum];
chptr->mastervol = mastervol;
}
void judas_setmusicmastervolume(unsigned musicchannels, unsigned char mastervol)
{
CHANNEL *chptr = &judas_channel[0];
int count;
if (musicchannels > CHANNELS) musicchannels = CHANNELS;
for (count = 0; count < musicchannels; count++)
{
chptr->mastervol = mastervol;
chptr++;
}
}
void judas_setsfxmastervolume(unsigned musicchannels, unsigned char mastervol)
{
CHANNEL *chptr;
int count;
if (musicchannels >= CHANNELS) return;
chptr = &judas_channel[musicchannels];
for (count = musicchannels; count < CHANNELS; count++)
{
chptr->mastervol = mastervol;
chptr++;
}
}