2
$Id: resample_f.cpp,v 1.4 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 CSPLINE_INTERPOLATION
40
#define FILTER_INTERPOLATION
41
#define ENVELOPE_PITCH_MODULATION
45
#define MAX_DATAVAL 127
46
#define MIN_DATAVAL -128
48
#define MAX_DATAVAL 32767
49
#define MIN_DATAVAL -32768
52
#define OVERSHOOT_STEP 50
55
static sample_t *vib_resample_voice(int, uint32 *, int);
56
static sample_t *normal_resample_voice(int, uint32 *, int);
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;
83
static int calc_bw_index(int v)
85
FLOAT_T mod_amount=voice[v].modEnvToFilterFc;
86
int32 freq = voice[v].sample->cutoff_freq;
89
if (voice[v].lfo_phase_increment) update_lfo(v);
91
if (!voice[v].lfo_phase_increment && update_modulation_signal(v)) return 0;
93
/* printf("mod_amount %f ", mod_amount); */
94
if (voice[v].lfo_volume>0.001) {
95
if (mod_amount) mod_amount *= voice[v].lfo_volume;
96
else mod_amount = voice[v].lfo_volume;
97
/* printf("lfo %f -> mod %f ", voice[v].lfo_volume, mod_amount); */
100
if (mod_amount > 0.001) {
101
if (voice[v].modulation_volume)
103
(int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (voice[v].modulation_volume>>22) / 255.0) );
104
else freq = (int32)( (double)freq*mod_amount );
106
printf("v%d freq %d (was %d), modvol %d, mod_amount %f\n", v, (int)freq, (int)voice[v].sample->cutoff_freq,
107
(int)voice[v].modulation_volume>>22,
110
ix = 1 + (freq+50) / 100;
111
if (ix > 100) ix = 100;
112
voice[v].bw_index = ix;
115
voice[v].bw_index = 1 + (freq+50) / 100;
119
/*************** resampling with fixed increment *****************/
121
static sample_t *rs_plain(int v, uint32 *countptr)
123
/* Play sample until end, then free the voice. */
126
int32 v0, v1, v2, v3, temp, overshoot;
128
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
129
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
130
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
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;
153
a0 = butterworth[bw_index][0];
154
a1 = butterworth[bw_index][1];
155
a2 = butterworth[bw_index][2];
156
b0 = butterworth[bw_index][3];
157
b1 = butterworth[bw_index][4];
163
offset = ofs >> FRACTION_BITS;
166
int32 delta = (ofs - se)>>FRACTION_BITS;
167
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
168
v1 -= (delta+1) * v1 / overshoot;
169
}else v1 = (int32)src[offset];
170
if (ofs + (1L<<FRACTION_BITS) >= se) {
172
}else v2 = (int32)src[offset+1];
174
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
176
cc_count = control_ratio - 1;
177
if (calc_bw_index(v)) {
178
bw_index = vp->bw_index;
179
a0 = butterworth[bw_index][0];
180
a1 = butterworth[bw_index][1];
181
a2 = butterworth[bw_index][2];
182
b0 = butterworth[bw_index][3];
183
b1 = butterworth[bw_index][4];
185
incr = calc_mod_freq(v, incr);
187
if (dont_filter_melodic) bw_index = 0;
188
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
190
insamp = (FLOAT_T)newsample;
191
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
196
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
197
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
201
v0 = (int32)src[offset-1];
202
v3 = (int32)src[offset+2];
203
ofs &= FRACTION_MASK;
206
((((((5*v3 - 11*v2 + 7*v1 - v0)*
207
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
208
ofs = (1L << FRACTION_BITS) - ofs;
210
((((((5*v0 - 11*v1 + 7*temp - v3)*
211
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
212
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
214
cc_count = control_ratio - 1;
215
if (calc_bw_index(v)) {
216
bw_index = vp->bw_index;
217
a0 = butterworth[bw_index][0];
218
a1 = butterworth[bw_index][1];
219
a2 = butterworth[bw_index][2];
220
b0 = butterworth[bw_index][3];
221
b1 = butterworth[bw_index][4];
223
incr = calc_mod_freq(v, incr);
225
if (dont_filter_melodic) bw_index = 0;
226
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
228
insamp = (FLOAT_T)newsample;
229
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
234
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
235
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
241
if (ofs >= se + (overshoot << FRACTION_BITS))
243
if (!(vp->status&VOICE_FREE))
245
vp->status=VOICE_FREE;
253
vp->sample_offset=ofs; /* Update offset */
258
vp->bw_index=bw_index;
259
vp->modulation_counter=cc_count;
260
return resample_buffer+resample_buffer_offset;
263
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
265
/* Play sample until end-of-loop, skip back and continue. */
266
int32 v0, v1, v2, v3, temp, overshoot;
268
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
269
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
270
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
273
incr=vp->sample_increment;
275
ofs=vp->sample_offset;
278
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
280
#endif /* LAGRANGE_INTERPOLATION */
281
ll=le - vp->loop_start;
283
*dest=resample_buffer+resample_buffer_offset,
284
*src=vp->sample->data;
286
se=vp->sample->data_length,
293
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
294
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
295
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
297
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
298
if (overshoot < 0) overshoot = -overshoot;
299
a0 = butterworth[bw_index][0];
300
a1 = butterworth[bw_index][1];
301
a2 = butterworth[bw_index][2];
302
b0 = butterworth[bw_index][3];
303
b1 = butterworth[bw_index][4];
309
offset = ofs >> FRACTION_BITS;
312
int32 delta = (ofs - se)>>FRACTION_BITS;
313
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
314
v1 -= (delta+1) * v1 / overshoot;
315
}else v1 = (int32)src[offset];
316
if (ofs + (1L<<FRACTION_BITS) >= se) {
318
}else v2 = (int32)src[offset+1];
320
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
322
cc_count = control_ratio - 1;
323
if (calc_bw_index(v)) {
324
bw_index = vp->bw_index;
325
a0 = butterworth[bw_index][0];
326
a1 = butterworth[bw_index][1];
327
a2 = butterworth[bw_index][2];
328
b0 = butterworth[bw_index][3];
329
b1 = butterworth[bw_index][4];
331
incr = calc_mod_freq(v, incr);
333
if (dont_filter_melodic) bw_index = 0;
334
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
336
insamp = (FLOAT_T)newsample;
337
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
342
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
343
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
347
v0 = (int32)src[offset-1];
348
v3 = (int32)src[offset+2];
349
ofs &= FRACTION_MASK;
352
((((((5*v3 - 11*v2 + 7*v1 - v0)*
353
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
354
ofs = (1L << FRACTION_BITS) - ofs;
356
((((((5*v0 - 11*v1 + 7*temp - v3)*
357
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
358
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
360
cc_count = control_ratio - 1;
361
if (calc_bw_index(v)) {
362
bw_index = vp->bw_index;
363
a0 = butterworth[bw_index][0];
364
a1 = butterworth[bw_index][1];
365
a2 = butterworth[bw_index][2];
366
b0 = butterworth[bw_index][3];
367
b1 = butterworth[bw_index][4];
369
incr = calc_mod_freq(v, incr);
371
if (dont_filter_melodic) bw_index = 0;
372
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
374
insamp = (FLOAT_T)newsample;
375
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
380
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
381
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
391
vp->echo_delay -= ll >> FRACTION_BITS;
392
if (vp->echo_delay >= 0) ofs -= ll;
394
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
396
if (ofs >= se + (overshoot << FRACTION_BITS))
398
if (!(vp->status&VOICE_FREE))
400
vp->status=VOICE_FREE;
408
vp->sample_offset=ofs; /* Update offset */
413
vp->bw_index=bw_index;
414
vp->modulation_counter=cc_count;
415
return resample_buffer+resample_buffer_offset;
418
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
420
int32 v0, v1, v2, v3, temp, overshoot;
422
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
423
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
424
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
427
incr=vp->sample_increment;
432
ofs=vp->sample_offset,
433
se=vp->sample->data_length;
435
*dest=resample_buffer+resample_buffer_offset,
436
*src=vp->sample->data;
439
#ifdef USE_BIDIR_OVERSHOOT
446
/* Play normally until inside the loop region */
448
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
449
if (overshoot < 0) overshoot = -overshoot;
450
a0 = butterworth[bw_index][0];
451
a1 = butterworth[bw_index][1];
452
a2 = butterworth[bw_index][2];
453
b0 = butterworth[bw_index][3];
454
b1 = butterworth[bw_index][4];
458
/* NOTE: Assumes that incr > 0, which is NOT always the case
459
when doing bidirectional looping. I have yet to see a case
460
where both ofs <= ls AND incr < 0, however. */
461
if (incr < 0) i = ls - ofs;
463
i = (ls - ofs) / incr + 1;
470
for(j = 0; j < i; j++)
474
offset = ofs >> FRACTION_BITS;
477
int32 delta = (ofs - se)>>FRACTION_BITS;
478
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
479
v1 -= (delta+1) * v1 / overshoot;
480
}else v1 = (int32)src[offset];
481
if (ofs + (1L<<FRACTION_BITS) >= se) {
483
}else v2 = (int32)src[offset+1];
485
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
487
cc_count = control_ratio - 1;
488
if (calc_bw_index(v)) {
489
bw_index = vp->bw_index;
490
a0 = butterworth[bw_index][0];
491
a1 = butterworth[bw_index][1];
492
a2 = butterworth[bw_index][2];
493
b0 = butterworth[bw_index][3];
494
b1 = butterworth[bw_index][4];
496
incr = calc_mod_freq(v, incr);
498
if (dont_filter_melodic) bw_index = 0;
499
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
501
insamp = (FLOAT_T)newsample;
502
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
507
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
508
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
512
v0 = (int32)src[offset-1];
513
v3 = (int32)src[offset+2];
514
ofs &= FRACTION_MASK;
517
((((((5*v3 - 11*v2 + 7*v1 - v0)*
518
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
519
ofs = (1L << FRACTION_BITS) - ofs;
521
((((((5*v0 - 11*v1 + 7*temp - v3)*
522
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
523
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
525
cc_count = control_ratio - 1;
526
if (calc_bw_index(v)) {
527
bw_index = vp->bw_index;
528
a0 = butterworth[bw_index][0];
529
a1 = butterworth[bw_index][1];
530
a2 = butterworth[bw_index][2];
531
b0 = butterworth[bw_index][3];
532
b1 = butterworth[bw_index][4];
534
incr = calc_mod_freq(v, incr);
536
if (dont_filter_melodic) bw_index = 0;
537
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
539
insamp = (FLOAT_T)newsample;
540
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
545
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
546
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
555
/* Then do the bidirectional looping */
559
/* Precalc how many times we should go through the loop */
561
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
563
/* fix from M. Izumo */
564
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
572
for(j = 0; j < i && ofs < se; j++)
576
offset = ofs >> FRACTION_BITS;
579
int32 delta = (ofs - se)>>FRACTION_BITS;
580
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
581
v1 -= (delta+1) * v1 / overshoot;
582
}else v1 = (int32)src[offset];
583
if (ofs + (1L<<FRACTION_BITS) >= se) {
585
}else v2 = (int32)src[offset+1];
587
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
589
cc_count = control_ratio - 1;
590
if (calc_bw_index(v)) {
591
bw_index = vp->bw_index;
592
a0 = butterworth[bw_index][0];
593
a1 = butterworth[bw_index][1];
594
a2 = butterworth[bw_index][2];
595
b0 = butterworth[bw_index][3];
596
b1 = butterworth[bw_index][4];
598
incr = calc_mod_freq(v, incr);
600
if (dont_filter_melodic) bw_index = 0;
601
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
603
insamp = (FLOAT_T)newsample;
604
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
609
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
610
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
614
v0 = (int32)src[offset-1];
615
v3 = (int32)src[offset+2];
616
ofs &= FRACTION_MASK;
619
((((((5*v3 - 11*v2 + 7*v1 - v0)*
620
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
621
ofs = (1L << FRACTION_BITS) - ofs;
623
((((((5*v0 - 11*v1 + 7*temp - v3)*
624
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
625
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
627
cc_count = control_ratio - 1;
628
if (calc_bw_index(v)) {
629
bw_index = vp->bw_index;
630
a0 = butterworth[bw_index][0];
631
a1 = butterworth[bw_index][1];
632
a2 = butterworth[bw_index][2];
633
b0 = butterworth[bw_index][3];
634
b1 = butterworth[bw_index][4];
636
incr = calc_mod_freq(v, incr);
638
if (dont_filter_melodic) bw_index = 0;
639
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
641
insamp = (FLOAT_T)newsample;
642
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
647
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
648
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
655
#ifdef USE_BIDIR_OVERSHOOT
658
/* fold the overshoot back in */
672
vp->sample_increment=incr;
673
vp->sample_offset=ofs; /* Update offset */
678
vp->bw_index=bw_index;
679
vp->modulation_counter=cc_count;
680
return resample_buffer+resample_buffer_offset;
683
/*********************** vibrato versions ***************************/
685
/* We only need to compute one half of the vibrato sine cycle */
686
static uint32 vib_phase_to_inc_ptr(uint32 phase)
688
if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
689
return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
690
else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
691
return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
693
return phase-VIBRATO_SAMPLE_INCREMENTS/2;
696
static int32 update_vibrato(Voice *vp, int sign)
698
uint32 depth, freq=vp->frequency;
699
#ifdef ENVELOPE_PITCH_MODULATION
700
FLOAT_T mod_amount=vp->sample->modEnvToPitch;
706
if(vp->vibrato_delay > 0)
708
vp->vibrato_delay -= vp->vibrato_control_ratio;
709
if(vp->vibrato_delay > 0)
710
return vp->sample_increment;
713
if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
715
phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
717
if (vp->vibrato_sample_increment[phase])
720
return -vp->vibrato_sample_increment[phase];
722
return vp->vibrato_sample_increment[phase];
725
/* Need to compute this sample increment. */
727
depth = vp->vibrato_depth;
728
if(depth < vp->modulation_wheel)
729
depth = vp->modulation_wheel;
732
if (vp->vibrato_sweep && !vp->modulation_wheel)
734
/* Need to update sweep */
735
vp->vibrato_sweep_position += vp->vibrato_sweep;
736
if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
741
depth *= vp->vibrato_sweep_position;
742
depth >>= SWEEP_SHIFT;
746
#ifdef ENVELOPE_PITCH_MODULATION
747
#ifndef FILTER_INTERPOLATION
748
if (update_modulation_signal(0)) mod_amount = 0;
752
freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
755
pb=(int)((sine(vp->vibrato_phase *
756
(SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
757
* (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
759
a = FRSCALE(((double)(vp->sample->sample_rate) *
761
((double)(vp->sample->root_freq) *
762
(double)(play_mode->rate)),
767
a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
770
a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
773
/* If the sweep's over, we can store the newly computed sample_increment */
774
if (!vp->vibrato_sweep || vp->modulation_wheel)
775
vp->vibrato_sample_increment[phase]=(int32) a;
778
a = -a; /* need to preserve the loop direction */
783
static sample_t *rs_vib_plain(int v, uint32 *countptr)
786
/* Play sample until end, then free the voice. */
789
int32 v0, v1, v2, v3, temp, overshoot;
791
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
792
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
793
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
796
*dest=resample_buffer+resample_buffer_offset,
797
*src=vp->sample->data;
799
incr=vp->sample_increment;
801
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
804
le=vp->sample->data_length;
805
#endif /* LAGRANGE_INTERPOLATION */
807
ofs=vp->sample_offset,
808
se=vp->sample->data_length,
811
cc=vp->vibrato_control_counter;
813
/* This has never been tested */
815
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
817
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
818
if (overshoot < 0) overshoot = -overshoot;
819
a0 = butterworth[bw_index][0];
820
a1 = butterworth[bw_index][1];
821
a2 = butterworth[bw_index][2];
822
b0 = butterworth[bw_index][3];
823
b1 = butterworth[bw_index][4];
828
cc=vp->vibrato_control_ratio;
829
incr=update_vibrato(vp, 0);
833
offset = ofs >> FRACTION_BITS;
836
int32 delta = (ofs - se)>>FRACTION_BITS;
837
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
838
v1 -= (delta+1) * v1 / overshoot;
839
}else v1 = (int32)src[offset];
840
if (ofs + (1L<<FRACTION_BITS) >= se) {
842
}else v2 = (int32)src[offset+1];
844
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
846
cc_count = control_ratio - 1;
847
if (calc_bw_index(v)) {
848
bw_index = vp->bw_index;
849
a0 = butterworth[bw_index][0];
850
a1 = butterworth[bw_index][1];
851
a2 = butterworth[bw_index][2];
852
b0 = butterworth[bw_index][3];
853
b1 = butterworth[bw_index][4];
855
incr = calc_mod_freq(v, incr);
857
if (dont_filter_melodic) bw_index = 0;
858
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
860
insamp = (FLOAT_T)newsample;
861
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
866
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
867
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
871
v0 = (int32)src[offset-1];
872
v3 = (int32)src[offset+2];
873
ofs &= FRACTION_MASK;
876
((((((5*v3 - 11*v2 + 7*v1 - v0)*
877
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
878
ofs = (1L << FRACTION_BITS) - ofs;
880
((((((5*v0 - 11*v1 + 7*temp - v3)*
881
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
882
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
884
cc_count = control_ratio - 1;
885
if (calc_bw_index(v)) {
886
bw_index = vp->bw_index;
887
a0 = butterworth[bw_index][0];
888
a1 = butterworth[bw_index][1];
889
a2 = butterworth[bw_index][2];
890
b0 = butterworth[bw_index][3];
891
b1 = butterworth[bw_index][4];
893
incr = calc_mod_freq(v, incr);
895
if (dont_filter_melodic) bw_index = 0;
896
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
898
insamp = (FLOAT_T)newsample;
899
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
904
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
905
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
911
if (ofs >= se + (overshoot << FRACTION_BITS))
913
if (!(vp->status&VOICE_FREE))
915
vp->status=VOICE_FREE;
923
vp->vibrato_control_counter=cc;
924
vp->sample_increment=incr;
925
vp->sample_offset=ofs; /* Update offset */
930
vp->bw_index=bw_index;
931
vp->modulation_counter=cc_count;
932
return resample_buffer+resample_buffer_offset;
935
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
937
/* Play sample until end-of-loop, skip back and continue. */
938
int32 v0, v1, v2, v3, temp, overshoot;
940
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
941
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
942
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
945
incr=vp->sample_increment;
948
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
950
#endif /* LAGRANGE_INTERPOLATION */
952
ll=le - vp->loop_start;
954
*dest=resample_buffer+resample_buffer_offset,
955
*src=vp->sample->data;
957
ofs=vp->sample_offset,
958
se=vp->sample->data_length,
961
cc=vp->vibrato_control_counter;
967
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
968
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
969
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
971
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
972
if (overshoot < 0) overshoot = -overshoot;
973
a0 = butterworth[bw_index][0];
974
a1 = butterworth[bw_index][1];
975
a2 = butterworth[bw_index][2];
976
b0 = butterworth[bw_index][3];
977
b1 = butterworth[bw_index][4];
982
cc=vp->vibrato_control_ratio;
983
incr=update_vibrato(vp, 0);
987
offset = ofs >> FRACTION_BITS;
990
int32 delta = (ofs - se)>>FRACTION_BITS;
991
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
992
v1 -= (delta+1) * v1 / overshoot;
993
}else v1 = (int32)src[offset];
994
if (ofs + (1L<<FRACTION_BITS) >= se) {
996
}else v2 = (int32)src[offset+1];
998
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
1000
cc_count = control_ratio - 1;
1001
if (calc_bw_index(v)) {
1002
bw_index = vp->bw_index;
1003
a0 = butterworth[bw_index][0];
1004
a1 = butterworth[bw_index][1];
1005
a2 = butterworth[bw_index][2];
1006
b0 = butterworth[bw_index][3];
1007
b1 = butterworth[bw_index][4];
1009
incr = calc_mod_freq(v, incr);
1011
if (dont_filter_melodic) bw_index = 0;
1012
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
1014
insamp = (FLOAT_T)newsample;
1015
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1020
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1021
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
1025
v0 = (int32)src[offset-1];
1026
v3 = (int32)src[offset+2];
1027
ofs &= FRACTION_MASK;
1030
((((((5*v3 - 11*v2 + 7*v1 - v0)*
1031
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1032
ofs = (1L << FRACTION_BITS) - ofs;
1034
((((((5*v0 - 11*v1 + 7*temp - v3)*
1035
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1036
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
1038
cc_count = control_ratio - 1;
1039
if (calc_bw_index(v)) {
1040
bw_index = vp->bw_index;
1041
a0 = butterworth[bw_index][0];
1042
a1 = butterworth[bw_index][1];
1043
a2 = butterworth[bw_index][2];
1044
b0 = butterworth[bw_index][3];
1045
b1 = butterworth[bw_index][4];
1047
incr = calc_mod_freq(v, incr);
1049
if (dont_filter_melodic) bw_index = 0;
1050
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
1052
insamp = (FLOAT_T)newsample;
1053
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1058
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1059
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
1063
*dest++ = newsample;
1069
vp->echo_delay -= ll >> FRACTION_BITS;
1070
if (vp->echo_delay >= 0) ofs -= ll;
1072
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
1074
if (ofs >= se + (overshoot << FRACTION_BITS))
1076
if (!(vp->status&VOICE_FREE))
1078
vp->status=VOICE_FREE;
1086
vp->vibrato_control_counter=cc;
1087
vp->sample_increment=incr;
1088
vp->sample_offset=ofs; /* Update offset */
1093
vp->bw_index=bw_index;
1094
vp->modulation_counter=cc_count;
1095
return resample_buffer+resample_buffer_offset;
1098
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
1100
int32 v0, v1, v2, v3, temp, overshoot;
1102
FLOAT_T insamp, outsamp, a0, a1, a2, b0, b1,
1103
x0=vp->current_x0, x1=vp->current_x1, y0=vp->current_y0, y1=vp->current_y1;
1104
uint32 cc_count=vp->modulation_counter, bw_index=vp->bw_index, ofsdu;
1107
incr=vp->sample_increment;
1113
ofs=vp->sample_offset,
1114
se=vp->sample->data_length;
1116
*dest=resample_buffer+resample_buffer_offset,
1117
*src=vp->sample->data;
1119
cc=vp->vibrato_control_counter;
1122
#ifdef USE_BIDIR_OVERSHOOT
1133
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
1134
if (overshoot < 0) overshoot = -overshoot;
1135
a0 = butterworth[bw_index][0];
1136
a1 = butterworth[bw_index][1];
1137
a2 = butterworth[bw_index][2];
1138
b0 = butterworth[bw_index][3];
1139
b1 = butterworth[bw_index][4];
1140
/* Play normally until inside the loop region */
1141
while (count && (ofs <= ls))
1143
i = (ls - ofs) / incr + 1;
1144
if (i > count) i = count;
1152
for(j = 0; j < i; j++)
1156
offset = ofs >> FRACTION_BITS;
1159
int32 delta = (ofs - se)>>FRACTION_BITS;
1160
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
1161
v1 -= (delta+1) * v1 / overshoot;
1162
}else v1 = (int32)src[offset];
1163
if (ofs + (1L<<FRACTION_BITS) >= se) {
1165
}else v2 = (int32)src[offset+1];
1167
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
1169
cc_count = control_ratio - 1;
1170
if (calc_bw_index(v)) {
1171
bw_index = vp->bw_index;
1172
a0 = butterworth[bw_index][0];
1173
a1 = butterworth[bw_index][1];
1174
a2 = butterworth[bw_index][2];
1175
b0 = butterworth[bw_index][3];
1176
b1 = butterworth[bw_index][4];
1178
incr = calc_mod_freq(v, incr);
1180
if (dont_filter_melodic) bw_index = 0;
1181
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
1183
insamp = (FLOAT_T)newsample;
1184
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1189
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1190
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
1194
v0 = (int32)src[offset-1];
1195
v3 = (int32)src[offset+2];
1196
ofs &= FRACTION_MASK;
1199
((((((5*v3 - 11*v2 + 7*v1 - v0)*
1200
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1201
ofs = (1L << FRACTION_BITS) - ofs;
1203
((((((5*v0 - 11*v1 + 7*temp - v3)*
1204
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1205
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
1207
cc_count = control_ratio - 1;
1208
if (calc_bw_index(v)) {
1209
bw_index = vp->bw_index;
1210
a0 = butterworth[bw_index][0];
1211
a1 = butterworth[bw_index][1];
1212
a2 = butterworth[bw_index][2];
1213
b0 = butterworth[bw_index][3];
1214
b1 = butterworth[bw_index][4];
1216
incr = calc_mod_freq(v, incr);
1218
if (dont_filter_melodic) bw_index = 0;
1219
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
1221
insamp = (FLOAT_T)newsample;
1222
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1227
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1228
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
1232
*dest++ = newsample;
1237
cc = vp->vibrato_control_ratio;
1238
incr = update_vibrato(vp, 0);
1243
/* Then do the bidirectional looping */
1247
/* Precalc how many times we should go through the loop */
1249
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
1251
/* fix from M. Izumo */
1252
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
1254
if(i > count) i = count;
1262
while (i-- && ofs < se)
1266
offset = ofs >> FRACTION_BITS;
1269
int32 delta = (ofs - se)>>FRACTION_BITS;
1270
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
1271
v1 -= (delta+1) * v1 / overshoot;
1272
}else v1 = (int32)src[offset];
1273
if (ofs + (1L<<FRACTION_BITS) >= se) {
1275
}else v2 = (int32)src[offset+1];
1277
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
1279
cc_count = control_ratio - 1;
1280
if (calc_bw_index(v)) {
1281
bw_index = vp->bw_index;
1282
a0 = butterworth[bw_index][0];
1283
a1 = butterworth[bw_index][1];
1284
a2 = butterworth[bw_index][2];
1285
b0 = butterworth[bw_index][3];
1286
b1 = butterworth[bw_index][4];
1288
incr = calc_mod_freq(v, incr);
1290
if (dont_filter_melodic) bw_index = 0;
1291
newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
1293
insamp = (FLOAT_T)newsample;
1294
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1299
newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1300
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
1304
v0 = (int32)src[offset-1];
1305
v3 = (int32)src[offset+2];
1306
ofs &= FRACTION_MASK;
1309
((((((5*v3 - 11*v2 + 7*v1 - v0)*
1310
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1311
ofs = (1L << FRACTION_BITS) - ofs;
1313
((((((5*v0 - 11*v1 + 7*temp - v3)*
1314
ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
1315
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
1317
cc_count = control_ratio - 1;
1318
if (calc_bw_index(v)) {
1319
bw_index = vp->bw_index;
1320
a0 = butterworth[bw_index][0];
1321
a1 = butterworth[bw_index][1];
1322
a2 = butterworth[bw_index][2];
1323
b0 = butterworth[bw_index][3];
1324
b1 = butterworth[bw_index][4];
1326
incr = calc_mod_freq(v, incr);
1328
if (dont_filter_melodic) bw_index = 0;
1329
newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
1331
insamp = (FLOAT_T)newsample;
1332
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1337
newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
1338
((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
1342
*dest++ = newsample;
1347
cc = vp->vibrato_control_ratio;
1348
incr = update_vibrato(vp, (incr < 0));
1353
#ifdef USE_BIDIR_OVERSHOOT
1354
/* fold the overshoot back in */
1361
#ifdef USE_BIDIR_OVERSHOOT
1369
vp->vibrato_control_counter=cc;
1370
vp->sample_increment=incr;
1371
vp->sample_offset=ofs; /* Update offset */
1376
vp->bw_index=bw_index;
1377
vp->modulation_counter=cc_count;
1378
return resample_buffer+resample_buffer_offset;
1381
static int rs_update_porta(int v)
1383
Voice *vp=&voice[v];
1387
if(vp->porta_pb < 0)
1389
if(d > -vp->porta_pb)
1394
if(d > vp->porta_pb)
1401
if(vp->porta_pb == 0)
1403
vp->porta_control_ratio = 0;
1407
return vp->porta_control_ratio;
1410
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
1412
Voice *vp=&voice[v];
1413
uint32 n = *countptr;
1415
sample_t *(* resampler)(int, uint32 *, int);
1416
int cc = vp->porta_control_counter;
1419
if(vp->vibrato_control_ratio)
1420
resampler = vib_resample_voice;
1422
resampler = normal_resample_voice;
1428
/* vp->cache = NULL; */
1429
resample_buffer_offset = 0;
1430
while(resample_buffer_offset < n)
1434
if((cc = rs_update_porta(v)) == 0)
1436
i = n - resample_buffer_offset;
1437
resampler(v, &i, mode);
1438
resample_buffer_offset += i;
1443
i = n - resample_buffer_offset;
1446
resampler(v, &i, mode);
1447
resample_buffer_offset += i;
1449
/* if(!loop && vp->status == VOICE_FREE) */
1450
if(vp->status == VOICE_FREE)
1454
*countptr = resample_buffer_offset;
1455
resample_buffer_offset = 0;
1456
vp->porta_control_counter = cc;
1457
return resample_buffer;
1460
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
1462
Voice *vp = &voice[v];
1464
/* vp->cache = NULL; */
1466
return rs_vib_loop(v, vp, countptr);
1468
return rs_vib_plain(v, countptr);
1469
return rs_vib_bidir(v, vp, *countptr);
1472
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
1474
Voice *vp = &voice[v];
1476
return rs_loop(v, vp, countptr);
1478
return rs_plain(v, countptr);
1479
return rs_bidir(v, vp, *countptr);
1482
sample_t *resample_voice_filter(int v, uint32 *countptr)
1484
Voice *vp=&voice[v];
1487
mode = vp->sample->modes;
1488
if((mode & MODES_LOOPING) &&
1489
((mode & MODES_ENVELOPE) ||
1490
(vp->status & (VOICE_ON | VOICE_SUSTAINED))))
1492
if(mode & MODES_PINGPONG)
1494
/* vp->cache = NULL; */
1503
if(vp->porta_control_ratio)
1504
return porta_resample_voice(v, countptr, mode);
1506
if(vp->vibrato_control_ratio)
1507
return vib_resample_voice(v, countptr, mode);
1509
return normal_resample_voice(v, countptr, mode);