/project/jni/sdl_mixer/timidity/resample.c

https://github.com/aichunyu/FFPlayer · C · 748 lines · 596 code · 85 blank · 67 comment · 107 complexity · c8344a064085081f163f5ea9ad3ea092 MD5 · raw file

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