3
TiMidity -- Experimental MIDI to WAVE converter
4
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36
#ifdef LINEAR_INTERPOLATION
37
# if defined(LOOKUP_HACK) && defined(LOOKUP_INTERPOLATION)
38
# define RESAMPLATION \
39
v1=src[ofs>>FRACTION_BITS];\
40
v2=src[(ofs>>FRACTION_BITS)+1];\
41
*dest++ = v1 + (iplookup[(((v2-v1)<<5) & 0x03FE0) | \
42
((ofs & FRACTION_MASK) >> (FRACTION_BITS-5))]);
44
# define RESAMPLATION \
45
v1=src[ofs>>FRACTION_BITS];\
46
v2=src[(ofs>>FRACTION_BITS)+1];\
47
*dest++ = v1 + (((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
49
# define INTERPVARS sample_t v1, v2
51
/* Earplugs recommended for maximum listening enjoyment */
52
# define RESAMPLATION *dest++=src[ofs>>FRACTION_BITS];
56
#define FINALINTERP if (ofs == le) *dest++=src[ofs>>FRACTION_BITS];
57
/* So it isn't interpolation. At least it's final. */
59
extern sample_t *resample_buffer;
61
/*************** resampling with fixed increment *****************/
63
static sample_t *rs_plain(int v, int32 *countptr)
66
/* Play sample until end, then free the voice. */
72
*dest=resample_buffer,
73
*src=vp->sample->data;
75
ofs=vp->sample_offset,
76
incr=vp->sample_increment,
77
le=vp->sample->data_length,
83
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
85
/* Precalc how many times we should go through the loop.
86
NOTE: Assumes that incr > 0 and that ofs <= le */
87
i = (le - ofs) / incr + 1;
105
vp->status=VOICE_FREE;
110
#else /* PRECALC_LOOPS */
118
vp->status=VOICE_FREE;
124
#endif /* PRECALC_LOOPS */
126
vp->sample_offset=ofs; /* Update offset */
127
return resample_buffer;
130
static sample_t *rs_loop(Voice *vp, int32 count)
133
/* Play sample until end-of-loop, skip back and continue. */
137
ofs=vp->sample_offset,
138
incr=vp->sample_increment,
139
le=vp->sample->loop_end,
140
ll=le - vp->sample->loop_start;
142
*dest=resample_buffer,
143
*src=vp->sample->data;
151
/* NOTE: Assumes that ll > incr and that incr > 0. */
153
/* Precalc how many times we should go through the loop */
154
i = (le - ofs) / incr + 1;
173
ofs -= ll; /* Hopefully the loop is longer than an increment. */
177
vp->sample_offset=ofs; /* Update offset */
178
return resample_buffer;
181
static sample_t *rs_bidir(Voice *vp, int32 count)
185
ofs=vp->sample_offset,
186
incr=vp->sample_increment,
187
le=vp->sample->loop_end,
188
ls=vp->sample->loop_start;
190
*dest=resample_buffer,
191
*src=vp->sample->data;
198
/* Play normally until inside the loop region */
202
/* NOTE: Assumes that incr > 0, which is NOT always the case
203
when doing bidirectional looping. I have yet to see a case
204
where both ofs <= ls AND incr < 0, however. */
205
i = (ls - ofs) / incr + 1;
219
/* Then do the bidirectional looping */
223
/* Precalc how many times we should go through the loop */
224
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
238
/* fold the overshoot back in */
249
#else /* PRECALC_LOOPS */
250
/* Play normally until inside the loop region */
263
/* Then do the bidirectional looping */
272
/* fold the overshoot back in */
273
ofs = le - (ofs - le);
278
ofs = ls + (ls - ofs);
282
#endif /* PRECALC_LOOPS */
283
vp->sample_increment=incr;
284
vp->sample_offset=ofs; /* Update offset */
285
return resample_buffer;
288
/*********************** vibrato versions ***************************/
290
/* We only need to compute one half of the vibrato sine cycle */
291
static int vib_phase_to_inc_ptr(int phase)
293
if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
294
return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
295
else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
296
return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
298
return phase-VIBRATO_SAMPLE_INCREMENTS/2;
301
static int32 update_vibrato(Voice *vp, int sign)
307
if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
309
phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
311
if (vp->vibrato_sample_increment[phase])
314
return -vp->vibrato_sample_increment[phase];
316
return vp->vibrato_sample_increment[phase];
319
/* Need to compute this sample increment. */
321
depth=vp->sample->vibrato_depth<<7;
323
if (vp->vibrato_sweep)
325
/* Need to update sweep */
326
vp->vibrato_sweep_position += vp->vibrato_sweep;
327
if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
332
depth *= vp->vibrato_sweep_position;
333
depth >>= SWEEP_SHIFT;
337
a = FSCALE(((double)(vp->sample->sample_rate) *
338
(double)(vp->frequency)) /
339
((double)(vp->sample->root_freq) *
340
(double)(play_mode->rate)),
343
pb=(int)((sine(vp->vibrato_phase *
344
(SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
345
* (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
350
a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
353
a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
355
/* If the sweep's over, we can store the newly computed sample_increment */
356
if (!vp->vibrato_sweep)
357
vp->vibrato_sample_increment[phase]=(int32) a;
360
a = -a; /* need to preserve the loop direction */
365
static sample_t *rs_vib_plain(int v, int32 *countptr)
368
/* Play sample until end, then free the voice. */
373
*dest=resample_buffer,
374
*src=vp->sample->data;
376
le=vp->sample->data_length,
377
ofs=vp->sample_offset,
378
incr=vp->sample_increment,
381
cc=vp->vibrato_control_counter;
383
/* This has never been tested */
385
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
391
cc=vp->vibrato_control_ratio;
392
incr=update_vibrato(vp, 0);
399
vp->status=VOICE_FREE;
406
vp->vibrato_control_counter=cc;
407
vp->sample_increment=incr;
408
vp->sample_offset=ofs; /* Update offset */
409
return resample_buffer;
412
static sample_t *rs_vib_loop(Voice *vp, int32 count)
415
/* Play sample until end-of-loop, skip back and continue. */
419
ofs=vp->sample_offset,
420
incr=vp->sample_increment,
421
le=vp->sample->loop_end,
422
ll=le - vp->sample->loop_start;
424
*dest=resample_buffer,
425
*src=vp->sample->data;
427
cc=vp->vibrato_control_counter;
436
/* Hopefully the loop is longer than an increment */
439
/* Precalc how many times to go through the loop, taking
440
the vibrato control ratio into account this time. */
441
i = (le - ofs) / incr + 1;
442
if(i > count) i = count;
457
cc = vp->vibrato_control_ratio;
458
incr = update_vibrato(vp, 0);
463
#else /* PRECALC_LOOPS */
468
cc=vp->vibrato_control_ratio;
469
incr=update_vibrato(vp, 0);
474
ofs -= ll; /* Hopefully the loop is longer than an increment. */
476
#endif /* PRECALC_LOOPS */
478
vp->vibrato_control_counter=cc;
479
vp->sample_increment=incr;
480
vp->sample_offset=ofs; /* Update offset */
481
return resample_buffer;
484
static sample_t *rs_vib_bidir(Voice *vp, int32 count)
488
ofs=vp->sample_offset,
489
incr=vp->sample_increment,
490
le=vp->sample->loop_end,
491
ls=vp->sample->loop_start;
493
*dest=resample_buffer,
494
*src=vp->sample->data;
496
cc=vp->vibrato_control_counter;
506
/* Play normally until inside the loop region */
507
while (count && (ofs <= ls))
509
i = (ls - ofs) / incr + 1;
510
if (i > count) i = count;
525
cc = vp->vibrato_control_ratio;
526
incr = update_vibrato(vp, 0);
531
/* Then do the bidirectional looping */
535
/* Precalc how many times we should go through the loop */
536
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
537
if(i > count) i = count;
552
cc = vp->vibrato_control_ratio;
553
incr = update_vibrato(vp, (incr < 0));
558
/* fold the overshoot back in */
569
#else /* PRECALC_LOOPS */
570
/* Play normally until inside the loop region */
578
cc=vp->vibrato_control_ratio;
579
incr=update_vibrato(vp, 0);
588
/* Then do the bidirectional looping */
595
cc=vp->vibrato_control_ratio;
596
incr=update_vibrato(vp, (incr < 0));
602
/* fold the overshoot back in */
603
ofs = le - (ofs - le);
608
ofs = ls + (ls - ofs);
612
#endif /* PRECALC_LOOPS */
614
vp->vibrato_control_counter=cc;
615
vp->sample_increment=incr;
616
vp->sample_offset=ofs; /* Update offset */
617
return resample_buffer;
620
sample_t *resample_voice(int v, int32 *countptr)
626
if (!(vp->sample->sample_rate))
628
/* Pre-resampled data -- just update the offset and check if
629
we're out of data. */
630
ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
631
FRACTION_BITS here... */
632
if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
634
/* Note finished. Free the voice. */
635
vp->status = VOICE_FREE;
638
/* Let the caller know how much data we had left */
639
*countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
642
vp->sample_offset += *countptr << FRACTION_BITS;
644
return vp->sample->data+ofs;
647
/* Need to resample. Use the proper function. */
648
modes=vp->sample->modes;
650
if (vp->vibrato_control_ratio)
652
if ((modes & MODES_LOOPING) &&
653
((modes & MODES_ENVELOPE) ||
654
(vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
656
if (modes & MODES_PINGPONG)
657
return rs_vib_bidir(vp, *countptr);
659
return rs_vib_loop(vp, *countptr);
662
return rs_vib_plain(v, countptr);
666
if ((modes & MODES_LOOPING) &&
667
((modes & MODES_ENVELOPE) ||
668
(vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
670
if (modes & MODES_PINGPONG)
671
return rs_bidir(vp, *countptr);
673
return rs_loop(vp, *countptr);
676
return rs_plain(v, countptr);
680
void pre_resample(Sample * sp)
683
int32 incr, ofs, newlen, count;
684
int16 *newdata, *dest, *src = (int16 *) sp->data;
685
int16 v1, v2, v3, v4, *vptr;
686
static const char note_name[12][3] =
688
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
691
ctl->cmsg(CMSG_INFO, VERB_NOISY, " * pre-resampling for note %d (%s%d)",
693
note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
695
a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
696
((double) (sp->root_freq) * play_mode->rate);
697
newlen = (int32)(sp->data_length / a);
698
dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1));
700
count = (newlen >> FRACTION_BITS) - 1;
701
ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
706
/* Since we're pre-processing and this doesn't have to be done in
707
real-time, we go ahead and do the full sliding cubic interpolation. */
710
vptr = src + (ofs >> FRACTION_BITS);
715
xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
716
*dest++ = (int16)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
717
xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4))));
721
if (ofs & FRACTION_MASK)
723
v1 = src[ofs >> FRACTION_BITS];
724
v2 = src[(ofs >> FRACTION_BITS) + 1];
725
*dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
728
*dest++ = src[ofs >> FRACTION_BITS];
730
sp->data_length = newlen;
731
sp->loop_start = (int32)(sp->loop_start / a);
732
sp->loop_end = (int32)(sp->loop_end / a);
734
sp->data = (sample_t *) newdata;