2
$Id: resample_l.cpp,v 1.3 2001/04/12 09:09:23 garbanzo Exp $
4
TiMidity++ -- MIDI to WAVE converter and player
5
Copyright (C) 1999 Masanao Izumo <mo@goice.co.jp>
6
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38
#define LINEAR_INTERPOLATION
39
#define LAGRANGE_INTERPOLATION
40
#define ENVELOPE_PITCH_MODULATION
44
#define MAX_DATAVAL 127
45
#define MIN_DATAVAL -128
47
#define MAX_DATAVAL 32767
48
#define MIN_DATAVAL -32768
51
#define OVERSHOOT_STEP 50
54
static sample_t *vib_resample_voice(int, uint32 *, int);
55
static sample_t *normal_resample_voice(int, uint32 *, int);
58
#ifdef FILTER_INTERPOLATION
59
static void update_lfo(int v)
61
FLOAT_T depth=voice[v].modLfoToFilterFc;
63
if (voice[v].lfo_sweep)
65
/* Update sweep position */
67
voice[v].lfo_sweep_position += voice[v].lfo_sweep;
68
if (voice[v].lfo_sweep_position>=(1<<SWEEP_SHIFT))
69
voice[v].lfo_sweep=0; /* Swept to max amplitude */
72
/* Need to adjust depth */
73
depth *= (FLOAT_T)voice[v].lfo_sweep_position / (FLOAT_T)(1<<SWEEP_SHIFT);
77
voice[v].lfo_phase += voice[v].lfo_phase_increment;
79
voice[v].lfo_volume = depth;
84
#ifdef FILTER_INTERPOLATION
85
static int calc_bw_index(int v)
87
FLOAT_T mod_amount=voice[v].modEnvToFilterFc;
88
int32 freq = voice[v].sample->cutoff_freq;
91
if (voice[v].lfo_phase_increment) update_lfo(v);
93
if (!voice[v].lfo_phase_increment && update_modulation_signal(v)) return 0;
95
/* printf("mod_amount %f ", mod_amount); */
96
if (voice[v].lfo_volume>0.001) {
97
if (mod_amount) mod_amount *= voice[v].lfo_volume;
98
else mod_amount = voice[v].lfo_volume;
99
/* printf("lfo %f -> mod %f ", voice[v].lfo_volume, mod_amount); */
102
if (mod_amount > 0.001) {
103
if (voice[v].modulation_volume)
105
(int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (voice[v].modulation_volume>>22) / 255.0) );
106
else freq = (int32)( (double)freq*mod_amount );
108
printf("v%d freq %d (was %d), modvol %d, mod_amount %f\n", v, (int)freq, (int)voice[v].sample->cutoff_freq,
109
(int)voice[v].modulation_volume>>22,
112
ix = 1 + (freq+50) / 100;
113
if (ix > 100) ix = 100;
114
voice[v].bw_index = ix;
117
voice[v].bw_index = 1 + (freq+50) / 100;
122
/*************** resampling with fixed increment *****************/
124
static sample_t *rs_plain(int v, uint32 *countptr)
126
/* Play sample until end, then free the voice. */
129
int32 ofsd, v0, v1, v2, v3, overshoot;
131
uint32 cc_count=vp->modulation_counter;
133
*dest=resample_buffer+resample_buffer_offset,
134
*src=vp->sample->data;
136
incr=vp->sample_increment;
138
ofs=vp->sample_offset;
139
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
142
le=vp->sample->data_length;
143
#endif /* LAGRANGE_INTERPOLATION */
145
se=vp->sample->data_length;
149
if (!incr) return resample_buffer+resample_buffer_offset; /* --gl */
151
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
152
if (overshoot < 0) overshoot = -overshoot;
157
offset = ofs >> FRACTION_BITS;
160
int32 delta = (ofs - se)>>FRACTION_BITS ;
161
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
162
v1 -= (delta+1) * v1 / overshoot;
164
else v1 = (int32)src[offset];
166
if (ofs + (1L<<FRACTION_BITS) >= se) {
169
else v2 = (int32)src[offset+1];
172
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
173
((ofs+(2L<<FRACTION_BITS)) > le) ) {
174
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
177
v0 = (int32)src[offset-1];
178
v3 = (int32)src[offset+2];
179
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
180
v1 = v1*ofsd>>FRACTION_BITS;
181
v2 = v2*ofsd>>FRACTION_BITS;
182
v3 = v3*ofsd>>FRACTION_BITS;
183
ofsd -= (1L << FRACTION_BITS);
184
v0 = v0*ofsd>>FRACTION_BITS;
185
v2 = v2*ofsd>>FRACTION_BITS;
186
v3 = v3*ofsd>>FRACTION_BITS;
187
ofsd -= (1L << FRACTION_BITS);
188
v0 = v0*ofsd>>FRACTION_BITS;
189
v1 = v1*ofsd>>FRACTION_BITS;
191
ofsd -= (1L << FRACTION_BITS);
192
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
193
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
195
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
199
cc_count = control_ratio - 1;
200
if (!update_modulation_signal(v))
201
incr = calc_mod_freq(v, incr);
204
if (ofs >= se + (overshoot << FRACTION_BITS))
206
if (!(vp->status&VOICE_FREE))
208
vp->status=VOICE_FREE;
216
vp->sample_offset=ofs; /* Update offset */
217
vp->modulation_counter=cc_count;
218
return resample_buffer+resample_buffer_offset;
221
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
223
/* Play sample until end-of-loop, skip back and continue. */
224
int32 ofsd, v0, v1, v2, v3, overshoot;
226
uint32 cc_count=vp->modulation_counter;
228
incr=vp->sample_increment;
230
ofs=vp->sample_offset;
233
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
235
#endif /* LAGRANGE_INTERPOLATION */
236
ll=le - vp->loop_start;
238
*dest=resample_buffer+resample_buffer_offset,
239
*src=vp->sample->data;
241
se=vp->sample->data_length,
248
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
249
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
250
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
252
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
253
if (overshoot < 0) overshoot = -overshoot;
258
offset = ofs >> FRACTION_BITS;
261
int32 delta = (ofs - se)>>FRACTION_BITS ;
262
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
263
v1 -= (delta+1) * v1 / overshoot;
265
else v1 = (int32)src[offset];
267
if (ofs + (1L<<FRACTION_BITS) >= se) {
270
else v2 = (int32)src[offset+1];
273
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
274
((ofs+(2L<<FRACTION_BITS)) > le) ) {
275
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
278
v0 = (int32)src[offset-1];
279
v3 = (int32)src[offset+2];
280
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
281
v1 = v1*ofsd>>FRACTION_BITS;
282
v2 = v2*ofsd>>FRACTION_BITS;
283
v3 = v3*ofsd>>FRACTION_BITS;
284
ofsd -= (1L << FRACTION_BITS);
285
v0 = v0*ofsd>>FRACTION_BITS;
286
v2 = v2*ofsd>>FRACTION_BITS;
287
v3 = v3*ofsd>>FRACTION_BITS;
288
ofsd -= (1L << FRACTION_BITS);
289
v0 = v0*ofsd>>FRACTION_BITS;
290
v1 = v1*ofsd>>FRACTION_BITS;
292
ofsd -= (1L << FRACTION_BITS);
293
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
294
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
296
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
300
cc_count = control_ratio - 1;
301
if (!update_modulation_signal(v))
302
incr = calc_mod_freq(v, incr);
309
vp->echo_delay -= ll >> FRACTION_BITS;
310
if (vp->echo_delay >= 0) ofs -= ll;
312
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
314
if (ofs >= se + (overshoot << FRACTION_BITS))
316
if (!(vp->status&VOICE_FREE))
318
vp->status=VOICE_FREE;
326
vp->sample_offset=ofs; /* Update offset */
327
vp->modulation_counter=cc_count;
328
return resample_buffer+resample_buffer_offset;
331
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
333
int32 ofsd, v0, v1, v2, v3, overshoot;
336
incr=vp->sample_increment;
341
ofs=vp->sample_offset,
342
se=vp->sample->data_length;
344
*dest=resample_buffer+resample_buffer_offset,
345
*src=vp->sample->data;
348
#ifdef USE_BIDIR_OVERSHOOT
355
/* Play normally until inside the loop region */
357
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
358
if (overshoot < 0) overshoot = -overshoot;
362
/* NOTE: Assumes that incr > 0, which is NOT always the case
363
when doing bidirectional looping. I have yet to see a case
364
where both ofs <= ls AND incr < 0, however. */
365
if (incr < 0) i = ls - ofs;
367
i = (ls - ofs) / incr + 1;
374
for(j = 0; j < i; j++)
377
offset = ofs >> FRACTION_BITS;
380
int32 delta = (ofs - se)>>FRACTION_BITS ;
381
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
382
v1 -= (delta+1) * v1 / overshoot;
384
else v1 = (int32)src[offset];
386
if (ofs + (1L<<FRACTION_BITS) >= se) {
389
else v2 = (int32)src[offset+1];
392
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
393
((ofs+(2L<<FRACTION_BITS)) > le) ) {
394
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
397
v0 = (int32)src[offset-1];
398
v3 = (int32)src[offset+2];
399
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
400
v1 = v1*ofsd>>FRACTION_BITS;
401
v2 = v2*ofsd>>FRACTION_BITS;
402
v3 = v3*ofsd>>FRACTION_BITS;
403
ofsd -= (1L << FRACTION_BITS);
404
v0 = v0*ofsd>>FRACTION_BITS;
405
v2 = v2*ofsd>>FRACTION_BITS;
406
v3 = v3*ofsd>>FRACTION_BITS;
407
ofsd -= (1L << FRACTION_BITS);
408
v0 = v0*ofsd>>FRACTION_BITS;
409
v1 = v1*ofsd>>FRACTION_BITS;
411
ofsd -= (1L << FRACTION_BITS);
412
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
413
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
415
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
422
/* Then do the bidirectional looping */
426
/* Precalc how many times we should go through the loop */
428
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
430
/* fix from M. Izumo */
431
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
439
for(j = 0; j < i && ofs < se; j++)
442
offset = ofs >> FRACTION_BITS;
445
int32 delta = (ofs - se)>>FRACTION_BITS ;
446
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
447
v1 -= (delta+1) * v1 / overshoot;
449
else v1 = (int32)src[offset];
451
if (ofs + (1L<<FRACTION_BITS) >= se) {
454
else v2 = (int32)src[offset+1];
457
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
458
((ofs+(2L<<FRACTION_BITS)) > le) ) {
459
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
462
v0 = (int32)src[offset-1];
463
v3 = (int32)src[offset+2];
464
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
465
v1 = v1*ofsd>>FRACTION_BITS;
466
v2 = v2*ofsd>>FRACTION_BITS;
467
v3 = v3*ofsd>>FRACTION_BITS;
468
ofsd -= (1L << FRACTION_BITS);
469
v0 = v0*ofsd>>FRACTION_BITS;
470
v2 = v2*ofsd>>FRACTION_BITS;
471
v3 = v3*ofsd>>FRACTION_BITS;
472
ofsd -= (1L << FRACTION_BITS);
473
v0 = v0*ofsd>>FRACTION_BITS;
474
v1 = v1*ofsd>>FRACTION_BITS;
476
ofsd -= (1L << FRACTION_BITS);
477
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
478
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
480
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
485
#ifdef USE_BIDIR_OVERSHOOT
488
/* fold the overshoot back in */
502
vp->sample_increment=incr;
503
vp->sample_offset=ofs; /* Update offset */
504
return resample_buffer+resample_buffer_offset;
507
/*********************** vibrato versions ***************************/
509
/* We only need to compute one half of the vibrato sine cycle */
510
static uint32 vib_phase_to_inc_ptr(uint32 phase)
512
if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
513
return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
514
else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
515
return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
517
return phase-VIBRATO_SAMPLE_INCREMENTS/2;
520
static int32 update_vibrato(Voice *vp, int sign)
522
uint32 depth, freq=vp->frequency;
523
#ifdef ENVELOPE_PITCH_MODULATION
524
FLOAT_T mod_amount=vp->sample->modEnvToPitch;
530
if(vp->vibrato_delay > 0)
532
vp->vibrato_delay -= vp->vibrato_control_ratio;
533
if(vp->vibrato_delay > 0)
534
return vp->sample_increment;
537
if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
539
phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
541
if (vp->vibrato_sample_increment[phase])
544
return -vp->vibrato_sample_increment[phase];
546
return vp->vibrato_sample_increment[phase];
549
/* Need to compute this sample increment. */
551
depth = vp->vibrato_depth;
552
if(depth < vp->modulation_wheel)
553
depth = vp->modulation_wheel;
556
if (vp->vibrato_sweep && !vp->modulation_wheel)
558
/* Need to update sweep */
559
vp->vibrato_sweep_position += vp->vibrato_sweep;
560
if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
565
depth *= vp->vibrato_sweep_position;
566
depth >>= SWEEP_SHIFT;
570
#ifdef ENVELOPE_PITCH_MODULATION
571
#ifndef FILTER_INTERPOLATION
572
if (update_modulation_signal(0)) mod_amount = 0;
576
freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
579
pb=(int)((sine(vp->vibrato_phase *
580
(SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
581
* (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
583
a = FRSCALE(((double)(vp->sample->sample_rate) *
585
((double)(vp->sample->root_freq) *
586
(double)(play_mode->rate)),
591
a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
594
a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
597
/* If the sweep's over, we can store the newly computed sample_increment */
598
if (!vp->vibrato_sweep || vp->modulation_wheel)
599
vp->vibrato_sample_increment[phase]=(int32) a;
602
a = -a; /* need to preserve the loop direction */
607
static sample_t *rs_vib_plain(int v, uint32 *countptr)
610
/* Play sample until end, then free the voice. */
613
int32 ofsd, v0, v1, v2, v3, overshoot;
616
*dest=resample_buffer+resample_buffer_offset,
617
*src=vp->sample->data;
619
incr=vp->sample_increment;
621
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
624
le=vp->sample->data_length;
625
#endif /* LAGRANGE_INTERPOLATION */
627
ofs=vp->sample_offset,
628
se=vp->sample->data_length,
631
cc=vp->vibrato_control_counter;
633
/* This has never been tested */
635
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
637
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
638
if (overshoot < 0) overshoot = -overshoot;
643
cc=vp->vibrato_control_ratio;
644
incr=update_vibrato(vp, 0);
647
offset = ofs >> FRACTION_BITS;
650
int32 delta = (ofs - se)>>FRACTION_BITS ;
651
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
652
v1 -= (delta+1) * v1 / overshoot;
654
else v1 = (int32)src[offset];
656
if (ofs + (1L<<FRACTION_BITS) >= se) {
659
else v2 = (int32)src[offset+1];
662
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
663
((ofs+(2L<<FRACTION_BITS)) > le) ) {
664
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
667
v0 = (int32)src[offset-1];
668
v3 = (int32)src[offset+2];
669
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
670
v1 = v1*ofsd>>FRACTION_BITS;
671
v2 = v2*ofsd>>FRACTION_BITS;
672
v3 = v3*ofsd>>FRACTION_BITS;
673
ofsd -= (1L << FRACTION_BITS);
674
v0 = v0*ofsd>>FRACTION_BITS;
675
v2 = v2*ofsd>>FRACTION_BITS;
676
v3 = v3*ofsd>>FRACTION_BITS;
677
ofsd -= (1L << FRACTION_BITS);
678
v0 = v0*ofsd>>FRACTION_BITS;
679
v1 = v1*ofsd>>FRACTION_BITS;
681
ofsd -= (1L << FRACTION_BITS);
682
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
683
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
685
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
689
if (ofs >= se + (overshoot << FRACTION_BITS))
691
if (!(vp->status&VOICE_FREE))
693
vp->status=VOICE_FREE;
701
vp->vibrato_control_counter=cc;
702
vp->sample_increment=incr;
703
vp->sample_offset=ofs; /* Update offset */
704
return resample_buffer+resample_buffer_offset;
707
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
709
/* Play sample until end-of-loop, skip back and continue. */
710
int32 ofsd, v0, v1, v2, v3, overshoot;
713
incr=vp->sample_increment;
716
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
718
#endif /* LAGRANGE_INTERPOLATION */
720
ll=le - vp->loop_start;
722
*dest=resample_buffer+resample_buffer_offset,
723
*src=vp->sample->data;
725
ofs=vp->sample_offset,
726
se=vp->sample->data_length,
729
cc=vp->vibrato_control_counter;
735
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
736
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
737
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
739
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
740
if (overshoot < 0) overshoot = -overshoot;
745
cc=vp->vibrato_control_ratio;
746
incr=update_vibrato(vp, 0);
749
offset = ofs >> FRACTION_BITS;
752
int32 delta = (ofs - se)>>FRACTION_BITS ;
753
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
754
v1 -= (delta+1) * v1 / overshoot;
756
else v1 = (int32)src[offset];
758
if (ofs + (1L<<FRACTION_BITS) >= se) {
761
else v2 = (int32)src[offset+1];
764
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
765
((ofs+(2L<<FRACTION_BITS)) > le) ) {
766
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
769
v0 = (int32)src[offset-1];
770
v3 = (int32)src[offset+2];
771
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
772
v1 = v1*ofsd>>FRACTION_BITS;
773
v2 = v2*ofsd>>FRACTION_BITS;
774
v3 = v3*ofsd>>FRACTION_BITS;
775
ofsd -= (1L << FRACTION_BITS);
776
v0 = v0*ofsd>>FRACTION_BITS;
777
v2 = v2*ofsd>>FRACTION_BITS;
778
v3 = v3*ofsd>>FRACTION_BITS;
779
ofsd -= (1L << FRACTION_BITS);
780
v0 = v0*ofsd>>FRACTION_BITS;
781
v1 = v1*ofsd>>FRACTION_BITS;
783
ofsd -= (1L << FRACTION_BITS);
784
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
785
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
787
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
795
vp->echo_delay -= ll >> FRACTION_BITS;
796
if (vp->echo_delay >= 0) ofs -= ll;
798
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
800
if (ofs >= se + (overshoot << FRACTION_BITS))
802
if (!(vp->status&VOICE_FREE))
804
vp->status=VOICE_FREE;
812
vp->vibrato_control_counter=cc;
813
vp->sample_increment=incr;
814
vp->sample_offset=ofs; /* Update offset */
815
return resample_buffer+resample_buffer_offset;
818
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
820
int32 ofsd, v0, v1, v2, v3, overshoot;
823
incr=vp->sample_increment;
829
ofs=vp->sample_offset,
830
se=vp->sample->data_length;
832
*dest=resample_buffer+resample_buffer_offset,
833
*src=vp->sample->data;
835
cc=vp->vibrato_control_counter;
838
#ifdef USE_BIDIR_OVERSHOOT
849
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
850
if (overshoot < 0) overshoot = -overshoot;
851
/* Play normally until inside the loop region */
852
while (count && (ofs <= ls))
854
i = (ls - ofs) / incr + 1;
855
if (i > count) i = count;
863
for(j = 0; j < i; j++)
866
offset = ofs >> FRACTION_BITS;
869
int32 delta = (ofs - se)>>FRACTION_BITS ;
870
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
871
v1 -= (delta+1) * v1 / overshoot;
873
else v1 = (int32)src[offset];
875
if (ofs + (1L<<FRACTION_BITS) >= se) {
878
else v2 = (int32)src[offset+1];
881
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
882
((ofs+(2L<<FRACTION_BITS)) > le) ) {
883
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
886
v0 = (int32)src[offset-1];
887
v3 = (int32)src[offset+2];
888
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
889
v1 = v1*ofsd>>FRACTION_BITS;
890
v2 = v2*ofsd>>FRACTION_BITS;
891
v3 = v3*ofsd>>FRACTION_BITS;
892
ofsd -= (1L << FRACTION_BITS);
893
v0 = v0*ofsd>>FRACTION_BITS;
894
v2 = v2*ofsd>>FRACTION_BITS;
895
v3 = v3*ofsd>>FRACTION_BITS;
896
ofsd -= (1L << FRACTION_BITS);
897
v0 = v0*ofsd>>FRACTION_BITS;
898
v1 = v1*ofsd>>FRACTION_BITS;
900
ofsd -= (1L << FRACTION_BITS);
901
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
902
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
904
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
911
cc = vp->vibrato_control_ratio;
912
incr = update_vibrato(vp, 0);
917
/* Then do the bidirectional looping */
921
/* Precalc how many times we should go through the loop */
923
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
925
/* fix from M. Izumo */
926
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
928
if(i > count) i = count;
936
while (i-- && ofs < se)
939
offset = ofs >> FRACTION_BITS;
942
int32 delta = (ofs - se)>>FRACTION_BITS ;
943
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
944
v1 -= (delta+1) * v1 / overshoot;
946
else v1 = (int32)src[offset];
948
if (ofs + (1L<<FRACTION_BITS) >= se) {
951
else v2 = (int32)src[offset+1];
954
( (ofs-(1L<<FRACTION_BITS)) < ls) ||
955
((ofs+(2L<<FRACTION_BITS)) > le) ) {
956
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
959
v0 = (int32)src[offset-1];
960
v3 = (int32)src[offset+2];
961
ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
962
v1 = v1*ofsd>>FRACTION_BITS;
963
v2 = v2*ofsd>>FRACTION_BITS;
964
v3 = v3*ofsd>>FRACTION_BITS;
965
ofsd -= (1L << FRACTION_BITS);
966
v0 = v0*ofsd>>FRACTION_BITS;
967
v2 = v2*ofsd>>FRACTION_BITS;
968
v3 = v3*ofsd>>FRACTION_BITS;
969
ofsd -= (1L << FRACTION_BITS);
970
v0 = v0*ofsd>>FRACTION_BITS;
971
v1 = v1*ofsd>>FRACTION_BITS;
973
ofsd -= (1L << FRACTION_BITS);
974
v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
975
v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
977
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
984
cc = vp->vibrato_control_ratio;
985
incr = update_vibrato(vp, (incr < 0));
990
#ifdef USE_BIDIR_OVERSHOOT
991
/* fold the overshoot back in */
998
#ifdef USE_BIDIR_OVERSHOOT
1006
vp->vibrato_control_counter=cc;
1007
vp->sample_increment=incr;
1008
vp->sample_offset=ofs; /* Update offset */
1009
return resample_buffer+resample_buffer_offset;
1012
static int rs_update_porta(int v)
1014
Voice *vp=&voice[v];
1018
if(vp->porta_pb < 0)
1020
if(d > -vp->porta_pb)
1025
if(d > vp->porta_pb)
1032
if(vp->porta_pb == 0)
1034
vp->porta_control_ratio = 0;
1038
return vp->porta_control_ratio;
1041
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
1043
Voice *vp=&voice[v];
1044
uint32 n = *countptr;
1046
sample_t *(* resampler)(int, uint32 *, int);
1047
int cc = vp->porta_control_counter;
1050
if(vp->vibrato_control_ratio)
1051
resampler = vib_resample_voice;
1053
resampler = normal_resample_voice;
1059
/* vp->cache = NULL; */
1060
resample_buffer_offset = 0;
1061
while(resample_buffer_offset < n)
1065
if((cc = rs_update_porta(v)) == 0)
1067
i = n - resample_buffer_offset;
1068
resampler(v, &i, mode);
1069
resample_buffer_offset += i;
1074
i = n - resample_buffer_offset;
1077
resampler(v, &i, mode);
1078
resample_buffer_offset += i;
1080
/* if(!loop && vp->status == VOICE_FREE) */
1081
if(vp->status == VOICE_FREE)
1085
*countptr = resample_buffer_offset;
1086
resample_buffer_offset = 0;
1087
vp->porta_control_counter = cc;
1088
return resample_buffer;
1091
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
1093
Voice *vp = &voice[v];
1095
/* vp->cache = NULL; */
1097
return rs_vib_loop(v, vp, countptr);
1099
return rs_vib_plain(v, countptr);
1100
return rs_vib_bidir(v, vp, *countptr);
1103
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
1105
Voice *vp = &voice[v];
1107
return rs_loop(v, vp, countptr);
1109
return rs_plain(v, countptr);
1110
return rs_bidir(v, vp, *countptr);
1113
sample_t *resample_voice_lagrange(int v, uint32 *countptr)
1115
Voice *vp=&voice[v];
1118
mode = vp->sample->modes;
1119
if((mode & MODES_LOOPING) &&
1120
((mode & MODES_ENVELOPE) ||
1121
(vp->status & (VOICE_ON | VOICE_SUSTAINED))))
1123
if(mode & MODES_PINGPONG)
1125
/* vp->cache = NULL; */
1134
if(vp->porta_control_ratio)
1135
return porta_resample_voice(v, countptr, mode);
1137
if(vp->vibrato_control_ratio)
1138
return vib_resample_voice(v, countptr, mode);
1140
return normal_resample_voice(v, countptr, mode);