resample.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 "controls.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++ = 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++ = 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 sample_t *resample_buffer;
  50. void Real_Tim_Free( void *pt );
  51. /*************** resampling with fixed increment *****************/
  52. static sample_t *rs_plain(int v, int32_t *countptr)
  53. {
  54. /* Play sample until end, then free the voice. */
  55. INTERPVARS;
  56. Voice
  57. *vp=&voice[v];
  58. sample_t
  59. *dest=resample_buffer,
  60. *src=vp->sample->data;
  61. int32_t
  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_t i;
  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. while (i--)
  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 sample_t *rs_loop(Voice *vp, int32_t count)
  109. {
  110. /* Play sample until end-of-loop, skip back and continue. */
  111. INTERPVARS;
  112. int32_t
  113. ofs=vp->sample_offset,
  114. incr=vp->sample_increment,
  115. le=vp->sample->loop_end,
  116. ll=le - vp->sample->loop_start;
  117. sample_t
  118. *dest=resample_buffer,
  119. *src=vp->sample->data;
  120. #ifdef PRECALC_LOOPS
  121. int32_t i;
  122. while (count)
  123. {
  124. if (ofs >= le)
  125. /* NOTE: Assumes that ll > incr and that incr > 0. */
  126. ofs -= ll;
  127. /* Precalc how many times we should go through the loop */
  128. i = (le - ofs) / incr + 1;
  129. if (i > count)
  130. {
  131. i = count;
  132. count = 0;
  133. }
  134. else count -= i;
  135. while (i--)
  136. {
  137. RESAMPLATION;
  138. ofs += incr;
  139. }
  140. }
  141. #else
  142. while (count--)
  143. {
  144. RESAMPLATION;
  145. ofs += incr;
  146. if (ofs>=le)
  147. ofs -= ll; /* Hopefully the loop is longer than an increment. */
  148. }
  149. #endif
  150. vp->sample_offset=ofs; /* Update offset */
  151. return resample_buffer;
  152. }
  153. static sample_t *rs_bidir(Voice *vp, int32_t count)
  154. {
  155. INTERPVARS;
  156. int32_t
  157. ofs=vp->sample_offset,
  158. incr=vp->sample_increment,
  159. le=vp->sample->loop_end,
  160. ls=vp->sample->loop_start;
  161. sample_t
  162. *dest=resample_buffer,
  163. *src=vp->sample->data;
  164. #ifdef PRECALC_LOOPS
  165. int32_t
  166. le2 = le<<1,
  167. ls2 = ls<<1,
  168. i;
  169. /* Play normally until inside the loop region */
  170. if (ofs <= ls)
  171. {
  172. /* NOTE: Assumes that incr > 0, which is NOT always the case
  173. when doing bidirectional looping. I have yet to see a case
  174. where both ofs <= ls AND incr < 0, however. */
  175. i = (ls - ofs) / incr + 1;
  176. if (i > count)
  177. {
  178. i = count;
  179. count = 0;
  180. }
  181. else count -= i;
  182. while (i--)
  183. {
  184. RESAMPLATION;
  185. ofs += incr;
  186. }
  187. }
  188. /* Then do the bidirectional looping */
  189. while(count)
  190. {
  191. /* Precalc how many times we should go through the loop */
  192. i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
  193. if (i > count)
  194. {
  195. i = count;
  196. count = 0;
  197. }
  198. else count -= i;
  199. while (i--)
  200. {
  201. RESAMPLATION;
  202. ofs += incr;
  203. }
  204. if (ofs>=le)
  205. {
  206. /* fold the overshoot back in */
  207. ofs = le2 - ofs;
  208. incr *= -1;
  209. }
  210. else if (ofs <= ls)
  211. {
  212. ofs = ls2 - ofs;
  213. incr *= -1;
  214. }
  215. }
  216. #else /* PRECALC_LOOPS */
  217. /* Play normally until inside the loop region */
  218. if (ofs < ls)
  219. {
  220. while (count--)
  221. {
  222. RESAMPLATION;
  223. ofs += incr;
  224. if (ofs>=ls)
  225. break;
  226. }
  227. }
  228. /* Then do the bidirectional looping */
  229. if (count>0)
  230. while (count--)
  231. {
  232. RESAMPLATION;
  233. ofs += incr;
  234. if (ofs>=le)
  235. {
  236. /* fold the overshoot back in */
  237. ofs = le - (ofs - le);
  238. incr = -incr;
  239. }
  240. else if (ofs <= ls)
  241. {
  242. ofs = ls + (ls - ofs);
  243. incr = -incr;
  244. }
  245. }
  246. #endif /* PRECALC_LOOPS */
  247. vp->sample_increment=incr;
  248. vp->sample_offset=ofs; /* Update offset */
  249. return resample_buffer;
  250. }
  251. /*********************** vibrato versions ***************************/
  252. /* We only need to compute one half of the vibrato sine cycle */
  253. static int vib_phase_to_inc_ptr(int phase)
  254. {
  255. if (phase < VIBRATO_SAMPLE_INCREMENTS/2)
  256. return VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
  257. else if (phase >= 3*VIBRATO_SAMPLE_INCREMENTS/2)
  258. return 5*VIBRATO_SAMPLE_INCREMENTS/2-1-phase;
  259. else
  260. return phase-VIBRATO_SAMPLE_INCREMENTS/2;
  261. }
  262. static int32_t update_vibrato(Voice *vp, int sign)
  263. {
  264. int32_t depth;
  265. int phase, pb;
  266. double a;
  267. if (vp->vibrato_phase++ >= 2*VIBRATO_SAMPLE_INCREMENTS-1)
  268. vp->vibrato_phase=0;
  269. phase=vib_phase_to_inc_ptr(vp->vibrato_phase);
  270. if (vp->vibrato_sample_increment[phase])
  271. {
  272. if (sign)
  273. return -vp->vibrato_sample_increment[phase];
  274. else
  275. return vp->vibrato_sample_increment[phase];
  276. }
  277. /* Need to compute this sample increment. */
  278. depth=vp->sample->vibrato_depth<<7;
  279. if (vp->vibrato_sweep)
  280. {
  281. /* Need to update sweep */
  282. vp->vibrato_sweep_position += vp->vibrato_sweep;
  283. if (vp->vibrato_sweep_position >= (1<<SWEEP_SHIFT))
  284. vp->vibrato_sweep=0;
  285. else
  286. {
  287. /* Adjust depth */
  288. depth *= vp->vibrato_sweep_position;
  289. depth >>= SWEEP_SHIFT;
  290. }
  291. }
  292. a = FSCALE(((double)(vp->sample->sample_rate) *
  293. (double)(vp->frequency)) /
  294. ((double)(vp->sample->root_freq) *
  295. (double)(play_mode->rate)),
  296. FRACTION_BITS);
  297. pb=(int)((sine(vp->vibrato_phase *
  298. (SINE_CYCLE_LENGTH/(2*VIBRATO_SAMPLE_INCREMENTS)))
  299. * (double)(depth) * VIBRATO_AMPLITUDE_TUNING));
  300. if (pb<0)
  301. {
  302. pb=-pb;
  303. a /= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
  304. }
  305. else
  306. a *= bend_fine[(pb>>5) & 0xFF] * bend_coarse[pb>>13];
  307. /* If the sweep's over, we can store the newly computed sample_increment */
  308. if (!vp->vibrato_sweep)
  309. vp->vibrato_sample_increment[phase]=(int32_t) a;
  310. if (sign)
  311. a = -a; /* need to preserve the loop direction */
  312. return (int32_t) a;
  313. }
  314. static sample_t *rs_vib_plain(int v, int32_t *countptr)
  315. {
  316. /* Play sample until end, then free the voice. */
  317. INTERPVARS;
  318. Voice *vp=&voice[v];
  319. sample_t
  320. *dest=resample_buffer,
  321. *src=vp->sample->data;
  322. int32_t
  323. le=vp->sample->data_length,
  324. ofs=vp->sample_offset,
  325. incr=vp->sample_increment,
  326. count=*countptr;
  327. int
  328. cc=vp->vibrato_control_counter;
  329. /* This has never been tested */
  330. if (incr<0) incr = -incr; /* In case we're coming out of a bidir loop */
  331. while (count--)
  332. {
  333. if (!cc--)
  334. {
  335. cc=vp->vibrato_control_ratio;
  336. incr=update_vibrato(vp, 0);
  337. }
  338. RESAMPLATION;
  339. ofs += incr;
  340. if (ofs >= le)
  341. {
  342. FINALINTERP;
  343. vp->status=VOICE_FREE;
  344. ctl->note(v);
  345. *countptr-=count+1;
  346. break;
  347. }
  348. }
  349. vp->vibrato_control_counter=cc;
  350. vp->sample_increment=incr;
  351. vp->sample_offset=ofs; /* Update offset */
  352. return resample_buffer;
  353. }
  354. static sample_t *rs_vib_loop(Voice *vp, int32_t count)
  355. {
  356. /* Play sample until end-of-loop, skip back and continue. */
  357. INTERPVARS;
  358. int32_t
  359. ofs=vp->sample_offset,
  360. incr=vp->sample_increment,
  361. le=vp->sample->loop_end,
  362. ll=le - vp->sample->loop_start;
  363. sample_t
  364. *dest=resample_buffer,
  365. *src=vp->sample->data;
  366. int
  367. cc=vp->vibrato_control_counter;
  368. #ifdef PRECALC_LOOPS
  369. int32_t i;
  370. int
  371. vibflag=0;
  372. while (count)
  373. {
  374. /* Hopefully the loop is longer than an increment */
  375. if(ofs >= le)
  376. ofs -= ll;
  377. /* Precalc how many times to go through the loop, taking
  378. the vibrato control ratio into account this time. */
  379. i = (le - ofs) / incr + 1;
  380. if(i > count) i = count;
  381. if(i > cc)
  382. {
  383. i = cc;
  384. vibflag = 1;
  385. }
  386. else cc -= i;
  387. count -= i;
  388. while(i--)
  389. {
  390. RESAMPLATION;
  391. ofs += incr;
  392. }
  393. if(vibflag)
  394. {
  395. cc = vp->vibrato_control_ratio;
  396. incr = update_vibrato(vp, 0);
  397. vibflag = 0;
  398. }
  399. }
  400. #else /* PRECALC_LOOPS */
  401. while (count--)
  402. {
  403. if (!cc--)
  404. {
  405. cc=vp->vibrato_control_ratio;
  406. incr=update_vibrato(vp, 0);
  407. }
  408. RESAMPLATION;
  409. ofs += incr;
  410. if (ofs>=le)
  411. ofs -= ll; /* Hopefully the loop is longer than an increment. */
  412. }
  413. #endif /* PRECALC_LOOPS */
  414. vp->vibrato_control_counter=cc;
  415. vp->sample_increment=incr;
  416. vp->sample_offset=ofs; /* Update offset */
  417. return resample_buffer;
  418. }
  419. static sample_t *rs_vib_bidir(Voice *vp, int32_t count)
  420. {
  421. INTERPVARS;
  422. int32_t
  423. ofs=vp->sample_offset,
  424. incr=vp->sample_increment,
  425. le=vp->sample->loop_end,
  426. ls=vp->sample->loop_start;
  427. sample_t
  428. *dest=resample_buffer,
  429. *src=vp->sample->data;
  430. int
  431. cc=vp->vibrato_control_counter;
  432. #ifdef PRECALC_LOOPS
  433. int32_t
  434. le2=le<<1,
  435. ls2=ls<<1,
  436. i;
  437. int
  438. vibflag = 0;
  439. /* Play normally until inside the loop region */
  440. while (count && (ofs <= ls))
  441. {
  442. i = (ls - ofs) / incr + 1;
  443. if (i > count) i = count;
  444. if (i > cc)
  445. {
  446. i = cc;
  447. vibflag = 1;
  448. }
  449. else cc -= i;
  450. count -= i;
  451. while (i--)
  452. {
  453. RESAMPLATION;
  454. ofs += incr;
  455. }
  456. if (vibflag)
  457. {
  458. cc = vp->vibrato_control_ratio;
  459. incr = update_vibrato(vp, 0);
  460. vibflag = 0;
  461. }
  462. }
  463. /* Then do the bidirectional looping */
  464. while (count)
  465. {
  466. /* Precalc how many times we should go through the loop */
  467. i = ((incr > 0 ? le : ls) - ofs) / incr + 1;
  468. if(i > count) i = count;
  469. if(i > cc)
  470. {
  471. i = cc;
  472. vibflag = 1;
  473. }
  474. else cc -= i;
  475. count -= i;
  476. while (i--)
  477. {
  478. RESAMPLATION;
  479. ofs += incr;
  480. }
  481. if (vibflag)
  482. {
  483. cc = vp->vibrato_control_ratio;
  484. incr = update_vibrato(vp, (incr < 0));
  485. vibflag = 0;
  486. }
  487. if (ofs >= le)
  488. {
  489. /* fold the overshoot back in */
  490. ofs = le2 - ofs;
  491. incr *= -1;
  492. }
  493. else if (ofs <= ls)
  494. {
  495. ofs = ls2 - ofs;
  496. incr *= -1;
  497. }
  498. }
  499. #else /* PRECALC_LOOPS */
  500. /* Play normally until inside the loop region */
  501. if (ofs < ls)
  502. {
  503. while (count--)
  504. {
  505. if (!cc--)
  506. {
  507. cc=vp->vibrato_control_ratio;
  508. incr=update_vibrato(vp, 0);
  509. }
  510. RESAMPLATION;
  511. ofs += incr;
  512. if (ofs>=ls)
  513. break;
  514. }
  515. }
  516. /* Then do the bidirectional looping */
  517. if (count>0)
  518. while (count--)
  519. {
  520. if (!cc--)
  521. {
  522. cc=vp->vibrato_control_ratio;
  523. incr=update_vibrato(vp, (incr < 0));
  524. }
  525. RESAMPLATION;
  526. ofs += incr;
  527. if (ofs>=le)
  528. {
  529. /* fold the overshoot back in */
  530. ofs = le - (ofs - le);
  531. incr = -incr;
  532. }
  533. else if (ofs <= ls)
  534. {
  535. ofs = ls + (ls - ofs);
  536. incr = -incr;
  537. }
  538. }
  539. #endif /* PRECALC_LOOPS */
  540. vp->vibrato_control_counter=cc;
  541. vp->sample_increment=incr;
  542. vp->sample_offset=ofs; /* Update offset */
  543. return resample_buffer;
  544. }
  545. sample_t *resample_voice(int v, int32_t *countptr)
  546. {
  547. int32_t ofs;
  548. uint8_t modes;
  549. Voice *vp=&voice[v];
  550. if (!(vp->sample->sample_rate))
  551. {
  552. /* Pre-resampled data -- just update the offset and check if
  553. we're out of data. */
  554. ofs=vp->sample_offset >> FRACTION_BITS; /* Kind of silly to use
  555. FRACTION_BITS here... */
  556. if (*countptr >= (vp->sample->data_length>>FRACTION_BITS) - ofs)
  557. {
  558. /* Note finished. Free the voice. */
  559. vp->status = VOICE_FREE;
  560. ctl->note(v);
  561. /* Let the caller know how much data we had left */
  562. *countptr = (vp->sample->data_length>>FRACTION_BITS) - ofs;
  563. }
  564. else
  565. vp->sample_offset += *countptr << FRACTION_BITS;
  566. return vp->sample->data+ofs;
  567. }
  568. /* Need to resample. Use the proper function. */
  569. modes=vp->sample->modes;
  570. if (vp->vibrato_control_ratio)
  571. {
  572. if ((modes & MODES_LOOPING) &&
  573. ((modes & MODES_ENVELOPE) ||
  574. (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
  575. {
  576. if (modes & MODES_PINGPONG)
  577. return rs_vib_bidir(vp, *countptr);
  578. else
  579. return rs_vib_loop(vp, *countptr);
  580. }
  581. else
  582. return rs_vib_plain(v, countptr);
  583. }
  584. else
  585. {
  586. if ((modes & MODES_LOOPING) &&
  587. ((modes & MODES_ENVELOPE) ||
  588. (vp->status==VOICE_ON || vp->status==VOICE_SUSTAINED)))
  589. {
  590. if (modes & MODES_PINGPONG)
  591. return rs_bidir(vp, *countptr);
  592. else
  593. return rs_loop(vp, *countptr);
  594. }
  595. else
  596. return rs_plain(v, countptr);
  597. }
  598. }
  599. void pre_resample(Sample * sp)
  600. {
  601. double a, xdiff;
  602. int32_t incr, ofs, newlen, count;
  603. int16_t *newdata, *dest, *src = (int16_t *) sp->data;
  604. int16_t v1, v2, v3, v4, *vptr;
  605. static const char note_name[12][3] =
  606. {
  607. "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
  608. };
  609. ctl->cmsg(CMSG_INFO, VERB_NOISY, " * pre-resampling for note %d (%s%d)",
  610. sp->note_to_use,
  611. note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12);
  612. a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) /
  613. ((double) (sp->root_freq) * play_mode->rate);
  614. newlen = (int32_t)(sp->data_length / a);
  615. dest = newdata = (int16_t*)safe_malloc(newlen >> (FRACTION_BITS - 1));
  616. count = (newlen >> FRACTION_BITS) - 1;
  617. ofs = incr = (sp->data_length - (1 << FRACTION_BITS)) / count;
  618. if (--count)
  619. *dest++ = src[0];
  620. /* Since we're pre-processing and this doesn't have to be done in
  621. real-time, we go ahead and do the full sliding cubic interpolation. */
  622. while (--count)
  623. {
  624. vptr = src + (ofs >> FRACTION_BITS);
  625. v1 = *(vptr - 1);
  626. v2 = *vptr;
  627. v3 = *(vptr + 1);
  628. v4 = *(vptr + 2);
  629. xdiff = FSCALENEG(ofs & FRACTION_MASK, FRACTION_BITS);
  630. *dest++ = (int16_t)(v2 + (xdiff / 6.0) * (-2 * v1 - 3 * v2 + 6 * v3 - v4 +
  631. xdiff * (3 * (v1 - 2 * v2 + v3) + xdiff * (-v1 + 3 * (v2 - v3) + v4))));
  632. ofs += incr;
  633. }
  634. if (ofs & FRACTION_MASK)
  635. {
  636. v1 = src[ofs >> FRACTION_BITS];
  637. v2 = src[(ofs >> FRACTION_BITS) + 1];
  638. *dest++ = v1 + (((v2 - v1) * (ofs & FRACTION_MASK)) >> FRACTION_BITS);
  639. }
  640. else
  641. *dest++ = src[ofs >> FRACTION_BITS];
  642. sp->data_length = newlen;
  643. sp->loop_start = (int32_t)(sp->loop_start / a);
  644. sp->loop_end = (int32_t)(sp->loop_end / a);
  645. Real_Tim_Free(sp->data);
  646. sp->data = (sample_t *) newdata;
  647. sp->sample_rate = 0;
  648. }