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