~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/resample_f.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        $Id: resample_f.cpp,v 1.4 2001/04/12 09:09:23 garbanzo Exp $
 
3
 
 
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>
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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.
 
21
 
 
22
*/
 
23
 
 
24
#include <math.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
 
 
28
#include "config.h"
 
29
#include "common.h"
 
30
#include "instrum.h"
 
31
#include "playmidi.h"
 
32
#include "output.h"
 
33
#include "controls.h"
 
34
#include "tables.h"
 
35
#include "resample.h"
 
36
 
 
37
 
 
38
#define LINEAR_INTERPOLATION
 
39
#define CSPLINE_INTERPOLATION
 
40
#define FILTER_INTERPOLATION
 
41
#define ENVELOPE_PITCH_MODULATION
 
42
 
 
43
 
 
44
#ifdef LOOKUP_HACK
 
45
#define MAX_DATAVAL 127
 
46
#define MIN_DATAVAL -128
 
47
#else
 
48
#define MAX_DATAVAL 32767
 
49
#define MIN_DATAVAL -32768
 
50
#endif
 
51
 
 
52
#define OVERSHOOT_STEP 50
 
53
 
 
54
 
 
55
static sample_t *vib_resample_voice(int, uint32 *, int);
 
56
static sample_t *normal_resample_voice(int, uint32 *, int);
 
57
 
 
58
 
 
59
static void update_lfo(int v)
 
60
{
 
61
  FLOAT_T depth=voice[v].modLfoToFilterFc;
 
62
 
 
63
  if (voice[v].lfo_sweep)
 
64
    {
 
65
      /* Update sweep position */
 
66
 
 
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 */
 
70
      else
 
71
        {
 
72
          /* Need to adjust depth */
 
73
          depth *= (FLOAT_T)voice[v].lfo_sweep_position / (FLOAT_T)(1<<SWEEP_SHIFT);
 
74
        }
 
75
    }
 
76
 
 
77
  voice[v].lfo_phase += voice[v].lfo_phase_increment;
 
78
 
 
79
  voice[v].lfo_volume = depth;
 
80
}
 
81
 
 
82
 
 
83
static int calc_bw_index(int v)
 
84
{
 
85
  FLOAT_T mod_amount=voice[v].modEnvToFilterFc;
 
86
  int32 freq = voice[v].sample->cutoff_freq;
 
87
  int ix;
 
88
 
 
89
  if (voice[v].lfo_phase_increment) update_lfo(v);
 
90
 
 
91
  if (!voice[v].lfo_phase_increment && update_modulation_signal(v)) return 0;
 
92
 
 
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); */
 
98
  }
 
99
 
 
100
  if (mod_amount > 0.001) {
 
101
    if (voice[v].modulation_volume)
 
102
       freq =
 
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 );
 
105
/*
 
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,
 
108
mod_amount);
 
109
*/
 
110
        ix = 1 + (freq+50) / 100;
 
111
        if (ix > 100) ix = 100;
 
112
        voice[v].bw_index = ix;
 
113
        return 1;
 
114
  }
 
115
  voice[v].bw_index = 1 + (freq+50) / 100;
 
116
  return 0;
 
117
}
 
118
 
 
119
/*************** resampling with fixed increment *****************/
 
120
 
 
121
static sample_t *rs_plain(int v, uint32 *countptr)
 
122
{
 
123
  /* Play sample until end, then free the voice. */
 
124
  Voice
 
125
    *vp=&voice[v];
 
126
  int32   v0, v1, v2, v3, temp, overshoot;
 
127
  int offset;
 
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;
 
131
  sample_t newsample;
 
132
  sample_t
 
133
    *dest=resample_buffer+resample_buffer_offset,
 
134
    *src=vp->sample->data;
 
135
  int32
 
136
    incr=vp->sample_increment;
 
137
  uint32
 
138
    ofs=vp->sample_offset;
 
139
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
140
  uint32
 
141
    ls=0,
 
142
    le=vp->sample->data_length;
 
143
#endif /* LAGRANGE_INTERPOLATION */
 
144
  uint32
 
145
    se=vp->sample->data_length;
 
146
  uint32
 
147
    count=*countptr;
 
148
 
 
149
  if (!incr) return resample_buffer+resample_buffer_offset; /* --gl */
 
150
 
 
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];
 
158
 
 
159
    while (count--)
 
160
    {
 
161
 
 
162
 
 
163
        offset = ofs >> FRACTION_BITS;
 
164
 
 
165
        if (ofs >= se) {
 
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) {
 
171
                v2 = v1;
 
172
        }else  v2 = (int32)src[offset+1];
 
173
        if(dont_cspline ||
 
174
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
175
                if (!cc_count--) {
 
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];
 
184
                    }
 
185
                    incr = calc_mod_freq(v, incr);
 
186
                }
 
187
                if (dont_filter_melodic) bw_index = 0;
 
188
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
189
                if (bw_index) {
 
190
                    insamp = (FLOAT_T)newsample;
 
191
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
192
                    x1 = x0;
 
193
                    x0 = insamp;
 
194
                    y1 = y0;
 
195
                    y0 = outsamp;
 
196
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
197
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
198
                }
 
199
        }else{
 
200
                ofsdu=ofs;
 
201
                v0 = (int32)src[offset-1];
 
202
                v3 = (int32)src[offset+2];
 
203
                ofs &= FRACTION_MASK;
 
204
                temp=v2;
 
205
                v2 = (6*v2 +
 
206
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
207
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
208
                ofs = (1L << FRACTION_BITS) - ofs;
 
209
                v1 = (6*v1 +
 
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);
 
213
                if (!cc_count--) {
 
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];
 
222
                    }
 
223
                    incr = calc_mod_freq(v, incr);
 
224
                }
 
225
                if (dont_filter_melodic) bw_index = 0;
 
226
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
227
                if (bw_index) {
 
228
                    insamp = (FLOAT_T)newsample;
 
229
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
230
                    x1 = x0;
 
231
                    x0 = insamp;
 
232
                    y1 = y0;
 
233
                    y0 = outsamp;
 
234
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
235
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
236
                }
 
237
                ofs=ofsdu;
 
238
        }
 
239
        *dest++ = newsample;
 
240
      ofs += incr;
 
241
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
242
        {
 
243
          if (!(vp->status&VOICE_FREE))
 
244
            {
 
245
              vp->status=VOICE_FREE;
 
246
              ctl->note(v);
 
247
            }
 
248
          *countptr-=count+1;
 
249
          break;
 
250
        }
 
251
    }
 
252
 
 
253
  vp->sample_offset=ofs; /* Update offset */
 
254
  vp->current_x0=x0;
 
255
  vp->current_x1=x1;
 
256
  vp->current_y0=y0;
 
257
  vp->current_y1=y1;
 
258
  vp->bw_index=bw_index;
 
259
  vp->modulation_counter=cc_count;
 
260
  return resample_buffer+resample_buffer_offset;
 
261
}
 
262
 
 
263
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
 
264
{
 
265
  /* Play sample until end-of-loop, skip back and continue. */
 
266
  int32   v0, v1, v2, v3, temp, overshoot;
 
267
  int offset;
 
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;
 
271
  sample_t newsample;
 
272
  int32
 
273
    incr=vp->sample_increment;
 
274
  uint32
 
275
    ofs=vp->sample_offset;
 
276
  uint32
 
277
    le=vp->loop_end,
 
278
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
279
    ls=vp->loop_start,
 
280
#endif /* LAGRANGE_INTERPOLATION */
 
281
    ll=le - vp->loop_start;
 
282
  sample_t
 
283
    *dest=resample_buffer+resample_buffer_offset,
 
284
    *src=vp->sample->data;
 
285
  uint32
 
286
    se=vp->sample->data_length,
 
287
    count = *countptr;
 
288
  int
 
289
    flag_exit_loop;
 
290
 
 
291
 
 
292
  flag_exit_loop =
 
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 ) ;
 
296
 
 
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];
 
304
 
 
305
  while (count--)
 
306
    {
 
307
 
 
308
 
 
309
        offset = ofs >> FRACTION_BITS;
 
310
 
 
311
        if (ofs >= se) {
 
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) {
 
317
                v2 = v1;
 
318
        }else  v2 = (int32)src[offset+1];
 
319
        if(dont_cspline ||
 
320
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
321
                if (!cc_count--) {
 
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];
 
330
                    }
 
331
                    incr = calc_mod_freq(v, incr);
 
332
                }
 
333
                if (dont_filter_melodic) bw_index = 0;
 
334
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
335
                if (bw_index) {
 
336
                    insamp = (FLOAT_T)newsample;
 
337
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
338
                    x1 = x0;
 
339
                    x0 = insamp;
 
340
                    y1 = y0;
 
341
                    y0 = outsamp;
 
342
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
343
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
344
                }
 
345
        }else{
 
346
                ofsdu=ofs;
 
347
                v0 = (int32)src[offset-1];
 
348
                v3 = (int32)src[offset+2];
 
349
                ofs &= FRACTION_MASK;
 
350
                temp=v2;
 
351
                v2 = (6*v2 +
 
352
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
353
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
354
                ofs = (1L << FRACTION_BITS) - ofs;
 
355
                v1 = (6*v1 +
 
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);
 
359
                if (!cc_count--) {
 
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];
 
368
                    }
 
369
                    incr = calc_mod_freq(v, incr);
 
370
                }
 
371
                if (dont_filter_melodic) bw_index = 0;
 
372
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
373
                if (bw_index) {
 
374
                    insamp = (FLOAT_T)newsample;
 
375
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
376
                    x1 = x0;
 
377
                    x0 = insamp;
 
378
                    y1 = y0;
 
379
                    y0 = outsamp;
 
380
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
381
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
382
                }
 
383
                ofs=ofsdu;
 
384
        }
 
385
        *dest++ = newsample;
 
386
      ofs += incr;
 
387
      if (ofs>=le)
 
388
        {
 
389
          if (flag_exit_loop)
 
390
            {
 
391
                vp->echo_delay -= ll >> FRACTION_BITS;
 
392
                if (vp->echo_delay >= 0) ofs -= ll;
 
393
            }
 
394
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
395
        }
 
396
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
397
        {
 
398
          if (!(vp->status&VOICE_FREE))
 
399
            {
 
400
              vp->status=VOICE_FREE;
 
401
              ctl->note(v);
 
402
            }
 
403
          *countptr-=count+1;
 
404
          break;
 
405
        }
 
406
    }
 
407
 
 
408
  vp->sample_offset=ofs; /* Update offset */
 
409
  vp->current_x0=x0;
 
410
  vp->current_x1=x1;
 
411
  vp->current_y0=y0;
 
412
  vp->current_y1=y1;
 
413
  vp->bw_index=bw_index;
 
414
  vp->modulation_counter=cc_count;
 
415
  return resample_buffer+resample_buffer_offset;
 
416
}
 
417
 
 
418
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
 
419
{
 
420
  int32   v0, v1, v2, v3, temp, overshoot;
 
421
  int offset;
 
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;
 
425
  sample_t newsample;
 
426
  int32
 
427
    incr=vp->sample_increment;
 
428
  uint32
 
429
    le=vp->loop_end,
 
430
    ls=vp->loop_start;
 
431
  uint32
 
432
    ofs=vp->sample_offset,
 
433
    se=vp->sample->data_length;
 
434
  sample_t
 
435
    *dest=resample_buffer+resample_buffer_offset,
 
436
    *src=vp->sample->data;
 
437
 
 
438
 
 
439
#ifdef USE_BIDIR_OVERSHOOT
 
440
  int32
 
441
    le2 = le<<1,
 
442
    ls2 = ls<<1;
 
443
#endif
 
444
  uint32
 
445
    i, j;
 
446
  /* Play normally until inside the loop region */
 
447
 
 
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];
 
455
 
 
456
  if (ofs <= ls)
 
457
    {
 
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;
 
462
        else
 
463
      i = (ls - ofs) / incr + 1;
 
464
      if (i > count)
 
465
        {
 
466
          i = count;
 
467
          count = 0;
 
468
        }
 
469
      else count -= i;
 
470
      for(j = 0; j < i; j++)
 
471
        {
 
472
 
 
473
 
 
474
        offset = ofs >> FRACTION_BITS;
 
475
 
 
476
        if (ofs >= se) {
 
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) {
 
482
                v2 = v1;
 
483
        }else  v2 = (int32)src[offset+1];
 
484
        if(dont_cspline ||
 
485
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
486
                if (!cc_count--) {
 
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];
 
495
                    }
 
496
                    incr = calc_mod_freq(v, incr);
 
497
                }
 
498
                if (dont_filter_melodic) bw_index = 0;
 
499
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
500
                if (bw_index) {
 
501
                    insamp = (FLOAT_T)newsample;
 
502
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
503
                    x1 = x0;
 
504
                    x0 = insamp;
 
505
                    y1 = y0;
 
506
                    y0 = outsamp;
 
507
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
508
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
509
                }
 
510
        }else{
 
511
                ofsdu=ofs;
 
512
                v0 = (int32)src[offset-1];
 
513
                v3 = (int32)src[offset+2];
 
514
                ofs &= FRACTION_MASK;
 
515
                temp=v2;
 
516
                v2 = (6*v2 +
 
517
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
518
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
519
                ofs = (1L << FRACTION_BITS) - ofs;
 
520
                v1 = (6*v1 +
 
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);
 
524
                if (!cc_count--) {
 
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];
 
533
                    }
 
534
                    incr = calc_mod_freq(v, incr);
 
535
                }
 
536
                if (dont_filter_melodic) bw_index = 0;
 
537
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
538
                if (bw_index) {
 
539
                    insamp = (FLOAT_T)newsample;
 
540
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
541
                    x1 = x0;
 
542
                    x0 = insamp;
 
543
                    y1 = y0;
 
544
                    y0 = outsamp;
 
545
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
546
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
547
                }
 
548
                ofs=ofsdu;
 
549
        }
 
550
        *dest++ = newsample;
 
551
          ofs += incr;
 
552
        }
 
553
    }
 
554
 
 
555
  /* Then do the bidirectional looping */
 
556
 
 
557
  while(count)
 
558
    {
 
559
      /* Precalc how many times we should go through the loop */
 
560
#if 1
 
561
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
562
#else
 
563
/* fix from M. Izumo */
 
564
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
565
#endif
 
566
      if (i > count)
 
567
        {
 
568
          i = count;
 
569
          count = 0;
 
570
        }
 
571
      else count -= i;
 
572
      for(j = 0; j < i && ofs < se; j++)
 
573
        {
 
574
 
 
575
 
 
576
        offset = ofs >> FRACTION_BITS;
 
577
 
 
578
        if (ofs >= se) {
 
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) {
 
584
                v2 = v1;
 
585
        }else  v2 = (int32)src[offset+1];
 
586
        if(dont_cspline ||
 
587
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
588
                if (!cc_count--) {
 
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];
 
597
                    }
 
598
                    incr = calc_mod_freq(v, incr);
 
599
                }
 
600
                if (dont_filter_melodic) bw_index = 0;
 
601
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
602
                if (bw_index) {
 
603
                    insamp = (FLOAT_T)newsample;
 
604
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
605
                    x1 = x0;
 
606
                    x0 = insamp;
 
607
                    y1 = y0;
 
608
                    y0 = outsamp;
 
609
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
610
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
611
                }
 
612
        }else{
 
613
                ofsdu=ofs;
 
614
                v0 = (int32)src[offset-1];
 
615
                v3 = (int32)src[offset+2];
 
616
                ofs &= FRACTION_MASK;
 
617
                temp=v2;
 
618
                v2 = (6*v2 +
 
619
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
620
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
621
                ofs = (1L << FRACTION_BITS) - ofs;
 
622
                v1 = (6*v1 +
 
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);
 
626
                if (!cc_count--) {
 
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];
 
635
                    }
 
636
                    incr = calc_mod_freq(v, incr);
 
637
                }
 
638
                if (dont_filter_melodic) bw_index = 0;
 
639
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
640
                if (bw_index) {
 
641
                    insamp = (FLOAT_T)newsample;
 
642
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
643
                    x1 = x0;
 
644
                    x0 = insamp;
 
645
                    y1 = y0;
 
646
                    y0 = outsamp;
 
647
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
648
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
649
                }
 
650
                ofs=ofsdu;
 
651
        }
 
652
        *dest++ = newsample;
 
653
          ofs += incr;
 
654
        }
 
655
#ifdef USE_BIDIR_OVERSHOOT
 
656
      if (ofs>=le)
 
657
        {
 
658
          /* fold the overshoot back in */
 
659
          ofs = le2 - ofs;
 
660
          incr *= -1;
 
661
        }
 
662
      else if (ofs <= ls)
 
663
        {
 
664
          ofs = ls2 - ofs;
 
665
          incr *= -1;
 
666
        }
 
667
#else
 
668
          incr *= -1;
 
669
#endif
 
670
    }
 
671
 
 
672
  vp->sample_increment=incr;
 
673
  vp->sample_offset=ofs; /* Update offset */
 
674
  vp->current_x0=x0;
 
675
  vp->current_x1=x1;
 
676
  vp->current_y0=y0;
 
677
  vp->current_y1=y1;
 
678
  vp->bw_index=bw_index;
 
679
  vp->modulation_counter=cc_count;
 
680
  return resample_buffer+resample_buffer_offset;
 
681
}
 
682
 
 
683
/*********************** vibrato versions ***************************/
 
684
 
 
685
/* We only need to compute one half of the vibrato sine cycle */
 
686
static uint32 vib_phase_to_inc_ptr(uint32 phase)
 
687
{
 
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;
 
692
  else
 
693
    return phase-VIBRATO_SAMPLE_INCREMENTS/2;
 
694
}
 
695
 
 
696
static int32 update_vibrato(Voice *vp, int sign)
 
697
{
 
698
  uint32 depth, freq=vp->frequency;
 
699
#ifdef ENVELOPE_PITCH_MODULATION
 
700
  FLOAT_T mod_amount=vp->sample->modEnvToPitch;
 
701
#endif
 
702
  uint32 phase;
 
703
  int pb;
 
704
  double a;
 
705
 
 
706
  if(vp->vibrato_delay > 0)
 
707
  {
 
708
      vp->vibrato_delay -= vp->vibrato_control_ratio;
 
709
      if(vp->vibrato_delay > 0)
 
710
          return vp->sample_increment;
 
711
  }
 
712
 
 
713
  if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
 
714
    vp->vibrato_phase=0;
 
715
  phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
 
716
 
 
717
  if (vp->vibrato_sample_increment[phase])
 
718
    {
 
719
      if (sign)
 
720
        return -vp->vibrato_sample_increment[phase];
 
721
      else
 
722
        return vp->vibrato_sample_increment[phase];
 
723
    }
 
724
 
 
725
  /* Need to compute this sample increment. */
 
726
 
 
727
  depth = vp->vibrato_depth;
 
728
  if(depth < vp->modulation_wheel)
 
729
      depth = vp->modulation_wheel;
 
730
  depth <<= 7;
 
731
 
 
732
  if (vp->vibrato_sweep && !vp->modulation_wheel)
 
733
    {
 
734
      /* Need to update sweep */
 
735
      vp->vibrato_sweep_position += vp->vibrato_sweep;
 
736
      if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
 
737
        vp->vibrato_sweep=0;
 
738
      else
 
739
        {
 
740
          /* Adjust depth */
 
741
          depth *= vp->vibrato_sweep_position;
 
742
          depth >>= SWEEP_SHIFT;
 
743
        }
 
744
    }
 
745
 
 
746
#ifdef ENVELOPE_PITCH_MODULATION
 
747
#ifndef FILTER_INTERPOLATION
 
748
  if (update_modulation_signal(0)) mod_amount = 0;
 
749
  else
 
750
#endif
 
751
  if (mod_amount>0.02)
 
752
   freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
 
753
#endif
 
754
 
 
755
  pb=(int)((sine(vp->vibrato_phase *
 
756
                        (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
 
757
            * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
 
758
 
 
759
  a = FRSCALE(((double)(vp->sample->sample_rate) *
 
760
                  (double)(freq)) /
 
761
                 ((double)(vp->sample->root_freq) *
 
762
                  (double)(play_mode->rate)),
 
763
                 FRACTION_BITS);
 
764
  if(pb<0)
 
765
  {
 
766
      pb = -pb;
 
767
      a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
768
  }
 
769
  else
 
770
      a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
771
  a += 0.5;
 
772
 
 
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;
 
776
 
 
777
  if (sign)
 
778
    a = -a; /* need to preserve the loop direction */
 
779
 
 
780
  return (int32) a;
 
781
}
 
782
 
 
783
static sample_t *rs_vib_plain(int v, uint32 *countptr)
 
784
{
 
785
 
 
786
  /* Play sample until end, then free the voice. */
 
787
 
 
788
  Voice *vp=&voice[v];
 
789
  int32   v0, v1, v2, v3, temp, overshoot;
 
790
  int offset;
 
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;
 
794
  sample_t newsample;
 
795
  sample_t
 
796
    *dest=resample_buffer+resample_buffer_offset,
 
797
    *src=vp->sample->data;
 
798
  int32
 
799
    incr=vp->sample_increment;
 
800
/*WHY int32??*/
 
801
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
802
  uint32
 
803
    ls=0,
 
804
    le=vp->sample->data_length;
 
805
#endif /* LAGRANGE_INTERPOLATION */
 
806
  uint32
 
807
    ofs=vp->sample_offset,
 
808
    se=vp->sample->data_length,
 
809
    count=*countptr;
 
810
  uint32
 
811
    cc=vp->vibrato_control_counter;
 
812
 
 
813
  /* This has never been tested */
 
814
 
 
815
  if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
 
816
 
 
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];
 
824
  while (count--)
 
825
    {
 
826
      if (!cc--)
 
827
        {
 
828
          cc=vp->vibrato_control_ratio;
 
829
          incr=update_vibrato(vp, 0);
 
830
        }
 
831
 
 
832
 
 
833
        offset = ofs >> FRACTION_BITS;
 
834
 
 
835
        if (ofs >= se) {
 
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) {
 
841
                v2 = v1;
 
842
        }else  v2 = (int32)src[offset+1];
 
843
        if(dont_cspline ||
 
844
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
845
                if (!cc_count--) {
 
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];
 
854
                    }
 
855
                    incr = calc_mod_freq(v, incr);
 
856
                }
 
857
                if (dont_filter_melodic) bw_index = 0;
 
858
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
859
                if (bw_index) {
 
860
                    insamp = (FLOAT_T)newsample;
 
861
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
862
                    x1 = x0;
 
863
                    x0 = insamp;
 
864
                    y1 = y0;
 
865
                    y0 = outsamp;
 
866
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
867
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
868
                }
 
869
        }else{
 
870
                ofsdu=ofs;
 
871
                v0 = (int32)src[offset-1];
 
872
                v3 = (int32)src[offset+2];
 
873
                ofs &= FRACTION_MASK;
 
874
                temp=v2;
 
875
                v2 = (6*v2 +
 
876
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
877
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
878
                ofs = (1L << FRACTION_BITS) - ofs;
 
879
                v1 = (6*v1 +
 
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);
 
883
                if (!cc_count--) {
 
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];
 
892
                    }
 
893
                    incr = calc_mod_freq(v, incr);
 
894
                }
 
895
                if (dont_filter_melodic) bw_index = 0;
 
896
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
897
                if (bw_index) {
 
898
                    insamp = (FLOAT_T)newsample;
 
899
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
900
                    x1 = x0;
 
901
                    x0 = insamp;
 
902
                    y1 = y0;
 
903
                    y0 = outsamp;
 
904
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
905
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
906
                }
 
907
                ofs=ofsdu;
 
908
        }
 
909
        *dest++ = newsample;
 
910
      ofs += incr;
 
911
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
912
        {
 
913
          if (!(vp->status&VOICE_FREE))
 
914
            {
 
915
              vp->status=VOICE_FREE;
 
916
              ctl->note(v);
 
917
            }
 
918
          *countptr-=count+1;
 
919
          break;
 
920
        }
 
921
    }
 
922
 
 
923
  vp->vibrato_control_counter=cc;
 
924
  vp->sample_increment=incr;
 
925
  vp->sample_offset=ofs; /* Update offset */
 
926
  vp->current_x0=x0;
 
927
  vp->current_x1=x1;
 
928
  vp->current_y0=y0;
 
929
  vp->current_y1=y1;
 
930
  vp->bw_index=bw_index;
 
931
  vp->modulation_counter=cc_count;
 
932
  return resample_buffer+resample_buffer_offset;
 
933
}
 
934
 
 
935
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
 
936
{
 
937
  /* Play sample until end-of-loop, skip back and continue. */
 
938
  int32   v0, v1, v2, v3, temp, overshoot;
 
939
  int offset;
 
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;
 
943
  sample_t newsample;
 
944
  int32
 
945
    incr=vp->sample_increment;
 
946
/*WHY int32??*/
 
947
  uint32
 
948
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
949
    ls=vp->loop_start,
 
950
#endif /* LAGRANGE_INTERPOLATION */
 
951
    le=vp->loop_end,
 
952
    ll=le - vp->loop_start;
 
953
  sample_t
 
954
    *dest=resample_buffer+resample_buffer_offset,
 
955
    *src=vp->sample->data;
 
956
  uint32
 
957
    ofs=vp->sample_offset,
 
958
    se=vp->sample->data_length,
 
959
    count = *countptr;
 
960
  uint32
 
961
    cc=vp->vibrato_control_counter;
 
962
  int
 
963
    flag_exit_loop;
 
964
 
 
965
 
 
966
  flag_exit_loop =
 
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 ) ;
 
970
 
 
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];
 
978
  while (count--)
 
979
    {
 
980
      if (!cc--)
 
981
        {
 
982
          cc=vp->vibrato_control_ratio;
 
983
          incr=update_vibrato(vp, 0);
 
984
        }
 
985
 
 
986
 
 
987
        offset = ofs >> FRACTION_BITS;
 
988
 
 
989
        if (ofs >= se) {
 
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) {
 
995
                v2 = v1;
 
996
        }else  v2 = (int32)src[offset+1];
 
997
        if(dont_cspline ||
 
998
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
999
                if (!cc_count--) {
 
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];
 
1008
                    }
 
1009
                    incr = calc_mod_freq(v, incr);
 
1010
                }
 
1011
                if (dont_filter_melodic) bw_index = 0;
 
1012
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
1013
                if (bw_index) {
 
1014
                    insamp = (FLOAT_T)newsample;
 
1015
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1016
                    x1 = x0;
 
1017
                    x0 = insamp;
 
1018
                    y1 = y0;
 
1019
                    y0 = outsamp;
 
1020
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1021
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
1022
                }
 
1023
        }else{
 
1024
                ofsdu=ofs;
 
1025
                v0 = (int32)src[offset-1];
 
1026
                v3 = (int32)src[offset+2];
 
1027
                ofs &= FRACTION_MASK;
 
1028
                temp=v2;
 
1029
                v2 = (6*v2 +
 
1030
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
1031
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
1032
                ofs = (1L << FRACTION_BITS) - ofs;
 
1033
                v1 = (6*v1 +
 
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);
 
1037
                if (!cc_count--) {
 
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];
 
1046
                    }
 
1047
                    incr = calc_mod_freq(v, incr);
 
1048
                }
 
1049
                if (dont_filter_melodic) bw_index = 0;
 
1050
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
1051
                if (bw_index) {
 
1052
                    insamp = (FLOAT_T)newsample;
 
1053
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1054
                    x1 = x0;
 
1055
                    x0 = insamp;
 
1056
                    y1 = y0;
 
1057
                    y0 = outsamp;
 
1058
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1059
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
1060
                }
 
1061
                ofs=ofsdu;
 
1062
        }
 
1063
        *dest++ = newsample;
 
1064
      ofs += incr;
 
1065
      if (ofs>=le)
 
1066
        {
 
1067
          if (flag_exit_loop)
 
1068
            {
 
1069
                vp->echo_delay -= ll >> FRACTION_BITS;
 
1070
                if (vp->echo_delay >= 0) ofs -= ll;
 
1071
            }
 
1072
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
1073
        }
 
1074
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
1075
        {
 
1076
          if (!(vp->status&VOICE_FREE))
 
1077
            {
 
1078
              vp->status=VOICE_FREE;
 
1079
              ctl->note(v);
 
1080
            }
 
1081
          *countptr-=count+1;
 
1082
          break;
 
1083
        }
 
1084
    }
 
1085
 
 
1086
  vp->vibrato_control_counter=cc;
 
1087
  vp->sample_increment=incr;
 
1088
  vp->sample_offset=ofs; /* Update offset */
 
1089
  vp->current_x0=x0;
 
1090
  vp->current_x1=x1;
 
1091
  vp->current_y0=y0;
 
1092
  vp->current_y1=y1;
 
1093
  vp->bw_index=bw_index;
 
1094
  vp->modulation_counter=cc_count;
 
1095
  return resample_buffer+resample_buffer_offset;
 
1096
}
 
1097
 
 
1098
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
 
1099
{
 
1100
  int32   v0, v1, v2, v3, temp, overshoot;
 
1101
  int offset;
 
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;
 
1105
  sample_t newsample;
 
1106
  int32
 
1107
    incr=vp->sample_increment;
 
1108
/*WHY int32??*/
 
1109
  uint32
 
1110
    le=vp->loop_end,
 
1111
    ls=vp->loop_start;
 
1112
  uint32
 
1113
    ofs=vp->sample_offset,
 
1114
    se=vp->sample->data_length;
 
1115
  sample_t
 
1116
    *dest=resample_buffer+resample_buffer_offset,
 
1117
    *src=vp->sample->data;
 
1118
  uint32
 
1119
    cc=vp->vibrato_control_counter;
 
1120
 
 
1121
 
 
1122
#ifdef USE_BIDIR_OVERSHOOT
 
1123
  uint32
 
1124
    le2=le<<1,
 
1125
    ls2=ls<<1;
 
1126
#endif
 
1127
  uint32
 
1128
    i, j;
 
1129
  int
 
1130
    vibflag = 0;
 
1131
 
 
1132
 
 
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))
 
1142
    {
 
1143
      i = (ls - ofs) / incr + 1;
 
1144
      if (i > count) i = count;
 
1145
      if (i > cc)
 
1146
        {
 
1147
          i = cc;
 
1148
          vibflag = 1;
 
1149
        }
 
1150
      else cc -= i;
 
1151
      count -= i;
 
1152
      for(j = 0; j < i; j++)
 
1153
        {
 
1154
 
 
1155
 
 
1156
        offset = ofs >> FRACTION_BITS;
 
1157
 
 
1158
        if (ofs >= se) {
 
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) {
 
1164
                v2 = v1;
 
1165
        }else  v2 = (int32)src[offset+1];
 
1166
        if(dont_cspline ||
 
1167
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
1168
                if (!cc_count--) {
 
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];
 
1177
                    }
 
1178
                    incr = calc_mod_freq(v, incr);
 
1179
                }
 
1180
                if (dont_filter_melodic) bw_index = 0;
 
1181
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
1182
                if (bw_index) {
 
1183
                    insamp = (FLOAT_T)newsample;
 
1184
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1185
                    x1 = x0;
 
1186
                    x0 = insamp;
 
1187
                    y1 = y0;
 
1188
                    y0 = outsamp;
 
1189
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1190
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
1191
                }
 
1192
        }else{
 
1193
                ofsdu=ofs;
 
1194
                v0 = (int32)src[offset-1];
 
1195
                v3 = (int32)src[offset+2];
 
1196
                ofs &= FRACTION_MASK;
 
1197
                temp=v2;
 
1198
                v2 = (6*v2 +
 
1199
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
1200
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
1201
                ofs = (1L << FRACTION_BITS) - ofs;
 
1202
                v1 = (6*v1 +
 
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);
 
1206
                if (!cc_count--) {
 
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];
 
1215
                    }
 
1216
                    incr = calc_mod_freq(v, incr);
 
1217
                }
 
1218
                if (dont_filter_melodic) bw_index = 0;
 
1219
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
1220
                if (bw_index) {
 
1221
                    insamp = (FLOAT_T)newsample;
 
1222
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1223
                    x1 = x0;
 
1224
                    x0 = insamp;
 
1225
                    y1 = y0;
 
1226
                    y0 = outsamp;
 
1227
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1228
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
1229
                }
 
1230
                ofs=ofsdu;
 
1231
        }
 
1232
        *dest++ = newsample;
 
1233
          ofs += incr;
 
1234
        }
 
1235
      if (vibflag)
 
1236
        {
 
1237
          cc = vp->vibrato_control_ratio;
 
1238
          incr = update_vibrato(vp, 0);
 
1239
          vibflag = 0;
 
1240
        }
 
1241
    }
 
1242
 
 
1243
  /* Then do the bidirectional looping */
 
1244
 
 
1245
  while (count)
 
1246
    {
 
1247
      /* Precalc how many times we should go through the loop */
 
1248
#if 1
 
1249
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
1250
#else
 
1251
/* fix from M. Izumo */
 
1252
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
1253
#endif
 
1254
      if(i > count) i = count;
 
1255
      if(i > cc)
 
1256
        {
 
1257
          i = cc;
 
1258
          vibflag = 1;
 
1259
        }
 
1260
      else cc -= i;
 
1261
      count -= i;
 
1262
      while (i-- && ofs < se)
 
1263
        {
 
1264
 
 
1265
 
 
1266
        offset = ofs >> FRACTION_BITS;
 
1267
 
 
1268
        if (ofs >= se) {
 
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) {
 
1274
                v2 = v1;
 
1275
        }else  v2 = (int32)src[offset+1];
 
1276
        if(dont_cspline ||
 
1277
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
1278
                if (!cc_count--) {
 
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];
 
1287
                    }
 
1288
                    incr = calc_mod_freq(v, incr);
 
1289
                }
 
1290
                if (dont_filter_melodic) bw_index = 0;
 
1291
                newsample = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
1292
                if (bw_index) {
 
1293
                    insamp = (FLOAT_T)newsample;
 
1294
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1295
                    x1 = x0;
 
1296
                    x0 = insamp;
 
1297
                    y1 = y0;
 
1298
                    y0 = outsamp;
 
1299
                    newsample = (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1300
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp);
 
1301
                }
 
1302
        }else{
 
1303
                ofsdu=ofs;
 
1304
                v0 = (int32)src[offset-1];
 
1305
                v3 = (int32)src[offset+2];
 
1306
                ofs &= FRACTION_MASK;
 
1307
                temp=v2;
 
1308
                v2 = (6*v2 +
 
1309
                      ((((((5*v3 - 11*v2 + 7*v1 - v0)*
 
1310
                       ofs)>>FRACTION_BITS)*ofs)>>(FRACTION_BITS+2))-1))*ofs;
 
1311
                ofs = (1L << FRACTION_BITS) - ofs;
 
1312
                v1 = (6*v1 +
 
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);
 
1316
                if (!cc_count--) {
 
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];
 
1325
                    }
 
1326
                    incr = calc_mod_freq(v, incr);
 
1327
                }
 
1328
                if (dont_filter_melodic) bw_index = 0;
 
1329
                newsample = (v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)v1);
 
1330
                if (bw_index) {
 
1331
                    insamp = (FLOAT_T)newsample;
 
1332
                    outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1333
                    x1 = x0;
 
1334
                    x0 = insamp;
 
1335
                    y1 = y0;
 
1336
                    y0 = outsamp;
 
1337
                    newsample = (sample_t)( (outsamp > MAX_DATAVAL)? MAX_DATAVAL:
 
1338
                        ((outsamp < MIN_DATAVAL)? MIN_DATAVAL: (sample_t)outsamp) );
 
1339
                }
 
1340
                ofs=ofsdu;
 
1341
        }
 
1342
        *dest++ = newsample;
 
1343
          ofs += incr;
 
1344
        }
 
1345
      if (vibflag)
 
1346
        {
 
1347
          cc = vp->vibrato_control_ratio;
 
1348
          incr = update_vibrato(vp, (incr < 0));
 
1349
          vibflag = 0;
 
1350
        }
 
1351
      if (ofs >= le)
 
1352
        {
 
1353
#ifdef USE_BIDIR_OVERSHOOT
 
1354
          /* fold the overshoot back in */
 
1355
          ofs = le2 - ofs;
 
1356
#endif
 
1357
          incr *= -1;
 
1358
        }
 
1359
      else if (ofs <= ls)
 
1360
        {
 
1361
#ifdef USE_BIDIR_OVERSHOOT
 
1362
          ofs = ls2 - ofs;
 
1363
#endif
 
1364
          incr *= -1;
 
1365
        }
 
1366
    }
 
1367
 
 
1368
 
 
1369
  vp->vibrato_control_counter=cc;
 
1370
  vp->sample_increment=incr;
 
1371
  vp->sample_offset=ofs; /* Update offset */
 
1372
  vp->current_x0=x0;
 
1373
  vp->current_x1=x1;
 
1374
  vp->current_y0=y0;
 
1375
  vp->current_y1=y1;
 
1376
  vp->bw_index=bw_index;
 
1377
  vp->modulation_counter=cc_count;
 
1378
  return resample_buffer+resample_buffer_offset;
 
1379
}
 
1380
 
 
1381
static int rs_update_porta(int v)
 
1382
{
 
1383
    Voice *vp=&voice[v];
 
1384
    int32 d;
 
1385
 
 
1386
    d = vp->porta_dpb;
 
1387
    if(vp->porta_pb < 0)
 
1388
    {
 
1389
        if(d > -vp->porta_pb)
 
1390
            d = -vp->porta_pb;
 
1391
    }
 
1392
    else
 
1393
    {
 
1394
        if(d > vp->porta_pb)
 
1395
            d = -vp->porta_pb;
 
1396
        else
 
1397
            d = -d;
 
1398
    }
 
1399
 
 
1400
    vp->porta_pb += d;
 
1401
    if(vp->porta_pb == 0)
 
1402
    {
 
1403
        vp->porta_control_ratio = 0;
 
1404
        vp->porta_pb = 0;
 
1405
    }
 
1406
    recompute_freq(v);
 
1407
    return vp->porta_control_ratio;
 
1408
}
 
1409
 
 
1410
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
 
1411
{
 
1412
    Voice *vp=&voice[v];
 
1413
    uint32 n = *countptr;
 
1414
    uint32 i;
 
1415
    sample_t *(* resampler)(int, uint32 *, int);
 
1416
    int cc = vp->porta_control_counter;
 
1417
    int loop;
 
1418
 
 
1419
    if(vp->vibrato_control_ratio)
 
1420
        resampler = vib_resample_voice;
 
1421
    else
 
1422
        resampler = normal_resample_voice;
 
1423
    if(mode != 1)
 
1424
        loop = 1;
 
1425
    else
 
1426
        loop = 0;
 
1427
 
 
1428
    /* vp->cache = NULL; */
 
1429
    resample_buffer_offset = 0;
 
1430
    while(resample_buffer_offset < n)
 
1431
    {
 
1432
        if(cc == 0)
 
1433
        {
 
1434
            if((cc = rs_update_porta(v)) == 0)
 
1435
            {
 
1436
                i = n - resample_buffer_offset;
 
1437
                resampler(v, &i, mode);
 
1438
                resample_buffer_offset += i;
 
1439
                break;
 
1440
            }
 
1441
        }
 
1442
 
 
1443
        i = n - resample_buffer_offset;
 
1444
        if(i > (uint32)cc)
 
1445
            i = (uint32)cc;
 
1446
        resampler(v, &i, mode);
 
1447
        resample_buffer_offset += i;
 
1448
 
 
1449
        /* if(!loop && vp->status == VOICE_FREE) */
 
1450
        if(vp->status == VOICE_FREE)
 
1451
            break;
 
1452
        cc -= (int)i;
 
1453
    }
 
1454
    *countptr = resample_buffer_offset;
 
1455
    resample_buffer_offset = 0;
 
1456
    vp->porta_control_counter = cc;
 
1457
    return resample_buffer;
 
1458
}
 
1459
 
 
1460
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
 
1461
{
 
1462
    Voice *vp = &voice[v];
 
1463
 
 
1464
    /* vp->cache = NULL; */
 
1465
    if(mode == 0)
 
1466
        return rs_vib_loop(v, vp, countptr);
 
1467
    if(mode == 1)
 
1468
        return rs_vib_plain(v, countptr);
 
1469
    return rs_vib_bidir(v, vp, *countptr);
 
1470
}
 
1471
 
 
1472
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
 
1473
{
 
1474
    Voice *vp = &voice[v];
 
1475
    if(mode == 0)
 
1476
        return rs_loop(v, vp, countptr);
 
1477
    if(mode == 1)
 
1478
        return rs_plain(v, countptr);
 
1479
    return rs_bidir(v, vp, *countptr);
 
1480
}
 
1481
 
 
1482
sample_t *resample_voice_filter(int v, uint32 *countptr)
 
1483
{
 
1484
    Voice *vp=&voice[v];
 
1485
    int mode;
 
1486
 
 
1487
    mode = vp->sample->modes;
 
1488
    if((mode & MODES_LOOPING) &&
 
1489
       ((mode & MODES_ENVELOPE) ||
 
1490
        (vp->status & (VOICE_ON | VOICE_SUSTAINED))))
 
1491
    {
 
1492
        if(mode & MODES_PINGPONG)
 
1493
        {
 
1494
            /* vp->cache = NULL; */
 
1495
            mode = 2;
 
1496
        }
 
1497
        else
 
1498
            mode = 0;
 
1499
    }
 
1500
    else
 
1501
        mode = 1;
 
1502
 
 
1503
    if(vp->porta_control_ratio)
 
1504
        return porta_resample_voice(v, countptr, mode);
 
1505
 
 
1506
    if(vp->vibrato_control_ratio)
 
1507
        return vib_resample_voice(v, countptr, mode);
 
1508
 
 
1509
    return normal_resample_voice(v, countptr, mode);
 
1510
}
 
1511