playmidi.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. playmidi.c -- random stuff in need of rearrangement
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "config.h"
  21. #include "common.h"
  22. #include "instrum.h"
  23. #include "playmidi.h"
  24. #include "readmidi.h"
  25. #include "output.h"
  26. #include "mix.h"
  27. #include "controls.h"
  28. #include "timidity.h"
  29. #include "tables.h"
  30. #include "structs.h"
  31. //void Real_Tim_Free( void *pt );
  32. Channel channel[16];
  33. Voice voice[MAX_VOICES];
  34. int
  35. voices=DEFAULT_VOICES;
  36. int32_t
  37. control_ratio=0,
  38. amplification=DEFAULT_AMPLIFICATION;
  39. float
  40. master_volume;
  41. int32_t drumchannels=DEFAULT_DRUMCHANNELS;
  42. int adjust_panning_immediately=0;
  43. static int midi_playing = 0;
  44. static int32_t lost_notes, cut_notes;
  45. static int32_t *buffer_pointer;
  46. static int32_t buffered_count;
  47. extern int32_t *common_buffer;
  48. static MidiEvent *event_list, *current_event;
  49. static int32_t sample_count, current_sample;
  50. static void adjust_amplification(void)
  51. {
  52. master_volume = (float)(amplification) / (float)100.0;
  53. }
  54. static void reset_voices(void)
  55. {
  56. int i;
  57. for (i=0; i<MAX_VOICES; i++)
  58. voice[i].status=VOICE_FREE;
  59. }
  60. /* Process the Reset All Controllers event */
  61. static void reset_controllers(int c)
  62. {
  63. channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */
  64. channel[c].expression=127; /* SCC-1 does this. */
  65. channel[c].sustain=0;
  66. channel[c].pitchbend=0x2000;
  67. channel[c].pitchfactor=0; /* to be computed */
  68. }
  69. static void redraw_controllers(int c)
  70. {
  71. ctl->volume(c, channel[c].volume);
  72. ctl->expression(c, channel[c].expression);
  73. ctl->sustain(c, channel[c].sustain);
  74. ctl->pitch_bend(c, channel[c].pitchbend);
  75. }
  76. static void reset_midi(void)
  77. {
  78. int i;
  79. for (i=0; i<16; i++)
  80. {
  81. reset_controllers(i);
  82. /* The rest of these are unaffected by the Reset All Controllers event */
  83. channel[i].program=default_program;
  84. channel[i].panning=NO_PANNING;
  85. channel[i].pitchsens=2;
  86. channel[i].bank=0; /* tone bank or drum set */
  87. }
  88. reset_voices();
  89. }
  90. static void select_sample(int v, Instrument *ip)
  91. {
  92. int32_t f, cdiff, diff;
  93. int s,i;
  94. Sample *sp, *closest;
  95. s=ip->samples;
  96. sp=ip->sample;
  97. if (s==1)
  98. {
  99. voice[v].sample=sp;
  100. return;
  101. }
  102. f=voice[v].orig_frequency;
  103. for (i=0; i<s; i++)
  104. {
  105. if (sp->low_freq <= f && sp->high_freq >= f)
  106. {
  107. voice[v].sample=sp;
  108. return;
  109. }
  110. sp++;
  111. }
  112. /*
  113. No suitable sample found! We'll select the sample whose root
  114. frequency is closest to the one we want. (Actually we should
  115. probably convert the low, high, and root frequencies to MIDI note
  116. values and compare those.) */
  117. cdiff=0x7FFFFFFF;
  118. closest=sp=ip->sample;
  119. for(i=0; i<s; i++)
  120. {
  121. diff=sp->root_freq - f;
  122. if (diff<0) diff=-diff;
  123. if (diff<cdiff)
  124. {
  125. cdiff=diff;
  126. closest=sp;
  127. }
  128. sp++;
  129. }
  130. voice[v].sample=closest;
  131. return;
  132. }
  133. static void recompute_freq(int v)
  134. {
  135. int
  136. sign=(voice[v].sample_increment < 0), /* for bidirectional loops */
  137. pb=channel[voice[v].channel].pitchbend;
  138. double a;
  139. if (!voice[v].sample->sample_rate)
  140. return;
  141. if (voice[v].vibrato_control_ratio)
  142. {
  143. /* This instrument has vibrato. Invalidate any precomputed
  144. sample_increments. */
  145. int i=VIBRATO_SAMPLE_INCREMENTS;
  146. while (i--)
  147. voice[v].vibrato_sample_increment[i]=0;
  148. }
  149. if (pb==0x2000 || pb<0 || pb>0x3FFF)
  150. voice[v].frequency=voice[v].orig_frequency;
  151. else
  152. {
  153. pb-=0x2000;
  154. if (!(channel[voice[v].channel].pitchfactor))
  155. {
  156. /* Damn. Somebody bent the pitch. */
  157. int32_t i=pb*channel[voice[v].channel].pitchsens;
  158. if (pb<0)
  159. i=-i;
  160. channel[voice[v].channel].pitchfactor=
  161. (float)(bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13]);
  162. }
  163. if (pb>0)
  164. voice[v].frequency=
  165. (int32_t)(channel[voice[v].channel].pitchfactor *
  166. (double)(voice[v].orig_frequency));
  167. else
  168. voice[v].frequency=
  169. (int32_t)((double)(voice[v].orig_frequency) /
  170. channel[voice[v].channel].pitchfactor);
  171. }
  172. a = FSCALE(((double)(voice[v].sample->sample_rate) *
  173. (double)(voice[v].frequency)) /
  174. ((double)(voice[v].sample->root_freq) *
  175. (double)(play_mode->rate)),
  176. FRACTION_BITS);
  177. if (sign)
  178. a = -a; /* need to preserve the loop direction */
  179. voice[v].sample_increment = (int32_t)(a);
  180. }
  181. static void recompute_amp(int v)
  182. {
  183. int32_t tempamp;
  184. /* TODO: use fscale */
  185. tempamp= (voice[v].velocity *
  186. channel[voice[v].channel].volume *
  187. channel[voice[v].channel].expression); /* 21 bits */
  188. if (!(play_mode->encoding & PE_MONO))
  189. {
  190. if (voice[v].panning > 60 && voice[v].panning < 68)
  191. {
  192. voice[v].panned=PANNED_CENTER;
  193. voice[v].left_amp=
  194. FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  195. 21);
  196. }
  197. else if (voice[v].panning<5)
  198. {
  199. voice[v].panned = PANNED_LEFT;
  200. voice[v].left_amp=
  201. FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  202. 20);
  203. }
  204. else if (voice[v].panning>123)
  205. {
  206. voice[v].panned = PANNED_RIGHT;
  207. voice[v].left_amp= /* left_amp will be used */
  208. FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  209. 20);
  210. }
  211. else
  212. {
  213. voice[v].panned = PANNED_MYSTERY;
  214. voice[v].left_amp=
  215. FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  216. 27);
  217. voice[v].right_amp=voice[v].left_amp * (voice[v].panning);
  218. voice[v].left_amp *= (float)(127-voice[v].panning);
  219. }
  220. }
  221. else
  222. {
  223. voice[v].panned=PANNED_CENTER;
  224. voice[v].left_amp=
  225. FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume,
  226. 21);
  227. }
  228. }
  229. static void start_note(MidiEvent *e, int i)
  230. {
  231. Instrument *ip;
  232. int j;
  233. if (ISDRUMCHANNEL(e->channel))
  234. {
  235. if (!(ip=drumset[channel[e->channel].bank]->tone[e->a].instrument))
  236. {
  237. if (!(ip=drumset[0]->tone[e->a].instrument))
  238. return; /* No instrument? Then we can't play. */
  239. }
  240. if (ip->samples != 1)
  241. {
  242. ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
  243. "Strange: percussion instrument with %d samples!", ip->samples);
  244. }
  245. if (ip->sample->note_to_use) /* Do we have a fixed pitch? */
  246. voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  247. else
  248. voice[i].orig_frequency=freq_table[e->a & 0x7F];
  249. /* drums are supposed to have only one sample */
  250. voice[i].sample=ip->sample;
  251. }
  252. else
  253. {
  254. if (channel[e->channel].program==SPECIAL_PROGRAM)
  255. ip=default_instrument;
  256. else if (!(ip=tonebank[channel[e->channel].bank]->
  257. tone[channel[e->channel].program].instrument))
  258. {
  259. if (!(ip=tonebank[0]->tone[channel[e->channel].program].instrument))
  260. return; /* No instrument? Then we can't play. */
  261. }
  262. if (ip->sample->note_to_use) /* Fixed-pitch instrument? */
  263. voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)];
  264. else
  265. voice[i].orig_frequency=freq_table[e->a & 0x7F];
  266. select_sample(i, ip);
  267. }
  268. voice[i].status=VOICE_ON;
  269. voice[i].channel=e->channel;
  270. voice[i].note=e->a;
  271. voice[i].velocity=e->b;
  272. voice[i].sample_offset=0;
  273. voice[i].sample_increment=0; /* make sure it isn't negative */
  274. voice[i].tremolo_phase=0;
  275. voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment;
  276. voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment;
  277. voice[i].tremolo_sweep_position=0;
  278. voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment;
  279. voice[i].vibrato_sweep_position=0;
  280. voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio;
  281. voice[i].vibrato_control_counter=voice[i].vibrato_phase=0;
  282. for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++)
  283. voice[i].vibrato_sample_increment[j]=0;
  284. if (channel[e->channel].panning != NO_PANNING)
  285. voice[i].panning=channel[e->channel].panning;
  286. else
  287. voice[i].panning=voice[i].sample->panning;
  288. recompute_freq(i);
  289. recompute_amp(i);
  290. if (voice[i].sample->modes & MODES_ENVELOPE)
  291. {
  292. /* Ramp up from 0 */
  293. voice[i].envelope_stage=0;
  294. voice[i].envelope_volume=0;
  295. voice[i].control_counter=0;
  296. recompute_envelope(i);
  297. apply_envelope_to_amp(i);
  298. }
  299. else
  300. {
  301. voice[i].envelope_increment=0;
  302. apply_envelope_to_amp(i);
  303. }
  304. ctl->note(i);
  305. }
  306. static void kill_note(int i)
  307. {
  308. voice[i].status=VOICE_DIE;
  309. ctl->note(i);
  310. }
  311. /* Only one instance of a note can be playing on a single channel. */
  312. static void note_on(MidiEvent *e)
  313. {
  314. int i=voices, lowest=-1;
  315. int32_t lv=0x7FFFFFFF, v;
  316. while (i--)
  317. {
  318. if (voice[i].status == VOICE_FREE)
  319. lowest=i; /* Can't get a lower volume than silence */
  320. else if (voice[i].channel==e->channel &&
  321. (voice[i].note==e->a || channel[voice[i].channel].mono))
  322. kill_note(i);
  323. }
  324. if (lowest != -1)
  325. {
  326. /* Found a free voice. */
  327. start_note(e,lowest);
  328. return;
  329. }
  330. /* Look for the decaying note with the lowest volume */
  331. i=voices;
  332. while (i--)
  333. {
  334. if ((voice[i].status!=VOICE_ON) &&
  335. (voice[i].status!=VOICE_DIE))
  336. {
  337. v=voice[i].left_mix;
  338. if ((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v))
  339. v=voice[i].right_mix;
  340. if (v<lv)
  341. {
  342. lv=v;
  343. lowest=i;
  344. }
  345. }
  346. }
  347. if (lowest != -1)
  348. {
  349. /* This can still cause a click, but if we had a free voice to
  350. spare for ramping down this note, we wouldn't need to kill it
  351. in the first place... Still, this needs to be fixed. Perhaps
  352. we could use a reserve of voices to play dying notes only. */
  353. cut_notes++;
  354. voice[lowest].status=VOICE_FREE;
  355. ctl->note(lowest);
  356. start_note(e,lowest);
  357. }
  358. else
  359. lost_notes++;
  360. }
  361. static void finish_note(int i)
  362. {
  363. if (voice[i].sample->modes & MODES_ENVELOPE)
  364. {
  365. /* We need to get the envelope out of Sustain stage */
  366. voice[i].envelope_stage=3;
  367. voice[i].status=VOICE_OFF;
  368. recompute_envelope(i);
  369. apply_envelope_to_amp(i);
  370. ctl->note(i);
  371. }
  372. else
  373. {
  374. /* Set status to OFF so resample_voice() will let this voice out
  375. of its loop, if any. In any case, this voice dies when it
  376. hits the end of its data (ofs>=data_length). */
  377. voice[i].status=VOICE_OFF;
  378. }
  379. }
  380. static void note_off(MidiEvent *e)
  381. {
  382. int i=voices;
  383. while (i--)
  384. if (voice[i].status==VOICE_ON &&
  385. voice[i].channel==e->channel &&
  386. voice[i].note==e->a)
  387. {
  388. if (channel[e->channel].sustain)
  389. {
  390. voice[i].status=VOICE_SUSTAINED;
  391. ctl->note(i);
  392. }
  393. else
  394. finish_note(i);
  395. return;
  396. }
  397. }
  398. /* Process the All Notes Off event */
  399. static void all_notes_off(int c)
  400. {
  401. int i=voices;
  402. ctl->cmsg(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", c);
  403. while (i--)
  404. if (voice[i].status==VOICE_ON &&
  405. voice[i].channel==c)
  406. {
  407. if (channel[c].sustain)
  408. {
  409. voice[i].status=VOICE_SUSTAINED;
  410. ctl->note(i);
  411. }
  412. else
  413. finish_note(i);
  414. }
  415. }
  416. /* Process the All Sounds Off event */
  417. static void all_sounds_off(int c)
  418. {
  419. int i=voices;
  420. while (i--)
  421. if (voice[i].channel==c &&
  422. voice[i].status != VOICE_FREE &&
  423. voice[i].status != VOICE_DIE)
  424. {
  425. kill_note(i);
  426. }
  427. }
  428. static void adjust_pressure(MidiEvent *e)
  429. {
  430. int i=voices;
  431. while (i--)
  432. if (voice[i].status==VOICE_ON &&
  433. voice[i].channel==e->channel &&
  434. voice[i].note==e->a)
  435. {
  436. voice[i].velocity=e->b;
  437. recompute_amp(i);
  438. apply_envelope_to_amp(i);
  439. return;
  440. }
  441. }
  442. static void adjust_panning(int c)
  443. {
  444. int i=voices;
  445. while (i--)
  446. if ((voice[i].channel==c) &&
  447. (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  448. {
  449. voice[i].panning=channel[c].panning;
  450. recompute_amp(i);
  451. apply_envelope_to_amp(i);
  452. }
  453. }
  454. static void drop_sustain(int c)
  455. {
  456. int i=voices;
  457. while (i--)
  458. if (voice[i].status==VOICE_SUSTAINED && voice[i].channel==c)
  459. finish_note(i);
  460. }
  461. static void adjust_pitchbend(int c)
  462. {
  463. int i=voices;
  464. while (i--)
  465. if (voice[i].status!=VOICE_FREE && voice[i].channel==c)
  466. {
  467. recompute_freq(i);
  468. }
  469. }
  470. static void adjust_volume(int c)
  471. {
  472. int i=voices;
  473. while (i--)
  474. if (voice[i].channel==c &&
  475. (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED))
  476. {
  477. recompute_amp(i);
  478. apply_envelope_to_amp(i);
  479. }
  480. }
  481. static void seek_forward( int32_t until_time)
  482. {
  483. reset_voices();
  484. while (current_event->time < until_time)
  485. {
  486. switch(current_event->type)
  487. {
  488. /* All notes stay off. Just handle the parameter changes. */
  489. case ME_PITCH_SENS:
  490. channel[current_event->channel].pitchsens=
  491. current_event->a;
  492. channel[current_event->channel].pitchfactor=0;
  493. break;
  494. case ME_PITCHWHEEL:
  495. channel[current_event->channel].pitchbend=
  496. current_event->a + current_event->b * 128;
  497. channel[current_event->channel].pitchfactor=0;
  498. break;
  499. case ME_MAINVOLUME:
  500. channel[current_event->channel].volume=current_event->a;
  501. break;
  502. case ME_PAN:
  503. channel[current_event->channel].panning=current_event->a;
  504. break;
  505. case ME_EXPRESSION:
  506. channel[current_event->channel].expression=current_event->a;
  507. break;
  508. case ME_PROGRAM:
  509. if (ISDRUMCHANNEL(current_event->channel))
  510. /* Change drum set */
  511. channel[current_event->channel].bank=current_event->a;
  512. else
  513. channel[current_event->channel].program=current_event->a;
  514. break;
  515. case ME_SUSTAIN:
  516. channel[current_event->channel].sustain=current_event->a;
  517. break;
  518. case ME_RESET_CONTROLLERS:
  519. reset_controllers(current_event->channel);
  520. break;
  521. case ME_TONE_BANK:
  522. channel[current_event->channel].bank=current_event->a;
  523. break;
  524. case ME_EOT:
  525. current_sample=current_event->time;
  526. return;
  527. }
  528. current_event++;
  529. }
  530. /*current_sample=current_event->time;*/
  531. if (current_event != event_list)
  532. current_event--;
  533. current_sample=until_time;
  534. }
  535. static void skip_to( int32_t until_time)
  536. {
  537. if (current_sample > until_time)
  538. current_sample=0;
  539. reset_midi();
  540. buffered_count=0;
  541. buffer_pointer=common_buffer;
  542. current_event=event_list;
  543. if (until_time)
  544. seek_forward(until_time);
  545. ctl->reset();
  546. }
  547. static int apply_controls(void)
  548. {
  549. int rc, i, did_skip=0;
  550. int val;
  551. /* ASCII renditions of CD player pictograms indicate approximate effect */
  552. do
  553. switch(rc=ctl->read(&val))
  554. {
  555. case RC_QUIT: /* [] */
  556. case RC_LOAD_FILE:
  557. case RC_NEXT: /* >>| */
  558. case RC_REALLY_PREVIOUS: /* |<< */
  559. return rc;
  560. case RC_CHANGE_VOLUME:
  561. if (val>0 || amplification > -val)
  562. amplification += val;
  563. else
  564. amplification=0;
  565. if (amplification > MAX_AMPLIFICATION)
  566. amplification=MAX_AMPLIFICATION;
  567. adjust_amplification();
  568. for (i=0; i<voices; i++)
  569. if (voice[i].status != VOICE_FREE)
  570. {
  571. recompute_amp(i);
  572. apply_envelope_to_amp(i);
  573. }
  574. ctl->master_volume(amplification);
  575. break;
  576. case RC_PREVIOUS: /* |<< */
  577. if (current_sample < 2*play_mode->rate)
  578. return RC_REALLY_PREVIOUS;
  579. return RC_RESTART;
  580. case RC_RESTART: /* |<< */
  581. skip_to(0);
  582. did_skip=1;
  583. break;
  584. case RC_JUMP:
  585. if (val >= sample_count)
  586. return RC_NEXT;
  587. skip_to(val);
  588. return rc;
  589. case RC_FORWARD: /* >> */
  590. if (val+current_sample >= sample_count)
  591. return RC_NEXT;
  592. skip_to(val+current_sample);
  593. did_skip=1;
  594. break;
  595. case RC_BACK: /* << */
  596. if (current_sample > val)
  597. skip_to(current_sample-val);
  598. else
  599. skip_to(0); /* We can't seek to end of previous song. */
  600. did_skip=1;
  601. break;
  602. }
  603. while (rc!= RC_NO_RETURN_VALUE);
  604. /* Advertise the skip so that we stop computing the audio buffer */
  605. if (did_skip)
  606. return RC_JUMP;
  607. else
  608. return rc;
  609. }
  610. static void do_compute_data( int32_t count)
  611. {
  612. int i;
  613. memset(buffer_pointer, 0,
  614. (play_mode->encoding & PE_MONO) ? (count * 4) : (count * 8));
  615. for (i=0; i<voices; i++)
  616. {
  617. if(voice[i].status != VOICE_FREE)
  618. mix_voice(buffer_pointer, i, count);
  619. }
  620. current_sample += count;
  621. }
  622. /* count=0 means flush remaining buffered data to output device, then
  623. flush the device itself */
  624. static int compute_data(void *stream, int32_t count, int* bytes_written)
  625. {
  626. int rc, channels;
  627. if ( play_mode->encoding & PE_MONO )
  628. channels = 1;
  629. else
  630. channels = 2;
  631. if (!count)
  632. {
  633. if (buffered_count) {
  634. s32tobuf(stream, common_buffer, channels*buffered_count);
  635. if (bytes_written && (play_mode->encoding & PE_16BIT))
  636. *bytes_written += channels * buffered_count * 2;
  637. else
  638. *bytes_written += channels * buffered_count;
  639. //No need anymore
  640. //play_mode->output_data(stream, channels*buffered_count, bytes_written);
  641. }
  642. buffer_pointer=common_buffer;
  643. buffered_count=0;
  644. return RC_NO_RETURN_VALUE;
  645. }
  646. while ((count+buffered_count) >= AUDIO_BUFFER_SIZE)
  647. {
  648. do_compute_data(AUDIO_BUFFER_SIZE-buffered_count);
  649. count -= AUDIO_BUFFER_SIZE-buffered_count;
  650. s32tobuf(stream, common_buffer, channels*AUDIO_BUFFER_SIZE);
  651. if (bytes_written && (play_mode->encoding & PE_16BIT))
  652. *bytes_written += channels * AUDIO_BUFFER_SIZE * 2;
  653. else
  654. *bytes_written += channels * AUDIO_BUFFER_SIZE;
  655. //play_mode->output_data(stream, channels*AUDIO_BUFFER_SIZE, bytes_written);
  656. buffer_pointer=common_buffer;
  657. buffered_count=0;
  658. ctl->current_time(current_sample);
  659. if ((rc=apply_controls())!=RC_NO_RETURN_VALUE)
  660. return rc;
  661. }
  662. if (count>0)
  663. {
  664. do_compute_data(count);
  665. buffered_count += count;
  666. buffer_pointer += (play_mode->encoding & PE_MONO) ? count : count*2;
  667. }
  668. return RC_NO_RETURN_VALUE;
  669. }
  670. int Timidity_PlaySome(void *stream, int samples, int* bytes_written)
  671. {
  672. int rc = RC_NO_RETURN_VALUE;
  673. int32_t end_sample;
  674. bool endSong = false;
  675. if (bytes_written)
  676. *bytes_written = 0;
  677. if ( ! midi_playing ){
  678. return RC_NO_RETURN_VALUE;
  679. }
  680. end_sample = current_sample+samples;
  681. while ( current_sample < end_sample ){
  682. /* Handle all events that should happen at this time */
  683. while ( !endSong && current_event->time <= current_sample){
  684. switch(current_event->type){
  685. /* Effects affecting a single note */
  686. case ME_NOTEON:
  687. if (!(current_event->b)) /* Velocity 0? */
  688. note_off(current_event);
  689. else
  690. note_on(current_event);
  691. break;
  692. case ME_NOTEOFF:
  693. note_off(current_event);
  694. break;
  695. case ME_KEYPRESSURE:
  696. adjust_pressure(current_event);
  697. break;
  698. /* Effects affecting a single channel */
  699. case ME_PITCH_SENS:
  700. channel[current_event->channel].pitchsens=current_event->a;
  701. channel[current_event->channel].pitchfactor=0;
  702. break;
  703. case ME_PITCHWHEEL:
  704. channel[current_event->channel].pitchbend=
  705. current_event->a + current_event->b * 128;
  706. channel[current_event->channel].pitchfactor=0;
  707. /* Adjust pitch for notes already playing */
  708. adjust_pitchbend(current_event->channel);
  709. ctl->pitch_bend(current_event->channel,
  710. channel[current_event->channel].pitchbend);
  711. break;
  712. case ME_MAINVOLUME:
  713. channel[current_event->channel].volume=current_event->a;
  714. adjust_volume(current_event->channel);
  715. ctl->volume(current_event->channel, current_event->a);
  716. break;
  717. case ME_PAN:
  718. channel[current_event->channel].panning=current_event->a;
  719. if (adjust_panning_immediately)
  720. adjust_panning(current_event->channel);
  721. ctl->panning(current_event->channel, current_event->a);
  722. break;
  723. case ME_EXPRESSION:
  724. channel[current_event->channel].expression=current_event->a;
  725. adjust_volume(current_event->channel);
  726. ctl->expression(current_event->channel, current_event->a);
  727. break;
  728. case ME_PROGRAM:
  729. if (ISDRUMCHANNEL(current_event->channel)){
  730. /* Change drum set */
  731. channel[current_event->channel].bank=current_event->a;
  732. }
  733. else
  734. {
  735. channel[current_event->channel].program=current_event->a;
  736. }
  737. ctl->program(current_event->channel, current_event->a);
  738. break;
  739. case ME_SUSTAIN:
  740. channel[current_event->channel].sustain=current_event->a;
  741. if (!current_event->a)
  742. drop_sustain(current_event->channel);
  743. ctl->sustain(current_event->channel, current_event->a);
  744. break;
  745. case ME_RESET_CONTROLLERS:
  746. reset_controllers(current_event->channel);
  747. redraw_controllers(current_event->channel);
  748. break;
  749. case ME_ALL_NOTES_OFF:
  750. all_notes_off(current_event->channel);
  751. break;
  752. case ME_ALL_SOUNDS_OFF:
  753. all_sounds_off(current_event->channel);
  754. break;
  755. case ME_TONE_BANK:
  756. channel[current_event->channel].bank=current_event->a;
  757. break;
  758. case ME_EOT:
  759. /* Give the last notes a couple of seconds to decay */
  760. //ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  761. // "Playing time: ~%d seconds, ", current_sample/play_mode->rate+2);
  762. //ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  763. // "Notes cut: %d, ", cut_notes);
  764. //ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
  765. // "Notes lost totally: %d", lost_notes);
  766. midi_playing = 0;
  767. rc = RC_TUNE_END;
  768. endSong = true;
  769. current_event--;
  770. }
  771. current_event++;
  772. }
  773. if (current_event->time > end_sample)
  774. rc=compute_data(stream, end_sample-current_sample, bytes_written);
  775. else
  776. rc=compute_data(stream, current_event->time-current_sample, bytes_written);
  777. ctl->refresh();
  778. if ( endSong || ((rc!=RC_NO_RETURN_VALUE) && (rc!=RC_JUMP)))
  779. break;
  780. }
  781. if ( endSong ) {
  782. return RC_TUNE_END;
  783. }
  784. return rc;
  785. }
  786. void Timidity_SetVolume(int volume)
  787. {
  788. int i;
  789. if (volume > MAX_AMPLIFICATION)
  790. amplification=MAX_AMPLIFICATION;
  791. else
  792. if (volume < 0)
  793. amplification=0;
  794. else
  795. amplification=volume;
  796. adjust_amplification();
  797. for (i=0; i<voices; i++)
  798. if (voice[i].status != VOICE_FREE)
  799. {
  800. recompute_amp(i);
  801. apply_envelope_to_amp(i);
  802. }
  803. ctl->master_volume(amplification);
  804. }
  805. MidiSong *Timidity_LoadSong(char *midifile)
  806. {
  807. MidiSong *song;
  808. int32_t events;
  809. idFile * fp;
  810. /* Allocate memory for the song */
  811. song = (MidiSong *)safe_malloc(sizeof(*song));
  812. memset(song, 0, sizeof(*song));
  813. /* Open the file */
  814. fp = open_file(midifile, 1, OF_VERBOSE);
  815. if ( fp != NULL ) {
  816. song->events=read_midi_file(fp, &events, &song->samples);
  817. close_file(fp);
  818. }
  819. /* Make sure everything is okay */
  820. if (!song->events) {
  821. Real_Tim_Free(song);
  822. song = NULL;
  823. ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Song had null events! Returning NULL.");
  824. }
  825. return(song);
  826. }
  827. MidiSong *Timidity_LoadSongMem(unsigned char* buffer, size_t length)
  828. {
  829. MidiSong *song;
  830. int32_t events;
  831. song = (MidiSong *)safe_malloc(sizeof(*song));
  832. memset(song, 0, sizeof(*song));
  833. song->events = read_midi_buffer(buffer, length, &events, &song->samples);
  834. if (!song->events) {
  835. Real_Tim_Free(song);
  836. song = NULL;
  837. ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Song had null events! Returning NULL.");
  838. }
  839. return(song);
  840. }
  841. void Timidity_Start(MidiSong *song)
  842. {
  843. load_missing_instruments();
  844. adjust_amplification();
  845. sample_count = song->samples;
  846. event_list = song->events;
  847. lost_notes=cut_notes=0;
  848. skip_to(0);
  849. midi_playing = 1;
  850. }
  851. int Timidity_Active(void)
  852. {
  853. return(midi_playing);
  854. }
  855. void Timidity_Stop(void)
  856. {
  857. midi_playing = 0;
  858. }
  859. void Timidity_FreeSong(MidiSong *song)
  860. {
  861. //if (free_instruments_afterwards)
  862. //free_instruments();
  863. Real_Tim_Free(song->events);
  864. Real_Tim_Free(song);
  865. }
  866. extern sample_t *resample_buffer;
  867. extern int32_t *common_buffer;
  868. void Timidity_Shutdown(void) {
  869. free_instruments();
  870. Real_Tim_Free( resample_buffer );
  871. Real_Tim_Free( common_buffer );
  872. }