mix.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. TiMidity -- Experimental MIDI to WAVE converter
  3. Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  4. Suddenly, you realize that this program is free software; you get
  5. an overwhelming urge to redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 2 of the License, or (at your
  8. option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received another copy of the GNU General Public
  14. License along with this program; if not, write to the Free
  15. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. I bet they'll be amazed.
  17. mix.c */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "config.h"
  22. #include "common.h"
  23. #include "instrum.h"
  24. #include "playmidi.h"
  25. #include "output.h"
  26. #include "controls.h"
  27. #include "tables.h"
  28. #include "resample.h"
  29. #include "mix.h"
  30. /* Returns 1 if envelope runs out */
  31. int recompute_envelope(int v)
  32. {
  33. int stage;
  34. stage = voice[v].envelope_stage;
  35. if (stage>5)
  36. {
  37. /* Envelope ran out. */
  38. int tmp=(voice[v].status == VOICE_DIE); /* Already displayed as dead */
  39. voice[v].status = VOICE_FREE;
  40. if(!tmp)
  41. ctl->note(v);
  42. return 1;
  43. }
  44. if (voice[v].sample->modes & MODES_ENVELOPE)
  45. {
  46. if (voice[v].status==VOICE_ON || voice[v].status==VOICE_SUSTAINED)
  47. {
  48. if (stage>2)
  49. {
  50. /* Freeze envelope until note turns off. Trumpets want this. */
  51. voice[v].envelope_increment=0;
  52. return 0;
  53. }
  54. }
  55. }
  56. voice[v].envelope_stage=stage+1;
  57. if (voice[v].envelope_volume==voice[v].sample->envelope_offset[stage])
  58. return recompute_envelope(v);
  59. voice[v].envelope_target=voice[v].sample->envelope_offset[stage];
  60. voice[v].envelope_increment = voice[v].sample->envelope_rate[stage];
  61. if (voice[v].envelope_target<voice[v].envelope_volume)
  62. voice[v].envelope_increment = -voice[v].envelope_increment;
  63. return 0;
  64. }
  65. void apply_envelope_to_amp(int v)
  66. {
  67. float lamp=voice[v].left_amp, ramp;
  68. int32_t la,ra;
  69. if (voice[v].panned == PANNED_MYSTERY)
  70. {
  71. ramp=voice[v].right_amp;
  72. if (voice[v].tremolo_phase_increment)
  73. {
  74. lamp *= voice[v].tremolo_volume;
  75. ramp *= voice[v].tremolo_volume;
  76. }
  77. if (voice[v].sample->modes & MODES_ENVELOPE)
  78. {
  79. lamp *= (float)vol_table[voice[v].envelope_volume>>23];
  80. ramp *= (float)vol_table[voice[v].envelope_volume>>23];
  81. }
  82. la = (int32_t)FSCALE(lamp,AMP_BITS);
  83. if (la>MAX_AMP_VALUE)
  84. la=MAX_AMP_VALUE;
  85. ra = (int32_t)FSCALE(ramp,AMP_BITS);
  86. if (ra>MAX_AMP_VALUE)
  87. ra=MAX_AMP_VALUE;
  88. voice[v].left_mix=FINAL_VOLUME(la);
  89. voice[v].right_mix=FINAL_VOLUME(ra);
  90. }
  91. else
  92. {
  93. if (voice[v].tremolo_phase_increment)
  94. lamp *= voice[v].tremolo_volume;
  95. if (voice[v].sample->modes & MODES_ENVELOPE)
  96. lamp *= (float)vol_table[voice[v].envelope_volume>>23];
  97. la = (int32_t)FSCALE(lamp,AMP_BITS);
  98. if (la>MAX_AMP_VALUE)
  99. la=MAX_AMP_VALUE;
  100. voice[v].left_mix=FINAL_VOLUME(la);
  101. }
  102. }
  103. static int update_envelope(int v)
  104. {
  105. voice[v].envelope_volume += voice[v].envelope_increment;
  106. /* Why is there no ^^ operator?? */
  107. if (((voice[v].envelope_increment < 0) &&
  108. (voice[v].envelope_volume <= voice[v].envelope_target)) ||
  109. ((voice[v].envelope_increment > 0) &&
  110. (voice[v].envelope_volume >= voice[v].envelope_target)))
  111. {
  112. voice[v].envelope_volume = voice[v].envelope_target;
  113. if (recompute_envelope(v))
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. static void update_tremolo(int v)
  119. {
  120. int32_t depth=voice[v].sample->tremolo_depth<<7;
  121. if (voice[v].tremolo_sweep)
  122. {
  123. /* Update sweep position */
  124. voice[v].tremolo_sweep_position += voice[v].tremolo_sweep;
  125. if (voice[v].tremolo_sweep_position>=(1<<SWEEP_SHIFT))
  126. voice[v].tremolo_sweep=0; /* Swept to max amplitude */
  127. else
  128. {
  129. /* Need to adjust depth */
  130. depth *= voice[v].tremolo_sweep_position;
  131. depth >>= SWEEP_SHIFT;
  132. }
  133. }
  134. voice[v].tremolo_phase += voice[v].tremolo_phase_increment;
  135. /* if (voice[v].tremolo_phase >= (SINE_CYCLE_LENGTH<<RATE_SHIFT))
  136. voice[v].tremolo_phase -= SINE_CYCLE_LENGTH<<RATE_SHIFT; */
  137. voice[v].tremolo_volume = (float)
  138. (1.0 - FSCALENEG((sine(voice[v].tremolo_phase >> RATE_SHIFT) + 1.0)
  139. * depth * TREMOLO_AMPLITUDE_TUNING,
  140. 17));
  141. /* I'm not sure about the +1.0 there -- it makes tremoloed voices'
  142. volumes on average the lower the higher the tremolo amplitude. */
  143. }
  144. /* Returns 1 if the note died */
  145. static int update_signal(int v)
  146. {
  147. if (voice[v].envelope_increment && update_envelope(v))
  148. return 1;
  149. if (voice[v].tremolo_phase_increment)
  150. update_tremolo(v);
  151. apply_envelope_to_amp(v);
  152. return 0;
  153. }
  154. #ifdef LOOKUP_HACK
  155. # define MIXATION(a) *lp++ += mixup[(a<<8) | (uint8)s];
  156. #else
  157. # define MIXATION(a) *lp++ += (a)*s;
  158. #endif
  159. static void mix_mystery_signal(sample_t *sp, int32_t *lp, int v, int count)
  160. {
  161. Voice *vp = voice + v;
  162. final_volume_t
  163. left=vp->left_mix,
  164. right=vp->right_mix;
  165. int cc;
  166. sample_t s;
  167. if (!(cc = vp->control_counter))
  168. {
  169. cc = control_ratio;
  170. if (update_signal(v))
  171. return; /* Envelope ran out */
  172. left = vp->left_mix;
  173. right = vp->right_mix;
  174. }
  175. while (count)
  176. if (cc < count)
  177. {
  178. count -= cc;
  179. while (cc--)
  180. {
  181. s = *sp++;
  182. MIXATION(left);
  183. MIXATION(right);
  184. }
  185. cc = control_ratio;
  186. if (update_signal(v))
  187. return; /* Envelope ran out */
  188. left = vp->left_mix;
  189. right = vp->right_mix;
  190. }
  191. else
  192. {
  193. vp->control_counter = cc - count;
  194. while (count--)
  195. {
  196. s = *sp++;
  197. MIXATION(left);
  198. MIXATION(right);
  199. }
  200. return;
  201. }
  202. }
  203. static void mix_center_signal(sample_t *sp, int32_t *lp, int v, int count)
  204. {
  205. Voice *vp = voice + v;
  206. final_volume_t
  207. left=vp->left_mix;
  208. int cc;
  209. sample_t s;
  210. if (!(cc = vp->control_counter))
  211. {
  212. cc = control_ratio;
  213. if (update_signal(v))
  214. return; /* Envelope ran out */
  215. left = vp->left_mix;
  216. }
  217. while (count)
  218. if (cc < count)
  219. {
  220. count -= cc;
  221. while (cc--)
  222. {
  223. s = *sp++;
  224. MIXATION(left);
  225. MIXATION(left);
  226. }
  227. cc = control_ratio;
  228. if (update_signal(v))
  229. return; /* Envelope ran out */
  230. left = vp->left_mix;
  231. }
  232. else
  233. {
  234. vp->control_counter = cc - count;
  235. while (count--)
  236. {
  237. s = *sp++;
  238. MIXATION(left);
  239. MIXATION(left);
  240. }
  241. return;
  242. }
  243. }
  244. static void mix_single_signal(sample_t *sp, int32_t *lp, int v, int count)
  245. {
  246. Voice *vp = voice + v;
  247. final_volume_t
  248. left=vp->left_mix;
  249. int cc;
  250. sample_t s;
  251. if (!(cc = vp->control_counter))
  252. {
  253. cc = control_ratio;
  254. if (update_signal(v))
  255. return; /* Envelope ran out */
  256. left = vp->left_mix;
  257. }
  258. while (count)
  259. if (cc < count)
  260. {
  261. count -= cc;
  262. while (cc--)
  263. {
  264. s = *sp++;
  265. MIXATION(left);
  266. lp++;
  267. }
  268. cc = control_ratio;
  269. if (update_signal(v))
  270. return; /* Envelope ran out */
  271. left = vp->left_mix;
  272. }
  273. else
  274. {
  275. vp->control_counter = cc - count;
  276. while (count--)
  277. {
  278. s = *sp++;
  279. MIXATION(left);
  280. lp++;
  281. }
  282. return;
  283. }
  284. }
  285. static void mix_mono_signal(sample_t *sp, int32_t *lp, int v, int count)
  286. {
  287. Voice *vp = voice + v;
  288. final_volume_t
  289. left=vp->left_mix;
  290. int cc;
  291. sample_t s;
  292. if (!(cc = vp->control_counter))
  293. {
  294. cc = control_ratio;
  295. if (update_signal(v))
  296. return; /* Envelope ran out */
  297. left = vp->left_mix;
  298. }
  299. while (count)
  300. if (cc < count)
  301. {
  302. count -= cc;
  303. while (cc--)
  304. {
  305. s = *sp++;
  306. MIXATION(left);
  307. }
  308. cc = control_ratio;
  309. if (update_signal(v))
  310. return; /* Envelope ran out */
  311. left = vp->left_mix;
  312. }
  313. else
  314. {
  315. vp->control_counter = cc - count;
  316. while (count--)
  317. {
  318. s = *sp++;
  319. MIXATION(left);
  320. }
  321. return;
  322. }
  323. }
  324. static void mix_mystery(sample_t *sp, int32_t *lp, int v, int count)
  325. {
  326. final_volume_t
  327. left=voice[v].left_mix,
  328. right=voice[v].right_mix;
  329. sample_t s;
  330. while (count--)
  331. {
  332. s = *sp++;
  333. MIXATION(left);
  334. MIXATION(right);
  335. }
  336. }
  337. static void mix_center(sample_t *sp, int32_t *lp, int v, int count)
  338. {
  339. final_volume_t
  340. left=voice[v].left_mix;
  341. sample_t s;
  342. while (count--)
  343. {
  344. s = *sp++;
  345. MIXATION(left);
  346. MIXATION(left);
  347. }
  348. }
  349. static void mix_single(sample_t *sp, int32_t *lp, int v, int count)
  350. {
  351. final_volume_t
  352. left=voice[v].left_mix;
  353. sample_t s;
  354. while (count--)
  355. {
  356. s = *sp++;
  357. MIXATION(left);
  358. lp++;
  359. }
  360. }
  361. static void mix_mono(sample_t *sp, int32_t *lp, int v, int count)
  362. {
  363. final_volume_t
  364. left=voice[v].left_mix;
  365. sample_t s;
  366. while (count--)
  367. {
  368. s = *sp++;
  369. MIXATION(left);
  370. }
  371. }
  372. /* Ramp a note out in c samples */
  373. static void ramp_out(sample_t *sp, int32_t *lp, int v, int32_t c)
  374. {
  375. /* should be final_volume_t, but uint8_t gives trouble. */
  376. int32_t left, right, li, ri;
  377. sample_t s=0; /* silly warning about uninitialized s */
  378. /* Fix by James Caldwell */
  379. if ( c == 0 ) c = 1;
  380. left=voice[v].left_mix;
  381. li=-(left/c);
  382. if (!li) li=-1;
  383. /* I_Printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li); */
  384. if (!(play_mode->encoding & PE_MONO))
  385. {
  386. if (voice[v].panned==PANNED_MYSTERY)
  387. {
  388. right=voice[v].right_mix;
  389. ri=-(right/c);
  390. while (c--)
  391. {
  392. left += li;
  393. if (left<0)
  394. left=0;
  395. right += ri;
  396. if (right<0)
  397. right=0;
  398. s=*sp++;
  399. MIXATION(left);
  400. MIXATION(right);
  401. }
  402. }
  403. else if (voice[v].panned==PANNED_CENTER)
  404. {
  405. while (c--)
  406. {
  407. left += li;
  408. if (left<0)
  409. return;
  410. s=*sp++;
  411. MIXATION(left);
  412. MIXATION(left);
  413. }
  414. }
  415. else if (voice[v].panned==PANNED_LEFT)
  416. {
  417. while (c--)
  418. {
  419. left += li;
  420. if (left<0)
  421. return;
  422. s=*sp++;
  423. MIXATION(left);
  424. lp++;
  425. }
  426. }
  427. else if (voice[v].panned==PANNED_RIGHT)
  428. {
  429. while (c--)
  430. {
  431. left += li;
  432. if (left<0)
  433. return;
  434. s=*sp++;
  435. lp++;
  436. MIXATION(left);
  437. }
  438. }
  439. }
  440. else
  441. {
  442. /* Mono output. */
  443. while (c--)
  444. {
  445. left += li;
  446. if (left<0)
  447. return;
  448. s=*sp++;
  449. MIXATION(left);
  450. }
  451. }
  452. }
  453. /**************** interface function ******************/
  454. void mix_voice( int32_t *buf, int v, int32_t c)
  455. {
  456. Voice *vp=voice+v;
  457. sample_t *sp;
  458. if (vp->status==VOICE_DIE)
  459. {
  460. if (c>=MAX_DIE_TIME)
  461. c=MAX_DIE_TIME;
  462. sp=resample_voice(v, &c);
  463. ramp_out(sp, buf, v, c);
  464. vp->status=VOICE_FREE;
  465. }
  466. else
  467. {
  468. sp=resample_voice(v, &c);
  469. if (play_mode->encoding & PE_MONO)
  470. {
  471. /* Mono output. */
  472. if (vp->envelope_increment || vp->tremolo_phase_increment)
  473. mix_mono_signal(sp, buf, v, c);
  474. else
  475. mix_mono(sp, buf, v, c);
  476. }
  477. else
  478. {
  479. if (vp->panned == PANNED_MYSTERY)
  480. {
  481. if (vp->envelope_increment || vp->tremolo_phase_increment)
  482. mix_mystery_signal(sp, buf, v, c);
  483. else
  484. mix_mystery(sp, buf, v, c);
  485. }
  486. else if (vp->panned == PANNED_CENTER)
  487. {
  488. if (vp->envelope_increment || vp->tremolo_phase_increment)
  489. mix_center_signal(sp, buf, v, c);
  490. else
  491. mix_center(sp, buf, v, c);
  492. }
  493. else
  494. {
  495. /* It's either full left or full right. In either case,
  496. every other sample is 0. Just get the offset right: */
  497. if (vp->panned == PANNED_RIGHT) buf++;
  498. if (vp->envelope_increment || vp->tremolo_phase_increment)
  499. mix_single_signal(sp, buf, v, c);
  500. else
  501. mix_single(sp, buf, v, c);
  502. }
  503. }
  504. }
  505. }