Mercurial > audlegacy-plugins
comparison src/Input/timidity/libtimidity/resample.c @ 0:13389e613d67 trunk
[svn] - initial import of audacious-plugins tree (lots to do)
| author | nenolod |
|---|---|
| date | Mon, 18 Sep 2006 01:11:49 -0700 |
| parents | |
| children | 088092a52fea |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:13389e613d67 |
|---|---|
| 1 /* | |
| 2 | |
| 3 TiMidity -- Experimental MIDI to WAVE converter | |
| 4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 19 | |
| 20 resample.c | |
| 21 */ | |
| 22 | |
| 23 #if HAVE_CONFIG_H | |
| 24 # include <config.h> | |
| 25 #endif | |
| 26 | |
| 27 #include "libaudacious/vfs.h" | |
| 28 #include <math.h> | |
| 29 #include <stdlib.h> | |
| 30 | |
| 31 #include "timidity.h" | |
| 32 #include "timidity_internal.h" | |
| 33 #include "options.h" | |
| 34 #include "common.h" | |
| 35 #include "instrum.h" | |
| 36 #include "playmidi.h" | |
| 37 #include "tables.h" | |
| 38 #include "resample.h" | |
| 39 | |
| 40 /*************** resampling with fixed increment *****************/ | |
| 41 | |
| 42 static sample_t *rs_plain(MidSong *song, int v, sint32 *countptr) | |
| 43 { | |
| 44 | |
| 45 /* Play sample until end, then free the voice. */ | |
| 46 | |
| 47 sample_t v1, v2; | |
| 48 MidVoice | |
| 49 *vp=&(song->voice[v]); | |
| 50 sample_t | |
| 51 *dest=song->resample_buffer, | |
| 52 *src=vp->sample->data; | |
| 53 sint32 | |
| 54 ofs=vp->sample_offset, | |
| 55 incr=vp->sample_increment, | |
| 56 le=vp->sample->data_length, | |
| 57 count=*countptr; | |
| 58 sint32 i; | |
| 59 | |
| 60 if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */ | |
| 61 | |
| 62 /* Precalc how many times we should go through the loop. | |
| 63 NOTE: Assumes that incr > 0 and that ofs <= le */ | |
| 64 i = (le - ofs) / incr + 1; | |
| 65 | |
| 66 if (i > count) | |
| 67 { | |
| 68 i = count; | |
| 69 count = 0; | |
| 70 } | |
| 71 else count -= i; | |
| 72 | |
| 73 while (i--) | |
| 74 { | |
| 75 v1 = src[ofs >> FRACTION_BITS]; | |
| 76 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 77 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 78 ofs += incr; | |
| 79 } | |
| 80 | |
| 81 if (ofs >= le) | |
| 82 { | |
| 83 if (ofs == le) | |
| 84 *dest++ = src[ofs >> FRACTION_BITS]; | |
| 85 vp->status=VOICE_FREE; | |
| 86 *countptr-=count+1; | |
| 87 } | |
| 88 | |
| 89 vp->sample_offset=ofs; /* Update offset */ | |
| 90 return song->resample_buffer; | |
| 91 } | |
| 92 | |
| 93 static sample_t *rs_loop(MidSong *song, MidVoice *vp, sint32 count) | |
| 94 { | |
| 95 | |
| 96 /* Play sample until end-of-loop, skip back and continue. */ | |
| 97 | |
| 98 sample_t v1, v2; | |
| 99 sint32 | |
| 100 ofs=vp->sample_offset, | |
| 101 incr=vp->sample_increment, | |
| 102 le=vp->sample->loop_end, | |
| 103 ll=le - vp->sample->loop_start; | |
| 104 sample_t | |
| 105 *dest=song->resample_buffer, | |
| 106 *src=vp->sample->data; | |
| 107 sint32 i; | |
| 108 | |
| 109 while (count) | |
| 110 { | |
| 111 if (ofs >= le) | |
| 112 /* NOTE: Assumes that ll > incr and that incr > 0. */ | |
| 113 ofs -= ll; | |
| 114 /* Precalc how many times we should go through the loop */ | |
| 115 i = (le - ofs) / incr + 1; | |
| 116 if (i > count) | |
| 117 { | |
| 118 i = count; | |
| 119 count = 0; | |
| 120 } | |
| 121 else count -= i; | |
| 122 while (i--) | |
| 123 { | |
| 124 v1 = src[ofs >> FRACTION_BITS]; | |
| 125 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 126 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 127 ofs += incr; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 vp->sample_offset=ofs; /* Update offset */ | |
| 132 return song->resample_buffer; | |
| 133 } | |
| 134 | |
| 135 static sample_t *rs_bidir(MidSong *song, MidVoice *vp, sint32 count) | |
| 136 { | |
| 137 sample_t v1, v2; | |
| 138 sint32 | |
| 139 ofs=vp->sample_offset, | |
| 140 incr=vp->sample_increment, | |
| 141 le=vp->sample->loop_end, | |
| 142 ls=vp->sample->loop_start; | |
| 143 sample_t | |
| 144 *dest=song->resample_buffer, | |
| 145 *src=vp->sample->data; | |
| 146 sint32 | |
| 147 le2 = le<<1, | |
| 148 ls2 = ls<<1, | |
| 149 i; | |
| 150 /* Play normally until inside the loop region */ | |
| 151 | |
| 152 if (ofs <= ls) | |
| 153 { | |
| 154 /* NOTE: Assumes that incr > 0, which is NOT always the case | |
| 155 when doing bidirectional looping. I have yet to see a case | |
| 156 where both ofs <= ls AND incr < 0, however. */ | |
| 157 i = (ls - ofs) / incr + 1; | |
| 158 if (i > count) | |
| 159 { | |
| 160 i = count; | |
| 161 count = 0; | |
| 162 } | |
| 163 else count -= i; | |
| 164 while (i--) | |
| 165 { | |
| 166 v1 = src[ofs >> FRACTION_BITS]; | |
| 167 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 168 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 169 ofs += incr; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 /* Then do the bidirectional looping */ | |
| 174 | |
| 175 while(count) | |
| 176 { | |
| 177 /* Precalc how many times we should go through the loop */ | |
| 178 i = ((incr > 0 ? le : ls) - ofs) / incr + 1; | |
| 179 if (i > count) | |
| 180 { | |
| 181 i = count; | |
| 182 count = 0; | |
| 183 } | |
| 184 else count -= i; | |
| 185 while (i--) | |
| 186 { | |
| 187 v1 = src[ofs >> FRACTION_BITS]; | |
| 188 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 189 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 190 ofs += incr; | |
| 191 } | |
| 192 if (ofs>=le) | |
| 193 { | |
| 194 /* fold the overshoot back in */ | |
| 195 ofs = le2 - ofs; | |
| 196 incr *= -1; | |
| 197 } | |
| 198 else if (ofs <= ls) | |
| 199 { | |
| 200 ofs = ls2 - ofs; | |
| 201 incr *= -1; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 vp->sample_increment=incr; | |
| 206 vp->sample_offset=ofs; /* Update offset */ | |
| 207 return song->resample_buffer; | |
| 208 } | |
| 209 | |
| 210 /*********************** vibrato versions ***************************/ | |
| 211 | |
| 212 /* We only need to compute one half of the vibrato sine cycle */ | |
| 213 static int vib_phase_to_inc_ptr(int phase) | |
| 214 { | |
| 215 if (phase < MID_VIBRATO_SAMPLE_INCREMENTS/2) | |
| 216 return MID_VIBRATO_SAMPLE_INCREMENTS/2-1-phase; | |
| 217 else if (phase >= 3*MID_VIBRATO_SAMPLE_INCREMENTS/2) | |
| 218 return 5*MID_VIBRATO_SAMPLE_INCREMENTS/2-1-phase; | |
| 219 else | |
| 220 return phase-MID_VIBRATO_SAMPLE_INCREMENTS/2; | |
| 221 } | |
| 222 | |
| 223 static sint32 update_vibrato(MidSong *song, MidVoice *vp, int sign) | |
| 224 { | |
| 225 sint32 depth; | |
| 226 int phase, pb; | |
| 227 double a; | |
| 228 | |
| 229 if (vp->vibrato_phase++ >= 2*MID_VIBRATO_SAMPLE_INCREMENTS-1) | |
| 230 vp->vibrato_phase=0; | |
| 231 phase=vib_phase_to_inc_ptr(vp->vibrato_phase); | |
| 232 | |
| 233 if (vp->vibrato_sample_increment[phase]) | |
| 234 { | |
| 235 if (sign) | |
| 236 return -vp->vibrato_sample_increment[phase]; | |
| 237 else | |
| 238 return vp->vibrato_sample_increment[phase]; | |
| 239 } | |
| 240 | |
| 241 /* Need to compute this sample increment. */ | |
| 242 | |
| 243 depth=vp->sample->vibrato_depth<<7; | |
| 244 | |
| 245 if (vp->vibrato_sweep) | |
| 246 { | |
| 247 /* Need to update sweep */ | |
| 248 vp->vibrato_sweep_position += vp->vibrato_sweep; | |
| 249 if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT)) | |
| 250 vp->vibrato_sweep=0; | |
| 251 else | |
| 252 { | |
| 253 /* Adjust depth */ | |
| 254 depth *= vp->vibrato_sweep_position; | |
| 255 depth >>= SWEEP_SHIFT; | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 a = FSCALE(((double)(vp->sample->sample_rate) * | |
| 260 (double)(vp->frequency)) / | |
| 261 ((double)(vp->sample->root_freq) * | |
| 262 (double)(song->rate)), | |
| 263 FRACTION_BITS); | |
| 264 | |
| 265 pb=(int)((sine(vp->vibrato_phase * | |
| 266 (SINE_CYCLE_LENGTH/(2*MID_VIBRATO_SAMPLE_INCREMENTS))) | |
| 267 * (double)(depth) * VIBRATO_AMPLITUDE_TUNING)); | |
| 268 | |
| 269 if (pb<0) | |
| 270 { | |
| 271 pb=-pb; | |
| 272 a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13]; | |
| 273 } | |
| 274 else | |
| 275 a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13]; | |
| 276 | |
| 277 /* If the sweep's over, we can store the newly computed sample_increment */ | |
| 278 if (!vp->vibrato_sweep) | |
| 279 vp->vibrato_sample_increment[phase]=(sint32) a; | |
| 280 | |
| 281 if (sign) | |
| 282 a = -a; /* need to preserve the loop direction */ | |
| 283 | |
| 284 return (sint32) a; | |
| 285 } | |
| 286 | |
| 287 static sample_t *rs_vib_plain(MidSong *song, int v, sint32 *countptr) | |
| 288 { | |
| 289 | |
| 290 /* Play sample until end, then free the voice. */ | |
| 291 | |
| 292 sample_t v1, v2; | |
| 293 MidVoice *vp=&(song->voice[v]); | |
| 294 sample_t | |
| 295 *dest=song->resample_buffer, | |
| 296 *src=vp->sample->data; | |
| 297 sint32 | |
| 298 le=vp->sample->data_length, | |
| 299 ofs=vp->sample_offset, | |
| 300 incr=vp->sample_increment, | |
| 301 count=*countptr; | |
| 302 int | |
| 303 cc=vp->vibrato_control_counter; | |
| 304 | |
| 305 /* This has never been tested */ | |
| 306 | |
| 307 if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */ | |
| 308 | |
| 309 while (count--) | |
| 310 { | |
| 311 if (!cc--) | |
| 312 { | |
| 313 cc=vp->vibrato_control_ratio; | |
| 314 incr=update_vibrato(song, vp, 0); | |
| 315 } | |
| 316 v1 = src[ofs >> FRACTION_BITS]; | |
| 317 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 318 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 319 ofs += incr; | |
| 320 if (ofs >= le) | |
| 321 { | |
| 322 if (ofs == le) | |
| 323 *dest++ = src[ofs >> FRACTION_BITS]; | |
| 324 vp->status=VOICE_FREE; | |
| 325 *countptr-=count+1; | |
| 326 break; | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 vp->vibrato_control_counter=cc; | |
| 331 vp->sample_increment=incr; | |
| 332 vp->sample_offset=ofs; /* Update offset */ | |
| 333 return song->resample_buffer; | |
| 334 } | |
| 335 | |
| 336 static sample_t *rs_vib_loop(MidSong *song, MidVoice *vp, sint32 count) | |
| 337 { | |
| 338 | |
| 339 /* Play sample until end-of-loop, skip back and continue. */ | |
| 340 | |
| 341 sample_t v1, v2; | |
| 342 sint32 | |
| 343 ofs=vp->sample_offset, | |
| 344 incr=vp->sample_increment, | |
| 345 le=vp->sample->loop_end, | |
| 346 ll=le - vp->sample->loop_start; | |
| 347 sample_t | |
| 348 *dest=song->resample_buffer, | |
| 349 *src=vp->sample->data; | |
| 350 int | |
| 351 cc=vp->vibrato_control_counter; | |
| 352 sint32 i; | |
| 353 int | |
| 354 vibflag=0; | |
| 355 | |
| 356 while (count) | |
| 357 { | |
| 358 /* Hopefully the loop is longer than an increment */ | |
| 359 if(ofs >= le) | |
| 360 ofs -= ll; | |
| 361 /* Precalc how many times to go through the loop, taking | |
| 362 the vibrato control ratio into account this time. */ | |
| 363 i = (le - ofs) / incr + 1; | |
| 364 if(i > count) i = count; | |
| 365 if(i > cc) | |
| 366 { | |
| 367 i = cc; | |
| 368 vibflag = 1; | |
| 369 } | |
| 370 else cc -= i; | |
| 371 count -= i; | |
| 372 while(i--) | |
| 373 { | |
| 374 v1 = src[ofs >> FRACTION_BITS]; | |
| 375 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 376 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 377 ofs += incr; | |
| 378 } | |
| 379 if(vibflag) | |
| 380 { | |
| 381 cc = vp->vibrato_control_ratio; | |
| 382 incr = update_vibrato(song, vp, 0); | |
| 383 vibflag = 0; | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 vp->vibrato_control_counter=cc; | |
| 388 vp->sample_increment=incr; | |
| 389 vp->sample_offset=ofs; /* Update offset */ | |
| 390 return song->resample_buffer; | |
| 391 } | |
| 392 | |
| 393 static sample_t *rs_vib_bidir(MidSong *song, MidVoice *vp, sint32 count) | |
| 394 { | |
| 395 sample_t v1, v2; | |
| 396 sint32 | |
| 397 ofs=vp->sample_offset, | |
| 398 incr=vp->sample_increment, | |
| 399 le=vp->sample->loop_end, | |
| 400 ls=vp->sample->loop_start; | |
| 401 sample_t | |
| 402 *dest=song->resample_buffer, | |
| 403 *src=vp->sample->data; | |
| 404 int | |
| 405 cc=vp->vibrato_control_counter; | |
| 406 sint32 | |
| 407 le2=le<<1, | |
| 408 ls2=ls<<1, | |
| 409 i; | |
| 410 int | |
| 411 vibflag = 0; | |
| 412 | |
| 413 /* Play normally until inside the loop region */ | |
| 414 while (count && (ofs <= ls)) | |
| 415 { | |
| 416 i = (ls - ofs) / incr + 1; | |
| 417 if (i > count) i = count; | |
| 418 if (i > cc) | |
| 419 { | |
| 420 i = cc; | |
| 421 vibflag = 1; | |
| 422 } | |
| 423 else cc -= i; | |
| 424 count -= i; | |
| 425 while (i--) | |
| 426 { | |
| 427 v1 = src[ofs >> FRACTION_BITS]; | |
| 428 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 429 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 430 ofs += incr; | |
| 431 } | |
| 432 if (vibflag) | |
| 433 { | |
| 434 cc = vp->vibrato_control_ratio; | |
| 435 incr = update_vibrato(song, vp, 0); | |
| 436 vibflag = 0; | |
| 437 } | |
| 438 } | |
| 439 | |
| 440 /* Then do the bidirectional looping */ | |
| 441 | |
| 442 while (count) | |
| 443 { | |
| 444 /* Precalc how many times we should go through the loop */ | |
| 445 i = ((incr > 0 ? le : ls) - ofs) / incr + 1; | |
| 446 if(i > count) i = count; | |
| 447 if(i > cc) | |
| 448 { | |
| 449 i = cc; | |
| 450 vibflag = 1; | |
| 451 } | |
| 452 else cc -= i; | |
| 453 count -= i; | |
| 454 while (i--) | |
| 455 { | |
| 456 v1 = src[ofs >> FRACTION_BITS]; | |
| 457 v2 = src[(ofs >> FRACTION_BITS)+1]; | |
| 458 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 459 ofs += incr; | |
| 460 } | |
| 461 if (vibflag) | |
| 462 { | |
| 463 cc = vp->vibrato_control_ratio; | |
| 464 incr = update_vibrato(song, vp, (incr < 0)); | |
| 465 vibflag = 0; | |
| 466 } | |
| 467 if (ofs >= le) | |
| 468 { | |
| 469 /* fold the overshoot back in */ | |
| 470 ofs = le2 - ofs; | |
| 471 incr *= -1; | |
| 472 } | |
| 473 else if (ofs <= ls) | |
| 474 { | |
| 475 ofs = ls2 - ofs; | |
| 476 incr *= -1; | |
| 477 } | |
| 478 } | |
| 479 | |
| 480 vp->vibrato_control_counter=cc; | |
| 481 vp->sample_increment=incr; | |
| 482 vp->sample_offset=ofs; /* Update offset */ | |
| 483 return song->resample_buffer; | |
| 484 } | |
| 485 | |
| 486 sample_t *resample_voice(MidSong *song, int v, sint32 *countptr) | |
| 487 { | |
| 488 sint32 ofs; | |
| 489 uint8 modes; | |
| 490 MidVoice *vp=&(song->voice[v]); | |
| 491 | |
| 492 if (!(vp->sample->sample_rate)) | |
| 493 { | |
| 494 /* Pre-resampled data -- just update the offset and check if | |
| 495 we're out of data. */ | |
| 496 ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use | |
| 497 FRACTION_BITS here... */ | |
| 498 if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs) | |
| 499 { | |
| 500 /* Note finished. Free the voice. */ | |
| 501 vp->status = VOICE_FREE; | |
| 502 | |
| 503 /* Let the caller know how much data we had left */ | |
| 504 *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs; | |
| 505 } | |
| 506 else | |
| 507 vp->sample_offset += *countptr << FRACTION_BITS; | |
| 508 | |
| 509 return vp->sample->data+ofs; | |
| 510 } | |
| 511 | |
| 512 /* Need to resample. Use the proper function. */ | |
| 513 modes=vp->sample->modes; | |
| 514 | |
| 515 if (vp->vibrato_control_ratio) | |
| 516 { | |
| 517 if ((modes & MODES_LOOPING) && | |
| 518 ((modes & MODES_ENVELOPE) || | |
| 519 (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED))) | |
| 520 { | |
| 521 if (modes & MODES_PINGPONG) | |
| 522 return rs_vib_bidir(song, vp, *countptr); | |
| 523 else | |
| 524 return rs_vib_loop(song, vp, *countptr); | |
| 525 } | |
| 526 else | |
| 527 return rs_vib_plain(song, v, countptr); | |
| 528 } | |
| 529 else | |
| 530 { | |
| 531 if ((modes & MODES_LOOPING) && | |
| 532 ((modes & MODES_ENVELOPE) || | |
| 533 (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED))) | |
| 534 { | |
| 535 if (modes & MODES_PINGPONG) | |
| 536 return rs_bidir(song, vp, *countptr); | |
| 537 else | |
| 538 return rs_loop(song, vp, *countptr); | |
| 539 } | |
| 540 else | |
| 541 return rs_plain(song, v, countptr); | |
| 542 } | |
| 543 } | |
| 544 | |
| 545 void pre_resample(MidSong *song, MidSample *sp) | |
| 546 { | |
| 547 double a, xdiff; | |
| 548 sint32 incr, ofs, newlen, count; | |
| 549 sint16 *newdata, *dest, *src = (sint16 *) sp->data; | |
| 550 sint16 v1, v2, v3, v4, *vptr; | |
| 551 #ifdef DEBUG_CHATTER | |
| 552 static const char note_name[12][3] = | |
| 553 { | |
| 554 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" | |
| 555 }; | |
| 556 #endif | |
| 557 | |
| 558 DEBUG_MSG(" * pre-resampling for note %d (%s%d)\n", | |
| 559 sp->note_to_use, | |
| 560 note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12); | |
| 561 | |
| 562 a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) / | |
| 563 ((double) (sp->root_freq) * song->rate); | |
| 564 newlen = (sint32)(sp->data_length / a); | |
| 565 dest = newdata = safe_malloc(newlen >> (FRACTION_BITS - 1)); | |
| 566 | |
| 567 count = (newlen >> FRACTION_BITS) - 1; | |
| 568 ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count; | |
| 569 | |
| 570 if (--count) | |
| 571 *dest++ = src[0]; | |
| 572 | |
| 573 /* Since we're pre-processing and this doesn't have to be done in | |
| 574 real-time, we go ahead and do the full sliding cubic interpolation. */ | |
| 575 while (--count) | |
| 576 { | |
| 577 vptr = src + (ofs >> FRACTION_BITS); | |
| 578 /* | |
| 579 * Electric Fence to the rescue: Accessing *(vptr - 1) is not a | |
| 580 * good thing to do when vptr <= src. (TiMidity++ has a similar | |
| 581 * safe-guard here.) | |
| 582 */ | |
| 583 v1 = (vptr > src) ? *(vptr - 1) : 0; | |
| 584 v2 = *vptr; | |
| 585 v3 = *(vptr + 1); | |
| 586 v4 = *(vptr + 2); | |
| 587 xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS); | |
| 588 *dest++ = (sint16)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 + | |
| 589 xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4)))); | |
| 590 ofs += incr; | |
| 591 } | |
| 592 | |
| 593 if (ofs & FRACTION_MASK) | |
| 594 { | |
| 595 v1 = src[ofs >> FRACTION_BITS]; | |
| 596 v2 = src[(ofs >> FRACTION_BITS) + 1]; | |
| 597 *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS); | |
| 598 } | |
| 599 else | |
| 600 *dest++ = src[ofs >> FRACTION_BITS]; | |
| 601 | |
| 602 sp->data_length = newlen; | |
| 603 sp->loop_start = (sint32)(sp->loop_start / a); | |
| 604 sp->loop_end = (sint32)(sp->loop_end / a); | |
| 605 free(sp->data); | |
| 606 sp->data = (sample_t *) newdata; | |
| 607 sp->sample_rate = 0; | |
| 608 } |
