1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4     Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20     resample.c
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif /* HAVE_CONFIG_H */
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #ifndef NO_STRING_H
31 #include <string.h>
32 #else
33 #include <strings.h>
34 #endif
35 
36 #include "timidity.h"
37 #include "common.h"
38 #include "instrum.h"
39 #include "playmidi.h"
40 #include "output.h"
41 #include "controls.h"
42 #include "tables.h"
43 #include "resample.h"
44 #include "recache.h"
45 
46 
47 /* for start/end of samples */
48 static float newt_coeffs[58][58] = {
49 #include "newton_table.c"
50 };
51 
52 int sample_bounds_min, sample_bounds_max; /* min/max bounds for sample data */
53 
54 /* 4-point interpolation by cubic spline curve. */
55 
resample_cspline(sample_t * src,splen_t ofs,resample_rec_t * rec)56 static resample_t resample_cspline(sample_t *src, splen_t ofs, resample_rec_t *rec)
57 {
58     int32 ofsi, ofsf, v0, v1, v2, v3, temp;
59 
60     ofsi = ofs >> FRACTION_BITS;
61     v1 = src[ofsi];
62     v2 = src[ofsi + 1];
63     if((ofs<rec->loop_start+(1L<<FRACTION_BITS))||
64        ((ofs+(2L<<FRACTION_BITS))>rec->loop_end)){
65 	return (v1 + ((resample_t)((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
66     } else {
67 	v0 = src[ofsi - 1];
68 	v3 = src[ofsi + 2];
69 	ofsf = ofs & FRACTION_MASK;
70 	temp = v2;
71 	v2 = (6 * v2 + ((((5 * v3 - 11 * v2 + 7 * v1 - v0) >> 2) *
72 			 (ofsf + (1L << FRACTION_BITS)) >> FRACTION_BITS) *
73 			(ofsf - (1L << FRACTION_BITS)) >> FRACTION_BITS))
74 	    * ofsf;
75 	v1 = (((6 * v1+((((5 * v0 - 11 * v1 + 7 * temp - v3) >> 2) *
76 			 ofsf >> FRACTION_BITS) * (ofsf - (2L << FRACTION_BITS))
77 			>> FRACTION_BITS)) * ((1L << FRACTION_BITS) - ofsf)) + v2)
78 	    / (6L << FRACTION_BITS);
79 	return ((v1 > sample_bounds_max) ? sample_bounds_max :
80 		((v1 < sample_bounds_min) ? sample_bounds_min : v1));
81     }
82 }
83 
84 
85 /* 4-point interpolation by Lagrange method.
86    Lagrange is now faster than C-spline.  Both have about the same accuracy,
87    so choose Lagrange over C-spline, since it is faster.  Technically, it is
88    really a 3rd order Newton polynomial (whereas the old Lagrange truely was
89    the Lagrange form of the polynomial).  Both Newton and Lagrange forms
90    yield the same numerical results, but the Newton form is faster.  Since
91    n'th order Newton interpolaiton is resample_newton(), it made sense to
92    just keep this labeled as resample_lagrange(), even if it really is the
93    Newton form of the polynomial. */
94 
resample_lagrange(sample_t * src,splen_t ofs,resample_rec_t * rec)95 static resample_t resample_lagrange(sample_t *src, splen_t ofs, resample_rec_t *rec)
96 {
97     int32 ofsi, ofsf, v0, v1, v2, v3;
98 
99     ofsi = ofs >> FRACTION_BITS;
100     v1 = (int32)src[ofsi];
101     v2 = (int32)src[ofsi + 1];
102     if((ofs<rec->loop_start+(1L<<FRACTION_BITS))||
103        ((ofs+(2L<<FRACTION_BITS))>rec->loop_end)) {
104 	return (v1 + ((resample_t)((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
105     } else {
106 	v0 = (int32)src[ofsi - 1];
107 	v3 = (int32)src[ofsi + 2];
108 	ofsf = (ofs & FRACTION_MASK) + (1<<FRACTION_BITS);
109 	v3 += -3*v2 + 3*v1 - v0;
110 	v3 *= (ofsf - (2<<FRACTION_BITS)) / 6;
111 	v3 >>= FRACTION_BITS;
112 	v3 += v2 - v1 - v1 + v0;
113 	v3 *= (ofsf - (1<<FRACTION_BITS)) >> 1;
114 	v3 >>= FRACTION_BITS;
115 	v3 += v1 - v0;
116 	v3 *= ofsf;
117 	v3 >>= FRACTION_BITS;
118 	v3 += v0;
119 	return ((v3 > sample_bounds_max) ? sample_bounds_max :
120 		((v3 < sample_bounds_min) ? sample_bounds_min : v3));
121     }
122 }
123 
124 
125 /* Very fast and accurate table based interpolation.  Better speed and higher
126    accuracy than Newton.  This isn't *quite* true Gauss interpolation; it's
127    more a slightly modified Gauss interpolation that I accidently stumbled
128    upon.  Rather than normalize all x values in the window to be in the range
129    [0 to 2*PI], it simply divides them all by 2*PI instead.  I don't know why
130    this works, but it does.  Gauss should only work on periodic data with the
131    window spanning exactly one period, so it is no surprise that regular Gauss
132    interpolation doesn't work too well on general audio data.  But dividing
133    the x values by 2*PI magically does.  Any other scaling produces degraded
134    results or total garbage.  If anyone can work out the theory behind why
135    this works so well (at first glance, it shouldn't ??), please contact me
136    (Eric A. Welsh, ewelsh@ccb.wustl.edu), as I would really like to have some
137    mathematical justification for doing this.  Despite the lack of any sound
138    theoretical basis, this method DOES result in highly accurate interpolation
139    (or possibly approximaton, not sure yet if it truly interpolates, but it
140    looks like it does).  -N 34 is as high as it can go before errors start
141    appearing.  But even at -N 34, it is more accurate than Newton at -N 57.
142    -N 34 has no problem running in realtime on my system, but -N 25 is the
143    default, since that is the optimal compromise between speed and accuracy.
144    I strongly recommend using Gauss interpolation.  It is the highest
145    quality interpolation option available, and is much faster than using
146    Newton polynomials. */
147 
148 #define DEFAULT_GAUSS_ORDER	25
149 static float *gauss_table[(1<<FRACTION_BITS)] = {0};	/* don't need doubles */
150 static int gauss_n = DEFAULT_GAUSS_ORDER;
151 
resample_gauss(sample_t * src,splen_t ofs,resample_rec_t * rec)152 static resample_t resample_gauss(sample_t *src, splen_t ofs, resample_rec_t *rec)
153 {
154     sample_t *sptr;
155     int32 left, right, temp_n;
156 
157     left = (ofs>>FRACTION_BITS);
158     right = (rec->data_length>>FRACTION_BITS) - left - 1;
159     temp_n = (right<<1)-1;
160     if (temp_n > (left<<1)+1)
161 	temp_n = (left<<1)+1;
162     if (temp_n < gauss_n) {
163 	int ii, jj;
164 	float xd, y;
165 	if (temp_n <= 0)
166 	    temp_n = 1;
167 	xd = ofs & FRACTION_MASK;
168 	xd /= (1L<<FRACTION_BITS);
169 	xd += temp_n>>1;
170 	y = 0;
171 	sptr = src + (ofs>>FRACTION_BITS) - (temp_n>>1);
172 	for (ii = temp_n; ii;) {
173 	    for (jj = 0; jj <= ii; jj++)
174 		y += sptr[jj] * newt_coeffs[ii][jj];
175 	    y *= xd - --ii;
176 	}
177 	y += *sptr;
178 	return ((y > sample_bounds_max) ? sample_bounds_max :
179 		((y < sample_bounds_min) ? sample_bounds_min : y));
180     } else {
181 	float *gptr, *gend;
182 	float y;
183 	y = 0;
184 	sptr = src + left - (gauss_n>>1);
185 	gptr = gauss_table[ofs&FRACTION_MASK];
186 	if (gauss_n == DEFAULT_GAUSS_ORDER) {
187 	    /* expanding the loop for the default case.
188 	     * this will allow intensive optimization when compiled
189 	     * with SSE2 capability.
190 	     */
191 #define do_gauss  y += *(sptr++) * *(gptr++);
192 	    do_gauss;
193 	    do_gauss;
194 	    do_gauss;
195 	    do_gauss;
196 	    do_gauss;
197 	    do_gauss;
198 	    do_gauss;
199 	    do_gauss;
200 	    do_gauss;
201 	    do_gauss;
202 	    do_gauss;
203 	    do_gauss;
204 	    do_gauss;
205 	    do_gauss;
206 	    do_gauss;
207 	    do_gauss;
208 	    do_gauss;
209 	    do_gauss;
210 	    do_gauss;
211 	    do_gauss;
212 	    do_gauss;
213 	    do_gauss;
214 	    do_gauss;
215 	    do_gauss;
216 	    do_gauss;
217 	    y += *sptr * *gptr;
218 #undef do_gauss
219 	} else {
220 	    gend = gptr + gauss_n;
221 	    do {
222 		y += *(sptr++) * *(gptr++);
223 	    } while (gptr <= gend);
224 	}
225 	return ((y > sample_bounds_max) ? sample_bounds_max :
226 		((y < sample_bounds_min) ? sample_bounds_min : y));
227     }
228 }
229 
230 
231 /* (at least) n+1 point interpolation using Newton polynomials.
232    n can be set with a command line option, and
233    must be an odd number from 1 to 57 (57 is as high as double precision
234    can go without precision errors).  Default n = 11 is good for a 1.533 MHz
235    Athlon.  Larger values for n require very fast processors for real time
236    playback.  Some points will be interpolated at orders > n to both increase
237    accuracy and save CPU. */
238 
239 static int newt_n = 11;
240 static int32 newt_old_trunc_x = -1;
241 static int newt_grow = -1;
242 static int newt_max = 13;
243 static double newt_divd[60][60];
244 static double newt_recip[60] = { 0, 1, 1.0/2, 1.0/3, 1.0/4, 1.0/5, 1.0/6, 1.0/7,
245 			1.0/8, 1.0/9, 1.0/10, 1.0/11, 1.0/12, 1.0/13, 1.0/14,
246 			1.0/15, 1.0/16, 1.0/17, 1.0/18, 1.0/19, 1.0/20, 1.0/21,
247 			1.0/22, 1.0/23, 1.0/24, 1.0/25, 1.0/26, 1.0/27, 1.0/28,
248 			1.0/29, 1.0/30, 1.0/31, 1.0/32, 1.0/33, 1.0/34, 1.0/35,
249 			1.0/36, 1.0/37, 1.0/38, 1.0/39, 1.0/40, 1.0/41, 1.0/42,
250 			1.0/43, 1.0/44, 1.0/45, 1.0/46, 1.0/47, 1.0/48, 1.0/49,
251 			1.0/50, 1.0/51, 1.0/52, 1.0/53, 1.0/54, 1.0/55, 1.0/56,
252 			1.0/57, 1.0/58, 1.0/59 };
253 static sample_t *newt_old_src = NULL;
254 
resample_newton(sample_t * src,splen_t ofs,resample_rec_t * rec)255 static resample_t resample_newton(sample_t *src, splen_t ofs, resample_rec_t *rec)
256 {
257     int n_new, n_old;
258     int32 v1, v2, diff = 0;
259     sample_t *sptr;
260     double y, xd;
261     int32 left, right, temp_n;
262     int ii, jj;
263 
264     left = (ofs>>FRACTION_BITS);
265     right = (rec->data_length>>FRACTION_BITS)-(ofs>>FRACTION_BITS)-1;
266     temp_n = (right<<1)-1;
267     if (temp_n <= 0)
268 	temp_n = 1;
269     if (temp_n > (left<<1)+1)
270 	temp_n = (left<<1)+1;
271     if (temp_n < newt_n) {
272 	xd = ofs & FRACTION_MASK;
273 	xd /= (1L<<FRACTION_BITS);
274 	xd += temp_n>>1;
275 	y = 0;
276 	sptr = src + (ofs>>FRACTION_BITS) - (temp_n>>1);
277 	for (ii = temp_n; ii;) {
278 	    for (jj = 0; jj <= ii; jj++)
279 		y += sptr[jj] * newt_coeffs[ii][jj];
280 	    y *= xd - --ii;
281 	} y += *sptr;
282     }else{
283 	if (newt_grow >= 0 && src == newt_old_src &&
284 	    (diff = (ofs>>FRACTION_BITS) - newt_old_trunc_x) > 0){
285 	    n_new = newt_n + ((newt_grow + diff)<<1);
286 	    if (n_new <= newt_max){
287 		n_old = newt_n + (newt_grow<<1);
288 		newt_grow += diff;
289 		for (v1=(ofs>>FRACTION_BITS)+(n_new>>1)+1,v2=n_new;
290 		     v2 > n_old; --v1, --v2){
291 		    newt_divd[0][v2] = src[v1];
292 		}for (v1 = 1; v1 <= n_new; v1++)
293 		    for (v2 = n_new; v2 > n_old; --v2)
294 			newt_divd[v1][v2] = (newt_divd[v1-1][v2] -
295 					     newt_divd[v1-1][v2-1]) *
296 			    newt_recip[v1];
297 	    }else newt_grow = -1;
298 	}
299 	if (newt_grow < 0 || src != newt_old_src || diff < 0){
300 	    newt_grow = 0;
301 	    for (v1=(ofs>>FRACTION_BITS)-(newt_n>>1),v2=0;
302 		 v2 <= newt_n; v1++, v2++){
303 		newt_divd[0][v2] = src[v1];
304 	    }for (v1 = 1; v1 <= newt_n; v1++)
305 		for (v2 = newt_n; v2 >= v1; --v2)
306 		    newt_divd[v1][v2] = (newt_divd[v1-1][v2] -
307 					 newt_divd[v1-1][v2-1]) *
308 			newt_recip[v1];
309 	}
310 	n_new = newt_n + (newt_grow<<1);
311 	v2 = n_new;
312 	y = newt_divd[v2][v2];
313 	xd = (double)(ofs&FRACTION_MASK) / (1L<<FRACTION_BITS) +
314 	    (newt_n>>1) + newt_grow;
315 	for (--v2; v2; --v2){
316 	    y *= xd - v2;
317 	    y += newt_divd[v2][v2];
318 	}y = y*xd + **newt_divd;
319 	newt_old_src = src;
320 	newt_old_trunc_x = (ofs>>FRACTION_BITS);
321     }
322     return ((y > sample_bounds_max) ? sample_bounds_max :
323     	    ((y < sample_bounds_min) ? sample_bounds_min : y));
324 }
325 
326 
327 /* Simple linear interpolation */
328 
resample_linear(sample_t * src,splen_t ofs,resample_rec_t * rec)329 static resample_t resample_linear(sample_t *src, splen_t ofs, resample_rec_t *rec)
330 {
331     int32 v1, v2, ofsi;
332 
333     ofsi = ofs >> FRACTION_BITS;
334     v1 = src[ofsi];
335     v2 = src[ofsi + 1];
336 #if defined(LOOKUP_HACK) && defined(LOOKUP_INTERPOLATION)
337     return (sample_t)(v1 + (iplookup[(((v2 - v1) << 5) & 0x03FE0) |
338 				     ((ofs & FRACTION_MASK) >> (FRACTION_BITS-5))]));
339 #else
340     return (v1 + ((resample_t)((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS));
341 #endif
342 }
343 
344 
345 /* No interpolation -- Earplugs recommended for maximum listening enjoyment */
346 
resample_none(sample_t * src,splen_t ofs,resample_rec_t * rec)347 static resample_t resample_none(sample_t *src, splen_t ofs, resample_rec_t *rec)
348 {
349     return src[ofs >> FRACTION_BITS];
350 }
351 
352 
353 /*
354  */
355 
356 typedef resample_t (*resampler_t)(sample_t*, splen_t, resample_rec_t *);
357 static resampler_t resamplers[] = {
358     resample_cspline,
359     resample_lagrange,
360     resample_gauss,
361     resample_newton,
362     resample_linear,
363     resample_none
364 };
365 
366 #ifdef FIXED_RESAMPLATION
367 /* don't allow to change the resamplation algorighm.
368  * accessing directly to the given function.
369  * hope the compiler will optimize the overhead of function calls in this case.
370  */
371 #define cur_resample DEFAULT_RESAMPLATION
372 #else
373 static resampler_t cur_resample = DEFAULT_RESAMPLATION;
374 #endif
375 
376 #define RESAMPLATION *dest++ = cur_resample(src, ofs, &resrc);
377 
378 /* exported for recache.c */
do_resamplation(sample_t * src,splen_t ofs,resample_rec_t * rec)379 resample_t do_resamplation(sample_t *src, splen_t ofs, resample_rec_t *rec)
380 {
381     return cur_resample(src, ofs, rec);
382 }
383 
384 /* return the current resampling algorithm */
get_current_resampler(void)385 int get_current_resampler(void)
386 {
387     int i;
388     for (i = 0; i < (int)(sizeof(resamplers)/sizeof(resamplers[0])); i++)
389 	if (resamplers[i] == cur_resample)
390 	    return i;
391     return 0;
392 }
393 
394 /* set the current resampling algorithm */
set_current_resampler(int type)395 int set_current_resampler(int type)
396 {
397 #ifdef FIXED_RESAMPLATION
398     return -1;
399 #else
400     if (type < 0 || type > RESAMPLE_NONE)
401 	return -1;
402     cur_resample = resamplers[type];
403     return 0;
404 #endif
405 }
406 
407 /* #define FINALINTERP if (ofs < le) *dest++=src[(ofs>>FRACTION_BITS)-1]/2; */
408 #define FINALINTERP /* Nothing to do after TiMidity++ 2.9.0 */
409 /* So it isn't interpolation. At least it's final. */
410 
411 static resample_t resample_buffer[AUDIO_BUFFER_SIZE];
412 static int resample_buffer_offset;
413 static resample_t *vib_resample_voice(int, int32 *, int);
414 static resample_t *normal_resample_voice(int, int32 *, int);
415 
416 #ifdef PRECALC_LOOPS
417 #if SAMPLE_LENGTH_BITS == 32 && TIMIDITY_HAVE_INT64
418 #define PRECALC_LOOP_COUNT(start, end, incr) (int32)(((int64)((end) - (start) + (incr) - 1)) / (incr))
419 #else
420 #define PRECALC_LOOP_COUNT(start, end, incr) (int32)(((splen_t)((end) - (start) + (incr) - 1)) / (incr))
421 #endif
422 #endif /* PRECALC_LOOPS */
423 
initialize_gauss_table(int n)424 void initialize_gauss_table(int n)
425 {
426     int m, i, k, n_half = (n>>1);
427     double ck;
428     double x, x_inc, xz;
429     double z[35], zsin_[34 + 35], *zsin, xzsin[35];
430     float *gptr;
431 
432     for (i = 0; i <= n; i++)
433     	z[i] = i / (4*M_PI);
434     zsin = &zsin_[34];
435     for (i = -n; i <= n; i++)
436     	zsin[i] = sin(i / (4*M_PI));
437 
438     x_inc = 1.0 / (1<<FRACTION_BITS);
439     gptr = safe_realloc(gauss_table[0], (n+1)*sizeof(float)*(1<<FRACTION_BITS));
440     for (m = 0, x = 0.0; m < (1<<FRACTION_BITS); m++, x += x_inc)
441     {
442     	xz = (x + n_half) / (4*M_PI);
443     	for (i = 0; i <= n; i++)
444 	    xzsin[i] = sin(xz - z[i]);
445     	gauss_table[m] = gptr;
446 
447     	for (k = 0; k <= n; k++)
448     	{
449     	    ck = 1.0;
450 
451     	    for (i = 0; i <= n; i++)
452     	    {
453     	    	if (i == k)
454     	    	    continue;
455 
456     	    	ck *= xzsin[i] / zsin[k - i];
457     	    }
458 
459     	    *gptr++ = ck;
460     	}
461     }
462 }
463 
free_gauss_table(void)464 void free_gauss_table(void)
465 {
466 	if(gauss_table[0] != 0)
467 	  free(gauss_table[0]);
468 	gauss_table[0] = NULL;
469 }
470 
471 #if 0 /* NOT USED */
472 /* the was calculated statically in newton_table.c */
473 static void initialize_newton_coeffs(void)
474 {
475     int i, j, n = 57;
476     int sign;
477 
478     newt_coeffs[0][0] = 1;
479     for (i = 0; i <= n; i++)
480     {
481     	newt_coeffs[i][0] = 1;
482     	newt_coeffs[i][i] = 1;
483 
484 	if (i > 1)
485 	{
486 	    newt_coeffs[i][0] = newt_coeffs[i-1][0] / i;
487 	    newt_coeffs[i][i] = newt_coeffs[i-1][0] / i;
488 	}
489 
490     	for (j = 1; j < i; j++)
491     	{
492     	    newt_coeffs[i][j] = newt_coeffs[i-1][j-1] + newt_coeffs[i-1][j];
493 
494 	    if (i > 1)
495 	    	newt_coeffs[i][j] /= i;
496 	}
497     }
498     for (i = 0; i <= n; i++)
499     	for (j = 0, sign = pow(-1, i); j <= i; j++, sign *= -1)
500     	    newt_coeffs[i][j] *= sign;
501 }
502 #endif /* NOT USED */
503 
504 /* initialize the coefficients of the current resampling algorithm */
initialize_resampler_coeffs(void)505 void initialize_resampler_coeffs(void)
506 {
507     /* initialize_newton_coeffs(); */
508     initialize_gauss_table(gauss_n);
509     /* we don't have to initialize newton table any more */
510 
511     /* bounds checking values for the appropriate sample types */
512     /* this is as good a place as any to initialize them */
513     if (play_mode->encoding & PE_24BIT)
514     {
515     	sample_bounds_min = -8388608;
516     	sample_bounds_max = 8388607;
517     }
518     else /* 16-bit */
519     {
520     	sample_bounds_min = -32768;
521     	sample_bounds_max = 32767;
522     }
523 }
524 
525 /* change the parameter for the current resampling algorithm */
set_resampler_parm(int val)526 int set_resampler_parm(int val)
527 {
528     if (cur_resample == resample_gauss) {
529 	if (val < 1 || val > 34)
530 	    return -1;
531 	else
532 	    gauss_n = val;
533     } else if (cur_resample == resample_newton) {
534 	if (val < 1 || val > 57)
535 	    return -1;
536 	else if (val % 2 == 0)
537 	    return -1;
538 	else {
539 	    newt_n = val;
540 	    /* set optimal value for newt_max */
541 	    newt_max = newt_n * 1.57730263158 - 1.875328947;
542 	    if (newt_max < newt_n)
543 		    newt_max = newt_n;
544 	    if (newt_max > 57)
545 		    newt_max = 57;
546 	}
547     }
548     return 0;
549 }
550 
551 /*************** resampling with fixed increment *****************/
552 
rs_plain_c(int v,int32 * countptr)553 static resample_t *rs_plain_c(int v, int32 *countptr)
554 {
555     Voice *vp = &voice[v];
556     resample_t *dest = resample_buffer + resample_buffer_offset;
557 	sample_t *src = vp->sample->data;
558     int32 ofs, count = *countptr, i, le;
559 
560     le = (int32)(vp->sample->loop_end >> FRACTION_BITS);
561     ofs = (int32)(vp->sample_offset >> FRACTION_BITS);
562 
563     i = ofs + count;
564     if(i > le)
565 	i = le;
566     count = i - ofs;
567 
568 	for (i = 0; i < count; i++) {
569 		dest[i] = src[i + ofs];
570 	}
571 
572     ofs += count;
573     if(ofs == le)
574     {
575 	vp->timeout = 1;
576 	*countptr = count;
577     }
578     vp->sample_offset = ((splen_t)ofs << FRACTION_BITS);
579     return resample_buffer + resample_buffer_offset;
580 }
581 
rs_plain(int v,int32 * countptr)582 static resample_t *rs_plain(int v, int32 *countptr)
583 {
584   /* Play sample until end, then free the voice. */
585   Voice *vp = &voice[v];
586   resample_t *dest = resample_buffer + resample_buffer_offset;
587   sample_t *src = vp->sample->data;
588   splen_t
589     ofs = vp->sample_offset,
590     ls = 0,
591     le = vp->sample->data_length;
592   resample_rec_t resrc;
593   int32 count = *countptr, incr = vp->sample_increment;
594 #ifdef PRECALC_LOOPS
595   int32 i, j;
596 #endif
597 
598   if(vp->cache && incr == (1 << FRACTION_BITS))
599       return rs_plain_c(v, countptr);
600 
601   resrc.loop_start = ls;
602   resrc.loop_end = le;
603   resrc.data_length = vp->sample->data_length;
604 #ifdef PRECALC_LOOPS
605   if (incr < 0) incr = -incr; /* In case we're coming out of a bidir loop */
606 
607   /* Precalc how many times we should go through the loop.
608      NOTE: Assumes that incr > 0 and that ofs <= le */
609   i = PRECALC_LOOP_COUNT(ofs, le, incr);
610 
611   if (i > count)
612     {
613       i = count;
614       count = 0;
615     }
616   else count -= i;
617 
618   for(j = 0; j < i; j++)
619     {
620       RESAMPLATION;
621       ofs += incr;
622     }
623 
624   if (ofs >= le)
625     {
626       FINALINTERP;
627       vp->timeout = 1;
628       *countptr -= count;
629     }
630 #else /* PRECALC_LOOPS */
631     while (count--)
632     {
633       RESAMPLATION;
634       ofs += incr;
635       if (ofs >= le)
636 	{
637 	  FINALINTERP;
638 	  vp->timeout = 1;
639 	  *countptr -= count;
640 	  break;
641 	}
642     }
643 #endif /* PRECALC_LOOPS */
644 
645   vp->sample_offset = ofs; /* Update offset */
646   return resample_buffer + resample_buffer_offset;
647 }
648 
rs_loop_c(Voice * vp,int32 count)649 static resample_t *rs_loop_c(Voice *vp, int32 count)
650 {
651   int32
652     ofs = (int32)(vp->sample_offset >> FRACTION_BITS),
653     le = (int32)(vp->sample->loop_end >> FRACTION_BITS),
654     ll = le - (int32)(vp->sample->loop_start >> FRACTION_BITS);
655   resample_t *dest = resample_buffer + resample_buffer_offset;
656   sample_t *src = vp->sample->data;
657   int32 i, j;
658 
659   while(count)
660   {
661       while(ofs >= le)
662 	  ofs -= ll;
663       /* Precalc how many times we should go through the loop */
664       i = le - ofs;
665       if(i > count)
666 	  i = count;
667       count -= i;
668 	  for (j = 0; j < i; j++) {
669 		  dest[j] = src[j + ofs];
670 	  }
671       dest += i;
672       ofs += i;
673   }
674   vp->sample_offset = ((splen_t)ofs << FRACTION_BITS);
675   return resample_buffer + resample_buffer_offset;
676 }
677 
rs_loop(Voice * vp,int32 count)678 static resample_t *rs_loop(Voice *vp, int32 count)
679 {
680   /* Play sample until end-of-loop, skip back and continue. */
681   splen_t
682     ofs = vp->sample_offset,
683     ls, le, ll;
684   resample_rec_t resrc;
685   resample_t *dest = resample_buffer + resample_buffer_offset;
686   sample_t *src = vp->sample->data;
687 #ifdef PRECALC_LOOPS
688   int32 i, j;
689 #endif
690   int32 incr = vp->sample_increment;
691 
692   if(vp->cache && incr == (1 << FRACTION_BITS))
693       return rs_loop_c(vp, count);
694 
695   resrc.loop_start = ls = vp->sample->loop_start;
696   resrc.loop_end = le = vp->sample->loop_end;
697   ll = le - ls;
698   resrc.data_length = vp->sample->data_length;
699 
700 #ifdef PRECALC_LOOPS
701   while (count)
702     {
703       while (ofs >= le)	{ofs -= ll;}
704       /* Precalc how many times we should go through the loop */
705       i = PRECALC_LOOP_COUNT(ofs, le, incr);
706       if (i > count) {
707 		  i = count;
708 		  count = 0;
709 	  } else {count -= i;}
710       for(j = 0; j < i; j++) {
711 		  RESAMPLATION;
712 		  ofs += incr;
713 	  }
714     }
715 #else
716   while (count--)
717     {
718       RESAMPLATION;
719       ofs += incr;
720       if (ofs >= le)
721 	ofs -= ll; /* Hopefully the loop is longer than an increment. */
722     }
723 #endif
724 
725   vp->sample_offset = ofs; /* Update offset */
726   return resample_buffer + resample_buffer_offset;
727 }
728 
rs_bidir(Voice * vp,int32 count)729 static resample_t *rs_bidir(Voice *vp, int32 count)
730 {
731 #if SAMPLE_LENGTH_BITS == 32
732   int32
733 #else
734   splen_t
735 #endif
736     ofs = vp->sample_offset,
737     le = vp->sample->loop_end,
738     ls = vp->sample->loop_start;
739   resample_t *dest = resample_buffer + resample_buffer_offset;
740   sample_t *src = vp->sample->data;
741   int32 incr = vp->sample_increment;
742   resample_rec_t resrc;
743 
744 #ifdef PRECALC_LOOPS
745 #if SAMPLE_LENGTH_BITS == 32
746   int32
747 #else
748   splen_t
749 #endif
750     le2 = le << 1,
751     ls2 = ls << 1;
752   int32 i, j;
753   /* Play normally until inside the loop region */
754 
755   resrc.loop_start = ls;
756   resrc.loop_end = le;
757   resrc.data_length = vp->sample->data_length;
758 
759   if (incr > 0 && ofs < ls)
760     {
761       /* NOTE: Assumes that incr > 0, which is NOT always the case
762 	 when doing bidirectional looping.  I have yet to see a case
763 	 where both ofs <= ls AND incr < 0, however. */
764       i = PRECALC_LOOP_COUNT(ofs, ls, incr);
765       if (i > count)
766 	{
767 	  i = count;
768 	  count = 0;
769 	}
770       else count -= i;
771       for(j = 0; j < i; j++)
772 	{
773 	  RESAMPLATION;
774 	  ofs += incr;
775 	}
776     }
777 
778   /* Then do the bidirectional looping */
779 
780   while(count)
781     {
782       /* Precalc how many times we should go through the loop */
783       i = PRECALC_LOOP_COUNT(ofs, incr > 0 ? le : ls, incr);
784       if (i > count)
785 	{
786 	  i = count;
787 	  count = 0;
788 	}
789       else count -= i;
790       for(j = 0; j < i; j++)
791 	{
792 	  RESAMPLATION;
793 	  ofs += incr;
794 	}
795       if(ofs >= 0 && ofs >= le)
796 	{
797 	  /* fold the overshoot back in */
798 	  ofs = le2 - ofs;
799 	  incr *= -1;
800 	}
801       else if (ofs <= 0 || ofs <= ls)
802 	{
803 	  ofs = ls2 - ofs;
804 	  incr *= -1;
805 	}
806     }
807 
808 #else /* PRECALC_LOOPS */
809   /* Play normally until inside the loop region */
810 
811   if (ofs < ls)
812     {
813       while (count--)
814 	{
815 	  RESAMPLATION;
816 	  ofs += incr;
817 	  if (ofs >= ls)
818 	    break;
819 	}
820     }
821 
822   /* Then do the bidirectional looping */
823 
824   if (count > 0)
825     while (count--)
826       {
827 	RESAMPLATION;
828 	ofs += incr;
829 	if (ofs >= le)
830 	  {
831 	    /* fold the overshoot back in */
832 	    ofs = le - (ofs - le);
833 	    incr = -incr;
834 	  }
835 	else if (ofs <= ls)
836 	  {
837 	    ofs = ls + (ls - ofs);
838 	    incr = -incr;
839 	  }
840       }
841 #endif /* PRECALC_LOOPS */
842   vp->sample_increment = incr;
843   vp->sample_offset = ofs; /* Update offset */
844   return resample_buffer + resample_buffer_offset;
845 }
846 
847 /*********************** vibrato versions ***************************/
848 
849 /* We only need to compute one half of the vibrato sine cycle */
vib_phase_to_inc_ptr(int phase)850 static int vib_phase_to_inc_ptr(int phase)
851 {
852   if (phase < VIBRATO_SAMPLE_INCREMENTS / 2)
853     return VIBRATO_SAMPLE_INCREMENTS / 2 - 1 - phase;
854   else if (phase >= 3 * VIBRATO_SAMPLE_INCREMENTS / 2)
855     return 5 * VIBRATO_SAMPLE_INCREMENTS / 2 - 1 - phase;
856   else
857     return phase - VIBRATO_SAMPLE_INCREMENTS / 2;
858 }
859 
update_vibrato(Voice * vp,int sign)860 static int32 update_vibrato(Voice *vp, int sign)
861 {
862   int32 depth;
863   int phase, pb;
864   double a;
865   int ch = vp->channel;
866 
867   if(vp->vibrato_delay > 0)
868   {
869       vp->vibrato_delay -= vp->vibrato_control_ratio;
870       if(vp->vibrato_delay > 0)
871 	  return vp->sample_increment;
872   }
873 
874   if (vp->vibrato_phase++ >= 2 * VIBRATO_SAMPLE_INCREMENTS - 1)
875     vp->vibrato_phase = 0;
876   phase = vib_phase_to_inc_ptr(vp->vibrato_phase);
877 
878   if (vp->vibrato_sample_increment[phase])
879     {
880       if (sign)
881 	return -vp->vibrato_sample_increment[phase];
882       else
883 	return vp->vibrato_sample_increment[phase];
884     }
885 
886   /* Need to compute this sample increment. */
887 
888   depth = vp->vibrato_depth;
889   depth <<= 7;
890 
891   if (vp->vibrato_sweep && !channel[ch].mod.val)
892     {
893       /* Need to update sweep */
894       vp->vibrato_sweep_position += vp->vibrato_sweep;
895       if (vp->vibrato_sweep_position >= (1 << SWEEP_SHIFT))
896 	vp->vibrato_sweep=0;
897       else
898 	{
899 	  /* Adjust depth */
900 	  depth *= vp->vibrato_sweep_position;
901 	  depth >>= SWEEP_SHIFT;
902 	}
903     }
904 
905   if(vp->sample->inst_type == INST_SF2) {
906   pb = (int)((lookup_triangular(vp->vibrato_phase *
907 			(SINE_CYCLE_LENGTH / (2 * VIBRATO_SAMPLE_INCREMENTS)))
908 	    * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
909   } else {
910   pb = (int)((lookup_sine(vp->vibrato_phase *
911 			(SINE_CYCLE_LENGTH / (2 * VIBRATO_SAMPLE_INCREMENTS)))
912 	    * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
913   }
914 
915   a = TIM_FSCALE(((double)(vp->sample->sample_rate) *
916 		  (double)(vp->frequency)) /
917 		 ((double)(vp->sample->root_freq) *
918 		  (double)(play_mode->rate)),
919 		 FRACTION_BITS);
920 
921   if(pb < 0) {
922       pb = -pb;
923       a /= bend_fine[(pb >> 5) & 0xFF] * bend_coarse[pb >> 13];
924 	  pb = -pb;
925   } else {
926       a *= bend_fine[(pb >> 5) & 0xFF] * bend_coarse[pb >> 13];
927   }
928   a += 0.5;
929 
930   /* If the sweep's over, we can store the newly computed sample_increment */
931   if (!vp->vibrato_sweep || channel[ch].mod.val)
932     vp->vibrato_sample_increment[phase] = (int32) a;
933 
934   if (sign)
935     a = -a; /* need to preserve the loop direction */
936 
937   return (int32) a;
938 }
939 
rs_vib_plain(int v,int32 * countptr)940 static resample_t *rs_vib_plain(int v, int32 *countptr)
941 {
942   /* Play sample until end, then free the voice. */
943   Voice *vp = &voice[v];
944   resample_t *dest = resample_buffer + resample_buffer_offset;
945   sample_t *src = vp->sample->data;
946   splen_t
947     ls = 0,
948     le = vp->sample->data_length,
949     ofs = vp->sample_offset;
950   resample_rec_t resrc;
951 
952   int32 count = *countptr, incr = vp->sample_increment;
953   int cc = vp->vibrato_control_counter;
954 
955   resrc.loop_start = ls;
956   resrc.loop_end = le;
957   resrc.data_length = vp->sample->data_length;
958   /* This has never been tested */
959 
960   if (incr < 0) incr = -incr; /* In case we're coming out of a bidir loop */
961 
962   while (count--)
963     {
964       if (!cc--)
965 	{
966 	  cc = vp->vibrato_control_ratio;
967 	  incr = update_vibrato(vp, 0);
968 	}
969       RESAMPLATION;
970       ofs += incr;
971       if (ofs >= le)
972 	{
973 	  FINALINTERP;
974 	  vp->timeout = 1;
975 	  *countptr -= count;
976 	  break;
977 	}
978     }
979 
980   vp->vibrato_control_counter = cc;
981   vp->sample_increment = incr;
982   vp->sample_offset = ofs; /* Update offset */
983   return resample_buffer + resample_buffer_offset;
984 }
985 
rs_vib_loop(Voice * vp,int32 count)986 static resample_t *rs_vib_loop(Voice *vp, int32 count)
987 {
988   /* Play sample until end-of-loop, skip back and continue. */
989   splen_t
990     ofs = vp->sample_offset,
991     ls = vp->sample->loop_start,
992     le = vp->sample->loop_end,
993     ll = le - vp->sample->loop_start;
994   resample_t *dest = resample_buffer + resample_buffer_offset;
995   sample_t *src = vp->sample->data;
996   int cc = vp->vibrato_control_counter;
997   int32 incr = vp->sample_increment;
998   resample_rec_t resrc;
999 #ifdef PRECALC_LOOPS
1000   int32 i, j;
1001   int vibflag=0;
1002 #endif
1003 
1004   resrc.loop_start = ls;
1005   resrc.loop_end = le;
1006   resrc.data_length =vp->sample->data_length;
1007 
1008 #ifdef PRECALC_LOOPS
1009   while (count)
1010     {
1011       /* Hopefully the loop is longer than an increment */
1012       while(ofs >= le) {ofs -= ll;}
1013       /* Precalc how many times to go through the loop, taking
1014 	 the vibrato control ratio into account this time. */
1015       i = PRECALC_LOOP_COUNT(ofs, le, incr);
1016       if(i > count) {
1017 		  i = count;
1018 	  }
1019       if(i > cc) {
1020 		  i = cc;
1021 		  vibflag = 1;
1022 	  } else {cc -= i;}
1023       count -= i;
1024       if(vibflag) {
1025 		  cc = vp->vibrato_control_ratio;
1026 		  incr = update_vibrato(vp, 0);
1027 		  vibflag = 0;
1028 	  }
1029       for(j = 0; j < i; j++) {
1030 		  RESAMPLATION;
1031 		  ofs += incr;
1032 	  }
1033     }
1034 #else /* PRECALC_LOOPS */
1035   while (count--)
1036     {
1037       if (!cc--)
1038 	{
1039 	  cc=vp->vibrato_control_ratio;
1040 	  incr=update_vibrato(vp, 0);
1041 	}
1042       RESAMPLATION;
1043       ofs += incr;
1044       if (ofs >= le)
1045 	ofs -= ll; /* Hopefully the loop is longer than an increment. */
1046     }
1047 #endif /* PRECALC_LOOPS */
1048 
1049   vp->vibrato_control_counter = cc;
1050   vp->sample_increment = incr;
1051   vp->sample_offset = ofs; /* Update offset */
1052   return resample_buffer + resample_buffer_offset;
1053 }
1054 
rs_vib_bidir(Voice * vp,int32 count)1055 static resample_t *rs_vib_bidir(Voice *vp, int32 count)
1056 {
1057 #if SAMPLE_LENGTH_BITS == 32
1058   int32
1059 #else
1060   splen_t
1061 #endif
1062     ofs = vp->sample_offset,
1063     le = vp->sample->loop_end,
1064     ls = vp->sample->loop_start;
1065   resample_t *dest = resample_buffer + resample_buffer_offset;
1066   sample_t *src = vp->sample->data;
1067   int cc=vp->vibrato_control_counter;
1068   int32 incr = vp->sample_increment;
1069   resample_rec_t resrc;
1070 
1071 #if 0 /*def PRECALC_LOOPS*/
1072 #if SAMPLE_LENGTH_BITS == 32
1073   int32
1074 #else
1075   splen_t
1076 #endif
1077     le2 = le << 1,
1078     ls2 = ls << 1;
1079   int32 i, j;
1080   int vibflag = 0;
1081 
1082   resrc.loop_start = ls;
1083   resrc.loop_end = le;
1084   resrc.data_length = vp->sample->data_length;
1085   /* Play normally until inside the loop region */
1086   while (count && incr > 0 && ofs < ls)
1087     {
1088       i = PRECALC_LOOP_COUNT(ofs, ls, incr);
1089       if (i > count) i = count;
1090       if (i > cc)
1091 	{
1092 	  i = cc;
1093 	  vibflag = 1;
1094 	}
1095       else cc -= i;
1096       count -= i;
1097       if (vibflag)
1098 	{
1099 	  cc = vp->vibrato_control_ratio;
1100 	  incr = update_vibrato(vp, 0);
1101 	  vibflag = 0;
1102 	}
1103       for(j = 0; j < i; j++)
1104 	{
1105 	  RESAMPLATION;
1106 	  ofs += incr;
1107 	}
1108     }
1109 
1110   /* Then do the bidirectional looping */
1111 
1112   while (count)
1113     {
1114       /* Precalc how many times we should go through the loop */
1115       i = PRECALC_LOOP_COUNT(ofs, incr > 0 ? le : ls, incr);
1116       if(i > count) i = count;
1117       if(i > cc)
1118 	{
1119 	  i = cc;
1120 	  vibflag = 1;
1121 	}
1122       else cc -= i;
1123       count -= i;
1124       if (vibflag)
1125 	{
1126 	  cc = vp->vibrato_control_ratio;
1127 	  incr = update_vibrato(vp, (incr < 0));
1128 	  vibflag = 0;
1129 	}
1130       while (i--)
1131 	{
1132 	  RESAMPLATION;
1133 	  ofs += incr;
1134 	}
1135       if (ofs >= 0 && ofs >= le)
1136 	{
1137 	  /* fold the overshoot back in */
1138 	  ofs = le2 - ofs;
1139 	  incr *= -1;
1140 	}
1141       else if (ofs <= 0 || ofs <= ls)
1142 	{
1143 	  ofs = ls2 - ofs;
1144 	  incr *= -1;
1145 	}
1146     }
1147 
1148 #else /* PRECALC_LOOPS */
1149 
1150   resrc.loop_start = ls;
1151   resrc.loop_end = le;
1152   resrc.data_length = vp->sample->data_length;
1153   /* Play normally until inside the loop region */
1154 
1155   if (ofs < ls)
1156     {
1157       while (count--)
1158 	{
1159 	  if (!cc--)
1160 	    {
1161 	      cc = vp->vibrato_control_ratio;
1162 	      incr = update_vibrato(vp, 0);
1163 	    }
1164 	  RESAMPLATION;
1165 	  ofs += incr;
1166 	  if (ofs >= ls)
1167 	    break;
1168 	}
1169     }
1170 
1171   /* Then do the bidirectional looping */
1172 
1173   if (count > 0)
1174     while (count--)
1175       {
1176 	if (!cc--)
1177 	  {
1178 	    cc=vp->vibrato_control_ratio;
1179 	    incr=update_vibrato(vp, (incr < 0));
1180 	  }
1181 	RESAMPLATION;
1182 	ofs += incr;
1183 	if (ofs >= le)
1184 	  {
1185 	    /* fold the overshoot back in */
1186 	    ofs = le - (ofs - le);
1187 	    incr = -incr;
1188 	  }
1189 	else if (ofs <= ls)
1190 	  {
1191 	    ofs = ls + (ls - ofs);
1192 	    incr = -incr;
1193 	  }
1194       }
1195 #endif /* PRECALC_LOOPS */
1196 
1197   /* Update changed values */
1198   vp->vibrato_control_counter = cc;
1199   vp->sample_increment = incr;
1200   vp->sample_offset = ofs;
1201   return resample_buffer + resample_buffer_offset;
1202 }
1203 
1204 /*********************** portamento versions ***************************/
1205 
rs_update_porta(int v)1206 static int rs_update_porta(int v)
1207 {
1208     Voice *vp = &voice[v];
1209     int32 d;
1210 
1211     d = vp->porta_dpb;
1212     if(vp->porta_pb < 0)
1213     {
1214 	if(d > -vp->porta_pb)
1215 	    d = -vp->porta_pb;
1216     }
1217     else
1218     {
1219 	if(d > vp->porta_pb)
1220 	    d = -vp->porta_pb;
1221 	else
1222 	    d = -d;
1223     }
1224 
1225     vp->porta_pb += d;
1226     if(vp->porta_pb == 0)
1227     {
1228 	vp->porta_control_ratio = 0;
1229 	vp->porta_pb = 0;
1230     }
1231     recompute_freq(v);
1232     return vp->porta_control_ratio;
1233 }
1234 
porta_resample_voice(int v,int32 * countptr,int mode)1235 static resample_t *porta_resample_voice(int v, int32 *countptr, int mode)
1236 {
1237     Voice *vp = &voice[v];
1238     int32 n = *countptr, i;
1239     resample_t *(* resampler)(int, int32 *, int);
1240     int cc = vp->porta_control_counter;
1241     int loop;
1242 
1243     if(vp->vibrato_control_ratio)
1244 	resampler = vib_resample_voice;
1245     else
1246 	resampler = normal_resample_voice;
1247     if(mode != 1)
1248 	loop = 1;
1249     else
1250 	loop = 0;
1251 
1252     vp->cache = NULL;
1253     resample_buffer_offset = 0;
1254     while(resample_buffer_offset < n)
1255     {
1256 	if(cc == 0)
1257 	{
1258 	    if((cc = rs_update_porta(v)) == 0)
1259 	    {
1260 		i = n - resample_buffer_offset;
1261 		resampler(v, &i, mode);
1262 		resample_buffer_offset += i;
1263 		break;
1264 	    }
1265 	}
1266 
1267 	i = n - resample_buffer_offset;
1268 	if(i > cc)
1269 	    i = cc;
1270 	resampler(v, &i, mode);
1271 	resample_buffer_offset += i;
1272 
1273 	if(!loop && (i == 0 || vp->status == VOICE_FREE))
1274 	    break;
1275 	cc -= i;
1276     }
1277     *countptr = resample_buffer_offset;
1278     resample_buffer_offset = 0;
1279     vp->porta_control_counter = cc;
1280     return resample_buffer;
1281 }
1282 
1283 /* interface function */
vib_resample_voice(int v,int32 * countptr,int mode)1284 static resample_t *vib_resample_voice(int v, int32 *countptr, int mode)
1285 {
1286     Voice *vp = &voice[v];
1287 
1288     vp->cache = NULL;
1289     if(mode == 0)
1290 	return rs_vib_loop(vp, *countptr);
1291     if(mode == 1)
1292 	return rs_vib_plain(v, countptr);
1293     return rs_vib_bidir(vp, *countptr);
1294 }
1295 
1296 /* interface function */
normal_resample_voice(int v,int32 * countptr,int mode)1297 static resample_t *normal_resample_voice(int v, int32 *countptr, int mode)
1298 {
1299     Voice *vp = &voice[v];
1300     if(mode == 0)
1301 	return rs_loop(vp, *countptr);
1302     if(mode == 1)
1303 	return rs_plain(v, countptr);
1304     return rs_bidir(vp, *countptr);
1305 }
1306 
1307 /* interface function */
resample_voice(int v,int32 * countptr)1308 resample_t *resample_voice(int v, int32 *countptr)
1309 {
1310     Voice *vp = &voice[v];
1311     int mode;
1312     resample_t *result;
1313     resampler_t saved_resample;
1314 	int32 i;
1315 
1316     if(vp->sample->sample_rate == play_mode->rate &&
1317        vp->sample->root_freq == get_note_freq(vp->sample, vp->sample->note_to_use) &&
1318        vp->frequency == vp->orig_frequency)
1319     {
1320 	int32 ofs;
1321 
1322 	/* Pre-resampled data -- just update the offset and check if
1323 	   we're out of data. */
1324 	ofs = (int32)(vp->sample_offset >> FRACTION_BITS); /* Kind of silly to use
1325 						   FRACTION_BITS here... */
1326 	if(*countptr >= (vp->sample->data_length >> FRACTION_BITS) - ofs)
1327 	{
1328 	    /* Note finished. Free the voice. */
1329 	    vp->timeout = 1;
1330 
1331 	    /* Let the caller know how much data we had left */
1332 	    *countptr = (int32)(vp->sample->data_length >> FRACTION_BITS) - ofs;
1333 	}
1334 	else
1335 	    vp->sample_offset += *countptr << FRACTION_BITS;
1336 
1337 	for (i = 0; i < *countptr; i++) {
1338 		resample_buffer[i] = vp->sample->data[i + ofs];
1339 	}
1340 	return resample_buffer;
1341     }
1342 
1343     mode = vp->sample->modes;
1344     if((mode & MODES_LOOPING) &&
1345        ((mode & MODES_ENVELOPE) ||
1346 	(vp->status & (VOICE_ON | VOICE_SUSTAINED))))
1347     {
1348 	if(mode & MODES_PINGPONG)
1349 	{
1350 	    vp->cache = NULL;
1351 	    mode = 2;	/* Bidir loop */
1352 	}
1353 	else
1354 	    mode = 0;	/* loop */
1355     }
1356     else
1357 	mode = 1;	/* no loop */
1358 
1359     saved_resample = cur_resample;
1360 #ifndef FIXED_RESAMPLATION
1361     if (reduce_quality_flag && cur_resample != resample_none)
1362 	cur_resample = resample_linear;
1363 #endif
1364     if(vp->porta_control_ratio)
1365 	result = porta_resample_voice(v, countptr, mode);
1366     else if(vp->vibrato_control_ratio)
1367 	result = vib_resample_voice(v, countptr, mode);
1368     else
1369 	result = normal_resample_voice(v, countptr, mode);
1370 
1371 #ifndef FIXED_RESAMPLATION
1372     cur_resample = saved_resample; /* get back */
1373 #endif
1374     return result;
1375 }
1376 
pre_resample(Sample * sp)1377 void pre_resample(Sample * sp)
1378 {
1379   double a, b;
1380   splen_t ofs, newlen;
1381   sample_t *newdata, *dest, *src = (sample_t *)sp->data;
1382   int32 i, count, incr, f, x;
1383   resample_rec_t resrc;
1384 
1385   ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * pre-resampling for note %d (%s%d)",
1386 	    sp->note_to_use,
1387 	    note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
1388 
1389   f = get_note_freq(sp, sp->note_to_use);
1390   a = b = ((double) (sp->root_freq) * play_mode->rate) /
1391       ((double) (sp->sample_rate) * f);
1392   if((int64)sp->data_length * a >= 0x7fffffffL)
1393   {
1394       /* Too large to compute */
1395       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
1396 		sp->note_to_use);
1397       return;
1398   }
1399   newlen = (splen_t)(sp->data_length * a);
1400   count = (newlen >> FRACTION_BITS);
1401   ofs = incr = (sp->data_length - 1) / (count - 1);
1402 
1403   if((double)newlen + incr >= 0x7fffffffL)
1404   {
1405       /* Too large to compute */
1406       ctl->cmsg(CMSG_INFO, VERB_DEBUG, " *** Can't pre-resampling for note %d",
1407 		sp->note_to_use);
1408       return;
1409   }
1410 
1411   dest = newdata = (sample_t *)safe_malloc((int32)(newlen >> (FRACTION_BITS - 1)) + 2);
1412   dest[newlen >> FRACTION_BITS] = 0;
1413 
1414   *dest++ = src[0];
1415 
1416   resrc.loop_start = 0;
1417   resrc.loop_end = sp->data_length;
1418   resrc.data_length = sp->data_length;
1419 
1420   /* Since we're pre-processing and this doesn't have to be done in
1421      real-time, we go ahead and do the higher order interpolation. */
1422   for(i = 1; i < count; i++)
1423   {
1424 	x = cur_resample(src, ofs, &resrc);
1425     *dest++ = (int16)((x > 32767) ? 32767: ((x < -32768) ? -32768 : x));
1426     ofs += incr;
1427   }
1428 
1429   sp->data_length = newlen;
1430   sp->loop_start = (splen_t)(sp->loop_start * b);
1431   sp->loop_end = (splen_t)(sp->loop_end * b);
1432   free(sp->data);
1433   sp->data = (sample_t *) newdata;
1434   sp->root_freq = f;
1435   sp->sample_rate = play_mode->rate;
1436   sp->low_freq = freq_table[0];
1437   sp->high_freq = freq_table[127];
1438 }
1439