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

« back to all changes in this revision

Viewing changes to kmidi/resample_l.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_l.cpp,v 1.3 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 LAGRANGE_INTERPOLATION
 
40
#define ENVELOPE_PITCH_MODULATION
 
41
 
 
42
 
 
43
#ifdef LOOKUP_HACK
 
44
#define MAX_DATAVAL 127
 
45
#define MIN_DATAVAL -128
 
46
#else
 
47
#define MAX_DATAVAL 32767
 
48
#define MIN_DATAVAL -32768
 
49
#endif
 
50
 
 
51
#define OVERSHOOT_STEP 50
 
52
 
 
53
 
 
54
static sample_t *vib_resample_voice(int, uint32 *, int);
 
55
static sample_t *normal_resample_voice(int, uint32 *, int);
 
56
 
 
57
 
 
58
#ifdef FILTER_INTERPOLATION
 
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
#endif
 
82
 
 
83
 
 
84
#ifdef FILTER_INTERPOLATION
 
85
static int calc_bw_index(int v)
 
86
{
 
87
  FLOAT_T mod_amount=voice[v].modEnvToFilterFc;
 
88
  int32 freq = voice[v].sample->cutoff_freq;
 
89
  int ix;
 
90
 
 
91
  if (voice[v].lfo_phase_increment) update_lfo(v);
 
92
 
 
93
  if (!voice[v].lfo_phase_increment && update_modulation_signal(v)) return 0;
 
94
 
 
95
/* printf("mod_amount %f ", mod_amount); */
 
96
  if (voice[v].lfo_volume>0.001) {
 
97
        if (mod_amount) mod_amount *= voice[v].lfo_volume;
 
98
        else mod_amount = voice[v].lfo_volume;
 
99
/* printf("lfo %f -> mod %f ", voice[v].lfo_volume, mod_amount); */
 
100
  }
 
101
 
 
102
  if (mod_amount > 0.001) {
 
103
    if (voice[v].modulation_volume)
 
104
       freq =
 
105
        (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (voice[v].modulation_volume>>22) / 255.0) );
 
106
    else freq = (int32)( (double)freq*mod_amount );
 
107
/*
 
108
printf("v%d freq %d (was %d), modvol %d, mod_amount %f\n", v, (int)freq, (int)voice[v].sample->cutoff_freq,
 
109
(int)voice[v].modulation_volume>>22,
 
110
mod_amount);
 
111
*/
 
112
        ix = 1 + (freq+50) / 100;
 
113
        if (ix > 100) ix = 100;
 
114
        voice[v].bw_index = ix;
 
115
        return 1;
 
116
  }
 
117
  voice[v].bw_index = 1 + (freq+50) / 100;
 
118
  return 0;
 
119
}
 
120
#endif
 
121
 
 
122
/*************** resampling with fixed increment *****************/
 
123
 
 
124
static sample_t *rs_plain(int v, uint32 *countptr)
 
125
{
 
126
  /* Play sample until end, then free the voice. */
 
127
  Voice
 
128
    *vp=&voice[v];
 
129
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
130
   int offset;
 
131
  uint32 cc_count=vp->modulation_counter;
 
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
 
 
154
    while (count--)
 
155
    {
 
156
 
 
157
        offset = ofs >> FRACTION_BITS;
 
158
 
 
159
        if (ofs >= se) {
 
160
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
161
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
162
                v1 -=  (delta+1) * v1 / overshoot;
 
163
        }
 
164
        else  v1 = (int32)src[offset];
 
165
 
 
166
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
167
                v2 = v1;
 
168
        }
 
169
        else  v2 = (int32)src[offset+1];
 
170
 
 
171
        if(dont_cspline ||
 
172
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
173
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
174
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
175
        }
 
176
        else {
 
177
                v0 = (int32)src[offset-1];
 
178
                v3 = (int32)src[offset+2];
 
179
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
180
                v1 = v1*ofsd>>FRACTION_BITS;
 
181
                v2 = v2*ofsd>>FRACTION_BITS;
 
182
                v3 = v3*ofsd>>FRACTION_BITS;
 
183
                ofsd -= (1L << FRACTION_BITS);
 
184
                v0 = v0*ofsd>>FRACTION_BITS;
 
185
                v2 = v2*ofsd>>FRACTION_BITS;
 
186
                v3 = v3*ofsd>>FRACTION_BITS;
 
187
                ofsd -= (1L << FRACTION_BITS);
 
188
                v0 = v0*ofsd>>FRACTION_BITS;
 
189
                v1 = v1*ofsd>>FRACTION_BITS;
 
190
                v3 = v3*ofsd;
 
191
                ofsd -= (1L << FRACTION_BITS);
 
192
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
193
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
194
                v1 += v0;
 
195
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
196
        }
 
197
 
 
198
                if (!cc_count--) {
 
199
                    cc_count = control_ratio - 1;
 
200
                    if (!update_modulation_signal(v))
 
201
                        incr = calc_mod_freq(v, incr);
 
202
                }
 
203
      ofs += incr;
 
204
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
205
        {
 
206
          if (!(vp->status&VOICE_FREE))
 
207
            {
 
208
              vp->status=VOICE_FREE;
 
209
              ctl->note(v);
 
210
            }
 
211
          *countptr-=count+1;
 
212
          break;
 
213
        }
 
214
    }
 
215
 
 
216
  vp->sample_offset=ofs; /* Update offset */
 
217
  vp->modulation_counter=cc_count;
 
218
  return resample_buffer+resample_buffer_offset;
 
219
}
 
220
 
 
221
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
 
222
{
 
223
  /* Play sample until end-of-loop, skip back and continue. */
 
224
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
225
   int offset;
 
226
  uint32 cc_count=vp->modulation_counter;
 
227
  int32
 
228
    incr=vp->sample_increment;
 
229
  uint32
 
230
    ofs=vp->sample_offset;
 
231
  uint32
 
232
    le=vp->loop_end,
 
233
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
234
    ls=vp->loop_start,
 
235
#endif /* LAGRANGE_INTERPOLATION */
 
236
    ll=le - vp->loop_start;
 
237
  sample_t
 
238
    *dest=resample_buffer+resample_buffer_offset,
 
239
    *src=vp->sample->data;
 
240
  uint32
 
241
    se=vp->sample->data_length,
 
242
    count = *countptr;
 
243
  int
 
244
    flag_exit_loop;
 
245
 
 
246
 
 
247
  flag_exit_loop =
 
248
        (vp->status & (VOICE_FREE | VOICE_DIE)) ||
 
249
        ((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
 
250
        ((vp->status & VOICE_OFF) && dont_keep_looping ) ;
 
251
 
 
252
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
253
  if (overshoot < 0) overshoot = -overshoot;
 
254
 
 
255
  while (count--)
 
256
    {
 
257
 
 
258
        offset = ofs >> FRACTION_BITS;
 
259
 
 
260
        if (ofs >= se) {
 
261
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
262
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
263
                v1 -=  (delta+1) * v1 / overshoot;
 
264
        }
 
265
        else  v1 = (int32)src[offset];
 
266
 
 
267
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
268
                v2 = v1;
 
269
        }
 
270
        else  v2 = (int32)src[offset+1];
 
271
 
 
272
        if(dont_cspline ||
 
273
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
274
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
275
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
276
        }
 
277
        else {
 
278
                v0 = (int32)src[offset-1];
 
279
                v3 = (int32)src[offset+2];
 
280
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
281
                v1 = v1*ofsd>>FRACTION_BITS;
 
282
                v2 = v2*ofsd>>FRACTION_BITS;
 
283
                v3 = v3*ofsd>>FRACTION_BITS;
 
284
                ofsd -= (1L << FRACTION_BITS);
 
285
                v0 = v0*ofsd>>FRACTION_BITS;
 
286
                v2 = v2*ofsd>>FRACTION_BITS;
 
287
                v3 = v3*ofsd>>FRACTION_BITS;
 
288
                ofsd -= (1L << FRACTION_BITS);
 
289
                v0 = v0*ofsd>>FRACTION_BITS;
 
290
                v1 = v1*ofsd>>FRACTION_BITS;
 
291
                v3 = v3*ofsd;
 
292
                ofsd -= (1L << FRACTION_BITS);
 
293
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
294
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
295
                v1 += v0;
 
296
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
297
        }
 
298
 
 
299
                if (!cc_count--) {
 
300
                    cc_count = control_ratio - 1;
 
301
                    if (!update_modulation_signal(v))
 
302
                        incr = calc_mod_freq(v, incr);
 
303
                }
 
304
      ofs += incr;
 
305
      if (ofs>=le)
 
306
        {
 
307
          if (flag_exit_loop)
 
308
            {
 
309
                vp->echo_delay -= ll >> FRACTION_BITS;
 
310
                if (vp->echo_delay >= 0) ofs -= ll;
 
311
            }
 
312
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
313
        }
 
314
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
315
        {
 
316
          if (!(vp->status&VOICE_FREE))
 
317
            {
 
318
              vp->status=VOICE_FREE;
 
319
              ctl->note(v);
 
320
            }
 
321
          *countptr-=count+1;
 
322
          break;
 
323
        }
 
324
    }
 
325
 
 
326
  vp->sample_offset=ofs; /* Update offset */
 
327
  vp->modulation_counter=cc_count;
 
328
  return resample_buffer+resample_buffer_offset;
 
329
}
 
330
 
 
331
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
 
332
{
 
333
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
334
   int offset;
 
335
  int32
 
336
    incr=vp->sample_increment;
 
337
  uint32
 
338
    le=vp->loop_end,
 
339
    ls=vp->loop_start;
 
340
  uint32
 
341
    ofs=vp->sample_offset,
 
342
    se=vp->sample->data_length;
 
343
  sample_t
 
344
    *dest=resample_buffer+resample_buffer_offset,
 
345
    *src=vp->sample->data;
 
346
 
 
347
 
 
348
#ifdef USE_BIDIR_OVERSHOOT
 
349
  int32
 
350
    le2 = le<<1,
 
351
    ls2 = ls<<1;
 
352
#endif
 
353
  uint32
 
354
    i, j;
 
355
  /* Play normally until inside the loop region */
 
356
 
 
357
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
358
  if (overshoot < 0) overshoot = -overshoot;
 
359
 
 
360
  if (ofs <= ls)
 
361
    {
 
362
      /* NOTE: Assumes that incr > 0, which is NOT always the case
 
363
         when doing bidirectional looping.  I have yet to see a case
 
364
         where both ofs <= ls AND incr < 0, however. */
 
365
      if (incr < 0) i = ls - ofs;
 
366
        else
 
367
      i = (ls - ofs) / incr + 1;
 
368
      if (i > count)
 
369
        {
 
370
          i = count;
 
371
          count = 0;
 
372
        }
 
373
      else count -= i;
 
374
      for(j = 0; j < i; j++)
 
375
        {
 
376
 
 
377
        offset = ofs >> FRACTION_BITS;
 
378
 
 
379
        if (ofs >= se) {
 
380
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
381
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
382
                v1 -=  (delta+1) * v1 / overshoot;
 
383
        }
 
384
        else  v1 = (int32)src[offset];
 
385
 
 
386
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
387
                v2 = v1;
 
388
        }
 
389
        else  v2 = (int32)src[offset+1];
 
390
 
 
391
        if(dont_cspline ||
 
392
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
393
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
394
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
395
        }
 
396
        else {
 
397
                v0 = (int32)src[offset-1];
 
398
                v3 = (int32)src[offset+2];
 
399
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
400
                v1 = v1*ofsd>>FRACTION_BITS;
 
401
                v2 = v2*ofsd>>FRACTION_BITS;
 
402
                v3 = v3*ofsd>>FRACTION_BITS;
 
403
                ofsd -= (1L << FRACTION_BITS);
 
404
                v0 = v0*ofsd>>FRACTION_BITS;
 
405
                v2 = v2*ofsd>>FRACTION_BITS;
 
406
                v3 = v3*ofsd>>FRACTION_BITS;
 
407
                ofsd -= (1L << FRACTION_BITS);
 
408
                v0 = v0*ofsd>>FRACTION_BITS;
 
409
                v1 = v1*ofsd>>FRACTION_BITS;
 
410
                v3 = v3*ofsd;
 
411
                ofsd -= (1L << FRACTION_BITS);
 
412
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
413
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
414
                v1 += v0;
 
415
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
416
        }
 
417
 
 
418
          ofs += incr;
 
419
        }
 
420
    }
 
421
 
 
422
  /* Then do the bidirectional looping */
 
423
 
 
424
  while(count)
 
425
    {
 
426
      /* Precalc how many times we should go through the loop */
 
427
#if 1
 
428
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
429
#else
 
430
/* fix from M. Izumo */
 
431
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
432
#endif
 
433
      if (i > count)
 
434
        {
 
435
          i = count;
 
436
          count = 0;
 
437
        }
 
438
      else count -= i;
 
439
      for(j = 0; j < i && ofs < se; j++)
 
440
        {
 
441
 
 
442
        offset = ofs >> FRACTION_BITS;
 
443
 
 
444
        if (ofs >= se) {
 
445
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
446
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
447
                v1 -=  (delta+1) * v1 / overshoot;
 
448
        }
 
449
        else  v1 = (int32)src[offset];
 
450
 
 
451
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
452
                v2 = v1;
 
453
        }
 
454
        else  v2 = (int32)src[offset+1];
 
455
 
 
456
        if(dont_cspline ||
 
457
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
458
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
459
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
460
        }
 
461
        else {
 
462
                v0 = (int32)src[offset-1];
 
463
                v3 = (int32)src[offset+2];
 
464
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
465
                v1 = v1*ofsd>>FRACTION_BITS;
 
466
                v2 = v2*ofsd>>FRACTION_BITS;
 
467
                v3 = v3*ofsd>>FRACTION_BITS;
 
468
                ofsd -= (1L << FRACTION_BITS);
 
469
                v0 = v0*ofsd>>FRACTION_BITS;
 
470
                v2 = v2*ofsd>>FRACTION_BITS;
 
471
                v3 = v3*ofsd>>FRACTION_BITS;
 
472
                ofsd -= (1L << FRACTION_BITS);
 
473
                v0 = v0*ofsd>>FRACTION_BITS;
 
474
                v1 = v1*ofsd>>FRACTION_BITS;
 
475
                v3 = v3*ofsd;
 
476
                ofsd -= (1L << FRACTION_BITS);
 
477
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
478
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
479
                v1 += v0;
 
480
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
481
        }
 
482
 
 
483
          ofs += incr;
 
484
        }
 
485
#ifdef USE_BIDIR_OVERSHOOT
 
486
      if (ofs>=le)
 
487
        {
 
488
          /* fold the overshoot back in */
 
489
          ofs = le2 - ofs;
 
490
          incr *= -1;
 
491
        }
 
492
      else if (ofs <= ls)
 
493
        {
 
494
          ofs = ls2 - ofs;
 
495
          incr *= -1;
 
496
        }
 
497
#else
 
498
          incr *= -1;
 
499
#endif
 
500
    }
 
501
 
 
502
  vp->sample_increment=incr;
 
503
  vp->sample_offset=ofs; /* Update offset */
 
504
  return resample_buffer+resample_buffer_offset;
 
505
}
 
506
 
 
507
/*********************** vibrato versions ***************************/
 
508
 
 
509
/* We only need to compute one half of the vibrato sine cycle */
 
510
static uint32 vib_phase_to_inc_ptr(uint32 phase)
 
511
{
 
512
  if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
 
513
    return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
 
514
  else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
 
515
    return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
 
516
  else
 
517
    return phase-VIBRATO_SAMPLE_INCREMENTS/2;
 
518
}
 
519
 
 
520
static int32 update_vibrato(Voice *vp, int sign)
 
521
{
 
522
  uint32 depth, freq=vp->frequency;
 
523
#ifdef ENVELOPE_PITCH_MODULATION
 
524
  FLOAT_T mod_amount=vp->sample->modEnvToPitch;
 
525
#endif
 
526
  uint32 phase;
 
527
  int pb;
 
528
  double a;
 
529
 
 
530
  if(vp->vibrato_delay > 0)
 
531
  {
 
532
      vp->vibrato_delay -= vp->vibrato_control_ratio;
 
533
      if(vp->vibrato_delay > 0)
 
534
          return vp->sample_increment;
 
535
  }
 
536
 
 
537
  if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
 
538
    vp->vibrato_phase=0;
 
539
  phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
 
540
 
 
541
  if (vp->vibrato_sample_increment[phase])
 
542
    {
 
543
      if (sign)
 
544
        return -vp->vibrato_sample_increment[phase];
 
545
      else
 
546
        return vp->vibrato_sample_increment[phase];
 
547
    }
 
548
 
 
549
  /* Need to compute this sample increment. */
 
550
 
 
551
  depth = vp->vibrato_depth;
 
552
  if(depth < vp->modulation_wheel)
 
553
      depth = vp->modulation_wheel;
 
554
  depth <<= 7;
 
555
 
 
556
  if (vp->vibrato_sweep && !vp->modulation_wheel)
 
557
    {
 
558
      /* Need to update sweep */
 
559
      vp->vibrato_sweep_position += vp->vibrato_sweep;
 
560
      if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
 
561
        vp->vibrato_sweep=0;
 
562
      else
 
563
        {
 
564
          /* Adjust depth */
 
565
          depth *= vp->vibrato_sweep_position;
 
566
          depth >>= SWEEP_SHIFT;
 
567
        }
 
568
    }
 
569
 
 
570
#ifdef ENVELOPE_PITCH_MODULATION
 
571
#ifndef FILTER_INTERPOLATION
 
572
  if (update_modulation_signal(0)) mod_amount = 0;
 
573
  else
 
574
#endif
 
575
  if (mod_amount>0.02)
 
576
   freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
 
577
#endif
 
578
 
 
579
  pb=(int)((sine(vp->vibrato_phase *
 
580
                        (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
 
581
            * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
 
582
 
 
583
  a = FRSCALE(((double)(vp->sample->sample_rate) *
 
584
                  (double)(freq)) /
 
585
                 ((double)(vp->sample->root_freq) *
 
586
                  (double)(play_mode->rate)),
 
587
                 FRACTION_BITS);
 
588
  if(pb<0)
 
589
  {
 
590
      pb = -pb;
 
591
      a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
592
  }
 
593
  else
 
594
      a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
595
  a += 0.5;
 
596
 
 
597
  /* If the sweep's over, we can store the newly computed sample_increment */
 
598
  if (!vp->vibrato_sweep || vp->modulation_wheel)
 
599
    vp->vibrato_sample_increment[phase]=(int32) a;
 
600
 
 
601
  if (sign)
 
602
    a = -a; /* need to preserve the loop direction */
 
603
 
 
604
  return (int32) a;
 
605
}
 
606
 
 
607
static sample_t *rs_vib_plain(int v, uint32 *countptr)
 
608
{
 
609
 
 
610
  /* Play sample until end, then free the voice. */
 
611
 
 
612
  Voice *vp=&voice[v];
 
613
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
614
   int offset;
 
615
  sample_t
 
616
    *dest=resample_buffer+resample_buffer_offset,
 
617
    *src=vp->sample->data;
 
618
  int32
 
619
    incr=vp->sample_increment;
 
620
/*WHY int32??*/
 
621
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
622
  uint32
 
623
    ls=0,
 
624
    le=vp->sample->data_length;
 
625
#endif /* LAGRANGE_INTERPOLATION */
 
626
  uint32
 
627
    ofs=vp->sample_offset,
 
628
    se=vp->sample->data_length,
 
629
    count=*countptr;
 
630
  uint32
 
631
    cc=vp->vibrato_control_counter;
 
632
 
 
633
  /* This has never been tested */
 
634
 
 
635
  if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
 
636
 
 
637
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
638
  if (overshoot < 0) overshoot = -overshoot;
 
639
  while (count--)
 
640
    {
 
641
      if (!cc--)
 
642
        {
 
643
          cc=vp->vibrato_control_ratio;
 
644
          incr=update_vibrato(vp, 0);
 
645
        }
 
646
 
 
647
        offset = ofs >> FRACTION_BITS;
 
648
 
 
649
        if (ofs >= se) {
 
650
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
651
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
652
                v1 -=  (delta+1) * v1 / overshoot;
 
653
        }
 
654
        else  v1 = (int32)src[offset];
 
655
 
 
656
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
657
                v2 = v1;
 
658
        }
 
659
        else  v2 = (int32)src[offset+1];
 
660
 
 
661
        if(dont_cspline ||
 
662
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
663
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
664
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
665
        }
 
666
        else {
 
667
                v0 = (int32)src[offset-1];
 
668
                v3 = (int32)src[offset+2];
 
669
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
670
                v1 = v1*ofsd>>FRACTION_BITS;
 
671
                v2 = v2*ofsd>>FRACTION_BITS;
 
672
                v3 = v3*ofsd>>FRACTION_BITS;
 
673
                ofsd -= (1L << FRACTION_BITS);
 
674
                v0 = v0*ofsd>>FRACTION_BITS;
 
675
                v2 = v2*ofsd>>FRACTION_BITS;
 
676
                v3 = v3*ofsd>>FRACTION_BITS;
 
677
                ofsd -= (1L << FRACTION_BITS);
 
678
                v0 = v0*ofsd>>FRACTION_BITS;
 
679
                v1 = v1*ofsd>>FRACTION_BITS;
 
680
                v3 = v3*ofsd;
 
681
                ofsd -= (1L << FRACTION_BITS);
 
682
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
683
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
684
                v1 += v0;
 
685
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
686
        }
 
687
 
 
688
      ofs += incr;
 
689
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
690
        {
 
691
          if (!(vp->status&VOICE_FREE))
 
692
            {
 
693
              vp->status=VOICE_FREE;
 
694
              ctl->note(v);
 
695
            }
 
696
          *countptr-=count+1;
 
697
          break;
 
698
        }
 
699
    }
 
700
 
 
701
  vp->vibrato_control_counter=cc;
 
702
  vp->sample_increment=incr;
 
703
  vp->sample_offset=ofs; /* Update offset */
 
704
  return resample_buffer+resample_buffer_offset;
 
705
}
 
706
 
 
707
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
 
708
{
 
709
  /* Play sample until end-of-loop, skip back and continue. */
 
710
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
711
   int offset;
 
712
  int32
 
713
    incr=vp->sample_increment;
 
714
/*WHY int32??*/
 
715
  uint32
 
716
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
717
    ls=vp->loop_start,
 
718
#endif /* LAGRANGE_INTERPOLATION */
 
719
    le=vp->loop_end,
 
720
    ll=le - vp->loop_start;
 
721
  sample_t
 
722
    *dest=resample_buffer+resample_buffer_offset,
 
723
    *src=vp->sample->data;
 
724
  uint32
 
725
    ofs=vp->sample_offset,
 
726
    se=vp->sample->data_length,
 
727
    count = *countptr;
 
728
  uint32
 
729
    cc=vp->vibrato_control_counter;
 
730
  int
 
731
    flag_exit_loop;
 
732
 
 
733
 
 
734
  flag_exit_loop =
 
735
        (vp->status & (VOICE_FREE | VOICE_DIE)) ||
 
736
        ((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
 
737
        ((vp->status & VOICE_OFF) && dont_keep_looping ) ;
 
738
 
 
739
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
740
  if (overshoot < 0) overshoot = -overshoot;
 
741
  while (count--)
 
742
    {
 
743
      if (!cc--)
 
744
        {
 
745
          cc=vp->vibrato_control_ratio;
 
746
          incr=update_vibrato(vp, 0);
 
747
        }
 
748
 
 
749
        offset = ofs >> FRACTION_BITS;
 
750
 
 
751
        if (ofs >= se) {
 
752
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
753
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
754
                v1 -=  (delta+1) * v1 / overshoot;
 
755
        }
 
756
        else  v1 = (int32)src[offset];
 
757
 
 
758
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
759
                v2 = v1;
 
760
        }
 
761
        else  v2 = (int32)src[offset+1];
 
762
 
 
763
        if(dont_cspline ||
 
764
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
765
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
766
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
767
        }
 
768
        else {
 
769
                v0 = (int32)src[offset-1];
 
770
                v3 = (int32)src[offset+2];
 
771
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
772
                v1 = v1*ofsd>>FRACTION_BITS;
 
773
                v2 = v2*ofsd>>FRACTION_BITS;
 
774
                v3 = v3*ofsd>>FRACTION_BITS;
 
775
                ofsd -= (1L << FRACTION_BITS);
 
776
                v0 = v0*ofsd>>FRACTION_BITS;
 
777
                v2 = v2*ofsd>>FRACTION_BITS;
 
778
                v3 = v3*ofsd>>FRACTION_BITS;
 
779
                ofsd -= (1L << FRACTION_BITS);
 
780
                v0 = v0*ofsd>>FRACTION_BITS;
 
781
                v1 = v1*ofsd>>FRACTION_BITS;
 
782
                v3 = v3*ofsd;
 
783
                ofsd -= (1L << FRACTION_BITS);
 
784
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
785
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
786
                v1 += v0;
 
787
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
788
        }
 
789
 
 
790
      ofs += incr;
 
791
      if (ofs>=le)
 
792
        {
 
793
          if (flag_exit_loop)
 
794
            {
 
795
                vp->echo_delay -= ll >> FRACTION_BITS;
 
796
                if (vp->echo_delay >= 0) ofs -= ll;
 
797
            }
 
798
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
799
        }
 
800
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
801
        {
 
802
          if (!(vp->status&VOICE_FREE))
 
803
            {
 
804
              vp->status=VOICE_FREE;
 
805
              ctl->note(v);
 
806
            }
 
807
          *countptr-=count+1;
 
808
          break;
 
809
        }
 
810
    }
 
811
 
 
812
  vp->vibrato_control_counter=cc;
 
813
  vp->sample_increment=incr;
 
814
  vp->sample_offset=ofs; /* Update offset */
 
815
  return resample_buffer+resample_buffer_offset;
 
816
}
 
817
 
 
818
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
 
819
{
 
820
   int32   ofsd, v0, v1, v2, v3, overshoot;
 
821
   int offset;
 
822
  int32
 
823
    incr=vp->sample_increment;
 
824
/*WHY int32??*/
 
825
  uint32
 
826
    le=vp->loop_end,
 
827
    ls=vp->loop_start;
 
828
  uint32
 
829
    ofs=vp->sample_offset,
 
830
    se=vp->sample->data_length;
 
831
  sample_t
 
832
    *dest=resample_buffer+resample_buffer_offset,
 
833
    *src=vp->sample->data;
 
834
  uint32
 
835
    cc=vp->vibrato_control_counter;
 
836
 
 
837
 
 
838
#ifdef USE_BIDIR_OVERSHOOT
 
839
  uint32
 
840
    le2=le<<1,
 
841
    ls2=ls<<1;
 
842
#endif
 
843
  uint32
 
844
    i, j;
 
845
  int
 
846
    vibflag = 0;
 
847
 
 
848
 
 
849
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
850
  if (overshoot < 0) overshoot = -overshoot;
 
851
  /* Play normally until inside the loop region */
 
852
  while (count && (ofs <= ls))
 
853
    {
 
854
      i = (ls - ofs) / incr + 1;
 
855
      if (i > count) i = count;
 
856
      if (i > cc)
 
857
        {
 
858
          i = cc;
 
859
          vibflag = 1;
 
860
        }
 
861
      else cc -= i;
 
862
      count -= i;
 
863
      for(j = 0; j < i; j++)
 
864
        {
 
865
 
 
866
        offset = ofs >> FRACTION_BITS;
 
867
 
 
868
        if (ofs >= se) {
 
869
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
870
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
871
                v1 -=  (delta+1) * v1 / overshoot;
 
872
        }
 
873
        else  v1 = (int32)src[offset];
 
874
 
 
875
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
876
                v2 = v1;
 
877
        }
 
878
        else  v2 = (int32)src[offset+1];
 
879
 
 
880
        if(dont_cspline ||
 
881
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
882
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
883
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
884
        }
 
885
        else {
 
886
                v0 = (int32)src[offset-1];
 
887
                v3 = (int32)src[offset+2];
 
888
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
889
                v1 = v1*ofsd>>FRACTION_BITS;
 
890
                v2 = v2*ofsd>>FRACTION_BITS;
 
891
                v3 = v3*ofsd>>FRACTION_BITS;
 
892
                ofsd -= (1L << FRACTION_BITS);
 
893
                v0 = v0*ofsd>>FRACTION_BITS;
 
894
                v2 = v2*ofsd>>FRACTION_BITS;
 
895
                v3 = v3*ofsd>>FRACTION_BITS;
 
896
                ofsd -= (1L << FRACTION_BITS);
 
897
                v0 = v0*ofsd>>FRACTION_BITS;
 
898
                v1 = v1*ofsd>>FRACTION_BITS;
 
899
                v3 = v3*ofsd;
 
900
                ofsd -= (1L << FRACTION_BITS);
 
901
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
902
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
903
                v1 += v0;
 
904
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
905
        }
 
906
 
 
907
          ofs += incr;
 
908
        }
 
909
      if (vibflag)
 
910
        {
 
911
          cc = vp->vibrato_control_ratio;
 
912
          incr = update_vibrato(vp, 0);
 
913
          vibflag = 0;
 
914
        }
 
915
    }
 
916
 
 
917
  /* Then do the bidirectional looping */
 
918
 
 
919
  while (count)
 
920
    {
 
921
      /* Precalc how many times we should go through the loop */
 
922
#if 1
 
923
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
924
#else
 
925
/* fix from M. Izumo */
 
926
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
927
#endif
 
928
      if(i > count) i = count;
 
929
      if(i > cc)
 
930
        {
 
931
          i = cc;
 
932
          vibflag = 1;
 
933
        }
 
934
      else cc -= i;
 
935
      count -= i;
 
936
      while (i-- && ofs < se)
 
937
        {
 
938
 
 
939
        offset = ofs >> FRACTION_BITS;
 
940
 
 
941
        if (ofs >= se) {
 
942
                int32 delta = (ofs - se)>>FRACTION_BITS ;
 
943
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
944
                v1 -=  (delta+1) * v1 / overshoot;
 
945
        }
 
946
        else  v1 = (int32)src[offset];
 
947
 
 
948
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
949
                v2 = v1;
 
950
        }
 
951
        else  v2 = (int32)src[offset+1];
 
952
 
 
953
        if(dont_cspline ||
 
954
           ( (ofs-(1L<<FRACTION_BITS)) < ls) ||
 
955
             ((ofs+(2L<<FRACTION_BITS)) > le) ) {
 
956
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
957
        }
 
958
        else {
 
959
                v0 = (int32)src[offset-1];
 
960
                v3 = (int32)src[offset+2];
 
961
                ofsd = (int32)(ofs & FRACTION_MASK) + (1L << FRACTION_BITS);
 
962
                v1 = v1*ofsd>>FRACTION_BITS;
 
963
                v2 = v2*ofsd>>FRACTION_BITS;
 
964
                v3 = v3*ofsd>>FRACTION_BITS;
 
965
                ofsd -= (1L << FRACTION_BITS);
 
966
                v0 = v0*ofsd>>FRACTION_BITS;
 
967
                v2 = v2*ofsd>>FRACTION_BITS;
 
968
                v3 = v3*ofsd>>FRACTION_BITS;
 
969
                ofsd -= (1L << FRACTION_BITS);
 
970
                v0 = v0*ofsd>>FRACTION_BITS;
 
971
                v1 = v1*ofsd>>FRACTION_BITS;
 
972
                v3 = v3*ofsd;
 
973
                ofsd -= (1L << FRACTION_BITS);
 
974
                v0 = (v3 - v0*ofsd)/(6L << FRACTION_BITS);
 
975
                v1 = (v1 - v2)*ofsd>>(FRACTION_BITS+1);
 
976
                v1 += v0;
 
977
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
978
        }
 
979
 
 
980
          ofs += incr;
 
981
        }
 
982
      if (vibflag)
 
983
        {
 
984
          cc = vp->vibrato_control_ratio;
 
985
          incr = update_vibrato(vp, (incr < 0));
 
986
          vibflag = 0;
 
987
        }
 
988
      if (ofs >= le)
 
989
        {
 
990
#ifdef USE_BIDIR_OVERSHOOT
 
991
          /* fold the overshoot back in */
 
992
          ofs = le2 - ofs;
 
993
#endif
 
994
          incr *= -1;
 
995
        }
 
996
      else if (ofs <= ls)
 
997
        {
 
998
#ifdef USE_BIDIR_OVERSHOOT
 
999
          ofs = ls2 - ofs;
 
1000
#endif
 
1001
          incr *= -1;
 
1002
        }
 
1003
    }
 
1004
 
 
1005
 
 
1006
  vp->vibrato_control_counter=cc;
 
1007
  vp->sample_increment=incr;
 
1008
  vp->sample_offset=ofs; /* Update offset */
 
1009
  return resample_buffer+resample_buffer_offset;
 
1010
}
 
1011
 
 
1012
static int rs_update_porta(int v)
 
1013
{
 
1014
    Voice *vp=&voice[v];
 
1015
    int32 d;
 
1016
 
 
1017
    d = vp->porta_dpb;
 
1018
    if(vp->porta_pb < 0)
 
1019
    {
 
1020
        if(d > -vp->porta_pb)
 
1021
            d = -vp->porta_pb;
 
1022
    }
 
1023
    else
 
1024
    {
 
1025
        if(d > vp->porta_pb)
 
1026
            d = -vp->porta_pb;
 
1027
        else
 
1028
            d = -d;
 
1029
    }
 
1030
 
 
1031
    vp->porta_pb += d;
 
1032
    if(vp->porta_pb == 0)
 
1033
    {
 
1034
        vp->porta_control_ratio = 0;
 
1035
        vp->porta_pb = 0;
 
1036
    }
 
1037
    recompute_freq(v);
 
1038
    return vp->porta_control_ratio;
 
1039
}
 
1040
 
 
1041
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
 
1042
{
 
1043
    Voice *vp=&voice[v];
 
1044
    uint32 n = *countptr;
 
1045
    uint32 i;
 
1046
    sample_t *(* resampler)(int, uint32 *, int);
 
1047
    int cc = vp->porta_control_counter;
 
1048
    int loop;
 
1049
 
 
1050
    if(vp->vibrato_control_ratio)
 
1051
        resampler = vib_resample_voice;
 
1052
    else
 
1053
        resampler = normal_resample_voice;
 
1054
    if(mode != 1)
 
1055
        loop = 1;
 
1056
    else
 
1057
        loop = 0;
 
1058
 
 
1059
    /* vp->cache = NULL; */
 
1060
    resample_buffer_offset = 0;
 
1061
    while(resample_buffer_offset < n)
 
1062
    {
 
1063
        if(cc == 0)
 
1064
        {
 
1065
            if((cc = rs_update_porta(v)) == 0)
 
1066
            {
 
1067
                i = n - resample_buffer_offset;
 
1068
                resampler(v, &i, mode);
 
1069
                resample_buffer_offset += i;
 
1070
                break;
 
1071
            }
 
1072
        }
 
1073
 
 
1074
        i = n - resample_buffer_offset;
 
1075
        if(i > (uint32)cc)
 
1076
            i = (uint32)cc;
 
1077
        resampler(v, &i, mode);
 
1078
        resample_buffer_offset += i;
 
1079
 
 
1080
        /* if(!loop && vp->status == VOICE_FREE) */
 
1081
        if(vp->status == VOICE_FREE)
 
1082
            break;
 
1083
        cc -= (int)i;
 
1084
    }
 
1085
    *countptr = resample_buffer_offset;
 
1086
    resample_buffer_offset = 0;
 
1087
    vp->porta_control_counter = cc;
 
1088
    return resample_buffer;
 
1089
}
 
1090
 
 
1091
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
 
1092
{
 
1093
    Voice *vp = &voice[v];
 
1094
 
 
1095
    /* vp->cache = NULL; */
 
1096
    if(mode == 0)
 
1097
        return rs_vib_loop(v, vp, countptr);
 
1098
    if(mode == 1)
 
1099
        return rs_vib_plain(v, countptr);
 
1100
    return rs_vib_bidir(v, vp, *countptr);
 
1101
}
 
1102
 
 
1103
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
 
1104
{
 
1105
    Voice *vp = &voice[v];
 
1106
    if(mode == 0)
 
1107
        return rs_loop(v, vp, countptr);
 
1108
    if(mode == 1)
 
1109
        return rs_plain(v, countptr);
 
1110
    return rs_bidir(v, vp, *countptr);
 
1111
}
 
1112
 
 
1113
sample_t *resample_voice_lagrange(int v, uint32 *countptr)
 
1114
{
 
1115
    Voice *vp=&voice[v];
 
1116
    int mode;
 
1117
 
 
1118
    mode = vp->sample->modes;
 
1119
    if((mode & MODES_LOOPING) &&
 
1120
       ((mode & MODES_ENVELOPE) ||
 
1121
        (vp->status & (VOICE_ON | VOICE_SUSTAINED))))
 
1122
    {
 
1123
        if(mode & MODES_PINGPONG)
 
1124
        {
 
1125
            /* vp->cache = NULL; */
 
1126
            mode = 2;
 
1127
        }
 
1128
        else
 
1129
            mode = 0;
 
1130
    }
 
1131
    else
 
1132
        mode = 1;
 
1133
 
 
1134
    if(vp->porta_control_ratio)
 
1135
        return porta_resample_voice(v, countptr, mode);
 
1136
 
 
1137
    if(vp->vibrato_control_ratio)
 
1138
        return vib_resample_voice(v, countptr, mode);
 
1139
 
 
1140
    return normal_resample_voice(v, countptr, mode);
 
1141
}
 
1142