Skip to content

Latest commit

 

History

History
613 lines (537 loc) · 14.6 KB

resample.c

File metadata and controls

613 lines (537 loc) · 14.6 KB
 
Oct 21, 1999
Oct 21, 1999
1
/*
Oct 18, 2017
Oct 18, 2017
2
Oct 21, 1999
Oct 21, 1999
3
4
5
6
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
Dec 31, 2011
Dec 31, 2011
7
it under the terms of the Perl Artistic License, available in COPYING.
Oct 18, 2017
Oct 18, 2017
8
9
10
11
12
13
14
resample.c
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
Oct 21, 1999
Oct 21, 1999
15
16
17
#include <math.h>
#include <stdio.h>
Aug 19, 2001
Aug 19, 2001
18
#include <stdlib.h>
Oct 21, 1999
Oct 21, 1999
19
Oct 18, 2017
Oct 18, 2017
20
21
22
23
#include "SDL.h"
#include "timidity.h"
#include "options.h"
Oct 21, 1999
Oct 21, 1999
24
25
26
27
28
29
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "tables.h"
#include "resample.h"
Oct 21, 2017
Oct 21, 2017
30
31
#define PRECALC_LOOP_COUNT(start, end, incr) (((end) - (start) + (incr) - 1) / (incr))
Oct 21, 1999
Oct 21, 1999
32
33
/*************** resampling with fixed increment *****************/
Oct 18, 2017
Oct 18, 2017
34
static sample_t *rs_plain(MidiSong *song, int v, Sint32 *countptr)
Oct 21, 1999
Oct 21, 1999
35
36
37
38
{
/* Play sample until end, then free the voice. */
Oct 18, 2017
Oct 18, 2017
39
sample_t v1, v2;
Oct 21, 1999
Oct 21, 1999
40
Voice
Oct 18, 2017
Oct 18, 2017
41
*vp=&(song->voice[v]);
Oct 21, 1999
Oct 21, 1999
42
sample_t
Oct 18, 2017
Oct 18, 2017
43
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
44
*src=vp->sample->data;
Oct 18, 2017
Oct 18, 2017
45
Sint32
Oct 21, 1999
Oct 21, 1999
46
47
48
49
ofs=vp->sample_offset,
incr=vp->sample_increment,
le=vp->sample->data_length,
count=*countptr;
Oct 21, 2017
Oct 21, 2017
50
Sint32 i, j;
Oct 21, 1999
Oct 21, 1999
51
52
53
54
55
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
/* Precalc how many times we should go through the loop.
NOTE: Assumes that incr > 0 and that ofs <= le */
Oct 21, 2017
Oct 21, 2017
56
i = PRECALC_LOOP_COUNT(ofs, le, incr);
Oct 21, 1999
Oct 21, 1999
57
58
59
60
61
62
63
64
if (i > count)
{
i = count;
count = 0;
}
else count -= i;
Oct 21, 2017
Oct 21, 2017
65
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
66
{
Oct 18, 2017
Oct 18, 2017
67
68
69
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
70
71
72
73
74
ofs += incr;
}
if (ofs >= le)
{
Oct 18, 2017
Oct 18, 2017
75
if (ofs == le)
Oct 21, 2017
Oct 21, 2017
76
*dest++ = src[(ofs>>FRACTION_BITS)-1]/2;
Oct 21, 1999
Oct 21, 1999
77
78
79
80
81
vp->status=VOICE_FREE;
*countptr-=count+1;
}
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
82
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
83
84
}
Oct 18, 2017
Oct 18, 2017
85
static sample_t *rs_loop(MidiSong *song, Voice *vp, Sint32 count)
Oct 21, 1999
Oct 21, 1999
86
87
88
89
{
/* Play sample until end-of-loop, skip back and continue. */
Oct 18, 2017
Oct 18, 2017
90
91
sample_t v1, v2;
Sint32
Oct 21, 1999
Oct 21, 1999
92
93
94
95
96
ofs=vp->sample_offset,
incr=vp->sample_increment,
le=vp->sample->loop_end,
ll=le - vp->sample->loop_start;
sample_t
Oct 18, 2017
Oct 18, 2017
97
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
98
*src=vp->sample->data;
Oct 21, 2017
Oct 21, 2017
99
Sint32 i, j;
Oct 18, 2017
Oct 18, 2017
100
Oct 21, 1999
Oct 21, 1999
101
102
while (count)
{
Oct 21, 2017
Oct 21, 2017
103
while (ofs >= le)
Oct 21, 1999
Oct 21, 1999
104
105
ofs -= ll;
/* Precalc how many times we should go through the loop */
Oct 21, 2017
Oct 21, 2017
106
i = PRECALC_LOOP_COUNT(ofs, le, incr);
Oct 21, 1999
Oct 21, 1999
107
108
109
110
111
112
if (i > count)
{
i = count;
count = 0;
}
else count -= i;
Oct 21, 2017
Oct 21, 2017
113
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
114
{
Oct 18, 2017
Oct 18, 2017
115
116
117
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
118
119
120
121
122
ofs += incr;
}
}
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
123
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
124
125
}
Oct 18, 2017
Oct 18, 2017
126
static sample_t *rs_bidir(MidiSong *song, Voice *vp, Sint32 count)
Oct 21, 1999
Oct 21, 1999
127
{
Oct 18, 2017
Oct 18, 2017
128
129
sample_t v1, v2;
Sint32
Oct 21, 1999
Oct 21, 1999
130
131
132
133
134
ofs=vp->sample_offset,
incr=vp->sample_increment,
le=vp->sample->loop_end,
ls=vp->sample->loop_start;
sample_t
Oct 18, 2017
Oct 18, 2017
135
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
136
*src=vp->sample->data;
Oct 18, 2017
Oct 18, 2017
137
Sint32
Oct 21, 2017
Oct 21, 2017
138
le2 = le<<1,
Oct 21, 1999
Oct 21, 1999
139
ls2 = ls<<1,
Oct 21, 2017
Oct 21, 2017
140
i, j;
Oct 21, 1999
Oct 21, 1999
141
142
/* Play normally until inside the loop region */
Oct 21, 2017
Oct 21, 2017
143
if (incr > 0 && ofs < ls)
Oct 21, 1999
Oct 21, 1999
144
145
146
147
{
/* NOTE: Assumes that incr > 0, which is NOT always the case
when doing bidirectional looping. I have yet to see a case
where both ofs <= ls AND incr < 0, however. */
Oct 21, 2017
Oct 21, 2017
148
i = PRECALC_LOOP_COUNT(ofs, ls, incr);
Oct 21, 1999
Oct 21, 1999
149
150
151
152
153
154
if (i > count)
{
i = count;
count = 0;
}
else count -= i;
Oct 21, 2017
Oct 21, 2017
155
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
156
{
Oct 18, 2017
Oct 18, 2017
157
158
159
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
160
161
162
163
164
165
166
167
168
ofs += incr;
}
}
/* Then do the bidirectional looping */
while(count)
{
/* Precalc how many times we should go through the loop */
Oct 21, 2017
Oct 21, 2017
169
i = PRECALC_LOOP_COUNT(ofs, incr > 0 ? le : ls, incr);
Oct 21, 1999
Oct 21, 1999
170
171
172
173
174
175
if (i > count)
{
i = count;
count = 0;
}
else count -= i;
Oct 21, 2017
Oct 21, 2017
176
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
177
{
Oct 18, 2017
Oct 18, 2017
178
179
180
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
ofs += incr;
}
if (ofs>=le)
{
/* fold the overshoot back in */
ofs = le2 - ofs;
incr *= -1;
}
else if (ofs <= ls)
{
ofs = ls2 - ofs;
incr *= -1;
}
}
vp->sample_increment=incr;
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
198
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}
/*********************** vibrato versions ***************************/
/* We only need to compute one half of the vibrato sine cycle */
static int vib_phase_to_inc_ptr(int phase)
{
if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
else
return phase-VIBRATO_SAMPLE_INCREMENTS/2;
}
Oct 18, 2017
Oct 18, 2017
214
static Sint32 update_vibrato(MidiSong *song, Voice *vp, int sign)
Oct 21, 1999
Oct 21, 1999
215
{
Oct 18, 2017
Oct 18, 2017
216
Sint32 depth;
Oct 21, 1999
Oct 21, 1999
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
int phase, pb;
double a;
if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
vp->vibrato_phase=0;
phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
if (vp->vibrato_sample_increment[phase])
{
if (sign)
return -vp->vibrato_sample_increment[phase];
else
return vp->vibrato_sample_increment[phase];
}
/* Need to compute this sample increment. */
depth=vp->sample->vibrato_depth<<7;
if (vp->vibrato_sweep)
{
/* Need to update sweep */
vp->vibrato_sweep_position += vp->vibrato_sweep;
if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
vp->vibrato_sweep=0;
else
{
/* Adjust depth */
depth *= vp->vibrato_sweep_position;
depth >>= SWEEP_SHIFT;
}
}
a = FSCALE(((double)(vp->sample->sample_rate) *
(double)(vp->frequency)) /
((double)(vp->sample->root_freq) *
Oct 18, 2017
Oct 18, 2017
253
(double)(song->rate)),
Oct 21, 1999
Oct 21, 1999
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
FRACTION_BITS);
pb=(int)((sine(vp->vibrato_phase *
(SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
* (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
if (pb<0)
{
pb=-pb;
a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
}
else
a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
/* If the sweep's over, we can store the newly computed sample_increment */
if (!vp->vibrato_sweep)
Oct 18, 2017
Oct 18, 2017
270
vp->vibrato_sample_increment[phase]=(Sint32) a;
Oct 21, 1999
Oct 21, 1999
271
272
273
274
if (sign)
a = -a; /* need to preserve the loop direction */
Oct 18, 2017
Oct 18, 2017
275
return (Sint32) a;
Oct 21, 1999
Oct 21, 1999
276
277
}
Oct 18, 2017
Oct 18, 2017
278
static sample_t *rs_vib_plain(MidiSong *song, int v, Sint32 *countptr)
Oct 21, 1999
Oct 21, 1999
279
280
281
282
{
/* Play sample until end, then free the voice. */
Oct 18, 2017
Oct 18, 2017
283
284
sample_t v1, v2;
Voice *vp=&(song->voice[v]);
Oct 21, 1999
Oct 21, 1999
285
sample_t
Oct 18, 2017
Oct 18, 2017
286
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
287
*src=vp->sample->data;
Oct 18, 2017
Oct 18, 2017
288
Sint32
Oct 21, 1999
Oct 21, 1999
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
le=vp->sample->data_length,
ofs=vp->sample_offset,
incr=vp->sample_increment,
count=*countptr;
int
cc=vp->vibrato_control_counter;
/* This has never been tested */
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
while (count--)
{
if (!cc--)
{
cc=vp->vibrato_control_ratio;
Oct 18, 2017
Oct 18, 2017
305
incr=update_vibrato(song, vp, 0);
Oct 21, 1999
Oct 21, 1999
306
}
Oct 18, 2017
Oct 18, 2017
307
308
309
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
310
311
312
ofs += incr;
if (ofs >= le)
{
Oct 18, 2017
Oct 18, 2017
313
if (ofs == le)
Oct 21, 2017
Oct 21, 2017
314
*dest++ = src[(ofs>>FRACTION_BITS)-1]/2;
Oct 21, 1999
Oct 21, 1999
315
316
317
318
319
320
321
322
323
vp->status=VOICE_FREE;
*countptr-=count+1;
break;
}
}
vp->vibrato_control_counter=cc;
vp->sample_increment=incr;
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
324
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
325
326
}
Oct 18, 2017
Oct 18, 2017
327
static sample_t *rs_vib_loop(MidiSong *song, Voice *vp, Sint32 count)
Oct 21, 1999
Oct 21, 1999
328
329
330
331
{
/* Play sample until end-of-loop, skip back and continue. */
Oct 18, 2017
Oct 18, 2017
332
333
sample_t v1, v2;
Sint32
Oct 21, 1999
Oct 21, 1999
334
335
336
337
338
ofs=vp->sample_offset,
incr=vp->sample_increment,
le=vp->sample->loop_end,
ll=le - vp->sample->loop_start;
sample_t
Oct 18, 2017
Oct 18, 2017
339
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
340
341
342
*src=vp->sample->data;
int
cc=vp->vibrato_control_counter;
Oct 21, 2017
Oct 21, 2017
343
Sint32 i, j;
Oct 21, 1999
Oct 21, 1999
344
345
346
347
348
349
int
vibflag=0;
while (count)
{
/* Hopefully the loop is longer than an increment */
Oct 21, 2017
Oct 21, 2017
350
while(ofs >= le)
Oct 21, 1999
Oct 21, 1999
351
352
353
ofs -= ll;
/* Precalc how many times to go through the loop, taking
the vibrato control ratio into account this time. */
Oct 21, 2017
Oct 21, 2017
354
i = PRECALC_LOOP_COUNT(ofs, le, incr);
Oct 21, 1999
Oct 21, 1999
355
356
357
358
359
360
361
362
if(i > count) i = count;
if(i > cc)
{
i = cc;
vibflag = 1;
}
else cc -= i;
count -= i;
Oct 21, 2017
Oct 21, 2017
363
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
364
{
Oct 18, 2017
Oct 18, 2017
365
366
367
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
368
369
370
371
372
ofs += incr;
}
if(vibflag)
{
cc = vp->vibrato_control_ratio;
Oct 18, 2017
Oct 18, 2017
373
incr = update_vibrato(song, vp, 0);
Oct 21, 1999
Oct 21, 1999
374
375
376
377
378
379
380
vibflag = 0;
}
}
vp->vibrato_control_counter=cc;
vp->sample_increment=incr;
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
381
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
382
383
}
Oct 18, 2017
Oct 18, 2017
384
static sample_t *rs_vib_bidir(MidiSong *song, Voice *vp, Sint32 count)
Oct 21, 1999
Oct 21, 1999
385
{
Oct 18, 2017
Oct 18, 2017
386
387
sample_t v1, v2;
Sint32
Oct 21, 1999
Oct 21, 1999
388
389
390
391
392
ofs=vp->sample_offset,
incr=vp->sample_increment,
le=vp->sample->loop_end,
ls=vp->sample->loop_start;
sample_t
Oct 18, 2017
Oct 18, 2017
393
*dest=song->resample_buffer,
Oct 21, 1999
Oct 21, 1999
394
395
396
*src=vp->sample->data;
int
cc=vp->vibrato_control_counter;
Oct 18, 2017
Oct 18, 2017
397
Sint32
Oct 21, 1999
Oct 21, 1999
398
399
le2=le<<1,
ls2=ls<<1,
Oct 21, 2017
Oct 21, 2017
400
i, j;
Oct 21, 1999
Oct 21, 1999
401
402
403
404
int
vibflag = 0;
/* Play normally until inside the loop region */
Oct 21, 2017
Oct 21, 2017
405
while (count && incr > 0 && ofs < ls)
Oct 21, 1999
Oct 21, 1999
406
{
Oct 21, 2017
Oct 21, 2017
407
i = PRECALC_LOOP_COUNT(ofs, ls, incr);
Oct 21, 1999
Oct 21, 1999
408
409
410
411
412
413
414
415
if (i > count) i = count;
if (i > cc)
{
i = cc;
vibflag = 1;
}
else cc -= i;
count -= i;
Oct 21, 2017
Oct 21, 2017
416
for (j = 0; j < i; j++)
Oct 21, 1999
Oct 21, 1999
417
{
Oct 18, 2017
Oct 18, 2017
418
419
420
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
421
422
423
424
425
ofs += incr;
}
if (vibflag)
{
cc = vp->vibrato_control_ratio;
Oct 18, 2017
Oct 18, 2017
426
incr = update_vibrato(song, vp, 0);
Oct 21, 1999
Oct 21, 1999
427
428
429
430
431
432
433
434
435
vibflag = 0;
}
}
/* Then do the bidirectional looping */
while (count)
{
/* Precalc how many times we should go through the loop */
Oct 21, 2017
Oct 21, 2017
436
i = PRECALC_LOOP_COUNT(ofs, incr > 0 ? le : ls, incr);
Oct 21, 1999
Oct 21, 1999
437
438
439
440
441
442
443
444
445
446
if(i > count) i = count;
if(i > cc)
{
i = cc;
vibflag = 1;
}
else cc -= i;
count -= i;
while (i--)
{
Oct 18, 2017
Oct 18, 2017
447
448
449
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS)+1];
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
Oct 21, 1999
Oct 21, 1999
450
451
452
453
454
ofs += incr;
}
if (vibflag)
{
cc = vp->vibrato_control_ratio;
Oct 18, 2017
Oct 18, 2017
455
incr = update_vibrato(song, vp, (incr < 0));
Oct 21, 1999
Oct 21, 1999
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
vibflag = 0;
}
if (ofs >= le)
{
/* fold the overshoot back in */
ofs = le2 - ofs;
incr *= -1;
}
else if (ofs <= ls)
{
ofs = ls2 - ofs;
incr *= -1;
}
}
vp->vibrato_control_counter=cc;
vp->sample_increment=incr;
vp->sample_offset=ofs; /* Update offset */
Oct 18, 2017
Oct 18, 2017
474
return song->resample_buffer;
Oct 21, 1999
Oct 21, 1999
475
476
}
Oct 18, 2017
Oct 18, 2017
477
sample_t *resample_voice(MidiSong *song, int v, Sint32 *countptr)
Oct 21, 1999
Oct 21, 1999
478
{
Oct 18, 2017
Oct 18, 2017
479
480
481
Sint32 ofs;
Uint8 modes;
Voice *vp=&(song->voice[v]);
Oct 21, 1999
Oct 21, 1999
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
if (!(vp->sample->sample_rate))
{
/* Pre-resampled data -- just update the offset and check if
we're out of data. */
ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
FRACTION_BITS here... */
if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
{
/* Note finished. Free the voice. */
vp->status = VOICE_FREE;
/* Let the caller know how much data we had left */
*countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
}
else
vp->sample_offset += *countptr << FRACTION_BITS;
Oct 18, 2017
Oct 18, 2017
500
return vp->sample->data+ofs;
Oct 21, 1999
Oct 21, 1999
501
502
503
504
505
506
507
508
509
510
511
512
}
/* Need to resample. Use the proper function. */
modes=vp->sample->modes;
if (vp->vibrato_control_ratio)
{
if ((modes & MODES_LOOPING) &&
((modes & MODES_ENVELOPE) ||
(vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
{
if (modes & MODES_PINGPONG)
Oct 18, 2017
Oct 18, 2017
513
return rs_vib_bidir(song, vp, *countptr);
Oct 21, 1999
Oct 21, 1999
514
else
Oct 18, 2017
Oct 18, 2017
515
return rs_vib_loop(song, vp, *countptr);
Oct 21, 1999
Oct 21, 1999
516
517
}
else
Oct 18, 2017
Oct 18, 2017
518
return rs_vib_plain(song, v, countptr);
Oct 21, 1999
Oct 21, 1999
519
520
521
522
523
524
525
526
}
else
{
if ((modes & MODES_LOOPING) &&
((modes & MODES_ENVELOPE) ||
(vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
{
if (modes & MODES_PINGPONG)
Oct 18, 2017
Oct 18, 2017
527
return rs_bidir(song, vp, *countptr);
Oct 21, 1999
Oct 21, 1999
528
else
Oct 18, 2017
Oct 18, 2017
529
return rs_loop(song, vp, *countptr);
Oct 21, 1999
Oct 21, 1999
530
531
}
else
Oct 18, 2017
Oct 18, 2017
532
return rs_plain(song, v, countptr);
Oct 21, 1999
Oct 21, 1999
533
534
535
}
}
Oct 18, 2017
Oct 18, 2017
536
void pre_resample(MidiSong *song, Sample *sp)
Oct 21, 1999
Oct 21, 1999
537
538
{
double a, xdiff;
Oct 18, 2017
Oct 18, 2017
539
Sint32 incr, ofs, newlen, count;
Oct 21, 2017
Oct 21, 2017
540
541
Sint16 *newdata, *dest, *src = (Sint16 *) sp->data, *vptr;
Sint32 v, v1, v2, v3, v4, v5, i;
Oct 18, 2017
Oct 18, 2017
542
#ifdef DEBUG_CHATTER
Oct 21, 1999
Oct 21, 1999
543
544
545
546
547
static const char note_name[12][3] =
{
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
};
Oct 18, 2017
Oct 18, 2017
548
549
550
551
SNDDBG((" * pre-resampling for note %d (%s%d)\n",
sp->note_to_use,
note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12));
#endif
Oct 21, 1999
Oct 21, 1999
552
Oct 21, 2017
Oct 21, 2017
553
554
555
556
557
558
a = ((double) (sp->root_freq) * song->rate) /
((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]);
if(sp->data_length * a >= 0x7fffffffL) { /* Too large to compute */
SNDDBG((" *** Can't pre-resampling for note %d\n", sp->note_to_use));
return;
}
Oct 21, 1999
Oct 21, 1999
559
Oct 21, 2017
Oct 21, 2017
560
newlen = (Sint32)(sp->data_length * a);
Oct 21, 1999
Oct 21, 1999
561
562
563
count = (newlen >> FRACTION_BITS) - 1;
ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
Oct 21, 2017
Oct 21, 2017
564
565
566
567
568
569
570
571
572
if((double)newlen + incr >= 0x7fffffffL) { /* Too large to compute */
SNDDBG((" *** Can't pre-resampling for note %d\n", sp->note_to_use));
return;
}
dest = newdata = (Sint16 *) safe_malloc((newlen >> (FRACTION_BITS - 1)) + 2);
if (!dest)
return;
Oct 21, 1999
Oct 21, 1999
573
574
575
576
577
if (--count)
*dest++ = src[0];
/* Since we're pre-processing and this doesn't have to be done in
real-time, we go ahead and do the full sliding cubic interpolation. */
Oct 21, 2017
Oct 21, 2017
578
579
count--;
for(i = 0; i < count; i++)
Oct 21, 1999
Oct 21, 1999
580
581
{
vptr = src + (ofs >> FRACTION_BITS);
Oct 21, 2017
Oct 21, 2017
582
v1 = ((vptr>=src+1)? *(vptr - 1):0);
Oct 21, 1999
Oct 21, 1999
583
584
585
v2 = *vptr;
v3 = *(vptr + 1);
v4 = *(vptr + 2);
Oct 21, 2017
Oct 21, 2017
586
v5 = v2 - v3;
Oct 21, 1999
Oct 21, 1999
587
xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
Oct 21, 2017
Oct 21, 2017
588
589
590
v = (Sint32)(v2 + xdiff * (1.0/6.0) * (3 * (v3 - v5) - 2 * v1 - v4 +
xdiff * (3 * (v1 - v2 - v5) + xdiff * (3 * v5 + v4 - v1))));
*dest++ = (Sint16)((v > 32767) ? 32767 : ((v < -32768) ? -32768 : v));
Oct 21, 1999
Oct 21, 1999
591
592
593
594
595
596
597
ofs += incr;
}
if (ofs & FRACTION_MASK)
{
v1 = src[ofs >> FRACTION_BITS];
v2 = src[(ofs >> FRACTION_BITS) + 1];
Oct 21, 2017
Oct 21, 2017
598
*dest++ = (Sint16)(v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
Oct 21, 1999
Oct 21, 1999
599
600
601
602
}
else
*dest++ = src[ofs >> FRACTION_BITS];
Oct 21, 2017
Oct 21, 2017
603
604
605
606
*dest = *(dest - 1) / 2;
++dest;
*dest = *(dest - 1) / 2;
Oct 21, 1999
Oct 21, 1999
607
sp->data_length = newlen;
Oct 21, 2017
Oct 21, 2017
608
609
sp->loop_start = (Sint32)(sp->loop_start * a);
sp->loop_end = (Sint32)(sp->loop_end * a);
Oct 21, 1999
Oct 21, 1999
610
611
612
613
free(sp->data);
sp->data = (sample_t *) newdata;
sp->sample_rate = 0;
}