2
$Id: resample.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 CSPLINE_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
sample_t resample_buffer[AUDIO_BUFFER_SIZE+100];
55
uint32 resample_buffer_offset = 0;
57
static sample_t *vib_resample_voice(int, uint32 *, int);
58
static sample_t *normal_resample_voice(int, uint32 *, int);
60
/* Returns 1 if envelope runs out */
61
int recompute_modulation(int v)
65
stage = voice[v].modulation_stage;
69
/* Envelope ran out. */
70
voice[v].modulation_increment = 0;
74
if ((voice[v].sample->modes & MODES_ENVELOPE) && (voice[v].sample->modes & MODES_SUSTAIN))
76
if (voice[v].status & (VOICE_ON | VOICE_SUSTAINED))
80
/* Freeze modulation until note turns off. Trumpets want this. */
81
voice[v].modulation_increment=0;
86
voice[v].modulation_stage=stage+1;
89
if (voice[v].modulation_volume==(int)voice[v].sample->modulation_offset[stage] ||
90
(stage > 2 && voice[v].modulation_volume < (int)voice[v].sample->modulation_offset[stage]))
92
if (voice[v].modulation_volume==voice[v].sample->modulation_offset[stage])
94
return recompute_modulation(v);
95
voice[v].modulation_target=voice[v].sample->modulation_offset[stage];
96
voice[v].modulation_increment = voice[v].sample->modulation_rate[stage];
97
if ((int)voice[v].modulation_target<voice[v].modulation_volume)
98
voice[v].modulation_increment = -voice[v].modulation_increment;
102
int update_modulation(int v)
105
if(voice[v].modulation_delay > 0)
107
/* voice[v].modulation_delay -= control_ratio; I think units are already
108
in terms of control_ratio */
109
voice[v].modulation_delay -= 1;
110
if(voice[v].modulation_delay > 0)
115
voice[v].modulation_volume += voice[v].modulation_increment;
116
if (voice[v].modulation_volume < 0) voice[v].modulation_volume = 0;
117
/* Why is there no ^^ operator?? */
118
if (((voice[v].modulation_increment < 0) &&
119
(voice[v].modulation_volume <= (int)voice[v].modulation_target)) ||
120
((voice[v].modulation_increment > 0) &&
121
(voice[v].modulation_volume >= (int)voice[v].modulation_target)))
123
voice[v].modulation_volume = voice[v].modulation_target;
124
if (recompute_modulation(v))
130
/* Returns 1 if the note died */
131
int update_modulation_signal(int v)
133
if (voice[v].modulation_increment && update_modulation(v))
140
/* modulation_volume has been set by above routine */
141
int32 calc_mod_freq(int v, int32 incr)
145
/* already done in update_vibrato ? */
146
if (voice[v].vibrato_control_ratio) return incr;
147
if ((mod_amount=voice[v].sample->modEnvToPitch)<0.02) return incr;
148
if (incr < 0) return incr;
149
freq = voice[v].frequency;
150
freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (voice[v].modulation_volume>>22) / 255.0) );
152
return (int32) FRSCALE(((double)(voice[v].sample->sample_rate) *
154
((double)(voice[v].sample->root_freq) *
155
(double)(play_mode->rate)),
159
/*************** resampling with fixed increment *****************/
161
static sample_t *rs_plain(int v, uint32 *countptr)
163
/* Play sample until end, then free the voice. */
166
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
168
uint32 cc_count=vp->modulation_counter;
170
*dest=resample_buffer+resample_buffer_offset,
171
*src=vp->sample->data;
173
incr=vp->sample_increment;
175
ofs=vp->sample_offset;
176
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
179
le=vp->sample->data_length;
180
#endif /* LAGRANGE_INTERPOLATION */
182
se=vp->sample->data_length;
186
if (!incr) return resample_buffer+resample_buffer_offset; /* --gl */
188
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
189
if (overshoot < 0) overshoot = -overshoot;
194
offset = ofs >> FRACTION_BITS;
197
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
198
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
199
v1 -= (delta+1) * v1 / overshoot;
200
}else v1 = (int32)src[offset];
201
if (ofs + (1L<<FRACTION_BITS) >= se) {
203
}else v2 = (int32)src[offset+1];
205
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
206
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
209
v0 = (int32)src[offset-1];
210
v3 = (int32)src[offset+2];
211
ofs &= FRACTION_MASK;
214
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
215
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
216
ofs = (1L << FRACTION_BITS) - ofs;
218
((((((5*v0 - 11*v1 + 7*temp - v3)*
219
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
220
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
221
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
225
cc_count = control_ratio - 1;
226
if (!update_modulation_signal(v))
227
incr = calc_mod_freq(v, incr);
230
if (ofs >= se + (overshoot << FRACTION_BITS))
232
if (!(vp->status&VOICE_FREE))
234
vp->status=VOICE_FREE;
242
vp->sample_offset=ofs; /* Update offset */
243
vp->modulation_counter=cc_count;
244
return resample_buffer+resample_buffer_offset;
247
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
249
/* Play sample until end-of-loop, skip back and continue. */
250
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
252
uint32 cc_count=vp->modulation_counter;
254
incr=vp->sample_increment;
256
ofs=vp->sample_offset;
259
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
261
#endif /* LAGRANGE_INTERPOLATION */
262
ll=le - vp->loop_start;
264
*dest=resample_buffer+resample_buffer_offset,
265
*src=vp->sample->data;
267
se=vp->sample->data_length,
274
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
275
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
276
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
278
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
279
if (overshoot < 0) overshoot = -overshoot;
284
offset = ofs >> FRACTION_BITS;
287
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
288
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
289
v1 -= (delta+1) * v1 / overshoot;
290
}else v1 = (int32)src[offset];
291
if (ofs + (1L<<FRACTION_BITS) >= se) {
293
}else v2 = (int32)src[offset+1];
295
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
296
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
299
v0 = (int32)src[offset-1];
300
v3 = (int32)src[offset+2];
301
ofs &= FRACTION_MASK;
304
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
305
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
306
ofs = (1L << FRACTION_BITS) - ofs;
308
((((((5*v0 - 11*v1 + 7*temp - v3)*
309
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
310
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
311
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
315
cc_count = control_ratio - 1;
316
if (!update_modulation_signal(v))
317
incr = calc_mod_freq(v, incr);
324
vp->echo_delay -= ll >> FRACTION_BITS;
325
if (vp->echo_delay >= 0) ofs -= ll;
327
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
329
if (ofs >= se + (overshoot << FRACTION_BITS))
331
if (!(vp->status&VOICE_FREE))
333
vp->status=VOICE_FREE;
341
vp->sample_offset=ofs; /* Update offset */
342
vp->modulation_counter=cc_count;
343
return resample_buffer+resample_buffer_offset;
346
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
348
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
351
incr=vp->sample_increment;
356
ofs=vp->sample_offset,
357
se=vp->sample->data_length;
359
*dest=resample_buffer+resample_buffer_offset,
360
*src=vp->sample->data;
363
#ifdef USE_BIDIR_OVERSHOOT
370
/* Play normally until inside the loop region */
372
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
373
if (overshoot < 0) overshoot = -overshoot;
377
/* NOTE: Assumes that incr > 0, which is NOT always the case
378
when doing bidirectional looping. I have yet to see a case
379
where both ofs <= ls AND incr < 0, however. */
380
if (incr < 0) i = ls - ofs;
382
i = (ls - ofs) / incr + 1;
389
for(j = 0; j < i; j++)
392
offset = ofs >> FRACTION_BITS;
395
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
396
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
397
v1 -= (delta+1) * v1 / overshoot;
398
}else v1 = (int32)src[offset];
399
if (ofs + (1L<<FRACTION_BITS) >= se) {
401
}else v2 = (int32)src[offset+1];
403
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
404
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
407
v0 = (int32)src[offset-1];
408
v3 = (int32)src[offset+2];
409
ofs &= FRACTION_MASK;
412
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
413
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
414
ofs = (1L << FRACTION_BITS) - ofs;
416
((((((5*v0 - 11*v1 + 7*temp - v3)*
417
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
418
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
419
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
426
/* Then do the bidirectional looping */
430
/* Precalc how many times we should go through the loop */
432
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
434
/* fix from M. Izumo */
435
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
443
for(j = 0; j < i && ofs < se; j++)
446
offset = ofs >> FRACTION_BITS;
449
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
450
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
451
v1 -= (delta+1) * v1 / overshoot;
452
}else v1 = (int32)src[offset];
453
if (ofs + (1L<<FRACTION_BITS) >= se) {
455
}else v2 = (int32)src[offset+1];
457
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
458
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
461
v0 = (int32)src[offset-1];
462
v3 = (int32)src[offset+2];
463
ofs &= FRACTION_MASK;
466
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
467
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
468
ofs = (1L << FRACTION_BITS) - ofs;
470
((((((5*v0 - 11*v1 + 7*temp - v3)*
471
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
472
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
473
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
478
#ifdef USE_BIDIR_OVERSHOOT
481
/* fold the overshoot back in */
495
vp->sample_increment=incr;
496
vp->sample_offset=ofs; /* Update offset */
497
return resample_buffer+resample_buffer_offset;
500
/*********************** vibrato versions ***************************/
502
/* We only need to compute one half of the vibrato sine cycle */
503
static uint32 vib_phase_to_inc_ptr(uint32 phase)
505
if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
506
return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
507
else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
508
return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
510
return phase-VIBRATO_SAMPLE_INCREMENTS/2;
513
static int32 update_vibrato(Voice *vp, int sign)
515
uint32 depth, freq=vp->frequency;
516
#ifdef ENVELOPE_PITCH_MODULATION
517
FLOAT_T mod_amount=vp->sample->modEnvToPitch;
523
if(vp->vibrato_delay > 0)
525
vp->vibrato_delay -= vp->vibrato_control_ratio;
526
if(vp->vibrato_delay > 0)
527
return vp->sample_increment;
530
if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
532
phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
534
if (vp->vibrato_sample_increment[phase])
537
return -vp->vibrato_sample_increment[phase];
539
return vp->vibrato_sample_increment[phase];
542
/* Need to compute this sample increment. */
544
depth = vp->vibrato_depth;
545
if(depth < vp->modulation_wheel)
546
depth = vp->modulation_wheel;
549
if (vp->vibrato_sweep && !vp->modulation_wheel)
551
/* Need to update sweep */
552
vp->vibrato_sweep_position += vp->vibrato_sweep;
553
if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
558
depth *= vp->vibrato_sweep_position;
559
depth >>= SWEEP_SHIFT;
563
#ifdef ENVELOPE_PITCH_MODULATION
564
#ifndef FILTER_INTERPOLATION
565
if (update_modulation_signal(0)) mod_amount = 0;
569
freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
572
pb=(int)((sine(vp->vibrato_phase *
573
(SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
574
* (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
576
a = FRSCALE(((double)(vp->sample->sample_rate) *
578
((double)(vp->sample->root_freq) *
579
(double)(play_mode->rate)),
584
a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
587
a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
590
/* If the sweep's over, we can store the newly computed sample_increment */
591
if (!vp->vibrato_sweep || vp->modulation_wheel)
592
vp->vibrato_sample_increment[phase]=(int32) a;
595
a = -a; /* need to preserve the loop direction */
600
static sample_t *rs_vib_plain(int v, uint32 *countptr)
603
/* Play sample until end, then free the voice. */
606
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
609
*dest=resample_buffer+resample_buffer_offset,
610
*src=vp->sample->data;
612
incr=vp->sample_increment;
614
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
617
le=vp->sample->data_length;
618
#endif /* LAGRANGE_INTERPOLATION */
620
ofs=vp->sample_offset,
621
se=vp->sample->data_length,
624
cc=vp->vibrato_control_counter;
626
/* This has never been tested */
628
if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
630
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
631
if (overshoot < 0) overshoot = -overshoot;
636
cc=vp->vibrato_control_ratio;
637
incr=update_vibrato(vp, 0);
640
offset = ofs >> FRACTION_BITS;
643
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
644
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
645
v1 -= (delta+1) * v1 / overshoot;
646
}else v1 = (int32)src[offset];
647
if (ofs + (1L<<FRACTION_BITS) >= se) {
649
}else v2 = (int32)src[offset+1];
651
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
652
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
655
v0 = (int32)src[offset-1];
656
v3 = (int32)src[offset+2];
657
ofs &= FRACTION_MASK;
660
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
661
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
662
ofs = (1L << FRACTION_BITS) - ofs;
664
((((((5*v0 - 11*v1 + 7*temp - v3)*
665
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
666
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
667
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
671
if (ofs >= se + (overshoot << FRACTION_BITS))
673
if (!(vp->status&VOICE_FREE))
675
vp->status=VOICE_FREE;
683
vp->vibrato_control_counter=cc;
684
vp->sample_increment=incr;
685
vp->sample_offset=ofs; /* Update offset */
686
return resample_buffer+resample_buffer_offset;
689
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
691
/* Play sample until end-of-loop, skip back and continue. */
692
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
695
incr=vp->sample_increment;
698
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
700
#endif /* LAGRANGE_INTERPOLATION */
702
ll=le - vp->loop_start;
704
*dest=resample_buffer+resample_buffer_offset,
705
*src=vp->sample->data;
707
ofs=vp->sample_offset,
708
se=vp->sample->data_length,
711
cc=vp->vibrato_control_counter;
717
(vp->status & (VOICE_FREE | VOICE_DIE)) ||
718
((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
719
((vp->status & VOICE_OFF) && dont_keep_looping ) ;
721
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
722
if (overshoot < 0) overshoot = -overshoot;
727
cc=vp->vibrato_control_ratio;
728
incr=update_vibrato(vp, 0);
731
offset = ofs >> FRACTION_BITS;
734
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
735
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
736
v1 -= (delta+1) * v1 / overshoot;
737
}else v1 = (int32)src[offset];
738
if (ofs + (1L<<FRACTION_BITS) >= se) {
740
}else v2 = (int32)src[offset+1];
742
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
743
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
746
v0 = (int32)src[offset-1];
747
v3 = (int32)src[offset+2];
748
ofs &= FRACTION_MASK;
751
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
752
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
753
ofs = (1L << FRACTION_BITS) - ofs;
755
((((((5*v0 - 11*v1 + 7*temp - v3)*
756
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
757
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
758
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
766
vp->echo_delay -= ll >> FRACTION_BITS;
767
if (vp->echo_delay >= 0) ofs -= ll;
769
else ofs -= ll; /* Hopefully the loop is longer than an increment. */
771
if (ofs >= se + (overshoot << FRACTION_BITS))
773
if (!(vp->status&VOICE_FREE))
775
vp->status=VOICE_FREE;
783
vp->vibrato_control_counter=cc;
784
vp->sample_increment=incr;
785
vp->sample_offset=ofs; /* Update offset */
786
return resample_buffer+resample_buffer_offset;
789
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
791
int32 ofsd, v0, v1, v2, v3, temp, overshoot;
794
incr=vp->sample_increment;
800
ofs=vp->sample_offset,
801
se=vp->sample->data_length;
803
*dest=resample_buffer+resample_buffer_offset,
804
*src=vp->sample->data;
806
cc=vp->vibrato_control_counter;
809
#ifdef USE_BIDIR_OVERSHOOT
820
overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
821
if (overshoot < 0) overshoot = -overshoot;
822
/* Play normally until inside the loop region */
823
while (count && (ofs <= ls))
825
i = (ls - ofs) / incr + 1;
826
if (i > count) i = count;
834
for(j = 0; j < i; j++)
837
offset = ofs >> FRACTION_BITS;
840
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
841
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
842
v1 -= (delta+1) * v1 / overshoot;
843
}else v1 = (int32)src[offset];
844
if (ofs + (1L<<FRACTION_BITS) >= se) {
846
}else v2 = (int32)src[offset+1];
848
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
849
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
852
v0 = (int32)src[offset-1];
853
v3 = (int32)src[offset+2];
854
ofs &= FRACTION_MASK;
857
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
858
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
859
ofs = (1L << FRACTION_BITS) - ofs;
861
((((((5*v0 - 11*v1 + 7*temp - v3)*
862
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
863
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
864
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
871
cc = vp->vibrato_control_ratio;
872
incr = update_vibrato(vp, 0);
877
/* Then do the bidirectional looping */
881
/* Precalc how many times we should go through the loop */
883
i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
885
/* fix from M. Izumo */
886
i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
888
if(i > count) i = count;
896
while (i-- && ofs < se)
899
offset = ofs >> FRACTION_BITS;
902
int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
903
v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
904
v1 -= (delta+1) * v1 / overshoot;
905
}else v1 = (int32)src[offset];
906
if (ofs + (1L<<FRACTION_BITS) >= se) {
908
}else v2 = (int32)src[offset+1];
910
((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
911
*dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
914
v0 = (int32)src[offset-1];
915
v3 = (int32)src[offset+2];
916
ofs &= FRACTION_MASK;
919
(((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
920
(int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
921
ofs = (1L << FRACTION_BITS) - ofs;
923
((((((5*v0 - 11*v1 + 7*temp - v3)*
924
(int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
925
v1 = (v1 + v2)/(6L<<FRACTION_BITS);
926
*dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
933
cc = vp->vibrato_control_ratio;
934
incr = update_vibrato(vp, (incr < 0));
939
#ifdef USE_BIDIR_OVERSHOOT
940
/* fold the overshoot back in */
947
#ifdef USE_BIDIR_OVERSHOOT
955
vp->vibrato_control_counter=cc;
956
vp->sample_increment=incr;
957
vp->sample_offset=ofs; /* Update offset */
958
return resample_buffer+resample_buffer_offset;
961
static int rs_update_porta(int v)
969
if(d > -vp->porta_pb)
981
if(vp->porta_pb == 0)
983
vp->porta_control_ratio = 0;
987
return vp->porta_control_ratio;
990
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
993
uint32 n = *countptr;
995
sample_t *(* resampler)(int, uint32 *, int);
996
int cc = vp->porta_control_counter;
999
if(vp->vibrato_control_ratio)
1000
resampler = vib_resample_voice;
1002
resampler = normal_resample_voice;
1008
/* vp->cache = NULL; */
1009
resample_buffer_offset = 0;
1010
while(resample_buffer_offset < n)
1014
if((cc = rs_update_porta(v)) == 0)
1016
i = n - resample_buffer_offset;
1017
resampler(v, &i, mode);
1018
resample_buffer_offset += i;
1023
i = n - resample_buffer_offset;
1026
resampler(v, &i, mode);
1027
resample_buffer_offset += i;
1029
/* if(!loop && vp->status == VOICE_FREE) */
1030
if(vp->status == VOICE_FREE)
1034
*countptr = resample_buffer_offset;
1035
resample_buffer_offset = 0;
1036
vp->porta_control_counter = cc;
1037
return resample_buffer;
1040
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
1042
Voice *vp = &voice[v];
1044
/* vp->cache = NULL; */
1046
return rs_vib_loop(v, vp, countptr);
1048
return rs_vib_plain(v, countptr);
1049
return rs_vib_bidir(v, vp, *countptr);
1052
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
1054
Voice *vp = &voice[v];
1056
return rs_loop(v, vp, countptr);
1058
return rs_plain(v, countptr);
1059
return rs_bidir(v, vp, *countptr);
1062
sample_t *resample_voice(int v, uint32 *countptr)
1064
Voice *vp=&voice[v];
1067
if(!(vp->sample->sample_rate))
1071
/* Pre-resampled data -- just update the offset and check if
1072
we're out of data. */
1073
ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
1074
FRACTION_BITS here... */
1075
if(*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
1077
/* Note finished. Free the voice. */
1078
if (!(vp->status&VOICE_FREE))
1080
vp->status=VOICE_FREE;
1084
/* Let the caller know how much data we had left */
1085
*countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
1088
vp->sample_offset += *countptr << FRACTION_BITS;
1089
return vp->sample->data+ofs;
1092
if (current_interpolation == 2)
1093
return resample_voice_lagrange(v, countptr);
1094
else if (current_interpolation == 3)
1095
return resample_voice_filter(v, countptr);
1097
mode = vp->sample->modes;
1098
if((mode & MODES_LOOPING) &&
1099
((mode & MODES_ENVELOPE) ||
1100
(vp->status & (VOICE_ON | VOICE_SUSTAINED))))
1102
if(mode & MODES_PINGPONG)
1104
/* vp->cache = NULL; */
1113
if(vp->porta_control_ratio)
1114
return porta_resample_voice(v, countptr, mode);
1116
if(vp->vibrato_control_ratio)
1117
return vib_resample_voice(v, countptr, mode);
1119
return normal_resample_voice(v, countptr, mode);
1123
void do_lowpass(Sample *sample, uint32 srate, sample_t *buf, uint32 count, uint32 freq, FLOAT_T resonance)
1125
double a0=0, a1=0, a2=0, b0=0, b1=0;
1126
double x0=0, x1=0, y0=0, y1=0;
1128
double outsamp, insamp, mod_amount=0;
1130
uint32 current_freq;
1132
if (freq < 20) return;
1134
if (freq > srate * 2) {
1135
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1136
"Lowpass: center must be < data rate*2");
1140
current_freq = freq;
1142
if (sample->modEnvToFilterFc)
1143
mod_amount = sample->modEnvToFilterFc;
1145
if (mod_amount < 0.02 && freq >= 13500) return;
1147
voice[0].sample = sample;
1149
/* Ramp up from 0 */
1150
voice[0].modulation_stage=ATTACK;
1151
voice[0].modulation_volume=0;
1152
voice[0].modulation_delay=sample->modulation_rate[DELAY];
1153
cc = voice[0].modulation_counter=0;
1154
recompute_modulation(0);
1156
/* start modulation loop here */
1160
if (mod_amount>0.02) {
1161
if (update_modulation_signal(0)) mod_amount = 0;
1163
current_freq = (uint32)( (double)freq*(1.0 + (mod_amount - 1.0) *
1164
(voice[0].modulation_volume>>22) / 255.0) );
1167
findex = 1 + (current_freq+50) / 100;
1168
if (findex > 100) findex = 100;
1169
a0 = butterworth[findex][0];
1170
a1 = butterworth[findex][1];
1171
a2 = butterworth[findex][2];
1172
b0 = butterworth[findex][3];
1173
b1 = butterworth[findex][4];
1176
if (mod_amount>0.02) cc--;
1180
insamp = (double)samp;
1181
outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
1186
if (outsamp > MAX_DATAVAL) {
1187
outsamp = MAX_DATAVAL;
1189
else if (outsamp < MIN_DATAVAL) {
1190
outsamp = MIN_DATAVAL;
1192
*buf++ = (sample_t)outsamp;
1197
void pre_resample(Sample * sp)
1200
uint32 i, incr, ofs, newlen, count, overshoot;
1201
int16 *newdata, *dest, *src = (int16 *)sp->data, *vptr, *endptr;
1202
int32 v1, v2, v3, v4;
1203
static const char note_name[12][3] =
1205
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
1208
ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * pre-resampling for note %d (%s%d)",
1210
note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
1212
if (sp->sample_rate == play_mode->rate && sp->root_freq == freq_table[(int)(sp->note_to_use)]) {
1215
else a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
1216
((double) (sp->root_freq) * play_mode->rate);
1218
/* if (a<1.0) return; */
1219
if(sp->data_length / a >= 0x7fffffffL)
1221
/* Too large to compute */
1222
ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
1227
endptr = src + (sp->data_length >> FRACTION_BITS) - 1;
1228
if (*endptr < 0) overshoot = (uint32)(-(*endptr / OVERSHOOT_STEP));
1229
else overshoot = (uint32)(*endptr / OVERSHOOT_STEP);
1230
if (overshoot < 2) overshoot = 0;
1232
newlen = (int32)(sp->data_length / a);
1233
count = (newlen >> FRACTION_BITS) - 1;
1234
ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
1236
if((double)newlen + incr >= 0x7fffffffL)
1238
/* Too large to compute */
1239
ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
1244
dest = newdata = (int16 *)safe_malloc((newlen >> (FRACTION_BITS - 1)) + 2 + 2*overshoot);
1249
/* Since we're pre-processing and this doesn't have to be done in
1250
real-time, we go ahead and do the full sliding cubic interpolation. */
1252
for(i = 0; i < count + overshoot; i++)
1254
vptr = src + (ofs >> FRACTION_BITS);
1255
if (i < count - 2 || !overshoot)
1264
if (i < count + 1) v1 = *(vptr - 1);
1265
else v1 = *endptr - (count-i+2) * *endptr / overshoot;
1266
if (i < count) v2 = *vptr;
1267
else v2 = *endptr - (count-i+1) * *endptr / overshoot;
1268
if (i < count - 1) v3 = *(vptr + 1);
1269
else v3 = *endptr - (count-i) * *endptr / overshoot;
1270
v4 = *endptr - (count-i-1) * *endptr / overshoot;
1272
xdiff = FRSCALENEG((int32)(ofs & FRACTION_MASK), FRACTION_BITS);
1273
*dest++ = v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
1274
xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4)));
1280
if ((int32)(ofs & FRACTION_MASK))
1282
v1 = src[ofs >> FRACTION_BITS];
1283
v2 = src[(ofs >> FRACTION_BITS) + 1];
1284
*dest++ = (int16)(v1 + ((int32)((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
1287
*dest++ = src[ofs >> FRACTION_BITS];
1288
*dest++ = *(dest - 1) / 2;
1289
*dest++ = *(dest - 1) / 2;
1292
sp->data_length = newlen + (overshoot << FRACTION_BITS);
1293
sp->loop_start = (int32)(sp->loop_start / a);
1294
sp->loop_end = (int32)(sp->loop_end / a);
1296
sp->data = (sample_t *) newdata;
1297
sp->sample_rate = 0;