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

« back to all changes in this revision

Viewing changes to kmidi/resample.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.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 CSPLINE_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
sample_t resample_buffer[AUDIO_BUFFER_SIZE+100];
 
55
uint32 resample_buffer_offset = 0;
 
56
 
 
57
static sample_t *vib_resample_voice(int, uint32 *, int);
 
58
static sample_t *normal_resample_voice(int, uint32 *, int);
 
59
 
 
60
/* Returns 1 if envelope runs out */
 
61
int recompute_modulation(int v)
 
62
{
 
63
  int stage;
 
64
 
 
65
  stage = voice[v].modulation_stage;
 
66
 
 
67
  if (stage>5)
 
68
    {
 
69
      /* Envelope ran out. */
 
70
      voice[v].modulation_increment = 0;
 
71
      return 1;
 
72
    }
 
73
 
 
74
  if ((voice[v].sample->modes & MODES_ENVELOPE) && (voice[v].sample->modes & MODES_SUSTAIN))
 
75
    {
 
76
      if (voice[v].status & (VOICE_ON | VOICE_SUSTAINED))
 
77
        {
 
78
          if (stage>2)
 
79
            {
 
80
              /* Freeze modulation until note turns off. Trumpets want this. */
 
81
              voice[v].modulation_increment=0;
 
82
              return 0;
 
83
            }
 
84
        }
 
85
    }
 
86
  voice[v].modulation_stage=stage+1;
 
87
 
 
88
#ifdef tplus
 
89
  if (voice[v].modulation_volume==(int)voice[v].sample->modulation_offset[stage] ||
 
90
      (stage > 2 && voice[v].modulation_volume < (int)voice[v].sample->modulation_offset[stage]))
 
91
#else
 
92
  if (voice[v].modulation_volume==voice[v].sample->modulation_offset[stage])
 
93
#endif
 
94
    return recompute_modulation(v);
 
95
  voice[v].modulation_target=voice[v].sample->modulation_offset[stage];
 
96
  voice[v].modulation_increment = voice[v].sample->modulation_rate[stage];
 
97
  if ((int)voice[v].modulation_target<voice[v].modulation_volume)
 
98
    voice[v].modulation_increment = -voice[v].modulation_increment;
 
99
  return 0;
 
100
}
 
101
 
 
102
int update_modulation(int v)
 
103
{
 
104
 
 
105
  if(voice[v].modulation_delay > 0)
 
106
  {
 
107
      /* voice[v].modulation_delay -= control_ratio; I think units are already
 
108
         in terms of control_ratio */
 
109
      voice[v].modulation_delay -= 1;
 
110
      if(voice[v].modulation_delay > 0)
 
111
          return 0;
 
112
  }
 
113
 
 
114
 
 
115
  voice[v].modulation_volume += voice[v].modulation_increment;
 
116
  if (voice[v].modulation_volume < 0) voice[v].modulation_volume = 0;
 
117
  /* Why is there no ^^ operator?? */
 
118
  if (((voice[v].modulation_increment < 0) &&
 
119
       (voice[v].modulation_volume <= (int)voice[v].modulation_target)) ||
 
120
      ((voice[v].modulation_increment > 0) &&
 
121
           (voice[v].modulation_volume >= (int)voice[v].modulation_target)))
 
122
    {
 
123
      voice[v].modulation_volume = voice[v].modulation_target;
 
124
      if (recompute_modulation(v))
 
125
        return 1;
 
126
    }
 
127
  return 0;
 
128
}
 
129
 
 
130
/* Returns 1 if the note died */
 
131
int update_modulation_signal(int v)
 
132
{
 
133
  if (voice[v].modulation_increment && update_modulation(v))
 
134
    return 1;
 
135
  return 0;
 
136
}
 
137
 
 
138
 
 
139
 
 
140
/* modulation_volume has been set by above routine */
 
141
int32 calc_mod_freq(int v, int32 incr)
 
142
{
 
143
  FLOAT_T mod_amount;
 
144
  int32 freq;
 
145
  /* already done in update_vibrato ? */
 
146
  if (voice[v].vibrato_control_ratio) return incr;
 
147
  if ((mod_amount=voice[v].sample->modEnvToPitch)<0.02) return incr;
 
148
  if (incr < 0) return incr;
 
149
  freq = voice[v].frequency;
 
150
  freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (voice[v].modulation_volume>>22) / 255.0) );
 
151
 
 
152
  return (int32) FRSCALE(((double)(voice[v].sample->sample_rate) *
 
153
                  (double)(freq)) /
 
154
                 ((double)(voice[v].sample->root_freq) *
 
155
                  (double)(play_mode->rate)),
 
156
                 FRACTION_BITS);
 
157
}
 
158
 
 
159
/*************** resampling with fixed increment *****************/
 
160
 
 
161
static sample_t *rs_plain(int v, uint32 *countptr)
 
162
{
 
163
  /* Play sample until end, then free the voice. */
 
164
  Voice
 
165
    *vp=&voice[v];
 
166
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
167
  int offset;
 
168
  uint32 cc_count=vp->modulation_counter;
 
169
  sample_t
 
170
    *dest=resample_buffer+resample_buffer_offset,
 
171
    *src=vp->sample->data;
 
172
  int32
 
173
    incr=vp->sample_increment;
 
174
  uint32
 
175
    ofs=vp->sample_offset;
 
176
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
177
  uint32
 
178
    ls=0,
 
179
    le=vp->sample->data_length;
 
180
#endif /* LAGRANGE_INTERPOLATION */
 
181
  uint32
 
182
    se=vp->sample->data_length;
 
183
  uint32
 
184
    count=*countptr;
 
185
 
 
186
  if (!incr) return resample_buffer+resample_buffer_offset; /* --gl */
 
187
 
 
188
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
189
  if (overshoot < 0) overshoot = -overshoot;
 
190
 
 
191
    while (count--)
 
192
    {
 
193
 
 
194
        offset = ofs >> FRACTION_BITS;
 
195
 
 
196
        if (ofs >= se) {
 
197
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
198
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
199
                v1 -=  (delta+1) * v1 / overshoot;
 
200
        }else  v1 = (int32)src[offset];
 
201
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
202
                v2 = v1;
 
203
        }else  v2 = (int32)src[offset+1];
 
204
        if(dont_cspline ||
 
205
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
206
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
207
        }else{
 
208
                ofsd=ofs;
 
209
                v0 = (int32)src[offset-1];
 
210
                v3 = (int32)src[offset+2];
 
211
                ofs &= FRACTION_MASK;
 
212
                temp=v2;
 
213
                v2 = (6*v2 +
 
214
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
215
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
216
                ofs = (1L << FRACTION_BITS) - ofs;
 
217
                v1 = (6*v1 +
 
218
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
219
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
220
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
221
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
222
                ofs=ofsd;
 
223
        }
 
224
                if (!cc_count--) {
 
225
                    cc_count = control_ratio - 1;
 
226
                    if (!update_modulation_signal(v))
 
227
                        incr = calc_mod_freq(v, incr);
 
228
                }
 
229
      ofs += incr;
 
230
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
231
        {
 
232
          if (!(vp->status&VOICE_FREE))
 
233
            {
 
234
              vp->status=VOICE_FREE;
 
235
              ctl->note(v);
 
236
            }
 
237
          *countptr-=count+1;
 
238
          break;
 
239
        }
 
240
    }
 
241
 
 
242
  vp->sample_offset=ofs; /* Update offset */
 
243
  vp->modulation_counter=cc_count;
 
244
  return resample_buffer+resample_buffer_offset;
 
245
}
 
246
 
 
247
static sample_t *rs_loop(int v, Voice *vp, uint32 *countptr)
 
248
{
 
249
  /* Play sample until end-of-loop, skip back and continue. */
 
250
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
251
  int offset;
 
252
  uint32 cc_count=vp->modulation_counter;
 
253
  int32
 
254
    incr=vp->sample_increment;
 
255
  uint32
 
256
    ofs=vp->sample_offset;
 
257
  uint32
 
258
    le=vp->loop_end,
 
259
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
260
    ls=vp->loop_start,
 
261
#endif /* LAGRANGE_INTERPOLATION */
 
262
    ll=le - vp->loop_start;
 
263
  sample_t
 
264
    *dest=resample_buffer+resample_buffer_offset,
 
265
    *src=vp->sample->data;
 
266
  uint32
 
267
    se=vp->sample->data_length,
 
268
    count = *countptr;
 
269
  int
 
270
    flag_exit_loop;
 
271
 
 
272
 
 
273
  flag_exit_loop =
 
274
        (vp->status & (VOICE_FREE | VOICE_DIE)) ||
 
275
        ((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
 
276
        ((vp->status & VOICE_OFF) && dont_keep_looping ) ;
 
277
 
 
278
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
279
  if (overshoot < 0) overshoot = -overshoot;
 
280
 
 
281
  while (count--)
 
282
    {
 
283
 
 
284
        offset = ofs >> FRACTION_BITS;
 
285
 
 
286
        if (ofs >= se) {
 
287
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
288
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
289
                v1 -=  (delta+1) * v1 / overshoot;
 
290
        }else  v1 = (int32)src[offset];
 
291
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
292
                v2 = v1;
 
293
        }else  v2 = (int32)src[offset+1];
 
294
        if(dont_cspline ||
 
295
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
296
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
297
        }else{
 
298
                ofsd=ofs;
 
299
                v0 = (int32)src[offset-1];
 
300
                v3 = (int32)src[offset+2];
 
301
                ofs &= FRACTION_MASK;
 
302
                temp=v2;
 
303
                v2 = (6*v2 +
 
304
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
305
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
306
                ofs = (1L << FRACTION_BITS) - ofs;
 
307
                v1 = (6*v1 +
 
308
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
309
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
310
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
311
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
312
                ofs=ofsd;
 
313
        }
 
314
                if (!cc_count--) {
 
315
                    cc_count = control_ratio - 1;
 
316
                    if (!update_modulation_signal(v))
 
317
                        incr = calc_mod_freq(v, incr);
 
318
                }
 
319
      ofs += incr;
 
320
      if (ofs>=le)
 
321
        {
 
322
          if (flag_exit_loop)
 
323
            {
 
324
                vp->echo_delay -= ll >> FRACTION_BITS;
 
325
                if (vp->echo_delay >= 0) ofs -= ll;
 
326
            }
 
327
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
328
        }
 
329
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
330
        {
 
331
          if (!(vp->status&VOICE_FREE))
 
332
            {
 
333
              vp->status=VOICE_FREE;
 
334
              ctl->note(v);
 
335
            }
 
336
          *countptr-=count+1;
 
337
          break;
 
338
        }
 
339
    }
 
340
 
 
341
  vp->sample_offset=ofs; /* Update offset */
 
342
  vp->modulation_counter=cc_count;
 
343
  return resample_buffer+resample_buffer_offset;
 
344
}
 
345
 
 
346
static sample_t *rs_bidir(int v, Voice *vp, uint32 count)
 
347
{
 
348
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
349
  int offset;
 
350
  int32
 
351
    incr=vp->sample_increment;
 
352
  uint32
 
353
    le=vp->loop_end,
 
354
    ls=vp->loop_start;
 
355
  uint32
 
356
    ofs=vp->sample_offset,
 
357
    se=vp->sample->data_length;
 
358
  sample_t
 
359
    *dest=resample_buffer+resample_buffer_offset,
 
360
    *src=vp->sample->data;
 
361
 
 
362
 
 
363
#ifdef USE_BIDIR_OVERSHOOT
 
364
  int32
 
365
    le2 = le<<1,
 
366
    ls2 = ls<<1;
 
367
#endif
 
368
  uint32
 
369
    i, j;
 
370
  /* Play normally until inside the loop region */
 
371
 
 
372
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
373
  if (overshoot < 0) overshoot = -overshoot;
 
374
 
 
375
  if (ofs <= ls)
 
376
    {
 
377
      /* NOTE: Assumes that incr > 0, which is NOT always the case
 
378
         when doing bidirectional looping.  I have yet to see a case
 
379
         where both ofs <= ls AND incr < 0, however. */
 
380
      if (incr < 0) i = ls - ofs;
 
381
        else
 
382
      i = (ls - ofs) / incr + 1;
 
383
      if (i > count)
 
384
        {
 
385
          i = count;
 
386
          count = 0;
 
387
        }
 
388
      else count -= i;
 
389
      for(j = 0; j < i; j++)
 
390
        {
 
391
 
 
392
        offset = ofs >> FRACTION_BITS;
 
393
 
 
394
        if (ofs >= se) {
 
395
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
396
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
397
                v1 -=  (delta+1) * v1 / overshoot;
 
398
        }else  v1 = (int32)src[offset];
 
399
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
400
                v2 = v1;
 
401
        }else  v2 = (int32)src[offset+1];
 
402
        if(dont_cspline ||
 
403
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
404
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
405
        }else{
 
406
                ofsd=ofs;
 
407
                v0 = (int32)src[offset-1];
 
408
                v3 = (int32)src[offset+2];
 
409
                ofs &= FRACTION_MASK;
 
410
                temp=v2;
 
411
                v2 = (6*v2 +
 
412
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
413
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
414
                ofs = (1L << FRACTION_BITS) - ofs;
 
415
                v1 = (6*v1 +
 
416
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
417
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
418
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
419
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
420
                ofs=ofsd;
 
421
        }
 
422
          ofs += incr;
 
423
        }
 
424
    }
 
425
 
 
426
  /* Then do the bidirectional looping */
 
427
 
 
428
  while(count)
 
429
    {
 
430
      /* Precalc how many times we should go through the loop */
 
431
#if 1
 
432
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
433
#else
 
434
/* fix from M. Izumo */
 
435
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
436
#endif
 
437
      if (i > count)
 
438
        {
 
439
          i = count;
 
440
          count = 0;
 
441
        }
 
442
      else count -= i;
 
443
      for(j = 0; j < i && ofs < se; j++)
 
444
        {
 
445
 
 
446
        offset = ofs >> FRACTION_BITS;
 
447
 
 
448
        if (ofs >= se) {
 
449
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
450
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
451
                v1 -=  (delta+1) * v1 / overshoot;
 
452
        }else  v1 = (int32)src[offset];
 
453
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
454
                v2 = v1;
 
455
        }else  v2 = (int32)src[offset+1];
 
456
        if(dont_cspline ||
 
457
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
458
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
459
        }else{
 
460
                ofsd=ofs;
 
461
                v0 = (int32)src[offset-1];
 
462
                v3 = (int32)src[offset+2];
 
463
                ofs &= FRACTION_MASK;
 
464
                temp=v2;
 
465
                v2 = (6*v2 +
 
466
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
467
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
468
                ofs = (1L << FRACTION_BITS) - ofs;
 
469
                v1 = (6*v1 +
 
470
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
471
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
472
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
473
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
474
                ofs=ofsd;
 
475
        }
 
476
          ofs += incr;
 
477
        }
 
478
#ifdef USE_BIDIR_OVERSHOOT
 
479
      if (ofs>=le)
 
480
        {
 
481
          /* fold the overshoot back in */
 
482
          ofs = le2 - ofs;
 
483
          incr *= -1;
 
484
        }
 
485
      else if (ofs <= ls)
 
486
        {
 
487
          ofs = ls2 - ofs;
 
488
          incr *= -1;
 
489
        }
 
490
#else
 
491
          incr *= -1;
 
492
#endif
 
493
    }
 
494
 
 
495
  vp->sample_increment=incr;
 
496
  vp->sample_offset=ofs; /* Update offset */
 
497
  return resample_buffer+resample_buffer_offset;
 
498
}
 
499
 
 
500
/*********************** vibrato versions ***************************/
 
501
 
 
502
/* We only need to compute one half of the vibrato sine cycle */
 
503
static uint32 vib_phase_to_inc_ptr(uint32 phase)
 
504
{
 
505
  if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
 
506
    return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
 
507
  else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
 
508
    return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
 
509
  else
 
510
    return phase-VIBRATO_SAMPLE_INCREMENTS/2;
 
511
}
 
512
 
 
513
static int32 update_vibrato(Voice *vp, int sign)
 
514
{
 
515
  uint32 depth, freq=vp->frequency;
 
516
#ifdef ENVELOPE_PITCH_MODULATION
 
517
  FLOAT_T mod_amount=vp->sample->modEnvToPitch;
 
518
#endif
 
519
  uint32 phase;
 
520
  int pb;
 
521
  double a;
 
522
 
 
523
  if(vp->vibrato_delay > 0)
 
524
  {
 
525
      vp->vibrato_delay -= vp->vibrato_control_ratio;
 
526
      if(vp->vibrato_delay > 0)
 
527
          return vp->sample_increment;
 
528
  }
 
529
 
 
530
  if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
 
531
    vp->vibrato_phase=0;
 
532
  phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
 
533
 
 
534
  if (vp->vibrato_sample_increment[phase])
 
535
    {
 
536
      if (sign)
 
537
        return -vp->vibrato_sample_increment[phase];
 
538
      else
 
539
        return vp->vibrato_sample_increment[phase];
 
540
    }
 
541
 
 
542
  /* Need to compute this sample increment. */
 
543
 
 
544
  depth = vp->vibrato_depth;
 
545
  if(depth < vp->modulation_wheel)
 
546
      depth = vp->modulation_wheel;
 
547
  depth <<= 7;
 
548
 
 
549
  if (vp->vibrato_sweep && !vp->modulation_wheel)
 
550
    {
 
551
      /* Need to update sweep */
 
552
      vp->vibrato_sweep_position += vp->vibrato_sweep;
 
553
      if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
 
554
        vp->vibrato_sweep=0;
 
555
      else
 
556
        {
 
557
          /* Adjust depth */
 
558
          depth *= vp->vibrato_sweep_position;
 
559
          depth >>= SWEEP_SHIFT;
 
560
        }
 
561
    }
 
562
 
 
563
#ifdef ENVELOPE_PITCH_MODULATION
 
564
#ifndef FILTER_INTERPOLATION
 
565
  if (update_modulation_signal(0)) mod_amount = 0;
 
566
  else
 
567
#endif
 
568
  if (mod_amount>0.02)
 
569
   freq = (int32)( (double)freq*(1.0 + (mod_amount - 1.0) * (vp->modulation_volume>>22) / 255.0) );
 
570
#endif
 
571
 
 
572
  pb=(int)((sine(vp->vibrato_phase *
 
573
                        (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
 
574
            * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
 
575
 
 
576
  a = FRSCALE(((double)(vp->sample->sample_rate) *
 
577
                  (double)(freq)) /
 
578
                 ((double)(vp->sample->root_freq) *
 
579
                  (double)(play_mode->rate)),
 
580
                 FRACTION_BITS);
 
581
  if(pb<0)
 
582
  {
 
583
      pb = -pb;
 
584
      a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
585
  }
 
586
  else
 
587
      a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
 
588
  a += 0.5;
 
589
 
 
590
  /* If the sweep's over, we can store the newly computed sample_increment */
 
591
  if (!vp->vibrato_sweep || vp->modulation_wheel)
 
592
    vp->vibrato_sample_increment[phase]=(int32) a;
 
593
 
 
594
  if (sign)
 
595
    a = -a; /* need to preserve the loop direction */
 
596
 
 
597
  return (int32) a;
 
598
}
 
599
 
 
600
static sample_t *rs_vib_plain(int v, uint32 *countptr)
 
601
{
 
602
 
 
603
  /* Play sample until end, then free the voice. */
 
604
 
 
605
  Voice *vp=&voice[v];
 
606
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
607
  int offset;
 
608
  sample_t
 
609
    *dest=resample_buffer+resample_buffer_offset,
 
610
    *src=vp->sample->data;
 
611
  int32
 
612
    incr=vp->sample_increment;
 
613
/*WHY int32??*/
 
614
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
615
  uint32
 
616
    ls=0,
 
617
    le=vp->sample->data_length;
 
618
#endif /* LAGRANGE_INTERPOLATION */
 
619
  uint32
 
620
    ofs=vp->sample_offset,
 
621
    se=vp->sample->data_length,
 
622
    count=*countptr;
 
623
  uint32
 
624
    cc=vp->vibrato_control_counter;
 
625
 
 
626
  /* This has never been tested */
 
627
 
 
628
  if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
 
629
 
 
630
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
631
  if (overshoot < 0) overshoot = -overshoot;
 
632
  while (count--)
 
633
    {
 
634
      if (!cc--)
 
635
        {
 
636
          cc=vp->vibrato_control_ratio;
 
637
          incr=update_vibrato(vp, 0);
 
638
        }
 
639
 
 
640
        offset = ofs >> FRACTION_BITS;
 
641
 
 
642
        if (ofs >= se) {
 
643
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
644
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
645
                v1 -=  (delta+1) * v1 / overshoot;
 
646
        }else  v1 = (int32)src[offset];
 
647
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
648
                v2 = v1;
 
649
        }else  v2 = (int32)src[offset+1];
 
650
        if(dont_cspline ||
 
651
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
652
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
653
        }else{
 
654
                ofsd=ofs;
 
655
                v0 = (int32)src[offset-1];
 
656
                v3 = (int32)src[offset+2];
 
657
                ofs &= FRACTION_MASK;
 
658
                temp=v2;
 
659
                v2 = (6*v2 +
 
660
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
661
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
662
                ofs = (1L << FRACTION_BITS) - ofs;
 
663
                v1 = (6*v1 +
 
664
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
665
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
666
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
667
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
668
                ofs=ofsd;
 
669
        }
 
670
      ofs += incr;
 
671
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
672
        {
 
673
          if (!(vp->status&VOICE_FREE))
 
674
            {
 
675
              vp->status=VOICE_FREE;
 
676
              ctl->note(v);
 
677
            }
 
678
          *countptr-=count+1;
 
679
          break;
 
680
        }
 
681
    }
 
682
 
 
683
  vp->vibrato_control_counter=cc;
 
684
  vp->sample_increment=incr;
 
685
  vp->sample_offset=ofs; /* Update offset */
 
686
  return resample_buffer+resample_buffer_offset;
 
687
}
 
688
 
 
689
static sample_t *rs_vib_loop(int v, Voice *vp, uint32 *countptr)
 
690
{
 
691
  /* Play sample until end-of-loop, skip back and continue. */
 
692
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
693
  int offset;
 
694
  int32
 
695
    incr=vp->sample_increment;
 
696
/*WHY int32??*/
 
697
  uint32
 
698
#if defined(LAGRANGE_INTERPOLATION) || defined(CSPLINE_INTERPOLATION)
 
699
    ls=vp->loop_start,
 
700
#endif /* LAGRANGE_INTERPOLATION */
 
701
    le=vp->loop_end,
 
702
    ll=le - vp->loop_start;
 
703
  sample_t
 
704
    *dest=resample_buffer+resample_buffer_offset,
 
705
    *src=vp->sample->data;
 
706
  uint32
 
707
    ofs=vp->sample_offset,
 
708
    se=vp->sample->data_length,
 
709
    count = *countptr;
 
710
  uint32
 
711
    cc=vp->vibrato_control_counter;
 
712
  int
 
713
    flag_exit_loop;
 
714
 
 
715
 
 
716
  flag_exit_loop =
 
717
        (vp->status & (VOICE_FREE | VOICE_DIE)) ||
 
718
        ((vp->status & VOICE_OFF) && (vp->sample->modes & MODES_FAST_RELEASE) ) ||
 
719
        ((vp->status & VOICE_OFF) && dont_keep_looping ) ;
 
720
 
 
721
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
722
  if (overshoot < 0) overshoot = -overshoot;
 
723
  while (count--)
 
724
    {
 
725
      if (!cc--)
 
726
        {
 
727
          cc=vp->vibrato_control_ratio;
 
728
          incr=update_vibrato(vp, 0);
 
729
        }
 
730
 
 
731
        offset = ofs >> FRACTION_BITS;
 
732
 
 
733
        if (ofs >= se) {
 
734
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
735
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
736
                v1 -=  (delta+1) * v1 / overshoot;
 
737
        }else  v1 = (int32)src[offset];
 
738
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
739
                v2 = v1;
 
740
        }else  v2 = (int32)src[offset+1];
 
741
        if(dont_cspline ||
 
742
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
743
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
744
        }else{
 
745
                ofsd=ofs;
 
746
                v0 = (int32)src[offset-1];
 
747
                v3 = (int32)src[offset+2];
 
748
                ofs &= FRACTION_MASK;
 
749
                temp=v2;
 
750
                v2 = (6*v2 +
 
751
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
752
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
753
                ofs = (1L << FRACTION_BITS) - ofs;
 
754
                v1 = (6*v1 +
 
755
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
756
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
757
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
758
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
759
                ofs=ofsd;
 
760
        }
 
761
      ofs += incr;
 
762
      if (ofs>=le)
 
763
        {
 
764
          if (flag_exit_loop)
 
765
            {
 
766
                vp->echo_delay -= ll >> FRACTION_BITS;
 
767
                if (vp->echo_delay >= 0) ofs -= ll;
 
768
            }
 
769
          else ofs -= ll; /* Hopefully the loop is longer than an increment. */
 
770
        }
 
771
      if (ofs >= se + (overshoot << FRACTION_BITS))
 
772
        {
 
773
          if (!(vp->status&VOICE_FREE))
 
774
            {
 
775
              vp->status=VOICE_FREE;
 
776
              ctl->note(v);
 
777
            }
 
778
          *countptr-=count+1;
 
779
          break;
 
780
        }
 
781
    }
 
782
 
 
783
  vp->vibrato_control_counter=cc;
 
784
  vp->sample_increment=incr;
 
785
  vp->sample_offset=ofs; /* Update offset */
 
786
  return resample_buffer+resample_buffer_offset;
 
787
}
 
788
 
 
789
static sample_t *rs_vib_bidir(int v, Voice *vp, uint32 count)
 
790
{
 
791
  int32   ofsd, v0, v1, v2, v3, temp, overshoot;
 
792
  int offset;
 
793
  int32
 
794
    incr=vp->sample_increment;
 
795
/*WHY int32??*/
 
796
  uint32
 
797
    le=vp->loop_end,
 
798
    ls=vp->loop_start;
 
799
  uint32
 
800
    ofs=vp->sample_offset,
 
801
    se=vp->sample->data_length;
 
802
  sample_t
 
803
    *dest=resample_buffer+resample_buffer_offset,
 
804
    *src=vp->sample->data;
 
805
  uint32
 
806
    cc=vp->vibrato_control_counter;
 
807
 
 
808
 
 
809
#ifdef USE_BIDIR_OVERSHOOT
 
810
  uint32
 
811
    le2=le<<1,
 
812
    ls2=ls<<1;
 
813
#endif
 
814
  uint32
 
815
    i, j;
 
816
  int
 
817
    vibflag = 0;
 
818
 
 
819
 
 
820
  overshoot = src[(se>>FRACTION_BITS)-1] / OVERSHOOT_STEP;
 
821
  if (overshoot < 0) overshoot = -overshoot;
 
822
  /* Play normally until inside the loop region */
 
823
  while (count && (ofs <= ls))
 
824
    {
 
825
      i = (ls - ofs) / incr + 1;
 
826
      if (i > count) i = count;
 
827
      if (i > cc)
 
828
        {
 
829
          i = cc;
 
830
          vibflag = 1;
 
831
        }
 
832
      else cc -= i;
 
833
      count -= i;
 
834
      for(j = 0; j < i; j++)
 
835
        {
 
836
 
 
837
        offset = ofs >> FRACTION_BITS;
 
838
 
 
839
        if (ofs >= se) {
 
840
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
841
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
842
                v1 -=  (delta+1) * v1 / overshoot;
 
843
        }else  v1 = (int32)src[offset];
 
844
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
845
                v2 = v1;
 
846
        }else  v2 = (int32)src[offset+1];
 
847
        if(dont_cspline ||
 
848
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
849
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
850
        }else{
 
851
                ofsd=ofs;
 
852
                v0 = (int32)src[offset-1];
 
853
                v3 = (int32)src[offset+2];
 
854
                ofs &= FRACTION_MASK;
 
855
                temp=v2;
 
856
                v2 = (6*v2 +
 
857
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
858
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
859
                ofs = (1L << FRACTION_BITS) - ofs;
 
860
                v1 = (6*v1 +
 
861
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
862
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
863
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
864
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
865
                ofs=ofsd;
 
866
        }
 
867
          ofs += incr;
 
868
        }
 
869
      if (vibflag)
 
870
        {
 
871
          cc = vp->vibrato_control_ratio;
 
872
          incr = update_vibrato(vp, 0);
 
873
          vibflag = 0;
 
874
        }
 
875
    }
 
876
 
 
877
  /* Then do the bidirectional looping */
 
878
 
 
879
  while (count)
 
880
    {
 
881
      /* Precalc how many times we should go through the loop */
 
882
#if 1
 
883
      i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
 
884
#else
 
885
/* fix from M. Izumo */
 
886
      i = ((incr > 0 ? le : ls) - ofs + incr - 1) / incr;
 
887
#endif
 
888
      if(i > count) i = count;
 
889
      if(i > cc)
 
890
        {
 
891
          i = cc;
 
892
          vibflag = 1;
 
893
        }
 
894
      else cc -= i;
 
895
      count -= i;
 
896
      while (i-- && ofs < se)
 
897
        {
 
898
 
 
899
        offset = ofs >> FRACTION_BITS;
 
900
 
 
901
        if (ofs >= se) {
 
902
                int32 delta = (int32)((ofs - se)>>FRACTION_BITS) ;
 
903
                v1 = (int32)src[(int)(se>>FRACTION_BITS)-1];
 
904
                v1 -=  (delta+1) * v1 / overshoot;
 
905
        }else  v1 = (int32)src[offset];
 
906
        if (ofs + (1L<<FRACTION_BITS) >= se) {
 
907
                v2 = v1;
 
908
        }else  v2 = (int32)src[offset+1];
 
909
        if(dont_cspline ||
 
910
           ((ofs-(1L<<FRACTION_BITS))<ls)||((ofs+(2L<<FRACTION_BITS))>le)){
 
911
                *dest++ = (sample_t)(v1 + ((int32)((v2-v1) * (int32)(ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
912
        }else{
 
913
                ofsd=ofs;
 
914
                v0 = (int32)src[offset-1];
 
915
                v3 = (int32)src[offset+2];
 
916
                ofs &= FRACTION_MASK;
 
917
                temp=v2;
 
918
                v2 = (6*v2 +
 
919
                      (((( ( (5*v3 - 11*v2 + 7*v1 - v0)*
 
920
                       (int32)ofs) >>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
921
                ofs = (1L << FRACTION_BITS) - ofs;
 
922
                v1 = (6*v1 +
 
923
                      ((((((5*v0 - 11*v1 + 7*temp - v3)*
 
924
                       (int32)ofs)>>FRACTION_BITS)*(int32)ofs)>>(FRACTION_BITS+2))-1))*(int32)ofs;
 
925
                v1 = (v1 + v2)/(6L<<FRACTION_BITS);
 
926
                *dest++ = (sample_t)((v1 > MAX_DATAVAL)? MAX_DATAVAL: ((v1 < MIN_DATAVAL)? MIN_DATAVAL: v1));
 
927
                ofs=ofsd;
 
928
        }
 
929
          ofs += incr;
 
930
        }
 
931
      if (vibflag)
 
932
        {
 
933
          cc = vp->vibrato_control_ratio;
 
934
          incr = update_vibrato(vp, (incr < 0));
 
935
          vibflag = 0;
 
936
        }
 
937
      if (ofs >= le)
 
938
        {
 
939
#ifdef USE_BIDIR_OVERSHOOT
 
940
          /* fold the overshoot back in */
 
941
          ofs = le2 - ofs;
 
942
#endif
 
943
          incr *= -1;
 
944
        }
 
945
      else if (ofs <= ls)
 
946
        {
 
947
#ifdef USE_BIDIR_OVERSHOOT
 
948
          ofs = ls2 - ofs;
 
949
#endif
 
950
          incr *= -1;
 
951
        }
 
952
    }
 
953
 
 
954
 
 
955
  vp->vibrato_control_counter=cc;
 
956
  vp->sample_increment=incr;
 
957
  vp->sample_offset=ofs; /* Update offset */
 
958
  return resample_buffer+resample_buffer_offset;
 
959
}
 
960
 
 
961
static int rs_update_porta(int v)
 
962
{
 
963
    Voice *vp=&voice[v];
 
964
    int32 d;
 
965
 
 
966
    d = vp->porta_dpb;
 
967
    if(vp->porta_pb < 0)
 
968
    {
 
969
        if(d > -vp->porta_pb)
 
970
            d = -vp->porta_pb;
 
971
    }
 
972
    else
 
973
    {
 
974
        if(d > vp->porta_pb)
 
975
            d = -vp->porta_pb;
 
976
        else
 
977
            d = -d;
 
978
    }
 
979
 
 
980
    vp->porta_pb += d;
 
981
    if(vp->porta_pb == 0)
 
982
    {
 
983
        vp->porta_control_ratio = 0;
 
984
        vp->porta_pb = 0;
 
985
    }
 
986
    recompute_freq(v);
 
987
    return vp->porta_control_ratio;
 
988
}
 
989
 
 
990
static sample_t *porta_resample_voice(int v, uint32 *countptr, int mode)
 
991
{
 
992
    Voice *vp=&voice[v];
 
993
    uint32 n = *countptr;
 
994
    uint32 i;
 
995
    sample_t *(* resampler)(int, uint32 *, int);
 
996
    int cc = vp->porta_control_counter;
 
997
    int loop;
 
998
 
 
999
    if(vp->vibrato_control_ratio)
 
1000
        resampler = vib_resample_voice;
 
1001
    else
 
1002
        resampler = normal_resample_voice;
 
1003
    if(mode != 1)
 
1004
        loop = 1;
 
1005
    else
 
1006
        loop = 0;
 
1007
 
 
1008
    /* vp->cache = NULL; */
 
1009
    resample_buffer_offset = 0;
 
1010
    while(resample_buffer_offset < n)
 
1011
    {
 
1012
        if(cc == 0)
 
1013
        {
 
1014
            if((cc = rs_update_porta(v)) == 0)
 
1015
            {
 
1016
                i = n - resample_buffer_offset;
 
1017
                resampler(v, &i, mode);
 
1018
                resample_buffer_offset += i;
 
1019
                break;
 
1020
            }
 
1021
        }
 
1022
 
 
1023
        i = n - resample_buffer_offset;
 
1024
        if(i > (uint32)cc)
 
1025
            i = (uint32)cc;
 
1026
        resampler(v, &i, mode);
 
1027
        resample_buffer_offset += i;
 
1028
 
 
1029
        /* if(!loop && vp->status == VOICE_FREE) */
 
1030
        if(vp->status == VOICE_FREE)
 
1031
            break;
 
1032
        cc -= (int)i;
 
1033
    }
 
1034
    *countptr = resample_buffer_offset;
 
1035
    resample_buffer_offset = 0;
 
1036
    vp->porta_control_counter = cc;
 
1037
    return resample_buffer;
 
1038
}
 
1039
 
 
1040
static sample_t *vib_resample_voice(int v, uint32 *countptr, int mode)
 
1041
{
 
1042
    Voice *vp = &voice[v];
 
1043
 
 
1044
    /* vp->cache = NULL; */
 
1045
    if(mode == 0)
 
1046
        return rs_vib_loop(v, vp, countptr);
 
1047
    if(mode == 1)
 
1048
        return rs_vib_plain(v, countptr);
 
1049
    return rs_vib_bidir(v, vp, *countptr);
 
1050
}
 
1051
 
 
1052
static sample_t *normal_resample_voice(int v, uint32 *countptr, int mode)
 
1053
{
 
1054
    Voice *vp = &voice[v];
 
1055
    if(mode == 0)
 
1056
        return rs_loop(v, vp, countptr);
 
1057
    if(mode == 1)
 
1058
        return rs_plain(v, countptr);
 
1059
    return rs_bidir(v, vp, *countptr);
 
1060
}
 
1061
 
 
1062
sample_t *resample_voice(int v, uint32 *countptr)
 
1063
{
 
1064
    Voice *vp=&voice[v];
 
1065
    int mode;
 
1066
 
 
1067
    if(!(vp->sample->sample_rate))
 
1068
    {
 
1069
        uint32 ofs;
 
1070
 
 
1071
        /* Pre-resampled data -- just update the offset and check if
 
1072
           we're out of data. */
 
1073
        ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
 
1074
                                                   FRACTION_BITS here... */
 
1075
        if(*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
 
1076
        {
 
1077
            /* Note finished. Free the voice. */
 
1078
          if (!(vp->status&VOICE_FREE))
 
1079
            {
 
1080
              vp->status=VOICE_FREE;
 
1081
              ctl->note(v);
 
1082
            }
 
1083
 
 
1084
            /* Let the caller know how much data we had left */
 
1085
            *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
 
1086
        }
 
1087
        else
 
1088
            vp->sample_offset += *countptr << FRACTION_BITS;
 
1089
        return vp->sample->data+ofs;
 
1090
    }
 
1091
 
 
1092
    if (current_interpolation == 2)
 
1093
         return resample_voice_lagrange(v, countptr);
 
1094
    else if (current_interpolation == 3)
 
1095
         return resample_voice_filter(v, countptr);
 
1096
 
 
1097
    mode = vp->sample->modes;
 
1098
    if((mode & MODES_LOOPING) &&
 
1099
       ((mode & MODES_ENVELOPE) ||
 
1100
        (vp->status & (VOICE_ON | VOICE_SUSTAINED))))
 
1101
    {
 
1102
        if(mode & MODES_PINGPONG)
 
1103
        {
 
1104
            /* vp->cache = NULL; */
 
1105
            mode = 2;
 
1106
        }
 
1107
        else
 
1108
            mode = 0;
 
1109
    }
 
1110
    else
 
1111
        mode = 1;
 
1112
 
 
1113
    if(vp->porta_control_ratio)
 
1114
        return porta_resample_voice(v, countptr, mode);
 
1115
 
 
1116
    if(vp->vibrato_control_ratio)
 
1117
        return vib_resample_voice(v, countptr, mode);
 
1118
 
 
1119
    return normal_resample_voice(v, countptr, mode);
 
1120
}
 
1121
 
 
1122
 
 
1123
void do_lowpass(Sample *sample, uint32 srate, sample_t *buf, uint32 count, uint32 freq, FLOAT_T resonance)
 
1124
{
 
1125
    double a0=0, a1=0, a2=0, b0=0, b1=0;
 
1126
    double x0=0, x1=0, y0=0, y1=0;
 
1127
    sample_t samp;
 
1128
    double outsamp, insamp, mod_amount=0;
 
1129
    uint32 findex, cc;
 
1130
    uint32 current_freq;
 
1131
 
 
1132
    if (freq < 20) return;
 
1133
 
 
1134
    if (freq > srate * 2) {
 
1135
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
1136
                  "Lowpass: center must be < data rate*2");
 
1137
        return;
 
1138
    }
 
1139
 
 
1140
    current_freq = freq;
 
1141
 
 
1142
    if (sample->modEnvToFilterFc)
 
1143
        mod_amount = sample->modEnvToFilterFc;
 
1144
 
 
1145
    if (mod_amount < 0.02 && freq >= 13500) return;
 
1146
 
 
1147
    voice[0].sample = sample;
 
1148
 
 
1149
      /* Ramp up from 0 */
 
1150
    voice[0].modulation_stage=ATTACK;
 
1151
    voice[0].modulation_volume=0;
 
1152
    voice[0].modulation_delay=sample->modulation_rate[DELAY];
 
1153
    cc = voice[0].modulation_counter=0;
 
1154
    recompute_modulation(0);
 
1155
 
 
1156
/* start modulation loop here */
 
1157
    while (count--) {
 
1158
 
 
1159
        if (!cc) {
 
1160
            if (mod_amount>0.02) {
 
1161
                if (update_modulation_signal(0)) mod_amount = 0;
 
1162
                else
 
1163
                current_freq = (uint32)( (double)freq*(1.0 + (mod_amount - 1.0) *
 
1164
                         (voice[0].modulation_volume>>22) / 255.0) );
 
1165
            }
 
1166
            cc = control_ratio;
 
1167
            findex = 1 + (current_freq+50) / 100;
 
1168
            if (findex > 100) findex = 100;
 
1169
            a0 = butterworth[findex][0];
 
1170
            a1 = butterworth[findex][1];
 
1171
            a2 = butterworth[findex][2];
 
1172
            b0 = butterworth[findex][3];
 
1173
            b1 = butterworth[findex][4];
 
1174
        }
 
1175
 
 
1176
        if (mod_amount>0.02) cc--;
 
1177
 
 
1178
        samp = *buf;
 
1179
 
 
1180
        insamp = (double)samp;
 
1181
        outsamp = a0 * insamp + a1 * x0 + a2 * x1 - b0 * y0 - b1 * y1;
 
1182
        x1 = x0;
 
1183
        x0 = insamp;
 
1184
        y1 = y0;
 
1185
        y0 = outsamp;
 
1186
        if (outsamp > MAX_DATAVAL) {
 
1187
                outsamp = MAX_DATAVAL;
 
1188
        }
 
1189
        else if (outsamp < MIN_DATAVAL) {
 
1190
                outsamp = MIN_DATAVAL;
 
1191
        }
 
1192
        *buf++ = (sample_t)outsamp;
 
1193
    }
 
1194
}
 
1195
 
 
1196
 
 
1197
void pre_resample(Sample * sp)
 
1198
{
 
1199
  double a, xdiff;
 
1200
  uint32 i, incr, ofs, newlen, count, overshoot;
 
1201
  int16 *newdata, *dest, *src = (int16 *)sp->data, *vptr, *endptr;
 
1202
  int32 v1, v2, v3, v4;
 
1203
  static const char note_name[12][3] =
 
1204
  {
 
1205
    "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
 
1206
  };
 
1207
 
 
1208
  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * pre-resampling for note %d (%s%d)",
 
1209
            sp->note_to_use,
 
1210
            note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
 
1211
 
 
1212
  if (sp->sample_rate == play_mode->rate && sp->root_freq == freq_table[(int)(sp->note_to_use)]) {
 
1213
        a = 1;
 
1214
  }
 
1215
  else a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
 
1216
    ((double) (sp->root_freq) * play_mode->rate);
 
1217
 
 
1218
  /* if (a<1.0) return; */
 
1219
  if(sp->data_length / a >= 0x7fffffffL)
 
1220
  {
 
1221
      /* Too large to compute */
 
1222
      ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
 
1223
                sp->note_to_use);
 
1224
      return;
 
1225
  }
 
1226
 
 
1227
  endptr = src + (sp->data_length >> FRACTION_BITS) - 1;
 
1228
  if (*endptr < 0) overshoot = (uint32)(-(*endptr / OVERSHOOT_STEP));
 
1229
  else overshoot = (uint32)(*endptr / OVERSHOOT_STEP);
 
1230
  if (overshoot < 2) overshoot = 0;
 
1231
 
 
1232
  newlen = (int32)(sp->data_length / a);
 
1233
  count = (newlen >> FRACTION_BITS) - 1;
 
1234
  ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
 
1235
 
 
1236
  if((double)newlen + incr >= 0x7fffffffL)
 
1237
  {
 
1238
      /* Too large to compute */
 
1239
      ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
 
1240
                sp->note_to_use);
 
1241
      return;
 
1242
  }
 
1243
 
 
1244
  dest = newdata = (int16 *)safe_malloc((newlen >> (FRACTION_BITS - 1)) + 2 + 2*overshoot);
 
1245
 
 
1246
  if (--count)
 
1247
    *dest++ = src[0];
 
1248
 
 
1249
  /* Since we're pre-processing and this doesn't have to be done in
 
1250
     real-time, we go ahead and do the full sliding cubic interpolation. */
 
1251
  count--;
 
1252
  for(i = 0; i < count + overshoot; i++)
 
1253
    {
 
1254
      vptr = src + (ofs >> FRACTION_BITS);
 
1255
      if (i < count - 2 || !overshoot)
 
1256
        {
 
1257
          v1 = *(vptr - 1);
 
1258
          v2 = *vptr;
 
1259
          v3 = *(vptr + 1);
 
1260
          v4 = *(vptr + 2);
 
1261
        }
 
1262
      else
 
1263
        {
 
1264
          if (i < count + 1) v1 = *(vptr - 1);
 
1265
          else v1 = *endptr - (count-i+2) * *endptr / overshoot;
 
1266
          if (i < count) v2 = *vptr;
 
1267
          else v2 = *endptr - (count-i+1) * *endptr / overshoot;
 
1268
          if (i < count - 1) v3 = *(vptr + 1);
 
1269
          else v3 = *endptr - (count-i) * *endptr / overshoot;
 
1270
          v4 = *endptr - (count-i-1) * *endptr / overshoot;
 
1271
        }
 
1272
      xdiff = FRSCALENEG((int32)(ofs & FRACTION_MASK), FRACTION_BITS);
 
1273
      *dest++ = v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
 
1274
      xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4)));
 
1275
      ofs += incr;
 
1276
    }
 
1277
 
 
1278
 if (!overshoot)
 
1279
  {
 
1280
  if ((int32)(ofs & FRACTION_MASK))
 
1281
    {
 
1282
      v1 = src[ofs >> FRACTION_BITS];
 
1283
      v2 = src[(ofs >> FRACTION_BITS) + 1];
 
1284
      *dest++ = (int16)(v1 + ((int32)((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
 
1285
    }
 
1286
  else
 
1287
    *dest++ = src[ofs >> FRACTION_BITS];
 
1288
  *dest++ = *(dest - 1) / 2;
 
1289
  *dest++ = *(dest - 1) / 2;
 
1290
  }
 
1291
 
 
1292
  sp->data_length = newlen + (overshoot << FRACTION_BITS);
 
1293
  sp->loop_start = (int32)(sp->loop_start / a);
 
1294
  sp->loop_end = (int32)(sp->loop_end / a);
 
1295
  free(sp->data);
 
1296
  sp->data = (sample_t *) newdata;
 
1297
  sp->sample_rate = 0;
 
1298
}